File tree Expand file tree Collapse file tree 3 files changed +25
-1
lines changed Expand file tree Collapse file tree 3 files changed +25
-1
lines changed Original file line number Diff line number Diff line change 24
24
</PropertyGroup >
25
25
<ItemGroup >
26
26
<PackageReference Include =" Newtonsoft.Json" Version =" 10.0.3-*" />
27
+ <PackageReference Include =" Polly" Version =" 6.0.*" />
27
28
<PackageReference Include =" System.Collections.Immutable" Version =" 1.4.0" />
28
29
</ItemGroup >
29
30
<ItemGroup >
Original file line number Diff line number Diff line change @@ -99,7 +99,7 @@ public ApiClient(ApiContext apiContext)
99
99
100
100
private HttpClient CreateHttpClient ( )
101
101
{
102
- return new HttpClient ( CreateHttpClientHandler ( ) )
102
+ return new HttpClient ( new RetryHandler ( CreateHttpClientHandler ( ) ) )
103
103
{
104
104
BaseAddress = new Uri ( apiContext . GetBaseUri ( ) )
105
105
} ;
Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Net . Http ;
3
+ using System . Threading ;
4
+ using System . Threading . Tasks ;
5
+ using Polly ;
6
+
7
+ namespace Bunq . Sdk . Http
8
+ {
9
+ public class RetryHandler : DelegatingHandler
10
+ {
11
+ public RetryHandler ( HttpClientHandler handler ) : base ( handler ) { }
12
+
13
+ protected override Task < HttpResponseMessage > SendAsync (
14
+ HttpRequestMessage request ,
15
+ CancellationToken cancellationToken ) =>
16
+ Policy
17
+ . Handle < HttpRequestException > ( )
18
+ . Or < TaskCanceledException > ( )
19
+ . OrResult < HttpResponseMessage > ( x => ! x . IsSuccessStatusCode )
20
+ . WaitAndRetryAsync ( 3 , retryAttempt => TimeSpan . FromSeconds ( Math . Pow ( 3 , retryAttempt ) ) )
21
+ . ExecuteAsync ( ( ) => base . SendAsync ( request , cancellationToken ) ) ;
22
+ }
23
+ }
You can’t perform that action at this time.
0 commit comments