-
-
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.
- Added docs - Refactored tests for the hooks - Updated packages
- Loading branch information
1 parent
78d7d0b
commit 74970c2
Showing
18 changed files
with
266 additions
and
60 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,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
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,2 +1,24 @@ | ||
# About | ||
|
||
The `RestSharp.Serializers.CsvHelper` library provides a CSV serializer for RestSharp. It is based on the | ||
`CsvHelper` library. | ||
|
||
# How to use | ||
|
||
Use the extension method provided by the package to configure the client: | ||
|
||
```csharp | ||
var client = new RestClient( | ||
options, | ||
configureSerialization: s => s.UseCsvHelper() | ||
); | ||
``` | ||
|
||
You can also supply your instance of `CsvConfiguration` as a parameter for the extension method. | ||
|
||
```csharp | ||
var client = new RestClient( | ||
options, | ||
configureSerialization: s => s.UseCsvHelper(new CsvConfiguration(CultureInfo.InvariantCulture) {...}) | ||
); | ||
``` |
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,40 @@ | ||
// Copyright (c) .NET Foundation and Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
namespace RestSharp.Interceptors; | ||
|
||
/// <summary> | ||
/// This class allows easier migration of legacy request hooks to interceptors. | ||
/// </summary> | ||
public class CompatibilityInterceptor : Interceptor { | ||
public Action<RestResponse>? OnBeforeDeserialization { get; set; } | ||
public Func<HttpRequestMessage, ValueTask>? OnBeforeRequest { get; set; } | ||
public Func<HttpResponseMessage, ValueTask>? OnAfterRequest { get; set; } | ||
|
||
/// <inheritdoc /> | ||
public override ValueTask BeforeDeserialization(RestResponse response, CancellationToken cancellationToken) { | ||
OnBeforeDeserialization?.Invoke(response); | ||
return default; | ||
} | ||
|
||
public override ValueTask BeforeHttpRequest(HttpRequestMessage requestMessage, CancellationToken cancellationToken) { | ||
OnBeforeRequest?.Invoke(requestMessage); | ||
return default; | ||
} | ||
|
||
public override ValueTask AfterHttpRequest(HttpResponseMessage responseMessage, CancellationToken cancellationToken) { | ||
OnAfterRequest?.Invoke(responseMessage); | ||
return default; | ||
} | ||
} |
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
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
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
Oops, something went wrong.