44using System . Net . Http . Headers ;
55using Microsoft . Extensions . DependencyInjection ;
66using Polly ;
7+ using Polly . Extensions . Http ;
78using Refit ;
89
910namespace GitHubReadmeWebTrends . Common
@@ -23,7 +24,8 @@ public static void ConfigureServices(in IServiceCollection services)
2324 client . DefaultRequestHeaders . Authorization = getBearerToken ( ) ;
2425 } )
2526 . ConfigurePrimaryHttpMessageHandler ( config => new HttpClientHandler { AutomaticDecompression = getDecompressionMethods ( ) } )
26- . AddTransientHttpErrorPolicy ( builder => builder . WaitAndRetryAsync ( 3 , sleepDurationProvider ) ) ;
27+ . AddPolicyHandler ( getPolicyHandler ( ) ) ;
28+
2729
2830 services . AddRefitClient < IGitHubRestApiClient > ( )
2931 . ConfigureHttpClient ( client =>
@@ -32,7 +34,7 @@ public static void ConfigureServices(in IServiceCollection services)
3234 client . DefaultRequestHeaders . Authorization = getBearerToken ( ) ;
3335 } )
3436 . ConfigurePrimaryHttpMessageHandler ( config => new HttpClientHandler { AutomaticDecompression = getDecompressionMethods ( ) } )
35- . AddTransientHttpErrorPolicy ( builder => builder . WaitAndRetryAsync ( 3 , sleepDurationProvider ) ) ;
37+ . AddPolicyHandler ( getPolicyHandler ( ) ) ;
3638
3739 services . AddSingleton < YamlService > ( ) ;
3840 services . AddSingleton < OptOutDatabase > ( ) ;
@@ -41,10 +43,9 @@ public static void ConfigureServices(in IServiceCollection services)
4143 services . AddSingleton < CloudAdvocateService > ( ) ;
4244 services . AddSingleton < GitHubApiStatusService > ( ) ;
4345
44- static TimeSpan sleepDurationProvider ( int attemptNumber ) => TimeSpan . FromSeconds ( Math . Pow ( 2 , attemptNumber ) ) ;
45-
4646 static AuthenticationHeaderValue getBearerToken ( ) => new AuthenticationHeaderValue ( "bearer" , _token ) ;
4747 static DecompressionMethods getDecompressionMethods ( ) => DecompressionMethods . Deflate | DecompressionMethods . GZip ;
48+ static IAsyncPolicy < HttpResponseMessage > getPolicyHandler ( ) => HttpPolicyExtensions . HandleTransientHttpError ( ) . OrResult ( msg => msg . StatusCode is HttpStatusCode . Forbidden ) . WaitAndRetryAsync ( 10 , count => TimeSpan . FromSeconds ( 60 ) ) ;
4849 }
4950 }
5051}
0 commit comments