From d79873b0aea2ab4ccc5f282fc1113f164645ee9f Mon Sep 17 00:00:00 2001 From: Nazar Vovk Date: Sun, 23 Jul 2023 15:23:04 +0300 Subject: [PATCH] Use StreamedRequest with onSendProgress in adapter --- .../lib/src/conversion_layer_adapter.dart | 55 ++++++++++++------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/plugins/native_dio_adapter/lib/src/conversion_layer_adapter.dart b/plugins/native_dio_adapter/lib/src/conversion_layer_adapter.dart index a035f2c45..37330dc69 100644 --- a/plugins/native_dio_adapter/lib/src/conversion_layer_adapter.dart +++ b/plugins/native_dio_adapter/lib/src/conversion_layer_adapter.dart @@ -34,34 +34,51 @@ class ConversionLayerAdapter implements HttpClientAdapter { RequestOptions options, Stream? requestStream, ) async { - final request = Request( - options.method, - options.uri, - ); + final BaseRequest request; + if (options.onSendProgress != null && requestStream != null) { + final streamedRequest = StreamedRequest( + options.method, + options.uri, + ); + requestStream.listen( + streamedRequest.sink.add, + onDone: streamedRequest.sink.close, + onError: streamedRequest.sink.addError, + cancelOnError: true, + ); + request = streamedRequest; + } else { + final simpleRequest = Request( + options.method, + options.uri, + ); + if (requestStream != null) { + final completer = Completer(); + final sink = ByteConversionSink.withCallback( + (bytes) => completer.complete(Uint8List.fromList(bytes)), + ); + requestStream.listen( + sink.add, + onError: completer.completeError, + onDone: sink.close, + cancelOnError: true, + ); + final bytes = await completer.future; + simpleRequest.bodyBytes = bytes; + // simpleRequest.bodyBytes = await ByteStream(requestStream).toBytes(); + } + request = simpleRequest; + } request.headers.addAll( Map.fromEntries( options.headers.entries.map((e) => MapEntry(e.key, e.value.toString())), ), ); - request.followRedirects = options.followRedirects; request.maxRedirects = options.maxRedirects; + request.persistentConnection = options.persistentConnection; - if (requestStream != null) { - final completer = Completer(); - final sink = ByteConversionSink.withCallback( - (bytes) => completer.complete(Uint8List.fromList(bytes)), - ); - requestStream.listen( - sink.add, - onError: completer.completeError, - onDone: sink.close, - cancelOnError: true, - ); - final bytes = await completer.future; - request.bodyBytes = bytes; - } return request; } }