@@ -224,70 +224,69 @@ private async ValueTask<TResponse> SetBodyCoreAsync<TResponse>(bool isAsync,
224224 details . ResponseBodyInBytes = bytes ;
225225 }
226226
227- var isStreamResponse = typeof ( TResponse ) == typeof ( StreamResponse ) ;
227+ if ( SetSpecialTypes < TResponse > ( mimeType , bytes , responseStream , requestData . MemoryStreamFactory , out var r ) ) return r ;
228228
229- using ( isStreamResponse ? Stream . Null : responseStream ??= Stream . Null )
230- {
231- if ( SetSpecialTypes < TResponse > ( mimeType , bytes , responseStream , requestData . MemoryStreamFactory , out var r ) ) return r ;
229+ if ( details . HttpStatusCode . HasValue &&
230+ requestData . SkipDeserializationForStatusCodes . Contains ( details . HttpStatusCode . Value ) )
231+ return null ;
232232
233- if ( details . HttpStatusCode . HasValue &&
234- requestData . SkipDeserializationForStatusCodes . Contains ( details . HttpStatusCode . Value ) )
235- return null ;
233+ var serializer = requestData . ConnectionSettings . RequestResponseSerializer ;
236234
237- var serializer = requestData . ConnectionSettings . RequestResponseSerializer ;
235+ TResponse response ;
236+ if ( requestData . CustomResponseBuilder != null )
237+ {
238+ var beforeTicks = Stopwatch . GetTimestamp ( ) ;
238239
239- TResponse response ;
240- if ( requestData . CustomResponseBuilder != null )
241- {
242- var beforeTicks = Stopwatch . GetTimestamp ( ) ;
240+ if ( isAsync )
241+ response = await requestData . CustomResponseBuilder
242+ . DeserializeResponseAsync ( serializer , details , responseStream , cancellationToken )
243+ . ConfigureAwait ( false ) as TResponse ;
244+ else
245+ response = requestData . CustomResponseBuilder
246+ . DeserializeResponse ( serializer , details , responseStream ) as TResponse ;
243247
244- if ( isAsync )
245- response = await requestData . CustomResponseBuilder
246- . DeserializeResponseAsync ( serializer , details , responseStream , cancellationToken )
247- . ConfigureAwait ( false ) as TResponse ;
248- else
249- response = requestData . CustomResponseBuilder
250- . DeserializeResponse ( serializer , details , responseStream ) as TResponse ;
248+ var deserializeResponseMs = ( Stopwatch . GetTimestamp ( ) - beforeTicks ) / ( Stopwatch . Frequency / 1000 ) ;
249+ if ( deserializeResponseMs > OpenTelemetry . MinimumMillisecondsToEmitTimingSpanAttribute && OpenTelemetry . CurrentSpanIsElasticTransportOwnedHasListenersAndAllDataRequested )
250+ Activity . Current ? . SetTag ( OpenTelemetryAttributes . ElasticTransportDeserializeResponseMs , deserializeResponseMs ) ;
251251
252- var deserializeResponseMs = ( Stopwatch . GetTimestamp ( ) - beforeTicks ) / ( Stopwatch . Frequency / 1000 ) ;
253- if ( deserializeResponseMs > OpenTelemetry . MinimumMillisecondsToEmitTimingSpanAttribute && OpenTelemetry . CurrentSpanIsElasticTransportOwnedHasListenersAndAllDataRequested )
254- Activity . Current ? . SetTag ( OpenTelemetryAttributes . ElasticTransportDeserializeResponseMs , deserializeResponseMs ) ;
252+ return response ;
253+ }
255254
255+ // TODO: Handle empty data in a nicer way as throwing exceptions has a cost we'd like to avoid!
256+ // ie. check content-length (add to ApiCallDetails)? Content-length cannot be retrieved from a GZip content stream which is annoying.
257+ try
258+ {
259+ if ( requiresErrorDeserialization && TryGetError ( details , requestData , responseStream , out var error ) && error . HasError ( ) )
260+ {
261+ response = new TResponse ( ) ;
262+ SetErrorOnResponse ( response , error ) ;
256263 return response ;
257264 }
258265
259- // TODO: Handle empty data in a nicer way as throwing exceptions has a cost we'd like to avoid!
260- // ie. check content-length (add to ApiCallDetails)? Content-length cannot be retrieved from a GZip content stream which is annoying.
261- try
262- {
263- if ( requiresErrorDeserialization && TryGetError ( details , requestData , responseStream , out var error ) && error . HasError ( ) )
264- {
265- response = new TResponse ( ) ;
266- SetErrorOnResponse ( response , error ) ;
267- return response ;
268- }
266+ if ( ! requestData . ValidateResponseContentType ( mimeType ) )
267+ return default ;
269268
270- if ( ! requestData . ValidateResponseContentType ( mimeType ) )
271- return default ;
269+ var beforeTicks = Stopwatch . GetTimestamp ( ) ;
272270
273- var beforeTicks = Stopwatch . GetTimestamp ( ) ;
271+ if ( isAsync )
272+ response = await serializer . DeserializeAsync < TResponse > ( responseStream , cancellationToken ) . ConfigureAwait ( false ) ;
273+ else
274+ response = serializer . Deserialize < TResponse > ( responseStream ) ;
274275
275- if ( isAsync )
276- response = await serializer . DeserializeAsync < TResponse > ( responseStream , cancellationToken ) . ConfigureAwait ( false ) ;
277- else
278- response = serializer . Deserialize < TResponse > ( responseStream ) ;
276+ var deserializeResponseMs = ( Stopwatch . GetTimestamp ( ) - beforeTicks ) / ( Stopwatch . Frequency / 1000 ) ;
279277
280- var deserializeResponseMs = ( Stopwatch . GetTimestamp ( ) - beforeTicks ) / ( Stopwatch . Frequency / 1000 ) ;
278+ if ( deserializeResponseMs > OpenTelemetry . MinimumMillisecondsToEmitTimingSpanAttribute && OpenTelemetry . CurrentSpanIsElasticTransportOwnedHasListenersAndAllDataRequested )
279+ Activity . Current ? . SetTag ( OpenTelemetryAttributes . ElasticTransportDeserializeResponseMs , deserializeResponseMs ) ;
281280
282- if ( deserializeResponseMs > OpenTelemetry . MinimumMillisecondsToEmitTimingSpanAttribute && OpenTelemetry . CurrentSpanIsElasticTransportOwnedHasListenersAndAllDataRequested )
283- Activity . Current ? . SetTag ( OpenTelemetryAttributes . ElasticTransportDeserializeResponseMs , deserializeResponseMs ) ;
281+ if ( ! response . LeaveOpen )
282+ responseStream . Dispose ( ) ;
284283
285- return response ;
286- }
287- catch ( JsonException ex ) when ( ex . Message . Contains ( "The input does not contain any JSON tokens" ) )
288- {
289- return default ;
290- }
284+ return response ;
285+ }
286+ catch ( JsonException ex ) when ( ex . Message . Contains ( "The input does not contain any JSON tokens" ) )
287+ {
288+ responseStream . Dispose ( ) ;
289+ return default ;
291290 }
292291 }
293292
0 commit comments