From b1a72ad38dacc3877921a6f525928f87d5b84293 Mon Sep 17 00:00:00 2001 From: Nick Duijvelshoff Date: Fri, 22 Jun 2018 13:07:37 +0200 Subject: [PATCH 1/2] Added RetryHandler. (bunq/sdk_csharp#80) --- BunqSdk/BunqSdk.csproj | 1 + BunqSdk/Http/ApiClient.cs | 2 +- BunqSdk/Http/RetryHandler.cs | 23 +++++++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 BunqSdk/Http/RetryHandler.cs 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..781a3aa --- /dev/null +++ b/BunqSdk/Http/RetryHandler.cs @@ -0,0 +1,23 @@ +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() + .Or() + .OrResult(x => !x.IsSuccessStatusCode) + .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(3, retryAttempt))) + .ExecuteAsync(() => base.SendAsync(request, cancellationToken)); + } +} \ No newline at end of file From 8ce788480bde6b36935d910f89ac511ff2e96195 Mon Sep 17 00:00:00 2001 From: Nick Duijvelshoff Date: Fri, 29 Jun 2018 12:04:40 +0200 Subject: [PATCH 2/2] Updated RetryHandler. (bunq/sdk_csharp#80) --- BunqSdk/Http/RetryHandler.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/BunqSdk/Http/RetryHandler.cs b/BunqSdk/Http/RetryHandler.cs index 781a3aa..66a3de9 100644 --- a/BunqSdk/Http/RetryHandler.cs +++ b/BunqSdk/Http/RetryHandler.cs @@ -15,9 +15,8 @@ protected override Task SendAsync( CancellationToken cancellationToken) => Policy .Handle() - .Or() - .OrResult(x => !x.IsSuccessStatusCode) + .OrResult(x => x.StatusCode == (System.Net.HttpStatusCode)429) .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(3, retryAttempt))) .ExecuteAsync(() => base.SendAsync(request, cancellationToken)); } -} \ No newline at end of file +}