diff --git a/BunqSdk/BunqSdk.csproj b/BunqSdk/BunqSdk.csproj index 74508d1..c1073c6 100644 --- a/BunqSdk/BunqSdk.csproj +++ b/BunqSdk/BunqSdk.csproj @@ -24,6 +24,7 @@ + diff --git a/BunqSdk/Http/ApiClient.cs b/BunqSdk/Http/ApiClient.cs index c723134..cb9696c 100644 --- a/BunqSdk/Http/ApiClient.cs +++ b/BunqSdk/Http/ApiClient.cs @@ -99,7 +99,7 @@ public ApiClient(ApiContext apiContext) private HttpClient CreateHttpClient() { - return new HttpClient(CreateHttpClientHandler()) + return new HttpClient(new RetryHandler(CreateHttpClientHandler())) { BaseAddress = new Uri(apiContext.GetBaseUri()) }; diff --git a/BunqSdk/Http/RetryHandler.cs b/BunqSdk/Http/RetryHandler.cs new file mode 100644 index 0000000..66a3de9 --- /dev/null +++ b/BunqSdk/Http/RetryHandler.cs @@ -0,0 +1,22 @@ +using System; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; +using Polly; + +namespace Bunq.Sdk.Http +{ + public class RetryHandler : DelegatingHandler + { + public RetryHandler(HttpClientHandler handler) : base(handler) { } + + protected override Task SendAsync( + HttpRequestMessage request, + CancellationToken cancellationToken) => + Policy + .Handle() + .OrResult(x => x.StatusCode == (System.Net.HttpStatusCode)429) + .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(3, retryAttempt))) + .ExecuteAsync(() => base.SendAsync(request, cancellationToken)); + } +}