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 Can_use_XML_filetype_FredClient() { string apiKey = "secret"; IServiceCollection container = new ServiceCollection(); container.AddFredClient().UseAPIKey(apiKey).UseFileType(FredFileType.XML); container.AddLogging(builder => builder.AddSerilog()); IServiceProvider services = container.BuildServiceProvider(); IFredClient fredClient = services.GetService <IFredClient>(); Assert.IsTrue(fredClient is XMLFredClient); }
public void Can_resolve_default_FredClient() { string apiKey = "secret"; IServiceCollection container = new ServiceCollection(); container.AddLogging(builder => builder.AddSerilog()); container.AddFredClient().UseAPIKey(apiKey); IServiceProvider services = container.BuildServiceProvider(); IFredClient fredClient = services.GetService <IFredClient>(); Assert.IsTrue(fredClient is JsonFredClient); }
public void Can_resolve_multiple_instances_of_FredClient() { string apiKey = "secret"; IServiceCollection container = new ServiceCollection(); container.AddLogging(builder => builder.AddSerilog()); container.AddFredClient().UseAPIKey(apiKey); IServiceProvider services = container.BuildServiceProvider(); IFredClient fredClient_1 = services.GetService <IFredClient>(); IFredClient fredClient_2 = services.GetService <IFredClient>(); Assert.AreNotEqual(fredClient_1, fredClient_2); }
public void Can_use_custom_composer_FredClient() { IVintageComposer composerMock = new Mock <IVintageComposer>().Object; string apiKey = "secret"; IServiceCollection container = new ServiceCollection(); container.AddLogging(builder => builder.AddSerilog()); container.AddFredClient().UseAPIKey(apiKey).UseVintageComposer(x => composerMock); IServiceProvider services = container.BuildServiceProvider(); IFredClient fredClient = services.GetService <IFredClient>(); Assert.IsTrue(fredClient is JsonFredClient); Func <IServiceProvider, IVintageComposer> composerFactory = services.GetService <Func <IServiceProvider, IVintageComposer> >(); IVintageComposer composer = composerFactory(services); Assert.IsTrue(composer.GetType().FullName == "Castle.Proxies.IVintageComposerProxy"); }
public void Can_use_custom_config_FredClient() { string localhost = "https://localhost/"; string apiKey = "secret"; IServiceCollection container = new ServiceCollection(); container.AddLogging(builder => builder.AddSerilog()); container.AddFredClient().UseAPIKey(apiKey).UseConfig(x => new FredClientConfig { BaseURL = localhost }); IServiceProvider services = container.BuildServiceProvider(); IFredClient fredClient = services.GetService <IFredClient>(); Assert.IsTrue(fredClient is JsonFredClient); Func <IServiceProvider, HttpClient> httpClientFactory = services.GetService <Func <IServiceProvider, HttpClient> >(); HttpClient httpClient = httpClientFactory(services); Assert.AreEqual(localhost, httpClient.BaseAddress.AbsoluteUri); }
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); } }