using System; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; using StellaOps.Concelier.Connector.Common.Http; using StellaOps.Concelier.Connector.Vndr.Apple.Internal; namespace StellaOps.Concelier.Connector.Vndr.Apple; public static class AppleServiceCollectionExtensions { public static IServiceCollection AddAppleConnector(this IServiceCollection services, Action configure) { ArgumentNullException.ThrowIfNull(services); ArgumentNullException.ThrowIfNull(configure); services.AddOptions() .Configure(configure) .PostConfigure(static opts => opts.Validate()) .ValidateOnStart(); services.AddSourceHttpClient(AppleOptions.HttpClientName, static (sp, clientOptions) => { var options = sp.GetRequiredService>().Value; clientOptions.Timeout = TimeSpan.FromSeconds(30); clientOptions.UserAgent = "StellaOps.Concelier.Apple/1.0"; clientOptions.AllowedHosts.Clear(); if (options.SoftwareLookupUri is not null) { clientOptions.AllowedHosts.Add(options.SoftwareLookupUri.Host); } if (options.AdvisoryBaseUri is not null) { clientOptions.AllowedHosts.Add(options.AdvisoryBaseUri.Host); } }); services.TryAddSingleton(_ => TimeProvider.System); services.AddSingleton(); services.AddTransient(); return services; } }