private IServiceCollection Build() { services.AddSingleton <FredClientConfig>(); services.AddTransient <IVintageComposer, VintageComposer>(); UseFileType(FredFileType.JSON); UseVintageComposer(x => x.GetService <IVintageComposer>()); UseHttpClient(x => { Func <IServiceProvider, FredClientConfig> configFactory = x.GetService <Func <IServiceProvider, FredClientConfig> >(); FredClientConfig config = configFactory(x); HttpClient httpClient = new HttpClient(); httpClient.BaseAddress = new Uri(config.BaseURL); return(httpClient); }); UseConfig(x => x.GetService <FredClientConfig>()); services.AddTransient <IFredClient>(x => { Func <IServiceProvider, FredClientConfig> configFactory = x.GetService <Func <IServiceProvider, FredClientConfig> >(); Func <IServiceProvider, IVintageComposer> composerFactory = x.GetService <Func <IServiceProvider, IVintageComposer> >(); Func <IServiceProvider, HttpClient> httpClientFactory = x.GetService <Func <IServiceProvider, HttpClient> >(); ILogger <IFredClient> logger = x.GetService <ILogger <IFredClient> >() ?? throw new Exception("A Logger could not resolved. You must call AddLogging() when configuring IServiceCollection. For example: services.AddLogging(builder => builder.AddSerilog());"); IFredClient fredClient = this.fileType == FredFileType.JSON ? new JsonFredClient(this.apiKey, configFactory(x), composerFactory(x), httpClientFactory(x), logger) : new XMLFredClient(this.apiKey, configFactory(x), composerFactory(x), httpClientFactory(x), logger); return(fredClient); }); return(services); }
public void Setup() { HttpClient httpClient = new HttpClient() { BaseAddress = new Uri(FredClientConfig.BaseAPIURL) }; FredClientConfig config = new FredClientConfig { MaxDownloadRetries = 1 }; ILoggerFactory loggerFactory = new LoggerFactory().AddSerilog(); ILogger <IFredClient> logger = loggerFactory.CreateLogger <IFredClient>(); if (CurrentFileType == FredFileType.XML) { FredClient = new XMLFredClient(apiKey, config, new VintageComposer(), httpClient, logger); } else { FredClient = new JsonFredClient(apiKey, config, new VintageComposer(), httpClient, logger); } }
public BaseFredClient(string apiKey, FredClientConfig config, IVintageComposer composer, HttpClient httpClient, ILogger <IFredClient> logger) { API_key = "api_key=" + apiKey ?? throw new ArgumentNullException($"{nameof(apiKey)} can not be null. Call UseAPIKey() when calling the FredClient service registration. For example: .AddFredClient().UseAPIKey(\"your API key here\") "); this.config = config ?? throw new ArgumentNullException(nameof(config)); this.httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient)); this.composer = composer ?? throw new ArgumentNullException(nameof(composer)); if (httpClient.BaseAddress is null) { throw new Exception($"{nameof(httpClient)} BaseAddress must be set. The default value is {FredClientConfig.BaseAPIURL}"); } if (!httpClient.BaseAddress.OriginalString.EndsWith("/")) { httpClient.BaseAddress = new Uri(httpClient.BaseAddress.ToString() + "/"); } Logger = logger ?? throw new ArgumentNullException(nameof(logger)); concurrentRequestThrottle = new SemaphoreSlim(config.MaxConcurrentDownloads, config.MaxConcurrentDownloads); batchThrottle = new BatchThrottleAsync(120, 60000); // block when we reach the per-minute request limit. }
public XMLFredClient(string apiKey, FredClientConfig config, IVintageComposer composer, HttpClient httpClient, ILogger <IFredClient> logger) : base(apiKey, config, composer, httpClient, logger) { }