Skip to content

Commit

Permalink
Add factory docs
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyzimarev committed Mar 12, 2023
1 parent b9753f6 commit 6174a29
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 54 deletions.
22 changes: 22 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,28 @@ First, there's `DownloadDataAsync`, which returns `Task<byte[]`. It will read th

For larger responses, you can use `DownloadStreamAsync` that returns `Task<Stream>`. This function allows you to open a stream reader and asynchronously stream large responses to memory or disk.


## Reusing HttpClient

RestSharp uses `HttpClient` internally to make HTTP requests. It's possible to reuse the same `HttpClient` instance for multiple `RestClient` instances. This is useful when you want to share the same connection pool between multiple `RestClient` instances.

One way of doing it is to use `RestClient` constructors that accept an instance of `HttpClient` or `HttpMessageHandler` as an argument. Note that in that case not all the options provided via `RestClientOptions` will be used. Here is the list of options that will work:

- `BaseAddress` will be used to set the base address of the `HttpClient` instance if base address is not set there already.
- `MaxTimeout`
- `UserAgent` will be set if the `User-Agent` header is not set on the `HttpClient` instance already.
- `Expect100Continue`

Another option is to use a simple HTTP client factory. It is a static factory, which holds previously instantiated `HttpClient` instances. It can be used to create `RestClient` instances that share the same `HttpClient` instance. The cache key is the `BaseUrl` provided in the options. When you opt-in to use the factory and don't set `BaseUrl`, the `RestClient` constructor will crash.

```csharp
var client = new RestClient(new Uri("https://example.org/api"), useClientFactory: true);
```

::: warning
Note that the `RestClient` constructor will not reconfigure the `HttpClient` instance if it's already in the cache. Therefore, you should not try using the factory when providing different options for the same base URL.
:::

## Blazor support

Inside a Blazor webassembly app, you can make requests to external API endpoints. Microsoft examples show how to do it with `HttpClient`, and it's also possible to use RestSharp for the same purpose.
Expand Down
108 changes: 54 additions & 54 deletions docs/v107/interfaces.md
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
| `IRestClient` member | Where is it now? |
|:------------------------------------------------------------------------------------------------|:-----------------------------------|
| `CookieContainer` | `RestClient` |
| `AutomaticDecompression` | `RestClientOptions`, changed type |
| `MaxRedirects` | `RestClientOptions` |
| `UserAgent` | `RestClientOptions` |
| `Timeout` | `RestClientOptions`, `RestRequest` |
| `Authenticator` | `RestClient` |
| `BaseUrl` | `RestClientOptions` |
| `Encoding` | `RestClientOptions` |
| `ThrowOnDeserializationError` | `RestClientOptions` |
| `FailOnDeserializationError` | `RestClientOptions` |
| `ThrowOnAnyError` | `RestClientOptions` |
| `PreAuthenticate` | `RestClientOptions` |
| `BaseHost` | `RestClientOptions` |
| `AllowMultipleDefaultParametersWithSameName` | `RestClientOptions` |
| `ClientCertificates` | `RestClientOptions` |
| `Proxy` | `RestClientOptions` |
| `CachePolicy` | `RestClientOptions`, changed type |
| `FollowRedirects` | `RestClientOptions` |
| `RemoteCertificateValidationCallback` | `RestClientOptions` |
| `Pipelined` | Not supported |
| `UnsafeAuthenticatedConnectionSharing` | Not supported |
| `ConnectionGroupName` | Not supported |
| `ReadWriteTimeout` | Not supported |
| `UseSynchronizationContext` | Not supported |
| `DefaultParameters` | `RestClient` |
| `UseSerializer(Func<IRestSerializer> serializerFactory)` | `RestClient` |
| `UseSerializer<T>()` | `RestClient` |
| `Deserialize<T>(IRestResponse response)` | `RestClient` |
| `UseUrlEncoder(Func<string, string> encoder)` | `RestClientOptions.Encode` |
| `UseQueryEncoder(Func<string, Encoding, string> queryEncoder)` | `RestClientOptions.EncodeQuery` |
| `ExecuteAsync<T>(IRestRequest request, CancellationToken cancellationToken)` | `RestClient` |
| `ExecuteAsync<T>(IRestRequest request, Method httpMethod, CancellationToken cancellationToken)` | Extension |
| `ExecuteAsync(IRestRequest request, Method httpMethod, CancellationToken cancellationToken)` | Extension |
| `ExecuteAsync(IRestRequest request, CancellationToken cancellationToken)` | Extension |
| `ExecuteGetAsync<T>(IRestRequest request, CancellationToken cancellationToken)` | Extension |
| `ExecutePostAsync<T>(IRestRequest request, CancellationToken cancellationToken)` | Extension |
| `ExecuteGetAsync(IRestRequest request, CancellationToken cancellationToken)` | Extension |
| `ExecutePostAsync(IRestRequest request, CancellationToken cancellationToken)` | Extension |
| `Execute(IRestRequest request)` | Deprecated |
| `Execute(IRestRequest request, Method httpMethod)` | Deprecated |
| `Execute<T>(IRestRequest request)` | Deprecated |
| `Execute<T>(IRestRequest request, Method httpMethod)` | Deprecated |
| `DownloadData(IRestRequest request)` | Deprecated |
| `ExecuteAsGet(IRestRequest request, string httpMethod)` | Deprecated |
| `ExecuteAsPost(IRestRequest request, string httpMethod)` | Deprecated |
| `ExecuteAsGet<T>(IRestRequest request, string httpMethod)` | Deprecated |
| `ExecuteAsPost<T>(IRestRequest request, string httpMethod)` | Deprecated |
| `BuildUriWithoutQueryParameters(IRestRequest request)` | Removed |
| `ConfigureWebRequest(Action<HttpWebRequest> configurator)` | Removed |
| `AddHandler(string contentType, Func<IDeserializer> deserializerFactory)` | Removed |
| `RemoveHandler(string contentType)` | Removed |
| `ClearHandlers()` | Removed |
| `IRestClient` member | Where is it now? |
|:------------------------------------------------------------------------------------------------|:-------------------------------------------|
| `CookieContainer` | Not supported, use `RestRequest.AddCookie` |
| `AutomaticDecompression` | `RestClientOptions`, changed type |
| `MaxRedirects` | `RestClientOptions` |
| `UserAgent` | `RestClientOptions` |
| `Timeout` | `RestClientOptions`, `RestRequest` |
| `Authenticator` | `RestClient` |
| `BaseUrl` | `RestClientOptions` |
| `Encoding` | `RestClientOptions` |
| `ThrowOnDeserializationError` | `RestClientOptions` |
| `FailOnDeserializationError` | `RestClientOptions` |
| `ThrowOnAnyError` | `RestClientOptions` |
| `PreAuthenticate` | `RestClientOptions` |
| `BaseHost` | `RestClientOptions` |
| `AllowMultipleDefaultParametersWithSameName` | `RestClientOptions` |
| `ClientCertificates` | `RestClientOptions` |
| `Proxy` | `RestClientOptions` |
| `CachePolicy` | `RestClientOptions`, changed type |
| `FollowRedirects` | `RestClientOptions` |
| `RemoteCertificateValidationCallback` | `RestClientOptions` |
| `Pipelined` | Not supported |
| `UnsafeAuthenticatedConnectionSharing` | Not supported |
| `ConnectionGroupName` | Not supported |
| `ReadWriteTimeout` | Not supported |
| `UseSynchronizationContext` | Not supported |
| `DefaultParameters` | `RestClient` |
| `UseSerializer(Func<IRestSerializer> serializerFactory)` | `RestClient` |
| `UseSerializer<T>()` | `RestClient` |
| `Deserialize<T>(IRestResponse response)` | `RestClient` |
| `UseUrlEncoder(Func<string, string> encoder)` | `RestClientOptions.Encode` |
| `UseQueryEncoder(Func<string, Encoding, string> queryEncoder)` | `RestClientOptions.EncodeQuery` |
| `ExecuteAsync<T>(IRestRequest request, CancellationToken cancellationToken)` | `RestClient` |
| `ExecuteAsync<T>(IRestRequest request, Method httpMethod, CancellationToken cancellationToken)` | Extension |
| `ExecuteAsync(IRestRequest request, Method httpMethod, CancellationToken cancellationToken)` | Extension |
| `ExecuteAsync(IRestRequest request, CancellationToken cancellationToken)` | Extension |
| `ExecuteGetAsync<T>(IRestRequest request, CancellationToken cancellationToken)` | Extension |
| `ExecutePostAsync<T>(IRestRequest request, CancellationToken cancellationToken)` | Extension |
| `ExecuteGetAsync(IRestRequest request, CancellationToken cancellationToken)` | Extension |
| `ExecutePostAsync(IRestRequest request, CancellationToken cancellationToken)` | Extension |
| `Execute(IRestRequest request)` | Extension |
| `Execute(IRestRequest request, Method httpMethod)` | Extension |
| `Execute<T>(IRestRequest request)` | Extension |
| `Execute<T>(IRestRequest request, Method httpMethod)` | Extension |
| `DownloadData(IRestRequest request)` | Extension |
| `ExecuteAsGet(IRestRequest request, string httpMethod)` | Extension |
| `ExecuteAsPost(IRestRequest request, string httpMethod)` | Extension |
| `ExecuteAsGet<T>(IRestRequest request, string httpMethod)` | Extension |
| `ExecuteAsPost<T>(IRestRequest request, string httpMethod)` | Extension |
| `BuildUriWithoutQueryParameters(IRestRequest request)` | Removed |
| `ConfigureWebRequest(Action<HttpWebRequest> configurator)` | Removed |
| `AddHandler(string contentType, Func<IDeserializer> deserializerFactory)` | Removed |
| `RemoveHandler(string contentType)` | Removed |
| `ClearHandlers()` | Removed |

| `IRestRequest` member | Where is it now? |
|:-------------------------------------------------------------------------------------------------------|:---------------------------------|
Expand Down

0 comments on commit 6174a29

Please sign in to comment.