Skip to content

Commit

Permalink
Merge branch 'hotfix/improve-response-capture-semantics'
Browse files Browse the repository at this point in the history
  • Loading branch information
Antaris committed Dec 23, 2024
2 parents 968f857 + 34ecba3 commit 1838162
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions libs/TrybeSDK/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,22 +159,21 @@ protected internal async Task<TrybeResponse<TResponse>> FetchAsync<TResponse>(
httpResp = await _http.SendAsync(httpReq, cancellationToken)
.ConfigureAwait(false);

var transformedResponse = await TransformResponse<TResponse>(
var (transformedResponse, capturedResponseContent) = await TransformResponse<TResponse>(
httpReq.Method,
httpReq.RequestUri,
httpResp)
.ConfigureAwait(false); ;

if (_settings.CaptureRequestContent && httpReq.Content is not null)
if ((_settings.CaptureRequestContent || !httpResp.IsSuccessStatusCode) && httpReq.Content is not null)
{
transformedResponse.RequestContent = await httpReq.Content.ReadAsStringAsync()
.ConfigureAwait(false); ;
}

if (_settings.CaptureResponseContent && httpResp.Content is not null)
if (_settings.CaptureResponseContent || !httpResp.IsSuccessStatusCode)
{
transformedResponse.ResponseContent = await httpResp.Content.ReadAsStringAsync()
.ConfigureAwait(false);
transformedResponse.ResponseContent = capturedResponseContent;
}

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

var transformedResponse = await TransformResponse<TResponse>(
var (transformedResponse, capturedResponseContent) = await TransformResponse<TResponse>(
httpReq.Method,
httpReq.RequestUri,
httpResp)
.ConfigureAwait(false); ;

if (_settings.CaptureRequestContent && httpReq.Content is not null)
if ((_settings.CaptureRequestContent || !httpResp.IsSuccessStatusCode) && httpReq.Content is not null)
{
transformedResponse.RequestContent = await httpReq.Content.ReadAsStringAsync()
.ConfigureAwait(false);
.ConfigureAwait(false); ;
}

if (_settings.CaptureResponseContent && httpResp.Content is not null)
if (_settings.CaptureResponseContent || !httpResp.IsSuccessStatusCode)
{
transformedResponse.ResponseContent = await httpResp.Content.ReadAsStringAsync()
.ConfigureAwait(false);
transformedResponse.ResponseContent = capturedResponseContent;
}

return transformedResponse;
Expand Down Expand Up @@ -351,7 +349,7 @@ async Task<Error> GetTrybeError()
}
}

protected internal async Task<TrybeResponse<TResponse>> TransformResponse<TResponse>(
protected internal async Task<(TrybeResponse<TResponse>, string?)> TransformResponse<TResponse>(
HttpMethod method,
Uri uri,
HttpResponseMessage response,
Expand Down Expand Up @@ -386,15 +384,31 @@ async Task<Error> GetTrybeError()

var rateLimiting = GetRateLimiting(response);

Stream? content = null;
string? stringContent = null;
if (response.Content is not null)
{
if (_settings.CaptureResponseContent || !response.IsSuccessStatusCode)
{
stringContent = await response.Content.ReadAsStringAsync()
.ConfigureAwait(false);
}
else
{
content = await response.Content.ReadAsStreamAsync()
.ConfigureAwait(false);
}
}

if (response.IsSuccessStatusCode)
{
DataContainer<TResponse>? data = default;
Meta? meta = default;
if (response.Content is not null)
if (content is not null)
{
data = await response.Content.ReadFromJsonAsync<DataContainer<TResponse>>(
_deserializerOptions, cancellationToken)
.ConfigureAwait(false); ;
data = stringContent is { Length: > 0 }
? JsonSerializer.Deserialize<DataContainer<TResponse>>(stringContent, _deserializerOptions)
: await JsonSerializer.DeserializeAsync<DataContainer<TResponse>>(content, _deserializerOptions).ConfigureAwait(false);

if (data?.Meta is not null)
{
Expand All @@ -408,29 +422,29 @@ async Task<Error> GetTrybeError()
}
}

return new TrybeResponse<TResponse>(
return (new TrybeResponse<TResponse>(
method,
uri,
response.IsSuccessStatusCode,
response.StatusCode,
data: data?.Data,
meta: meta,
rateLimiting: rateLimiting
);
), stringContent);
}
else
{
Error? error = await GetTrybeError()
.ConfigureAwait(false);

return new TrybeResponse<TResponse>(
return (new TrybeResponse<TResponse>(
method,
uri,
response.IsSuccessStatusCode,
response.StatusCode,
rateLimiting: rateLimiting,
error: error
);
), stringContent);
}
}

Expand Down

0 comments on commit 1838162

Please sign in to comment.