Skip to content

Commit 96b705e

Browse files
feat(client): support request timeout
1 parent 01495b2 commit 96b705e

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

src/ArcadeDotnet/ArcadeClient.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Net.Http;
3+
using System.Threading;
34
using System.Threading.Tasks;
45
using ArcadeDotnet.Core;
56
using ArcadeDotnet.Exceptions;
@@ -28,6 +29,12 @@ public Uri BaseUrl
2829
init { this._options.BaseUrl = value; }
2930
}
3031

32+
public TimeSpan Timeout
33+
{
34+
get { return this._options.Timeout; }
35+
init { this._options.Timeout = value; }
36+
}
37+
3138
public string APIKey
3239
{
3340
get { return this._options.APIKey; }
@@ -78,11 +85,16 @@ public async Task<HttpResponse> Execute<T>(HttpRequest<T> request)
7885
Content = request.Params.BodyContent(),
7986
};
8087
request.Params.AddHeadersToRequest(requestMessage, this);
88+
using CancellationTokenSource cts = new(this.Timeout);
8189
HttpResponseMessage responseMessage;
8290
try
8391
{
8492
responseMessage = await this
85-
.HttpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead)
93+
.HttpClient.SendAsync(
94+
requestMessage,
95+
HttpCompletionOption.ResponseHeadersRead,
96+
cts.Token
97+
)
8698
.ConfigureAwait(false);
8799
}
88100
catch (HttpRequestException e1)

src/ArcadeDotnet/Core/ClientOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public Uri BaseUrl
1717
set { _baseUrl = new(() => value); }
1818
}
1919

20+
public TimeSpan Timeout { get; set; } = TimeSpan.FromMinutes(1);
21+
2022
/// <summary>
2123
/// API key used for authorization in header
2224
/// </summary>

src/ArcadeDotnet/IArcadeClient.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public interface IArcadeClient
1717

1818
Uri BaseUrl { get; init; }
1919

20+
TimeSpan Timeout { get; init; }
21+
2022
/// <summary>
2123
/// API key used for authorization in header
2224
/// </summary>

0 commit comments

Comments
 (0)