Skip to content

Commit

Permalink
Use Polly.Core
Browse files Browse the repository at this point in the history
Update to the new Polly v8 API.
  • Loading branch information
martincostello committed Nov 15, 2023
1 parent 03bdfec commit 0336c93
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="Newtonsoft.Json.Schema" Version="3.0.15" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="Polly" Version="8.2.0" />
<PackageVersion Include="Polly.Core" Version="8.2.0" />
<PackageVersion Include="Refit" Version="7.0.0" />
<PackageVersion Include="ReportGenerator" Version="5.2.0" />
<PackageVersion Include="Shouldly" Version="4.2.1" />
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ using var client = options.CreateHttpClient();
// The value of json will be: {"Id":1, "Link":"https://www.just-eat.co.uk/privacy-policy"}
string json = await client.GetStringAsync("https://public.je-apis.com/terms");
```
<sup><a href='/tests/HttpClientInterception.Tests/Examples.cs#L43-L65' title='Snippet source file'>snippet source</a> | <a href='#snippet-minimal-example' title='Start of snippet'>anchor</a></sup>
<sup><a href='/tests/HttpClientInterception.Tests/Examples.cs#L44-L66' title='Snippet source file'>snippet source</a> | <a href='#snippet-minimal-example' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

`HttpRequestInterceptionBuilder` objects are mutable, so properties can be freely changed once a particular setup has been registered with an instance of `HttpClientInterceptorOptions` as the state is captured at the point of registration. This allows multiple responses and paths to be configured from a single `HttpRequestInterceptionBuilder` instance where multiple registrations against a common hostname.
Expand Down Expand Up @@ -143,7 +143,7 @@ var client = options.CreateHttpClient();
await Assert.ThrowsAsync<HttpRequestException>(
() => client.GetStringAsync("http://public.je-apis.com"));
```
<sup><a href='/tests/HttpClientInterception.Tests/Examples.cs#L22-L37' title='Snippet source file'>snippet source</a> | <a href='#snippet-fault-injection' title='Start of snippet'>anchor</a></sup>
<sup><a href='/tests/HttpClientInterception.Tests/Examples.cs#L23-L38' title='Snippet source file'>snippet source</a> | <a href='#snippet-fault-injection' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

#### Registering Request Interception When Using IHttpClientFactory
Expand Down
19 changes: 12 additions & 7 deletions tests/HttpClientInterception.Tests/Examples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using JustEat.HttpClientInterception.GitHub;
using Newtonsoft.Json.Linq;
using Polly;
using Polly.Retry;
using Refit;

namespace JustEat.HttpClientInterception;
Expand Down Expand Up @@ -734,16 +735,20 @@ static bool IsHttpGetForJustEatGitHubOrg(HttpRequestMessage request)

var service = RestService.For<IGitHub>(options.CreateHttpClient("https://api.github.com"));

// Configure a Polly retry policy that will retry an HTTP request up to three times
// if the HTTP request fails due to an HTTP 429 response from the server.
// Configure a Polly resilience pipeline that will retry an HTTP request up to three
// times if the HTTP request fails due to an HTTP 429 response from the server.
int retryCount = 3;

var policy = Policy
.Handle<ApiException>((ex) => ex.StatusCode == HttpStatusCode.TooManyRequests)
.RetryAsync(retryCount);
var context = ResilienceContextPool.Shared.Get();
var pipeline = new ResiliencePipelineBuilder()
.AddRetry(new RetryStrategyOptions()
{
ShouldHandle = new PredicateBuilder().Handle<ApiException>((ex) => ex.StatusCode == HttpStatusCode.TooManyRequests),
MaxRetryAttempts = 3,
})
.Build();

// Act
Organization actual = await policy.ExecuteAsync(() => service.GetOrganizationAsync("justeattakeaway"));
Organization actual = await pipeline.ExecuteAsync(async (_) => await service.GetOrganizationAsync("justeattakeaway"), context);

// Assert
actual.ShouldNotBeNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Newtonsoft.Json.Schema" />
<PackageReference Include="NSubstitute" />
<PackageReference Include="Polly" />
<PackageReference Include="Polly.Core" />
<PackageReference Include="Refit" />
<PackageReference Include="Shouldly" />
<PackageReference Include="xunit" />
Expand Down

0 comments on commit 0336c93

Please sign in to comment.