Skip to content

Commit 2e152ec

Browse files
committed
Allow passing options when using custom HttpClient. It's a bit hacky as we can only use the timeout and the cookie container
1 parent 2074a5c commit 2e152ec

File tree

9 files changed

+83
-7
lines changed

9 files changed

+83
-7
lines changed

.github/workflows/build-dev.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ on:
66
- 'docs/**'
77
- 'yarn.lock'
88
- 'package.json'
9+
- '**/*.md'
910
branches:
1011
- dev
1112
tags:

.github/workflows/codeql-analysis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ name: CodeQL Analysis
22

33
on:
44
push:
5+
tags:
6+
- '*'
7+
branches:
8+
- dev
59
pull_request:
610
workflow_dispatch:
711
schedule:

.idea/.gitignore

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/RestSharp.iml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/v107/README.md

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,40 @@ This way you avoid instantiating `RestRequest` explicitly.
8888

8989
This way you can use a pre-configured `HttpClient` or `HttpMessageHandler`, customized for your needs.
9090

91-
### Recommended usage
91+
### Default serializers
92+
93+
For JSON, RestSharp will use `JsonSerializer` from the `System.Text.Json` package. This package is now referenced by default, and it is the only dependency of the RestSharp NuGet package.
94+
95+
The `Utf8` serializer package is deprecated as the package is not being updated.
96+
97+
For XML requests and responses RestSharp uses `DotNetXmlSerializer` and `DotNetXmlDeserializer`.
98+
Previously used default `XmlSerializer`, `XmlDeserializer`, and `XmlAttrobuteDeserializer` are moved to a separate package `RestSharp.Serializers.Xml`.
99+
100+
## Recommended usage
92101

93102
`RestClient` should be thread-safe. It holds an instance of `HttpClient` and `HttpMessageHandler` inside.
94103
Do not instantiate the client for a single call, otherwise you get issues with hanging connections and connection pooling won't be possible.
95104

96-
Do use use case-specific typed API clients. Use a single instance of `RestClient` internally in such an API client for making calls.
105+
Do create typed API clients for your use cases. Use a single instance of `RestClient` internally in such an API client for making calls.
106+
It would be similar to using typed clients using `HttpClient`, for example:
107+
108+
```csharp
109+
public class GitHubClient {
110+
readonly RestClient _client;
111+
112+
public GitHubClient() {
113+
_client = new RestClient("https://api.github.com/")
114+
.AddDefaultHeader(KnownHeaders.Accept, "application/vnd.github.v3+json");
115+
}
116+
117+
public Task<GitHubRepo[]> GetRepos()
118+
=> _client.GetAsync<GitHubRepo[]>("users/aspnet/repos");
119+
}
120+
```
97121

98122
Do not use one instance of `RestClient` across different API clients.
99123

100-
### Presumably solved issues
124+
## Presumably solved issues
101125

102126
The next RestSharp version presumably solves the following issues:
103127
- Connection pool starvation
@@ -108,3 +132,19 @@ The next RestSharp version presumably solves the following issues:
108132
- Intermediate certificate issue
109133
- Uploading large files (use file parameters with `Stream`)
110134
- Downloading large files (use `DownloadFileStreanAsync`)
135+
136+
## Deprecated interfaces
137+
138+
The following interfaces are removed from RestSharp:
139+
- `IRestClient`
140+
- `IRestRequest`
141+
- `IRestResponse`
142+
- `IHttp`
143+
144+
### Motivation
145+
146+
All the deprecated interfaces had only one implementation in RestSharp, so those interfaces were abstracting nothing. It is now unclear what was the purpose for adding those interfaces initially.
147+
148+
What about mocking it, you might ask? The answer is: what would you do if you use a plain `HttpClient` instance? It doesn't implement any interface for the same reason - there's nothing to abstract, and there's only one implementation. We don't recommend mocking `RestClient` in your tests when you are testing against APIs that are controlled by you or people in your organisation. Test your clients against the real thing, as REST calls are I/O-bound. Mocking REST calls is like mocking database calls, and lead to a lot of issues in production even if all your tests pass against mocks.
149+
150+
As mentioned in [Recommended usage](#recommended-usage), we advise against using `RestClient` in the application code, and advocate wrapping it inside particular API client classes. Those classes would be under your control, and you are totally free to use interfaces there. If you absolutely must mock, you can mock your interfaces instead.

src/RestSharp/RestClient.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Net;
22
using System.Text;
3-
using System.Text.RegularExpressions;
43
using RestSharp.Authenticators;
54
using RestSharp.Extensions;
65
using RestSharp.Serializers;
@@ -29,13 +28,13 @@ public partial class RestClient {
2928
/// </summary>
3029
public RestClient() : this(new RestClientOptions()) { }
3130

32-
public RestClient(HttpClient httpClient) {
31+
public RestClient(HttpClient httpClient, RestClientOptions? options = null) {
3332
UseSerializer<SystemTextJsonSerializer>();
3433
UseSerializer<XmlRestSerializer>();
3534

36-
Options = new RestClientOptions();
37-
_cookieContainer = new CookieContainer();
3835
HttpClient = httpClient;
36+
Options = options ?? new RestClientOptions();
37+
_cookieContainer = Options.CookieContainer ?? new CookieContainer();
3938

4039
if (Options.Timeout > 0)
4140
HttpClient.Timeout = TimeSpan.FromMilliseconds(Options.Timeout);

test/RestSharp.IntegrationTests/RestSharp.IntegrationTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<None Update="Assets\TestFile.txt" CopyToOutputDirectory="PreserveNewest" />
1313
</ItemGroup>
1414
<ItemGroup>
15+
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="6.0.1" />
1516
<PackageReference Include="Xunit.Extensions.Logging" Version="1.1.0" />
1617
</ItemGroup>
1718
<ItemGroup>

0 commit comments

Comments
 (0)