Skip to content

Commit c787d98

Browse files
committed
t push origin dev
Merge branch 'RomanSoloweow-dev' into dev
2 parents 6891792 + 1aa5b02 commit c787d98

File tree

7 files changed

+21
-22
lines changed

7 files changed

+21
-22
lines changed

src/RestSharp/Options/RestClientOptions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,9 @@ public RestClientOptions(string baseUrl) : this(new Uri(Ensure.NotEmptyString(ba
180180
public CookieContainer? CookieContainer { get; set; }
181181

182182
/// <summary>
183-
/// Maximum request duration in milliseconds. When the request timeout is specified using <seealso cref="RestRequest.Timeout"/>,
184-
/// the lowest value between the client timeout and request timeout will be used.
183+
/// Request duration. Used when the request timeout is not specified using <seealso cref="RestRequest.Timeout"/>,
185184
/// </summary>
186-
public int MaxTimeout { get; set; }
185+
public TimeSpan? Timeout { get; set; }
187186

188187
/// <summary>
189188
/// Default encoding to use when no encoding is specified in the content type header.

src/RestSharp/Request/RestRequest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public RestRequest(Uri resource, Method method = Method.Get)
139139
/// <summary>
140140
/// Custom request timeout
141141
/// </summary>
142-
public int Timeout { get; set; }
142+
public TimeSpan? Timeout { get; set; }
143143

144144
/// <summary>
145145
/// The Resource URL to make the request against.
@@ -163,7 +163,7 @@ public RestRequest(Uri resource, Method method = Method.Get)
163163

164164
/// <summary>
165165
/// Used by the default deserializers to determine where to start deserializing from.
166-
/// Can be used to skip container or root elements that do not have corresponding deserialzation targets.
166+
/// Can be used to skip container or root elements that do not have corresponding deserialization targets.
167167
/// </summary>
168168
public string? RootElement { get; set; }
169169

src/RestSharp/RestClient.Async.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
namespace RestSharp;
2020

2121
public partial class RestClient {
22+
// Default HttpClient timeout
23+
public TimeSpan DefaultTimeout = TimeSpan.FromSeconds(100);
2224
/// <inheritdoc />
2325
public async Task<RestResponse> ExecuteAsync(RestRequest request, CancellationToken cancellationToken = default) {
2426
using var internalResponse = await ExecuteRequestAsync(request, cancellationToken).ConfigureAwait(false);
@@ -113,7 +115,7 @@ async Task<HttpResponse> ExecuteRequestAsync(RestRequest request, CancellationTo
113115
message.Headers.Host = Options.BaseHost;
114116
message.Headers.CacheControl = request.CachePolicy ?? Options.CachePolicy;
115117

116-
using var timeoutCts = new CancellationTokenSource(request.Timeout > 0 ? request.Timeout : int.MaxValue);
118+
using var timeoutCts = new CancellationTokenSource(request.Timeout ?? Options.Timeout ?? DefaultTimeout);
117119
using var cts = CancellationTokenSource.CreateLinkedTokenSource(timeoutCts.Token, cancellationToken);
118120

119121
var ct = cts.Token;

src/RestSharp/RestClient.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ namespace RestSharp;
3333
/// <summary>
3434
/// Client to translate RestRequests into Http requests and process response result
3535
/// </summary>
36+
// ReSharper disable once ClassWithVirtualMembersNeverInherited.Global
3637
public partial class RestClient : IRestClient {
3738
/// <summary>
3839
/// Content types that will be sent in the Accept header. The list is populated from the known serializers.
@@ -55,11 +56,6 @@ public string[] AcceptedContentTypes {
5556
/// <inheritdoc/>
5657
public DefaultParameters DefaultParameters { get; }
5758

58-
[Obsolete("Use RestClientOptions.Authenticator instead")]
59-
public IAuthenticator? Authenticator => Options.Authenticator;
60-
61-
// set => Options.Authenticator = value;
62-
6359
/// <summary>
6460
/// Creates an instance of RestClient using the provided <see cref="RestClientOptions"/>
6561
/// </summary>
@@ -226,7 +222,8 @@ public RestClient(
226222
: this(new HttpClient(handler, disposeHandler), true, configureRestClient, configureSerialization) { }
227223

228224
static void ConfigureHttpClient(HttpClient httpClient, RestClientOptions options) {
229-
if (options.MaxTimeout > 0) httpClient.Timeout = TimeSpan.FromMilliseconds(options.MaxTimeout);
225+
// We will use Options.Timeout in ExecuteAsInternalAsync method
226+
httpClient.Timeout = Timeout.InfiniteTimeSpan;
230227

231228
if (options.Expect100Continue != null) httpClient.DefaultRequestHeaders.ExpectContinue = options.Expect100Continue;
232229
}
@@ -257,6 +254,7 @@ static void ConfigureHttpMessageHandler(HttpClientHandler handler, RestClientOpt
257254
handler.AllowAutoRedirect = options.FollowRedirects;
258255

259256
#if NET
257+
// ReSharper disable once InvertIf
260258
if (!OperatingSystem.IsBrowser() && !OperatingSystem.IsIOS() && !OperatingSystem.IsTvOS()) {
261259
#endif
262260
if (handler.SupportsProxy) handler.Proxy = options.Proxy;
@@ -276,12 +274,12 @@ void ConfigureSerializers(ConfigureSerialization? configureSerialization) {
276274
}
277275

278276
void ConfigureDefaultParameters(RestClientOptions options) {
279-
if (options.UserAgent != null) {
280-
if (!options.AllowMultipleDefaultParametersWithSameName
281-
&& DefaultParameters.Any(parameter => parameter.Type == ParameterType.HttpHeader && parameter.Name == KnownHeaders.UserAgent))
282-
DefaultParameters.RemoveParameter(KnownHeaders.UserAgent, ParameterType.HttpHeader);
283-
DefaultParameters.AddParameter(Parameter.CreateParameter(KnownHeaders.UserAgent, options.UserAgent, ParameterType.HttpHeader));
284-
}
277+
if (options.UserAgent == null) return;
278+
279+
if (!options.AllowMultipleDefaultParametersWithSameName
280+
&& DefaultParameters.Any(parameter => parameter.Type == ParameterType.HttpHeader && parameter.Name == KnownHeaders.UserAgent))
281+
DefaultParameters.RemoveParameter(KnownHeaders.UserAgent, ParameterType.HttpHeader);
282+
DefaultParameters.AddParameter(Parameter.CreateParameter(KnownHeaders.UserAgent, options.UserAgent, ParameterType.HttpHeader));
285283
}
286284

287285
readonly bool _disposeHttpClient;

test/RestSharp.Tests.Integrated/NonProtocolExceptionHandlingTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public async Task Handles_HttpClient_Timeout_Error() {
4545
public async Task Handles_Server_Timeout_Error() {
4646
using var client = new RestClient(_server.Url!);
4747

48-
var request = new RestRequest("timeout") { Timeout = 500 };
48+
var request = new RestRequest("404") { Timeout = TimeSpan.FromMilliseconds(500) };
4949
var response = await client.ExecuteAsync(request);
5050

5151
response.ErrorException.Should().BeOfType<TaskCanceledException>();
@@ -56,7 +56,7 @@ public async Task Handles_Server_Timeout_Error() {
5656
public async Task Handles_Server_Timeout_Error_With_Deserializer() {
5757
using var client = new RestClient(_server.Url!);
5858

59-
var request = new RestRequest("timeout") { Timeout = 500 };
59+
var request = new RestRequest("timeout") { Timeout = TimeSpan.FromMilliseconds(500) };
6060
var response = await client.ExecuteAsync<SuccessResponse>(request);
6161

6262
response.Data.Should().BeNull();

test/RestSharp.Tests.Integrated/PutTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public async Task Can_Timeout_PUT_Async() {
3131
var request = new RestRequest("/timeout", Method.Put).AddBody("Body_Content");
3232

3333
// Half the value of ResponseHandler.Timeout
34-
request.Timeout = 200;
34+
request.Timeout = TimeSpan.FromMilliseconds(200);
3535

3636
var response = await _client.ExecuteAsync(request);
3737

test/RestSharp.Tests.Integrated/RequestTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public async Task Can_Timeout_GET_Async() {
5555
var request = new RestRequest("timeout").AddBody("Body_Content");
5656

5757
// Half the value of ResponseHandler.Timeout
58-
request.Timeout = 200;
58+
request.Timeout = TimeSpan.FromMilliseconds(200);
5959

6060
var response = await _client.ExecuteAsync(request);
6161

0 commit comments

Comments
 (0)