using Insight.TelegramBot.Handling.Handlers; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher; using Nocr.TelegramClient.AppServices.Handlers; using Nocr.TelegramClient.AppServices.Options; using Nocr.TelegramClient.AppServices.TextSubscriptions; using Nocr.TelegramClient.AppServices.TextSubscriptions.Handlers; using Nocr.TelegramClient.AppServices.Users; using Nocr.TextMatcher.Api.Contracts.TextMatches; using Nocr.Users.Api.Contracts.Users; using Rebus.Config; using RestEase; namespace Nocr.TelegramClient.AppServices; public static class ServiceCollectionExtensions { public static IServiceCollection AddAppServices(this IServiceCollection services, IConfiguration configuration) { if (services == null) throw new ArgumentNullException(nameof(services)); // Add registrations here services.AddRebusHandler(); services.AddHttpClient(); services.Configure(configuration.GetSection(nameof(AdministrationOptions))); services.AddScoped(); services.AddScoped(); services.Configure(configuration.GetSection(nameof(UsersRestEaseOptions))); services.AddScoped(ctx => { var options = ctx.GetRequiredService>().Value; var httpClientFactory = ctx.GetRequiredService(); var client = httpClientFactory.CreateClient(nameof(IUsersController)); client.BaseAddress = new Uri(options.BasePath); return RestClient.For(client); }); services.Configure(configuration.GetSection(nameof(TextMatcherRestEaseOptions))); services.AddScoped(ctx => { var options = ctx.GetRequiredService>().Value; var httpClientFactory = ctx.GetRequiredService(); var client = httpClientFactory.CreateClient(nameof(ITextSubscriptionsController)); client.BaseAddress = new Uri(options.BasePath); return RestClient.For(client); }); services.AddScoped(); services.AddScoped(); services.Configure(configuration.GetSection(nameof(MessageDispatcherOptions))); services.AddSingleton(); services.AddScoped(); services.AddHostedService(); return services; } }