Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use StreamedRequest with onSendProgress in adapter #1901

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 36 additions & 19 deletions plugins/native_dio_adapter/lib/src/conversion_layer_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,51 @@ class ConversionLayerAdapter implements HttpClientAdapter {
RequestOptions options,
Stream<Uint8List>? 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<Uint8List>();
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<Uint8List>();
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;
}
}
Expand Down