Skip to content

Commit

Permalink
t push origin dev
Browse files Browse the repository at this point in the history
Merge branch 'RomanSoloweow-dev' into dev
  • Loading branch information
alexeyzimarev committed May 22, 2024
2 parents 6891792 + 1aa5b02 commit c787d98
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 22 deletions.
5 changes: 2 additions & 3 deletions src/RestSharp/Options/RestClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,9 @@ public RestClientOptions(string baseUrl) : this(new Uri(Ensure.NotEmptyString(ba
public CookieContainer? CookieContainer { get; set; }

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

/// <summary>
/// Default encoding to use when no encoding is specified in the content type header.
Expand Down
4 changes: 2 additions & 2 deletions src/RestSharp/Request/RestRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public RestRequest(Uri resource, Method method = Method.Get)
/// <summary>
/// Custom request timeout
/// </summary>
public int Timeout { get; set; }
public TimeSpan? Timeout { get; set; }

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

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

Expand Down
4 changes: 3 additions & 1 deletion src/RestSharp/RestClient.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
namespace RestSharp;

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

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

var ct = cts.Token;
Expand Down
22 changes: 10 additions & 12 deletions src/RestSharp/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace RestSharp;
/// <summary>
/// Client to translate RestRequests into Http requests and process response result
/// </summary>
// ReSharper disable once ClassWithVirtualMembersNeverInherited.Global
public partial class RestClient : IRestClient {
/// <summary>
/// Content types that will be sent in the Accept header. The list is populated from the known serializers.
Expand All @@ -55,11 +56,6 @@ public string[] AcceptedContentTypes {
/// <inheritdoc/>
public DefaultParameters DefaultParameters { get; }

[Obsolete("Use RestClientOptions.Authenticator instead")]
public IAuthenticator? Authenticator => Options.Authenticator;

// set => Options.Authenticator = value;

/// <summary>
/// Creates an instance of RestClient using the provided <see cref="RestClientOptions"/>
/// </summary>
Expand Down Expand Up @@ -226,7 +222,8 @@ public RestClient(
: this(new HttpClient(handler, disposeHandler), true, configureRestClient, configureSerialization) { }

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

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

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

void ConfigureDefaultParameters(RestClientOptions options) {
if (options.UserAgent != null) {
if (!options.AllowMultipleDefaultParametersWithSameName
&& DefaultParameters.Any(parameter => parameter.Type == ParameterType.HttpHeader && parameter.Name == KnownHeaders.UserAgent))
DefaultParameters.RemoveParameter(KnownHeaders.UserAgent, ParameterType.HttpHeader);
DefaultParameters.AddParameter(Parameter.CreateParameter(KnownHeaders.UserAgent, options.UserAgent, ParameterType.HttpHeader));
}
if (options.UserAgent == null) return;

if (!options.AllowMultipleDefaultParametersWithSameName
&& DefaultParameters.Any(parameter => parameter.Type == ParameterType.HttpHeader && parameter.Name == KnownHeaders.UserAgent))
DefaultParameters.RemoveParameter(KnownHeaders.UserAgent, ParameterType.HttpHeader);
DefaultParameters.AddParameter(Parameter.CreateParameter(KnownHeaders.UserAgent, options.UserAgent, ParameterType.HttpHeader));
}

readonly bool _disposeHttpClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task Handles_HttpClient_Timeout_Error() {
public async Task Handles_Server_Timeout_Error() {
using var client = new RestClient(_server.Url!);

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

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

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

response.Data.Should().BeNull();
Expand Down
2 changes: 1 addition & 1 deletion test/RestSharp.Tests.Integrated/PutTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async Task Can_Timeout_PUT_Async() {
var request = new RestRequest("/timeout", Method.Put).AddBody("Body_Content");

// Half the value of ResponseHandler.Timeout
request.Timeout = 200;
request.Timeout = TimeSpan.FromMilliseconds(200);

var response = await _client.ExecuteAsync(request);

Expand Down
2 changes: 1 addition & 1 deletion test/RestSharp.Tests.Integrated/RequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async Task Can_Timeout_GET_Async() {
var request = new RestRequest("timeout").AddBody("Body_Content");

// Half the value of ResponseHandler.Timeout
request.Timeout = 200;
request.Timeout = TimeSpan.FromMilliseconds(200);

var response = await _client.ExecuteAsync(request);

Expand Down

0 comments on commit c787d98

Please sign in to comment.