@@ -26,7 +26,7 @@ internal static class ResponseBuilderDefaults
2626
2727 public static readonly Type [ ] SpecialTypes =
2828 {
29- typeof ( StringResponse ) , typeof ( BytesResponse ) , typeof ( VoidResponse ) , typeof ( DynamicResponse )
29+ typeof ( StringResponse ) , typeof ( BytesResponse ) , typeof ( VoidResponse ) , typeof ( DynamicResponse ) , typeof ( StreamResponse )
3030 } ;
3131}
3232
@@ -224,68 +224,65 @@ private async ValueTask<TResponse> SetBodyCoreAsync<TResponse>(bool isAsync,
224224 details . ResponseBodyInBytes = bytes ;
225225 }
226226
227- using ( responseStream )
228- {
229- if ( SetSpecialTypes < TResponse > ( mimeType , bytes , responseStream , requestData . MemoryStreamFactory , out var r ) ) return r ;
227+ if ( SetSpecialTypes < TResponse > ( mimeType , bytes , responseStream , requestData . MemoryStreamFactory , out var r ) ) return r ;
230228
231- if ( details . HttpStatusCode . HasValue &&
232- requestData . SkipDeserializationForStatusCodes . Contains ( details . HttpStatusCode . Value ) )
233- return null ;
229+ if ( details . HttpStatusCode . HasValue &&
230+ requestData . SkipDeserializationForStatusCodes . Contains ( details . HttpStatusCode . Value ) )
231+ return null ;
234232
235- var serializer = requestData . ConnectionSettings . RequestResponseSerializer ;
233+ var serializer = requestData . ConnectionSettings . RequestResponseSerializer ;
236234
237- TResponse response ;
238- if ( requestData . CustomResponseBuilder != null )
239- {
240- var beforeTicks = Stopwatch . GetTimestamp ( ) ;
235+ TResponse response ;
236+ if ( requestData . CustomResponseBuilder != null )
237+ {
238+ var beforeTicks = Stopwatch . GetTimestamp ( ) ;
241239
242- if ( isAsync )
243- response = await requestData . CustomResponseBuilder
244- . DeserializeResponseAsync ( serializer , details , responseStream , cancellationToken )
245- . ConfigureAwait ( false ) as TResponse ;
246- else
247- response = requestData . CustomResponseBuilder
248- . DeserializeResponse ( serializer , details , responseStream ) as TResponse ;
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 ;
249247
250- var deserializeResponseMs = ( Stopwatch . GetTimestamp ( ) - beforeTicks ) / ( Stopwatch . Frequency / 1000 ) ;
251- if ( deserializeResponseMs > OpenTelemetry . MinimumMillisecondsToEmitTimingSpanAttribute && OpenTelemetry . CurrentSpanIsElasticTransportOwnedHasListenersAndAllDataRequested )
252- Activity . Current ? . SetTag ( OpenTelemetryAttributes . ElasticTransportDeserializeResponseMs , deserializeResponseMs ) ;
248+ var deserializeResponseMs = ( Stopwatch . GetTimestamp ( ) - beforeTicks ) / ( Stopwatch . Frequency / 1000 ) ;
249+ if ( deserializeResponseMs > OpenTelemetry . MinimumMillisecondsToEmitTimingSpanAttribute && OpenTelemetry . CurrentSpanIsElasticTransportOwnedHasListenersAndAllDataRequested )
250+ Activity . Current ? . SetTag ( OpenTelemetryAttributes . ElasticTransportDeserializeResponseMs , deserializeResponseMs ) ;
253251
254- return response ;
255- }
252+ return response ;
253+ }
256254
257- // TODO: Handle empty data in a nicer way as throwing exceptions has a cost we'd like to avoid!
258- // ie. check content-length (add to ApiCallDetails)? Content-length cannot be retrieved from a GZip content stream which is annoying.
259- try
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 ( ) )
260260 {
261- if ( requiresErrorDeserialization && TryGetError ( details , requestData , responseStream , out var error ) && error . HasError ( ) )
262- {
263- response = new TResponse ( ) ;
264- SetErrorOnResponse ( response , error ) ;
265- return response ;
266- }
261+ response = new TResponse ( ) ;
262+ SetErrorOnResponse ( response , error ) ;
263+ return response ;
264+ }
267265
268- if ( ! requestData . ValidateResponseContentType ( mimeType ) )
269- return default ;
266+ if ( ! requestData . ValidateResponseContentType ( mimeType ) )
267+ return default ;
270268
271- var beforeTicks = Stopwatch . GetTimestamp ( ) ;
269+ var beforeTicks = Stopwatch . GetTimestamp ( ) ;
272270
273- if ( isAsync )
274- response = await serializer . DeserializeAsync < TResponse > ( responseStream , cancellationToken ) . ConfigureAwait ( false ) ;
275- else
276- response = serializer . Deserialize < TResponse > ( responseStream ) ;
271+ if ( isAsync )
272+ response = await serializer . DeserializeAsync < TResponse > ( responseStream , cancellationToken ) . ConfigureAwait ( false ) ;
273+ else
274+ response = serializer . Deserialize < TResponse > ( responseStream ) ;
277275
278- var deserializeResponseMs = ( Stopwatch . GetTimestamp ( ) - beforeTicks ) / ( Stopwatch . Frequency / 1000 ) ;
276+ var deserializeResponseMs = ( Stopwatch . GetTimestamp ( ) - beforeTicks ) / ( Stopwatch . Frequency / 1000 ) ;
279277
280- if ( deserializeResponseMs > OpenTelemetry . MinimumMillisecondsToEmitTimingSpanAttribute && OpenTelemetry . CurrentSpanIsElasticTransportOwnedHasListenersAndAllDataRequested )
281- Activity . Current ? . SetTag ( OpenTelemetryAttributes . ElasticTransportDeserializeResponseMs , deserializeResponseMs ) ;
278+ if ( deserializeResponseMs > OpenTelemetry . MinimumMillisecondsToEmitTimingSpanAttribute && OpenTelemetry . CurrentSpanIsElasticTransportOwnedHasListenersAndAllDataRequested )
279+ Activity . Current ? . SetTag ( OpenTelemetryAttributes . ElasticTransportDeserializeResponseMs , deserializeResponseMs ) ;
282280
283- return response ;
284- }
285- catch ( JsonException ex ) when ( ex . Message . Contains ( "The input does not contain any JSON tokens" ) )
286- {
287- return default ;
288- }
281+ return response ;
282+ }
283+ catch ( JsonException ex ) when ( ex . Message . Contains ( "The input does not contain any JSON tokens" ) )
284+ {
285+ return default ;
289286 }
290287 }
291288
0 commit comments