using System; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using StellaOps.Concelier.Connector.CertIn.Configuration; using StellaOps.Concelier.Connector.CertIn.Internal; using StellaOps.Concelier.Connector.Common.Http; namespace StellaOps.Concelier.Connector.CertIn; public static class CertInServiceCollectionExtensions { public static IServiceCollection AddCertInConnector(this IServiceCollection services, Action configure) { ArgumentNullException.ThrowIfNull(services); ArgumentNullException.ThrowIfNull(configure); services.AddOptions() .Configure(configure) .PostConfigure(static opts => opts.Validate()); services.AddSourceHttpClient(CertInOptions.HttpClientName, (sp, clientOptions) => { var options = sp.GetRequiredService>().Value; clientOptions.BaseAddress = options.AlertsEndpoint; clientOptions.Timeout = TimeSpan.FromSeconds(30); clientOptions.UserAgent = "StellaOps.Concelier.CertIn/1.0"; clientOptions.AllowedHosts.Clear(); clientOptions.AllowedHosts.Add(options.AlertsEndpoint.Host); clientOptions.DefaultRequestHeaders["Accept"] = "application/json"; }); services.AddTransient(); services.AddTransient(); return services; } }