Skip to content

Commit ac317aa

Browse files
committed
OnUploadProgress argument removed from Client.send
1 parent 76ed6d7 commit ac317aa

File tree

10 files changed

+23
-60
lines changed

10 files changed

+23
-60
lines changed

pkgs/http/lib/retry.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import 'dart:math' as math;
88
import 'package:async/async.dart';
99

1010
import 'http.dart';
11-
import 'src/utils.dart';
1211

1312
/// An HTTP client wrapper that automatically retries failing requests.
1413
class RetryClient extends BaseClient {
@@ -102,8 +101,7 @@ class RetryClient extends BaseClient {
102101
);
103102

104103
@override
105-
Future<StreamedResponse> send(BaseRequest request,
106-
{OnUploadProgress? onUploadProgress}) async {
104+
Future<StreamedResponse> send(BaseRequest request) async {
107105
final splitter = StreamSplitter(request.finalize());
108106

109107
var i = 0;

pkgs/http/lib/src/base_client.dart

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import 'exception.dart';
1212
import 'request.dart';
1313
import 'response.dart';
1414
import 'streamed_response.dart';
15-
import 'utils.dart';
1615

1716
/// The abstract base class for an HTTP client.
1817
///
@@ -68,16 +67,8 @@ abstract class BaseClient implements Client {
6867
/// state of the stream; it could have data written to it asynchronously at a
6968
/// later point, or it could already be closed when it's returned. Any
7069
/// internal HTTP errors should be wrapped as [ClientException]s.
71-
///
72-
/// If [onUploadProgress] callback is provided and length is computable,
73-
/// [onUploadProgress] will execute for each chunk was sent.
74-
///
75-
/// lengthComputable :
76-
/// library.html : xhr.lengthComputable
77-
/// library.io : content-length is provided (MultipartRequest provide)
7870
@override
79-
Future<StreamedResponse> send(BaseRequest request,
80-
{OnUploadProgress? onUploadProgress});
71+
Future<StreamedResponse> send(BaseRequest request);
8172

8273
/// Sends a non-streaming [Request] and returns a non-streaming [Response].
8374
Future<Response> _sendUnstreamed(

pkgs/http/lib/src/base_request.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ abstract class BaseRequest {
106106
/// In IO, uses the yield length of the stream. The total length of the bytes
107107
/// yielded by the stream at any given moment is "uploaded" and the total
108108
/// length of the stream is "total"
109+
///
110+
/// If [onUploadProgress] callback is provided and length is computable,
111+
/// [onUploadProgress] will execute for each chunk was sent.
112+
///
113+
/// lengthComputable :
114+
/// library.html : xhr.lengthComputable
115+
/// library.io : content-length is provided (MultipartRequest provide)
109116
final OnUploadProgress? onUploadProgress;
110117

111118
BaseRequest(String method, this.url, {this.onUploadProgress})
@@ -142,8 +149,7 @@ abstract class BaseRequest {
142149
var client = Client();
143150

144151
try {
145-
var response =
146-
await client.send(this, onUploadProgress: onUploadProgress);
152+
var response = await client.send(this);
147153
var stream = onDone(response.stream, client.close);
148154
return StreamedResponse(ByteStream(stream), response.statusCode,
149155
contentLength: response.contentLength,

pkgs/http/lib/src/browser_client.dart

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import 'base_request.dart';
1111
import 'byte_stream.dart';
1212
import 'exception.dart';
1313
import 'streamed_response.dart';
14-
import 'utils.dart';
1514

1615
/// Create a [BrowserClient].
1716
///
@@ -39,16 +38,8 @@ class BrowserClient extends BaseClient {
3938
bool withCredentials = false;
4039

4140
/// Sends an HTTP request and asynchronously returns the response.
42-
///
43-
/// If [onUploadProgress] callback is provided and length is computable,
44-
/// [onUploadProgress] will execute for each chunk was sent.
45-
///
46-
/// lengthComputable :
47-
/// library.html : xhr.lengthComputable
48-
/// library.io : content-length is provided (MultipartRequest provide)
4941
@override
50-
Future<StreamedResponse> send(BaseRequest request,
51-
{OnUploadProgress? onUploadProgress}) async {
42+
Future<StreamedResponse> send(BaseRequest request) async {
5243
var bytes = await request.finalize().toBytes();
5344
var xhr = HttpRequest();
5445
_xhrs.add(xhr);
@@ -60,10 +51,10 @@ class BrowserClient extends BaseClient {
6051

6152
var completer = Completer<StreamedResponse>();
6253

63-
if (onUploadProgress != null) {
54+
if (request.onUploadProgress != null) {
6455
xhr.upload.onLoad.listen((event) {
6556
if (event.lengthComputable) {
66-
onUploadProgress(event.total!, event.loaded!);
57+
request.onUploadProgress!(event.total!, event.loaded!);
6758
}
6859
});
6960
}

pkgs/http/lib/src/client.dart

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import 'client_stub.dart'
1717
import 'exception.dart';
1818
import 'response.dart';
1919
import 'streamed_response.dart';
20-
import 'utils.dart';
2120

2221
/// The interface for HTTP clients that take care of maintaining persistent
2322
/// connections across multiple requests to the same server.
@@ -140,11 +139,7 @@ abstract class Client {
140139
Future<Uint8List> readBytes(Uri url, {Map<String, String>? headers});
141140

142141
/// Sends an HTTP request and asynchronously returns the response.
143-
///
144-
/// If [onUploadProgress] defined, the callback will be called when the
145-
/// upload progress changes.
146-
Future<StreamedResponse> send(BaseRequest request,
147-
{OnUploadProgress? onUploadProgress});
142+
Future<StreamedResponse> send(BaseRequest request);
148143

149144
/// Closes the client and cleans up any resources associated with it.
150145
///

pkgs/http/lib/src/io_client.dart

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import 'base_client.dart';
1010
import 'base_request.dart';
1111
import 'exception.dart';
1212
import 'io_streamed_response.dart';
13-
import 'utils.dart';
1413

1514
/// Create an [IOClient].
1615
///
@@ -48,16 +47,8 @@ class IOClient extends BaseClient {
4847
IOClient([HttpClient? inner]) : _inner = inner ?? HttpClient();
4948

5049
/// Sends an HTTP request and asynchronously returns the response.
51-
///
52-
/// If [onUploadProgress] callback is provided and length is computable,
53-
/// [onUploadProgress] will execute for each chunk was sent.
54-
///
55-
/// lengthComputable :
56-
/// library.html : xhr.lengthComputable
57-
/// library.io : content-length is provided (MultipartRequest provide)
5850
@override
59-
Future<IOStreamedResponse> send(BaseRequest request,
60-
{OnUploadProgress? onUploadProgress}) async {
51+
Future<IOStreamedResponse> send(BaseRequest request) async {
6152
if (_inner == null) {
6253
throw ClientException(
6354
'HTTP request failed. Client is already closed.', request.url);
@@ -68,13 +59,13 @@ class IOClient extends BaseClient {
6859
ByteStream? handledStream;
6960

7061
var contentLength = request.contentLength;
71-
if (onUploadProgress != null && contentLength != null) {
62+
if (request.onUploadProgress != null && contentLength != null) {
7263
var load = 0;
7364
handledStream =
7465
ByteStream(stream.transform(StreamTransformer.fromBind((d) async* {
7566
await for (var data in d) {
7667
load += data.length;
77-
onUploadProgress(contentLength, load);
68+
request.onUploadProgress!.call(contentLength, load);
7869
yield data;
7970
}
8071
})));

pkgs/http/lib/src/mock_client.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import 'request.dart';
1111
import 'response.dart';
1212
import 'streamed_request.dart';
1313
import 'streamed_response.dart';
14-
import 'utils.dart';
1514

1615
// TODO(nweiz): once Dart has some sort of Rack- or WSGI-like standard for
1716
// server APIs, MockClient should conform to it.
@@ -68,20 +67,19 @@ class MockClient extends BaseClient {
6867
});
6968

7069
@override
71-
Future<StreamedResponse> send(BaseRequest request,
72-
{OnUploadProgress? onUploadProgress}) async {
70+
Future<StreamedResponse> send(BaseRequest request) async {
7371
var bodyStream = request.finalize();
7472
ByteStream? handledStream;
7573

7674
var contentLength = request.contentLength;
7775

78-
if (onUploadProgress != null && contentLength != null) {
76+
if (request.onUploadProgress != null && contentLength != null) {
7977
var load = 0;
8078
handledStream = ByteStream(
8179
bodyStream.transform(StreamTransformer.fromBind((d) async* {
8280
await for (var data in d) {
8381
load += data.length;
84-
onUploadProgress(contentLength, load);
82+
request.onUploadProgress!(contentLength, load);
8583
yield data;
8684
}
8785
})));

pkgs/http/test/io/client_test.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,20 @@ import 'dart:io';
88

99
import 'package:http/http.dart' as http;
1010
import 'package:http/io_client.dart' as http_io;
11-
import 'package:http/src/utils.dart';
1211
import 'package:test/test.dart';
1312

1413
import '../utils.dart';
1514

1615
class TestClient extends http.BaseClient {
1716
@override
18-
Future<http.StreamedResponse> send(http.BaseRequest request,
19-
{OnUploadProgress? onUploadProgress}) {
17+
Future<http.StreamedResponse> send(http.BaseRequest request) {
2018
throw UnimplementedError();
2119
}
2220
}
2321

2422
class TestClient2 extends http.BaseClient {
2523
@override
26-
Future<http.StreamedResponse> send(http.BaseRequest request,
27-
{OnUploadProgress? onUploadProgress}) {
24+
Future<http.StreamedResponse> send(http.BaseRequest request) {
2825
throw UnimplementedError();
2926
}
3027
}

pkgs/http/test/io/http_test.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44

55
@TestOn('vm')
66
import 'package:http/http.dart' as http;
7-
import 'package:http/src/utils.dart';
87
import 'package:test/test.dart';
98

109
import '../utils.dart';
1110

1211
class TestClient extends http.BaseClient {
1312
@override
14-
Future<http.StreamedResponse> send(http.BaseRequest request,
15-
{OnUploadProgress? onUploadProgress}) {
13+
Future<http.StreamedResponse> send(http.BaseRequest request) {
1614
throw UnimplementedError();
1715
}
1816
}

pkgs/http/test/io/streamed_request_test.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ void main() {
7070

7171
var response = await (await request.send()).stream.bytesToString();
7272

73-
print(response);
74-
7573
expect(response, '1739');
7674
expect(contentLength, 1739);
7775
expect(totalLoaded, 1739);

0 commit comments

Comments
 (0)