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

Add onSendProgress callback #579

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkgs/http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.3

* Add `onSendProgress` callback

## 1.1.2

* Allow `web: '>=0.3.0 <0.5.0'`.
Expand Down
86 changes: 70 additions & 16 deletions pkgs/http/lib/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'dart:typed_data';

import 'src/client.dart';
import 'src/exception.dart';
import 'src/progress.dart';
import 'src/request.dart';
import 'src/response.dart';
import 'src/streamed_request.dart';
Expand All @@ -22,6 +23,7 @@ export 'src/client.dart' hide zoneClient;
export 'src/exception.dart';
export 'src/multipart_file.dart';
export 'src/multipart_request.dart';
export 'src/progress.dart';
export 'src/request.dart';
export 'src/response.dart';
export 'src/streamed_request.dart';
Expand Down Expand Up @@ -63,12 +65,25 @@ Future<Response> get(Uri url, {Map<String, String>? headers}) =>
///
/// [encoding] defaults to [utf8].
///
/// If [onSendProgress] is provided it will be called to indicate
/// the upload progress
///
/// For more fine-grained control over the request, use [Request] or
/// [StreamedRequest] instead.
Future<Response> post(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) =>
_withClient((client) =>
client.post(url, headers: headers, body: body, encoding: encoding));
Future<Response> post(
Uri url, {
Map<String, String>? headers,
Object? body,
Encoding? encoding,
Progress? onSendProgress,
}) =>
_withClient((client) => client.post(
url,
headers: headers,
body: body,
encoding: encoding,
onSendProgress: onSendProgress,
));

/// Sends an HTTP PUT request with the given headers and body to the given URL.
///
Expand All @@ -86,12 +101,25 @@ Future<Response> post(Uri url,
///
/// [encoding] defaults to [utf8].
///
/// If [onSendProgress] is provided it will be called to indicate
/// the upload progress
///
/// For more fine-grained control over the request, use [Request] or
/// [StreamedRequest] instead.
Future<Response> put(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) =>
_withClient((client) =>
client.put(url, headers: headers, body: body, encoding: encoding));
Future<Response> put(
Uri url, {
Map<String, String>? headers,
Object? body,
Encoding? encoding,
Progress? onSendProgress,
}) =>
_withClient((client) => client.put(
url,
headers: headers,
body: body,
encoding: encoding,
onSendProgress: onSendProgress,
));

/// Sends an HTTP PATCH request with the given headers and body to the given
/// URL.
Expand All @@ -110,24 +138,50 @@ Future<Response> put(Uri url,
///
/// [encoding] defaults to [utf8].
///
/// If [onSendProgress] is provided it will be called to indicate
/// the upload progress
///
/// For more fine-grained control over the request, use [Request] or
/// [StreamedRequest] instead.
Future<Response> patch(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) =>
_withClient((client) =>
client.patch(url, headers: headers, body: body, encoding: encoding));
Future<Response> patch(
Uri url, {
Map<String, String>? headers,
Object? body,
Encoding? encoding,
Progress? onSendProgress,
}) =>
_withClient((client) => client.patch(
url,
headers: headers,
body: body,
encoding: encoding,
onSendProgress: onSendProgress,
));

/// Sends an HTTP DELETE request with the given headers to the given URL.
///
/// This automatically initializes a new [Client] and closes that client once
/// the request is complete. If you're planning on making multiple requests to
/// the same server, you should use a single [Client] for all of those requests.
///
/// If [onSendProgress] is provided it will be called to indicate
/// the upload progress
///
/// For more fine-grained control over the request, use [Request] instead.
Future<Response> delete(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) =>
_withClient((client) =>
client.delete(url, headers: headers, body: body, encoding: encoding));
Future<Response> delete(
Uri url, {
Map<String, String>? headers,
Object? body,
Encoding? encoding,
Progress? onSendProgress,
}) =>
_withClient((client) => client.delete(
url,
headers: headers,
body: body,
encoding: encoding,
onSendProgress: onSendProgress,
));

/// Sends an HTTP GET request with the given headers to the given URL and
/// returns a Future that completes to the body of the response as a [String].
Expand Down
10 changes: 8 additions & 2 deletions pkgs/http/lib/retry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,20 @@ final class RetryClient extends BaseClient {
);

@override
Future<StreamedResponse> send(BaseRequest request) async {
Future<StreamedResponse> send(
BaseRequest request, {
Progress? onSendProgress,
}) async {
final splitter = StreamSplitter(request.finalize());

var i = 0;
for (;;) {
StreamedResponse? response;
try {
response = await _inner.send(_copyRequest(request, splitter.split()));
response = await _inner.send(
_copyRequest(request, splitter.split()),
onSendProgress: onSendProgress,
);
} catch (error, stackTrace) {
if (i == _retries || !await _whenError(error, stackTrace)) rethrow;
}
Expand Down
85 changes: 70 additions & 15 deletions pkgs/http/lib/src/base_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'base_request.dart';
import 'byte_stream.dart';
import 'client.dart';
import 'exception.dart';
import 'progress.dart';
import 'request.dart';
import 'response.dart';
import 'streamed_response.dart';
Expand All @@ -27,24 +28,72 @@ abstract mixin class BaseClient implements Client {
_sendUnstreamed('GET', url, headers);

@override
Future<Response> post(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) =>
_sendUnstreamed('POST', url, headers, body, encoding);
Future<Response> post(
Uri url, {
Map<String, String>? headers,
Object? body,
Encoding? encoding,
Progress? onSendProgress,
}) =>
_sendUnstreamed(
'POST',
url,
headers,
body,
encoding,
onSendProgress,
);

@override
Future<Response> put(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) =>
_sendUnstreamed('PUT', url, headers, body, encoding);
Future<Response> put(
Uri url, {
Map<String, String>? headers,
Object? body,
Encoding? encoding,
Progress? onSendProgress,
}) =>
_sendUnstreamed(
'PUT',
url,
headers,
body,
encoding,
onSendProgress,
);

@override
Future<Response> patch(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) =>
_sendUnstreamed('PATCH', url, headers, body, encoding);
Future<Response> patch(
Uri url, {
Map<String, String>? headers,
Object? body,
Encoding? encoding,
Progress? onSendProgress,
}) =>
_sendUnstreamed(
'PATCH',
url,
headers,
body,
encoding,
onSendProgress,
);

@override
Future<Response> delete(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) =>
_sendUnstreamed('DELETE', url, headers, body, encoding);
Future<Response> delete(
Uri url, {
Map<String, String>? headers,
Object? body,
Encoding? encoding,
Progress? onSendProgress,
}) =>
_sendUnstreamed(
'DELETE',
url,
headers,
body,
encoding,
onSendProgress,
);

@override
Future<String> read(Uri url, {Map<String, String>? headers}) async {
Expand All @@ -68,12 +117,15 @@ abstract mixin class BaseClient implements Client {
/// later point, or it could already be closed when it's returned. Any
/// internal HTTP errors should be wrapped as [ClientException]s.
@override
Future<StreamedResponse> send(BaseRequest request);
Future<StreamedResponse> send(
BaseRequest request, {
Progress? onSendProgress,
});

/// Sends a non-streaming [Request] and returns a non-streaming [Response].
Future<Response> _sendUnstreamed(
String method, Uri url, Map<String, String>? headers,
[Object? body, Encoding? encoding]) async {
[Object? body, Encoding? encoding, Progress? onSendProgress]) async {
var request = Request(method, url);

if (headers != null) request.headers.addAll(headers);
Expand All @@ -90,7 +142,10 @@ abstract mixin class BaseClient implements Client {
}
}

return Response.fromStream(await send(request));
return Response.fromStream(await send(
request,
onSendProgress: onSendProgress,
));
}

/// Throws an error if [response] is not successful.
Expand Down
9 changes: 8 additions & 1 deletion pkgs/http/lib/src/base_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'base_client.dart';
import 'base_response.dart';
import 'byte_stream.dart';
import 'client.dart';
import 'progress.dart';
import 'streamed_response.dart';
import 'utils.dart';

Expand All @@ -27,6 +28,9 @@ abstract class BaseRequest {
/// Non-standard method names are also supported.
final String method;

/// The callback to call for data send progress.
Progress? onSendProgress;

/// The URL to which the request will be sent.
final Uri url;

Expand Down Expand Up @@ -130,7 +134,10 @@ abstract class BaseRequest {
var client = Client();

try {
var response = await client.send(this);
var response = await client.send(
this,
onSendProgress: onSendProgress,
);
var stream = onDone(response.stream, client.close);
return StreamedResponse(ByteStream(stream), response.statusCode,
contentLength: response.contentLength,
Expand Down
18 changes: 17 additions & 1 deletion pkgs/http/lib/src/browser_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'base_client.dart';
import 'base_request.dart';
import 'byte_stream.dart';
import 'exception.dart';
import 'progress.dart';
import 'streamed_response.dart';

final _digitRegex = RegExp(r'^\d+$');
Expand Down Expand Up @@ -50,7 +51,10 @@ class BrowserClient extends BaseClient {

/// Sends an HTTP request and asynchronously returns the response.
@override
Future<StreamedResponse> send(BaseRequest request) async {
Future<StreamedResponse> send(
BaseRequest request, {
Progress? onSendProgress,
}) async {
if (_isClosed) {
throw ClientException(
'HTTP request failed. Client is already closed.', request.url);
Expand All @@ -62,6 +66,18 @@ class BrowserClient extends BaseClient {
..open(request.method, '${request.url}', true)
..responseType = 'arraybuffer'
..withCredentials = withCredentials;

if (onSendProgress != null) {
xhr.upload.addEventListener('progress', (event) {
if (event is ProgressEvent && event.lengthComputable) {
onSendProgress(
event.loaded,
event.total,
);
}
});
}

for (var header in request.headers.entries) {
xhr.setRequestHeader(header.key, header.value);
}
Expand Down