Skip to content

Commit 1838162

Browse files
committed
Merge branch 'hotfix/improve-response-capture-semantics'
2 parents 968f857 + 34ecba3 commit 1838162

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

libs/TrybeSDK/ApiClient.cs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -159,22 +159,21 @@ protected internal async Task<TrybeResponse<TResponse>> FetchAsync<TResponse>(
159159
httpResp = await _http.SendAsync(httpReq, cancellationToken)
160160
.ConfigureAwait(false);
161161

162-
var transformedResponse = await TransformResponse<TResponse>(
162+
var (transformedResponse, capturedResponseContent) = await TransformResponse<TResponse>(
163163
httpReq.Method,
164164
httpReq.RequestUri,
165165
httpResp)
166166
.ConfigureAwait(false); ;
167167

168-
if (_settings.CaptureRequestContent && httpReq.Content is not null)
168+
if ((_settings.CaptureRequestContent || !httpResp.IsSuccessStatusCode) && httpReq.Content is not null)
169169
{
170170
transformedResponse.RequestContent = await httpReq.Content.ReadAsStringAsync()
171171
.ConfigureAwait(false); ;
172172
}
173173

174-
if (_settings.CaptureResponseContent && httpResp.Content is not null)
174+
if (_settings.CaptureResponseContent || !httpResp.IsSuccessStatusCode)
175175
{
176-
transformedResponse.ResponseContent = await httpResp.Content.ReadAsStringAsync()
177-
.ConfigureAwait(false);
176+
transformedResponse.ResponseContent = capturedResponseContent;
178177
}
179178

180179
return transformedResponse;
@@ -219,22 +218,21 @@ protected internal async Task<TrybeResponse<TResponse>> FetchAsync<TRequest, TRe
219218
httpResp = await _http.SendAsync(httpReq, cancellationToken)
220219
.ConfigureAwait(false);
221220

222-
var transformedResponse = await TransformResponse<TResponse>(
221+
var (transformedResponse, capturedResponseContent) = await TransformResponse<TResponse>(
223222
httpReq.Method,
224223
httpReq.RequestUri,
225224
httpResp)
226225
.ConfigureAwait(false); ;
227226

228-
if (_settings.CaptureRequestContent && httpReq.Content is not null)
227+
if ((_settings.CaptureRequestContent || !httpResp.IsSuccessStatusCode) && httpReq.Content is not null)
229228
{
230229
transformedResponse.RequestContent = await httpReq.Content.ReadAsStringAsync()
231-
.ConfigureAwait(false);
230+
.ConfigureAwait(false); ;
232231
}
233232

234-
if (_settings.CaptureResponseContent && httpResp.Content is not null)
233+
if (_settings.CaptureResponseContent || !httpResp.IsSuccessStatusCode)
235234
{
236-
transformedResponse.ResponseContent = await httpResp.Content.ReadAsStringAsync()
237-
.ConfigureAwait(false);
235+
transformedResponse.ResponseContent = capturedResponseContent;
238236
}
239237

240238
return transformedResponse;
@@ -351,7 +349,7 @@ async Task<Error> GetTrybeError()
351349
}
352350
}
353351

354-
protected internal async Task<TrybeResponse<TResponse>> TransformResponse<TResponse>(
352+
protected internal async Task<(TrybeResponse<TResponse>, string?)> TransformResponse<TResponse>(
355353
HttpMethod method,
356354
Uri uri,
357355
HttpResponseMessage response,
@@ -386,15 +384,31 @@ async Task<Error> GetTrybeError()
386384

387385
var rateLimiting = GetRateLimiting(response);
388386

387+
Stream? content = null;
388+
string? stringContent = null;
389+
if (response.Content is not null)
390+
{
391+
if (_settings.CaptureResponseContent || !response.IsSuccessStatusCode)
392+
{
393+
stringContent = await response.Content.ReadAsStringAsync()
394+
.ConfigureAwait(false);
395+
}
396+
else
397+
{
398+
content = await response.Content.ReadAsStreamAsync()
399+
.ConfigureAwait(false);
400+
}
401+
}
402+
389403
if (response.IsSuccessStatusCode)
390404
{
391405
DataContainer<TResponse>? data = default;
392406
Meta? meta = default;
393-
if (response.Content is not null)
407+
if (content is not null)
394408
{
395-
data = await response.Content.ReadFromJsonAsync<DataContainer<TResponse>>(
396-
_deserializerOptions, cancellationToken)
397-
.ConfigureAwait(false); ;
409+
data = stringContent is { Length: > 0 }
410+
? JsonSerializer.Deserialize<DataContainer<TResponse>>(stringContent, _deserializerOptions)
411+
: await JsonSerializer.DeserializeAsync<DataContainer<TResponse>>(content, _deserializerOptions).ConfigureAwait(false);
398412

399413
if (data?.Meta is not null)
400414
{
@@ -408,29 +422,29 @@ async Task<Error> GetTrybeError()
408422
}
409423
}
410424

411-
return new TrybeResponse<TResponse>(
425+
return (new TrybeResponse<TResponse>(
412426
method,
413427
uri,
414428
response.IsSuccessStatusCode,
415429
response.StatusCode,
416430
data: data?.Data,
417431
meta: meta,
418432
rateLimiting: rateLimiting
419-
);
433+
), stringContent);
420434
}
421435
else
422436
{
423437
Error? error = await GetTrybeError()
424438
.ConfigureAwait(false);
425439

426-
return new TrybeResponse<TResponse>(
440+
return (new TrybeResponse<TResponse>(
427441
method,
428442
uri,
429443
response.IsSuccessStatusCode,
430444
response.StatusCode,
431445
rateLimiting: rateLimiting,
432446
error: error
433-
);
447+
), stringContent);
434448
}
435449
}
436450

0 commit comments

Comments
 (0)