public static IServiceCollection AddSportsmansGuideClient(this IServiceCollection services) { services.AddHttpClient <IProductService, ProductService>(RetailerNames.SportsmansGuide, client => { client.BaseAddress = new Uri(BaseUrl); client.DefaultRequestHeaders.Add("Accept", "text/html"); client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br"); client.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue { NoCache = true }; }) .ConfigurePrimaryHttpMessageHandler(config => new ClearanceHandler { InnerHandler = new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate | DecompressionMethods.Brotli }, MaxTries = 3, ClearanceDelay = 3000 }) .AddPolicyHandler(RateLimiterAsyncPolicy <HttpResponseMessage> .Create(25, TimeSpan.FromMinutes(1))); return(services); }
public void ReplaceMeWithRealTests() { /* * This test is for illustrative purposes, to show the interfaces a typical asynchronous generic policy fulfills. * Real tests should check policy behaviour. */ RateLimiterAsyncPolicy <int> policy = RateLimiterAsyncPolicy <int> .Create(2, TimeSpan.FromSeconds(30)); policy.Should().BeAssignableTo <IAsyncPolicy <int> >(); policy.Should().BeAssignableTo <IRateLimiterPolicy <int> >(); }
public async Task PolicyExecutesThePassedDelegate() { bool executed = false; RateLimiterAsyncPolicy <int> policy = RateLimiterAsyncPolicy <int> .Create(2, TimeSpan.FromSeconds(30)); await policy.ExecuteAsync(() => { executed = true; return(Task.FromResult(0)); }); executed.Should().BeTrue(); }
public async Task PolicyExecutesThePassedDelegate() { bool executed = false; RateLimiterAsyncPolicy policy = RateLimiterAsyncPolicy.Create(1, TimeSpan.FromSeconds(10)); await policy.ExecuteAsync(() => { executed = true; return(Task.CompletedTask); }); executed.Should().BeTrue(); }
public static IServiceCollection AddAimSurplusClient(this IServiceCollection services) { services.AddHttpClient <IProductService, ProductService>(RetailerNames.AimSurplus, client => { client.BaseAddress = new Uri(BaseUrl); client.DefaultRequestHeaders.Add("Accept", "application/json"); client.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue { NoCache = true }; }) .AddPolicyHandler(RateLimiterAsyncPolicy <HttpResponseMessage> .Create(25, TimeSpan.FromMinutes(1))); return(services); }
public static IServiceCollection AddCabelasClient(this IServiceCollection services) { services.AddHttpClient <IProductService, ProductService>(RetailerNames.Cabelas, client => { client.BaseAddress = new Uri(BaseUrl); client.DefaultRequestHeaders.Add("Accept", "text/html"); client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br"); client.DefaultRequestHeaders.Add("Origin", "https://www.cabelas.com"); client.DefaultRequestHeaders.Add("Host", "www.cabelas.com"); client.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue { NoCache = true }; }) .ConfigurePrimaryHttpMessageHandler(config => new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate | DecompressionMethods.Brotli }) .AddPolicyHandler(RateLimiterAsyncPolicy <HttpResponseMessage> .Create(25, TimeSpan.FromMinutes(1))); return(services); }