Skip to content

Commit 6efae38

Browse files
committed
fix: Capture HTTP content earlier due to HttpClient disposing of content after SendAsync on netfx
1 parent faaf924 commit 6efae38

File tree

1 file changed

+46
-28
lines changed

1 file changed

+46
-28
lines changed

libs/IssuuSDK/ApiClient.cs

+46-28
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public async Task<IssuuResponse> SendAsync(
9595
using var httpReq = CreateHttpRequest(request);
9696
HttpResponseMessage? httpResp = null;
9797

98+
string? requestContent = await CaptureRequestContent(httpReq).ConfigureAwait(false);
9899
try
99100
{
100101
httpResp = await _http.SendAsync(httpReq, cancellationToken)
@@ -106,13 +107,14 @@ public async Task<IssuuResponse> SendAsync(
106107
httpResp)
107108
.ConfigureAwait(false);
108109

109-
(transformedResponse.RequestContent, transformedResponse.ResponseContent) = await CaptureRequestResponseContent(httpReq, httpResp).ConfigureAwait(false);
110+
transformedResponse.RequestContent = requestContent;
111+
transformedResponse.ResponseContent = await CaptureResponseContent(httpResp).ConfigureAwait(false);
110112

111113
return transformedResponse;
112114
}
113115
catch (Exception ex)
114116
{
115-
return await GetIssuuErrorResponse(httpReq, httpResp, ex).ConfigureAwait(false);
117+
return await GetIssuuErrorResponse(httpReq, requestContent, httpResp, ex).ConfigureAwait(false);
116118
}
117119
finally
118120
{
@@ -129,6 +131,7 @@ public async Task<IssuuResponse> SendAsync<TRequest>(
129131
using var httpReq = CreateHttpRequest(request);
130132
HttpResponseMessage? httpResp = null;
131133

134+
string? requestContent = await CaptureRequestContent(httpReq).ConfigureAwait(false);
132135
try
133136
{
134137
httpResp = await _http.SendAsync(httpReq, cancellationToken).ConfigureAwait(false);
@@ -139,13 +142,14 @@ public async Task<IssuuResponse> SendAsync<TRequest>(
139142
httpResp)
140143
.ConfigureAwait(false);
141144

142-
(transformedResponse.RequestContent, transformedResponse.ResponseContent) = await CaptureRequestResponseContent(httpReq, httpResp).ConfigureAwait(false);
145+
transformedResponse.RequestContent = requestContent;
146+
transformedResponse.ResponseContent = await CaptureResponseContent(httpResp).ConfigureAwait(false);
143147

144148
return transformedResponse;
145149
}
146150
catch (Exception ex)
147151
{
148-
return await GetIssuuErrorResponse(httpReq, httpResp, ex).ConfigureAwait(false);
152+
return await GetIssuuErrorResponse(httpReq, requestContent, httpResp, ex).ConfigureAwait(false);
149153
}
150154
finally
151155
{
@@ -165,6 +169,7 @@ public async Task<IssuuResponse<TData>> FetchAsync<TData>(
165169
using var httpReq = CreateHttpRequest(request);
166170
HttpResponseMessage? httpResp = null;
167171

172+
string? requestContent = await CaptureRequestContent(httpReq).ConfigureAwait(false);
168173
try
169174
{
170175
httpResp = await _http.SendAsync(httpReq, cancellationToken)
@@ -178,13 +183,14 @@ public async Task<IssuuResponse<TData>> FetchAsync<TData>(
178183
cancellationToken)
179184
.ConfigureAwait(false);
180185

181-
(transformedResponse.RequestContent, transformedResponse.ResponseContent) = await CaptureRequestResponseContent(httpReq, httpResp).ConfigureAwait(false);
186+
transformedResponse.RequestContent = requestContent;
187+
transformedResponse.ResponseContent = await CaptureResponseContent(httpResp).ConfigureAwait(false);
182188

183189
return transformedResponse;
184190
}
185191
catch (Exception ex)
186192
{
187-
return await GetIssuuErrorResponse<TData>(httpReq, httpResp, ex).ConfigureAwait(false);
193+
return await GetIssuuErrorResponse<TData>(httpReq, requestContent, httpResp, ex).ConfigureAwait(false);
188194
}
189195
finally
190196
{
@@ -203,6 +209,7 @@ public async Task<IssuuResponse<TResponseData>> FetchAsync<TRequestData, TRespon
203209
using var httpReq = CreateHttpRequest(request);
204210
HttpResponseMessage? httpResp = null;
205211

212+
string? requestContent = await CaptureRequestContent(httpReq).ConfigureAwait(false);
206213
try
207214
{
208215
httpResp = await _http.SendAsync(httpReq, cancellationToken)
@@ -216,13 +223,18 @@ public async Task<IssuuResponse<TResponseData>> FetchAsync<TRequestData, TRespon
216223
cancellationToken)
217224
.ConfigureAwait(false);
218225

219-
(transformedResponse.RequestContent, transformedResponse.ResponseContent) = await CaptureRequestResponseContent(httpReq, httpResp).ConfigureAwait(false);
226+
transformedResponse.RequestContent = requestContent;
227+
transformedResponse.ResponseContent = await CaptureResponseContent(httpResp).ConfigureAwait(false);
220228

221229
return transformedResponse;
222230
}
223231
catch (Exception ex)
224232
{
225-
return await GetIssuuErrorResponse<TResponseData>(httpReq, httpResp, ex).ConfigureAwait(false);
233+
return await GetIssuuErrorResponse<TResponseData>(
234+
httpReq,
235+
requestContent,
236+
httpResp,
237+
ex).ConfigureAwait(false);
226238
}
227239
finally
228240
{
@@ -515,7 +527,11 @@ async Task<Error> GetIssuuError(HttpResponseMessage response, CancellationToken
515527
return error;
516528
}
517529

518-
async Task<IssuuResponse> GetIssuuErrorResponse(HttpRequestMessage httpReq, HttpResponseMessage? httpResp, Exception exception)
530+
async Task<IssuuResponse> GetIssuuErrorResponse(
531+
HttpRequestMessage httpReq,
532+
string? requestContent,
533+
HttpResponseMessage? httpResp,
534+
Exception exception)
519535
{
520536
var response = new IssuuResponse(
521537
httpReq.Method,
@@ -524,11 +540,7 @@ async Task<IssuuResponse> GetIssuuErrorResponse(HttpRequestMessage httpReq, Http
524540
(HttpStatusCode)0,
525541
error: new Error(exception.Message, exception: exception));
526542

527-
if (httpReq?.Content is not null)
528-
{
529-
response.RequestContent = await httpReq.Content.ReadAsStringAsync()
530-
.ConfigureAwait(false);
531-
}
543+
response.RequestContent = requestContent;
532544

533545
if (httpResp?.Content is not null)
534546
{
@@ -539,7 +551,11 @@ async Task<IssuuResponse> GetIssuuErrorResponse(HttpRequestMessage httpReq, Http
539551
return response;
540552
}
541553

542-
async Task<IssuuResponse<TResponse>> GetIssuuErrorResponse<TResponse>(HttpRequestMessage httpReq, HttpResponseMessage? httpResp, Exception exception)
554+
async Task<IssuuResponse<TResponse>> GetIssuuErrorResponse<TResponse>(
555+
HttpRequestMessage httpReq,
556+
string? requestContent,
557+
HttpResponseMessage? httpResp,
558+
Exception exception)
543559
where TResponse : class
544560
{
545561
var response = new IssuuResponse<TResponse>(
@@ -549,12 +565,7 @@ async Task<IssuuResponse<TResponse>> GetIssuuErrorResponse<TResponse>(HttpReques
549565
(HttpStatusCode)0,
550566
error: new Error(exception.Message, exception: exception));
551567

552-
if (httpReq?.Content is not null)
553-
{
554-
response.RequestContent = await httpReq.Content.ReadAsStringAsync()
555-
.ConfigureAwait(false);
556-
}
557-
568+
response.RequestContent = requestContent;
558569
if (httpResp?.Content is not null)
559570
{
560571
response.ResponseContent = await httpResp.Content.ReadAsStringAsync()
@@ -575,23 +586,30 @@ async Task<IssuuResponse<TResponse>> GetIssuuErrorResponse<TResponse>(HttpReques
575586
? new RateLimiting { Limit = limit, Remaining = remaining, Reset = DateTimeOffset.FromUnixTimeSeconds(reset) } : null;
576587
}
577588

578-
async Task<(string?, string?)> CaptureRequestResponseContent(HttpRequestMessage httpReq, HttpResponseMessage? httpResp)
589+
async Task<string?> CaptureRequestContent(HttpRequestMessage httpReq)
579590
{
580-
(string? request, string? response) = (default, default);
581-
582591
if (_settings.CaptureRequestContent && httpReq.Content is not null)
583592
{
584-
request = await httpReq.Content.ReadAsStringAsync()
593+
var request = await httpReq.Content.ReadAsStringAsync()
585594
.ConfigureAwait(false);
595+
596+
return request;
586597
}
587598

599+
return null;
600+
}
601+
602+
async Task<string?> CaptureResponseContent(HttpResponseMessage httpResp)
603+
{
588604
if (_settings.CaptureResponseContent && httpResp.Content is not null)
589605
{
590-
response = await httpResp.Content.ReadAsStringAsync()
591-
.ConfigureAwait(false); ;
606+
var request = await httpResp.Content.ReadAsStringAsync()
607+
.ConfigureAwait(false);
608+
609+
return request;
592610
}
593611

594-
return (request, response);
612+
return null;
595613
}
596614

597615
string? GetHeader(string name, HttpHeaders headers)

0 commit comments

Comments
 (0)