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