Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added RetryHandler. (bunq/sdk_csharp#80) #106

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions BunqSdk/BunqSdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="10.0.3-*" />
<PackageReference Include="Polly" Version="6.0.*" />
<PackageReference Include="System.Collections.Immutable" Version="1.4.0" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion BunqSdk/Http/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public ApiClient(ApiContext apiContext)

private HttpClient CreateHttpClient()
{
return new HttpClient(CreateHttpClientHandler())
return new HttpClient(new RetryHandler(CreateHttpClientHandler()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can the retry handler be included inside the CreateHttpClientHandler method ?

Looks better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this based on the example, but this can be done I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I move the RetryHandler is it showing some errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kept it on the same place due to error when changing it.

{
BaseAddress = new Uri(apiContext.GetBaseUri())
};
Expand Down
22 changes: 22 additions & 0 deletions BunqSdk/Http/RetryHandler.cs
Original file line number Diff line number Diff line change
@@ -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<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken) =>
Policy
.Handle<HttpRequestException>()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only do this on the bunq 429 exception please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now in the code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm i meant to only retry when this exception is thrown TooManyRequestsException Cant you use this exception to catch and handle ?

.OrResult<HttpResponseMessage>(x => x.StatusCode == (System.Net.HttpStatusCode)429)
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(3, retryAttempt)))
.ExecuteAsync(() => base.SendAsync(request, cancellationToken));
}
}