Skip to content

Commit 04be863

Browse files
feat(client): add HttpResponse.ReadAsStream method
1 parent 2de54d7 commit 04be863

File tree

5 files changed

+32
-12
lines changed

5 files changed

+32
-12
lines changed

src/ArcadeDotnet/ArcadeClient.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ public bool ResponseValidation
4141
init { this._options.ResponseValidation = value; }
4242
}
4343

44-
public int MaxRetries
44+
public int? MaxRetries
4545
{
4646
get { return this._options.MaxRetries; }
4747
init { this._options.MaxRetries = value; }
4848
}
4949

50-
public TimeSpan Timeout
50+
public TimeSpan? Timeout
5151
{
5252
get { return this._options.Timeout; }
5353
init { this._options.Timeout = value; }
@@ -106,7 +106,8 @@ public async Task<HttpResponse> Execute<T>(
106106
)
107107
where T : ParamsBase
108108
{
109-
if (this.MaxRetries <= 0)
109+
var maxRetries = this.MaxRetries ?? ClientOptions.DefaultMaxRetries;
110+
if (maxRetries <= 0)
110111
{
111112
return await ExecuteOnce(request, cancellationToken).ConfigureAwait(false);
112113
}
@@ -121,13 +122,13 @@ public async Task<HttpResponse> Execute<T>(
121122
}
122123
catch (Exception e)
123124
{
124-
if (++retries > this.MaxRetries || !ShouldRetry(e))
125+
if (++retries > maxRetries || !ShouldRetry(e))
125126
{
126127
throw;
127128
}
128129
}
129130

130-
if (response != null && (++retries > this.MaxRetries || !ShouldRetry(response)))
131+
if (response != null && (++retries > maxRetries || !ShouldRetry(response)))
131132
{
132133
if (response.Message.IsSuccessStatusCode)
133134
{
@@ -171,7 +172,9 @@ async Task<HttpResponse> ExecuteOnce<T>(
171172
Content = request.Params.BodyContent(),
172173
};
173174
request.Params.AddHeadersToRequest(requestMessage, this._options);
174-
using CancellationTokenSource timeoutCts = new(this.Timeout);
175+
using CancellationTokenSource timeoutCts = new(
176+
this.Timeout ?? ClientOptions.DefaultTimeout
177+
);
175178
using var cts = CancellationTokenSource.CreateLinkedTokenSource(
176179
timeoutCts.Token,
177180
cancellationToken

src/ArcadeDotnet/Core/ClientOptions.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ namespace ArcadeDotnet.Core;
66

77
public struct ClientOptions()
88
{
9+
public static readonly int DefaultMaxRetries = 2;
10+
11+
public static readonly TimeSpan DefaultTimeout = TimeSpan.FromMinutes(1);
12+
913
public HttpClient HttpClient { get; set; } = new();
1014

1115
Lazy<Uri> _baseUrl = new(() =>
@@ -19,9 +23,9 @@ public Uri BaseUrl
1923

2024
public bool ResponseValidation { get; set; } = false;
2125

22-
public int MaxRetries { get; set; } = 2;
26+
public int? MaxRetries { get; set; }
2327

24-
public TimeSpan Timeout { get; set; } = TimeSpan.FromMinutes(1);
28+
public TimeSpan? Timeout { get; set; }
2529

2630
/// <summary>
2731
/// API key used for authorization in header

src/ArcadeDotnet/Core/HttpResponse.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Net.Http;
34
using System.Text.Json;
45
using System.Threading;
@@ -22,7 +23,7 @@ public async Task<T> Deserialize<T>(CancellationToken cancellationToken = defaul
2223
try
2324
{
2425
return JsonSerializer.Deserialize<T>(
25-
await Message.Content.ReadAsStreamAsync(cts.Token).ConfigureAwait(false),
26+
await this.ReadAsStream(cts.Token).ConfigureAwait(false),
2627
ModelBase.SerializerOptions
2728
) ?? throw new ArcadeInvalidDataException("Response cannot be null");
2829
}
@@ -32,6 +33,15 @@ await Message.Content.ReadAsStreamAsync(cts.Token).ConfigureAwait(false),
3233
}
3334
}
3435

36+
public async Task<Stream> ReadAsStream(CancellationToken cancellationToken = default)
37+
{
38+
using var cts = CancellationTokenSource.CreateLinkedTokenSource(
39+
this.CancellationToken,
40+
cancellationToken
41+
);
42+
return await Message.Content.ReadAsStreamAsync(cts.Token).ConfigureAwait(false);
43+
}
44+
3545
public async Task<string> ReadAsString(CancellationToken cancellationToken = default)
3646
{
3747
using var cts = CancellationTokenSource.CreateLinkedTokenSource(

src/ArcadeDotnet/Core/ParamsBase.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,10 @@ protected static void AddDefaultHeaders(HttpRequestMessage request, ClientOption
194194
{
195195
request.Headers.Add("Authorization", options.APIKey);
196196
}
197-
request.Headers.Add("X-Stainless-Timeout", options.Timeout.TotalSeconds.ToString());
197+
request.Headers.Add(
198+
"X-Stainless-Timeout",
199+
(options.Timeout ?? ClientOptions.DefaultTimeout).TotalSeconds.ToString()
200+
);
198201
}
199202

200203
static string GetUserAgent() => $"{typeof(ArcadeClient).Name}/C# {GetPackageVersion()}";

src/ArcadeDotnet/IArcadeClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ public interface IArcadeClient
1515

1616
bool ResponseValidation { get; init; }
1717

18-
int MaxRetries { get; init; }
18+
int? MaxRetries { get; init; }
1919

20-
TimeSpan Timeout { get; init; }
20+
TimeSpan? Timeout { get; init; }
2121

2222
/// <summary>
2323
/// API key used for authorization in header

0 commit comments

Comments
 (0)