using System; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; using StellaOps.Concelier.Connector.CertFr.Configuration; using StellaOps.Concelier.Connector.CertFr.Internal; using StellaOps.Concelier.Connector.Common.Http; namespace StellaOps.Concelier.Connector.CertFr; public static class CertFrServiceCollectionExtensions { public static IServiceCollection AddCertFrConnector(this IServiceCollection services, Action configure) { ArgumentNullException.ThrowIfNull(services); ArgumentNullException.ThrowIfNull(configure); services.AddOptions() .Configure(configure) .PostConfigure(static options => options.Validate()); services.AddSourceHttpClient(CertFrOptions.HttpClientName, static (sp, clientOptions) => { var options = sp.GetRequiredService>().Value; clientOptions.BaseAddress = options.FeedUri; clientOptions.UserAgent = "StellaOps.Concelier.CertFr/1.0"; clientOptions.Timeout = TimeSpan.FromSeconds(20); clientOptions.AllowedHosts.Clear(); clientOptions.AllowedHosts.Add(options.FeedUri.Host); }); services.TryAddSingleton(); services.AddTransient(); return services; } }