@@ -6,14 +6,12 @@ import 'dart:async';
6
6
import 'dart:convert' ;
7
7
import 'dart:typed_data' ;
8
8
9
- import 'package:collection/collection .dart' ;
9
+ import 'package:async/async .dart' ;
10
10
11
- import 'base_request.dart' ;
12
11
import 'client.dart' ;
13
12
import 'exception.dart' ;
14
13
import 'request.dart' ;
15
14
import 'response.dart' ;
16
- import 'streamed_response.dart' ;
17
15
18
16
/// The abstract base class for an HTTP client. This is a mixin-style class;
19
17
/// subclasses only need to implement [send] and maybe [close] , and then they
@@ -24,14 +22,14 @@ abstract class BaseClient implements Client {
24
22
///
25
23
/// For more fine-grained control over the request, use [send] instead.
26
24
Future <Response > head (url, {Map <String , String > headers}) =>
27
- _sendUnstreamed ( "HEAD" , url, headers);
25
+ send ( new Request . head ( url, headers: headers) );
28
26
29
27
/// Sends an HTTP GET request with the given headers to the given URL, which
30
28
/// can be a [Uri] or a [String] .
31
29
///
32
30
/// For more fine-grained control over the request, use [send] instead.
33
31
Future <Response > get (url, {Map <String , String > headers}) =>
34
- _sendUnstreamed ( "GET" , url, headers);
32
+ send ( new Request . get ( url, headers: headers) );
35
33
36
34
/// Sends an HTTP POST request with the given headers and body to the given
37
35
/// URL, which can be a [Uri] or a [String] .
@@ -51,9 +49,10 @@ abstract class BaseClient implements Client {
51
49
/// [encoding] defaults to UTF-8.
52
50
///
53
51
/// For more fine-grained control over the request, use [send] instead.
54
- Future <Response > post (url, {Map <String , String > headers, body ,
52
+ Future <Response > post (url, body, {Map <String , String > headers,
55
53
Encoding encoding}) =>
56
- _sendUnstreamed ("POST" , url, headers, body, encoding);
54
+ send (new Request .post (url, body, headers: headers,
55
+ encoding: encoding));
57
56
58
57
/// Sends an HTTP PUT request with the given headers and body to the given
59
58
/// URL, which can be a [Uri] or a [String] .
@@ -73,9 +72,10 @@ abstract class BaseClient implements Client {
73
72
/// [encoding] defaults to UTF-8.
74
73
///
75
74
/// For more fine-grained control over the request, use [send] instead.
76
- Future <Response > put (url, {Map <String , String > headers, body ,
75
+ Future <Response > put (url, body, {Map <String , String > headers,
77
76
Encoding encoding}) =>
78
- _sendUnstreamed ("PUT" , url, headers, body, encoding);
77
+ send (new Request .put (url, body, headers: headers,
78
+ encoding: encoding));
79
79
80
80
/// Sends an HTTP PATCH request with the given headers and body to the given
81
81
/// URL, which can be a [Uri] or a [String] .
@@ -95,16 +95,17 @@ abstract class BaseClient implements Client {
95
95
/// [encoding] defaults to UTF-8.
96
96
///
97
97
/// For more fine-grained control over the request, use [send] instead.
98
- Future <Response > patch (url, {Map <String , String > headers, body ,
98
+ Future <Response > patch (url, body, {Map <String , String > headers,
99
99
Encoding encoding}) =>
100
- _sendUnstreamed ("PATCH" , url, headers, body, encoding);
100
+ send (new Request .patch (url, body, headers: headers,
101
+ encoding: encoding));
101
102
102
103
/// Sends an HTTP DELETE request with the given headers to the given URL,
103
104
/// which can be a [Uri] or a [String] .
104
105
///
105
106
/// For more fine-grained control over the request, use [send] instead.
106
107
Future <Response > delete (url, {Map <String , String > headers}) =>
107
- _sendUnstreamed ( "DELETE" , url, headers);
108
+ send ( new Request . delete ( url, headers: headers) );
108
109
109
110
/// Sends an HTTP GET request with the given headers to the given URL, which
110
111
/// can be a [Uri] or a [String] , and returns a Future that completes to the
@@ -115,11 +116,11 @@ abstract class BaseClient implements Client {
115
116
///
116
117
/// For more fine-grained control over the request and response, use [send] or
117
118
/// [get] instead.
118
- Future <String > read (url, {Map <String , String > headers}) {
119
- return get (url, headers: headers). then ((response) {
120
- _checkResponseSuccess (url, response);
121
- return response.body;
122
- } );
119
+ Future <String > read (url, {Map <String , String > headers}) async {
120
+ var response = await get (url, headers: headers);
121
+ _checkResponseSuccess (url, response);
122
+
123
+ return await response. readAsString ( );
123
124
}
124
125
125
126
/// Sends an HTTP GET request with the given headers to the given URL, which
@@ -131,11 +132,11 @@ abstract class BaseClient implements Client {
131
132
///
132
133
/// For more fine-grained control over the request and response, use [send] or
133
134
/// [get] instead.
134
- Future <Uint8List > readBytes (url, {Map <String , String > headers}) {
135
- return get (url, headers: headers). then ((response) {
136
- _checkResponseSuccess (url, response);
137
- return response.bodyBytes;
138
- } );
135
+ Future <Uint8List > readBytes (url, {Map <String , String > headers}) async {
136
+ var response = await get (url, headers: headers);
137
+ _checkResponseSuccess (url, response);
138
+
139
+ return await collectBytes (response. read () );
139
140
}
140
141
141
142
/// Sends an HTTP request and asynchronously returns the response.
@@ -145,31 +146,7 @@ abstract class BaseClient implements Client {
145
146
/// state of the stream; it could have data written to it asynchronously at a
146
147
/// later point, or it could already be closed when it's returned. Any
147
148
/// internal HTTP errors should be wrapped as [ClientException] s.
148
- Future <StreamedResponse > send (BaseRequest request);
149
-
150
- /// Sends a non-streaming [Request] and returns a non-streaming [Response] .
151
- Future <Response > _sendUnstreamed (String method, url,
152
- Map <String , String > headers, [body, Encoding encoding]) async {
153
-
154
- if (url is String ) url = Uri .parse (url);
155
- var request = new Request (method, url);
156
-
157
- if (headers != null ) request.headers.addAll (headers);
158
- if (encoding != null ) request.encoding = encoding;
159
- if (body != null ) {
160
- if (body is String ) {
161
- request.body = body;
162
- } else if (body is List ) {
163
- request.bodyBytes = DelegatingList .typed (body);
164
- } else if (body is Map ) {
165
- request.bodyFields = DelegatingMap .typed (body);
166
- } else {
167
- throw new ArgumentError ('Invalid request body "$body ".' );
168
- }
169
- }
170
-
171
- return Response .fromStream (await send (request));
172
- }
149
+ Future <Response > send (Request request);
173
150
174
151
/// Throws an error if [response] is not successful.
175
152
void _checkResponseSuccess (url, Response response) {
0 commit comments