using System; using System.Linq; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; using StellaOps.Concelier.Connector.Cccs.Configuration; using StellaOps.Concelier.Connector.Cccs.Internal; using StellaOps.Concelier.Connector.Common.Http; using StellaOps.Concelier.Connector.Common.Html; namespace StellaOps.Concelier.Connector.Cccs; public static class CccsServiceCollectionExtensions { public static IServiceCollection AddCccsConnector(this IServiceCollection services, Action configure) { ArgumentNullException.ThrowIfNull(services); ArgumentNullException.ThrowIfNull(configure); services.AddOptions() .Configure(configure) .PostConfigure(static options => options.Validate()); services.AddSourceHttpClient(CccsOptions.HttpClientName, static (sp, clientOptions) => { var options = sp.GetRequiredService>().Value; clientOptions.UserAgent = "StellaOps.Concelier.Cccs/1.0"; clientOptions.Timeout = options.RequestTimeout; clientOptions.AllowedHosts.Clear(); foreach (var feed in options.Feeds.Where(static feed => feed.Uri is not null)) { clientOptions.AllowedHosts.Add(feed.Uri!.Host); } clientOptions.AllowedHosts.Add("www.cyber.gc.ca"); clientOptions.AllowedHosts.Add("cyber.gc.ca"); }); services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); services.AddTransient(); return services; } }