Files
telegram-bot/modules/ContentStore/ContentStore.cs
T
2023-07-28 23:14:05 +03:00

53 lines
1.7 KiB
C#

using Kruzya.TelegramBot.Core;
using Kruzya.TelegramBot.Core.Extensions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Net.Http.Headers;
using West.TelegramBot.ContentStore.API;
using West.TelegramBot.ContentStore.Option;
using West.TelegramBot.ContentStore.Service;
namespace West.TelegramBot.ContentStore;
public class ContentStore : Module
{
public readonly List<Type> MediaServiceTypes = new();
public ContentStore(Core core) : base(core)
{
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (var exportedType in assembly.GetExportedTypes()
.Where(t => !t.IsAbstract && t.GetInterface(nameof(IRandomMediaService)) != null))
{
MediaServiceTypes.Add(exportedType);
}
}
}
public override void ConfigureServices(IServiceCollection services)
{
services.AddOption<AllowNsfw>();
var kittiesConfig = Configuration.GetSection("KittiesService");
services.AddHttpClient<IKittiesApi, KittiesApi>(c =>
{
c.BaseAddress = kittiesConfig.GetValue<Uri>("Uri");
c.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Bearer",
kittiesConfig.GetValue<string>("token")
);
});
services.AddHttpClient<ISomeRandomApi, SomeRandomApi>(c =>
{
c.BaseAddress = new Uri("https://some-random-api.com");
});
foreach (var serviceType in MediaServiceTypes)
{
services.AddScoped(typeof(IRandomMediaService), serviceType);
}
}
}