-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFluentTimeoutExtensions.cs
More file actions
58 lines (52 loc) · 2.22 KB
/
FluentTimeoutExtensions.cs
File metadata and controls
58 lines (52 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
namespace FluentHttpClient;
/// <summary>
/// Provides extension methods for configuring timeouts in Fluent HTTP Client.
/// </summary>
public static class FluentTimeoutExtensions
{
internal static readonly string MessageInvalidTimeout = "Timeout must be a positive value.";
/// <summary>
/// Sets a per-request timeout using the specified number of seconds. Must be positive.
/// </summary>
/// <remarks>
/// The timeout applies only to the current request and determines how long
/// the client will wait for the request to complete before canceling it.
/// It does not modify <see cref="HttpClient.Timeout"/> or apply to
/// other requests made with the same <see cref="HttpClient"/> instance.
/// </remarks>
/// <param name="builder"></param>
/// <param name="seconds"></param>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static HttpRequestBuilder WithTimeout(this HttpRequestBuilder builder, int seconds)
{
if (seconds <= 0)
{
throw new ArgumentOutOfRangeException(nameof(seconds), MessageInvalidTimeout);
}
return builder.WithTimeout(TimeSpan.FromSeconds(seconds));
}
/// <summary>
/// Sets a per-request timeout using the specified <see cref="TimeSpan"/> value. Must be positive.
/// </summary>
/// <remarks>
/// The timeout applies only to the current request and determines how long
/// the client will wait for the request to complete before canceling it.
/// It does not modify <see cref="HttpClient.Timeout"/> or apply to
/// other requests made with the same <see cref="HttpClient"/> instance.
/// </remarks>
/// <param name="builder"></param>
/// <param name="timeout"></param>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static HttpRequestBuilder WithTimeout(this HttpRequestBuilder builder, TimeSpan timeout)
{
Guard.AgainstNull(timeout, nameof(timeout));
if (timeout <= TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(timeout), MessageInvalidTimeout);
}
builder.Timeout = timeout;
return builder;
}
}