public static IServiceCollection AddHttpApi <TConfiguration, TContract, TService>(this IServiceCollection services) where TConfiguration : class, IHttpApiConfiguration, new() where TContract : class where TService : class, TContract { if (services == null) { throw new ArgumentNullException(nameof(services)); } services.AddTransient <TContract>(sp => { var(httpClient, _) = BuildHttpClient <TConfiguration>(sp); return((TContract)ActivatorUtilities.CreateInstance <TService>(sp, httpClient)); }); services.AddTransient <IStatusEndpointDependency>(sp => { var(httpClient, typedConfiguration) = BuildHttpClient <TConfiguration>(sp); var httpApiStatusEndpointDependency = new HttpApiStatusEndpointDependency(httpClient, typedConfiguration); if (typedConfiguration is IHttpApiWithCircuitBreaking httpApiWithCircuitBreaking) { var circuitBreakerManager = sp.GetRequiredService <ICircuitBreakerManager>(); return(new CircuitBreakingStatusEndpointDependency <HttpStatusCode>(httpApiStatusEndpointDependency, circuitBreakerManager.GetState(httpApiWithCircuitBreaking.CircuitBreakerPolicy))); } return(httpApiStatusEndpointDependency); }); return(services); }
public async Task setup_scenario() { var httpClient = new StubHttpClient <string> { Response = "Hey!" }; var testSubject = new HttpApiStatusEndpointDependency(httpClient, new HttpApiConfiguration { Name = "downstreamApiName" }); _result = await testSubject.GetStatusAsync(CancellationToken.None); }
public async Task setup_scenario() { var httpClient = new StubHttpClient <Status> { BaseAddress = new Uri("http://stubbaseaddress"), Exception = new HttpClientTimeoutException(HttpMethod.Get, new Uri("http://stubbaseaddress/.status")) }; var testSubject = new HttpApiStatusEndpointDependency(httpClient, new HttpApiConfiguration { Name = "expectedDependencyName" }); _result = await testSubject.GetStatusAsync(CancellationToken.None); }
public async Task setup_scenario() { var httpClient = new StubHttpClient <Status> { BaseAddress = new Uri("http://stubbaseaddress"), StatusCode = HttpStatusCode.InternalServerError }; var testSubject = new HttpApiStatusEndpointDependency(httpClient, new HttpApiConfiguration { Name = "dependencyName" }); _result = await testSubject.GetStatusAsync(CancellationToken.None); }
public async Task setup_scenario() { var httpClient = new StubHttpClient <Status> { BaseAddress = new Uri("http://stubbaseaddress"), Latency = TimeSpan.FromSeconds(3) }; var testSubject = new HttpApiStatusEndpointDependency(httpClient, new HttpApiConfiguration { Name = "expectedDependencyName" }); _result = await testSubject.GetStatusAsync(new CancellationTokenSource(20).Token); }