-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrapping up interceptors, fixing typos, removing Moq (#2141)
* Wrapping up interceptors, fixing typos, removing Moq * Fix the readme issue * Added compatibility interceptor * Added docs * Updated packages * Refactoring tests to WireMock * Added matrix
- Loading branch information
1 parent
1c86286
commit d758e40
Showing
109 changed files
with
2,154 additions
and
1,601 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
name: Test Results | ||
|
||
on: | ||
workflow_run: | ||
workflows: ["Build and test PRs"] | ||
types: | ||
- completed | ||
permissions: {} | ||
|
||
jobs: | ||
test-results: | ||
name: Test Results | ||
runs-on: ubuntu-latest | ||
if: github.event.workflow_run.conclusion != 'skipped' | ||
|
||
permissions: | ||
checks: write | ||
pull-requests: write | ||
actions: read | ||
|
||
steps: | ||
- | ||
name: Download and Extract Artifacts | ||
uses: dawidd6/action-download-artifact@e7466d1a7587ed14867642c2ca74b5bcc1e19a2d | ||
with: | ||
run_id: ${{ github.event.workflow_run.id }} | ||
path: artifacts | ||
- | ||
name: Publish Test Results | ||
uses: EnricoMi/publish-unit-test-result-action@v2 | ||
with: | ||
commit: ${{ github.event.workflow_run.head_sha }} | ||
event_file: artifacts/Event File/event.json | ||
event_name: ${{ github.event.workflow_run.event }} | ||
files: | | ||
artifacts/**/*.xml | ||
artifacts/**/*.trx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally> | ||
</PropertyGroup> | ||
<PropertyGroup Label="Package versions for .NET 6" Condition="$(TargetFramework) == 'net6.0'"> | ||
<MicrosoftTestHostVer>[6.0.28,7)</MicrosoftTestHostVer> | ||
</PropertyGroup> | ||
<PropertyGroup Label="Package versions for .NET 7" Condition="$(TargetFramework) == 'net7.0'"> | ||
<MicrosoftTestHostVer>7.0.17</MicrosoftTestHostVer> | ||
</PropertyGroup> | ||
<PropertyGroup Label="Package versions for .NET 8" Condition="$(TargetFramework) == 'net8.0'"> | ||
<MicrosoftTestHostVer>8.0.3</MicrosoftTestHostVer> | ||
</PropertyGroup> | ||
<ItemGroup Label="Runtime dependencies"> | ||
<PackageVersion Include="HttpMultipartParser" Version="8.3.0" /> | ||
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" /> | ||
<PackageVersion Include="CsvHelper" Version="30.0.1" /> | ||
<PackageVersion Include="PolySharp" Version="1.14.1" /> | ||
<PackageVersion Include="System.Text.Json" Version="8.0.3" /> | ||
<PackageVersion Include="WireMock.Net" Version="1.5.51" /> | ||
<PackageVersion Include="WireMock.Net.FluentAssertions" Version="1.5.51" /> | ||
</ItemGroup> | ||
<ItemGroup Label="Compile dependencies"> | ||
<PackageVersion Include="BenchmarkDotNet" Version="0.13.12" /> | ||
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" /> | ||
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" /> | ||
<PackageVersion Include="MinVer" Version="5.0.0" /> | ||
<PackageVersion Include="Nullable" Version="1.3.1" /> | ||
<PackageVersion Include="Microsoft.NETFramework.ReferenceAssemblies.net472" Version="1.0.3" /> | ||
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" /> | ||
<PackageVersion Include="JetBrains.Annotations" Version="2023.2.0" /> | ||
</ItemGroup> | ||
<ItemGroup Label="Testing dependencies"> | ||
<PackageVersion Include="AutoFixture" Version="4.18.0" /> | ||
<PackageVersion Include="coverlet.collector" Version="6.0.2" /> | ||
<PackageVersion Include="FluentAssertions" Version="6.12.0" /> | ||
<PackageVersion Include="HttpTracer" Version="2.1.1" /> | ||
<PackageVersion Include="Microsoft.AspNetCore.TestHost" Version="$(MicrosoftTestHostVer)" /> | ||
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" /> | ||
<PackageVersion Include="Moq" Version="4.20.69" /> | ||
<PackageVersion Include="Polly" Version="8.3.1" /> | ||
<PackageVersion Include="rest-mock-core" Version="0.7.12" /> | ||
<PackageVersion Include="RichardSzalay.MockHttp" Version="7.0.0" /> | ||
<PackageVersion Include="System.Net.Http.Json" Version="8.0.0" /> | ||
<PackageVersion Include="Xunit.Extensions.Logging" Version="1.1.0" /> | ||
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.7" PrivateAssets="All" /> | ||
<PackageVersion Include="xunit" Version="2.7.0" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 11 additions & 10 deletions
21
benchmarks/RestSharp.Benchmarks/RestSharp.Benchmarks.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
<LangVersion>10</LangVersion> | ||
<LangVersion>preview</LangVersion> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<RepoRoot>$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildProjectDirectory), 'RestSharp.sln'))</RepoRoot> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AutoFixture" Version="4.18.0" /> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.13.7" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.6.0" /> | ||
<PackageReference Include="AutoFixture"/> | ||
<PackageReference Include="BenchmarkDotNet"/> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\RestSharp.Serializers.NewtonsoftJson\RestSharp.Serializers.NewtonsoftJson.csproj" /> | ||
<ProjectReference Include="..\..\test\RestSharp.Tests.Shared\RestSharp.Tests.Shared.csproj" /> | ||
<ProjectReference Include="$(RepoRoot)\src\RestSharp.Serializers.NewtonsoftJson\RestSharp.Serializers.NewtonsoftJson.csproj"/> | ||
<ProjectReference Include="$(RepoRoot)\test\RestSharp.Tests.Shared\RestSharp.Tests.Shared.csproj"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Remove="BenchmarkDotNet.Artifacts\**" /> | ||
<EmbeddedResource Remove="BenchmarkDotNet.Artifacts\**" /> | ||
<None Remove="BenchmarkDotNet.Artifacts\**" /> | ||
<Compile Remove="BenchmarkDotNet.Artifacts\**"/> | ||
<EmbeddedResource Remove="BenchmarkDotNet.Artifacts\**"/> | ||
<None Remove="BenchmarkDotNet.Artifacts\**"/> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
--- | ||
title: Interceptors | ||
--- | ||
|
||
## Intercepting requests and responses | ||
|
||
Interceptors are a powerful feature of RestSharp that allows you to modify requests and responses before they are sent or received. You can use interceptors to add headers, modify the request body, or even cancel the request. You can also use interceptors to modify the response before it is returned to the caller. | ||
|
||
### Implementing an interceptor | ||
|
||
To implement an interceptor, you need to create a class that inherits the `Interceptor` base class. The base class implements all interceptor methods as virtual, so you can override them in your derived class. | ||
|
||
Methods that you can override are: | ||
- `BeforeRequest(RestRequest request, CancellationToken cancellationToken)` | ||
- `AfterRequest(RestResponse response, CancellationToken cancellationToken)` | ||
- `BeforeHttpRequest(HttpRequestMessage requestMessage, CancellationToken cancellationToken)` | ||
- `AfterHttpResponse(HttpResponseMessage responseMessage, CancellationToken cancellationToken)` | ||
- `BeforeDeserialization(RestResponse response, CancellationToken cancellationToken)` | ||
|
||
All those functions must return a `ValueTask` instance. | ||
|
||
Here's an example of an interceptor that adds a header to a request: | ||
|
||
```csharp | ||
// This interceptor adds a header to the request | ||
// You'd not normally use this interceptor, as RestSharp already has a method | ||
// to add headers to the request | ||
class HeaderInterceptor(string headerName, string headerValue) : Interceptors.Interceptor { | ||
public override ValueTask BeforeHttpRequest(HttpRequestMessage requestMessage, CancellationToken cancellationToken) { | ||
requestMessage.Headers.Add(headerName, headerValue); | ||
return ValueTask.CompletedTask; | ||
} | ||
} | ||
``` | ||
|
||
Because interceptor functions return `ValueTask`, you can use `async` and `await` inside them. | ||
|
||
### Using an interceptor | ||
|
||
It's possible to add as many interceptors as you want, both to the client and to the request. The interceptors are executed in the order they were added. | ||
|
||
Adding interceptors to the client is done via the client options: | ||
|
||
```csharp | ||
var options = new RestClientOptions("https://api.example.com") { | ||
Interceptors = [new HeaderInterceptor("Authorization", token)] | ||
}; | ||
var client = new RestClient(options); | ||
``` | ||
|
||
When you add an interceptor to the client, it will be executed for every request made by that client. | ||
|
||
You can also add an interceptor to a specific request: | ||
|
||
```csharp | ||
var request = new RestRequest("resource") { | ||
Interceptors = [new HeaderInterceptor("Authorization", token)] | ||
}; | ||
``` | ||
|
||
In this case, the interceptor will only be executed for that specific request. | ||
|
||
### Deprecation notice | ||
|
||
Interceptors aim to replace the existing request hooks available in RestSharp prior to version 111.0. Those hooks are marked with `Obsolete` attribute and will be removed in the future. If you are using those hooks, we recommend migrating to interceptors as soon as possible. | ||
|
||
To make the migration easier, RestSharp provides a class called `CompatibilityInterceptor`. It has properties for the hooks available in RestSharp 110.0 and earlier. You can use it to migrate your code to interceptors without changing the existing logic. | ||
|
||
For example, a code that uses `OnBeforeRequest` hook: | ||
|
||
```csharp | ||
var request = new RestRequest("success"); | ||
request.OnBeforeDeserialization += _ => throw new Exception(exceptionMessage); | ||
``` | ||
|
||
Can be migrated to interceptors like this: | ||
|
||
```csharp | ||
var request = new RestRequest("success") { | ||
Interceptors = [new CompatibilityInterceptor { | ||
OnBeforeDeserialization = _ => throw new Exception(exceptionMessage) | ||
}] | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.