Skip to content

Commit e9dbebd

Browse files
donny-dontnex3
authored andcommitted
Update Client interface (#56)
1 parent 2d2738d commit e9dbebd

10 files changed

+87
-365
lines changed

lib/http.dart

+10-14
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import 'src/client.dart';
1111
import 'src/response.dart';
1212

1313
export 'src/base_client.dart';
14-
export 'src/base_request.dart';
15-
export 'src/base_response.dart';
1614
export 'src/byte_stream.dart';
1715
export 'src/client.dart';
1816
export 'src/exception.dart';
@@ -21,8 +19,6 @@ export 'src/multipart_file.dart';
2119
export 'src/multipart_request.dart';
2220
export 'src/request.dart';
2321
export 'src/response.dart';
24-
export 'src/streamed_request.dart';
25-
export 'src/streamed_response.dart';
2622

2723
/// Sends an HTTP HEAD request with the given headers to the given URL, which
2824
/// can be a [Uri] or a [String].
@@ -65,10 +61,10 @@ Future<Response> get(url, {Map<String, String> headers}) =>
6561
///
6662
/// For more fine-grained control over the request, use [Request] or
6763
/// [StreamedRequest] instead.
68-
Future<Response> post(url, {Map<String, String> headers, body,
64+
Future<Response> post(url, body, {Map<String, String> headers,
6965
Encoding encoding}) =>
70-
_withClient((client) => client.post(url,
71-
headers: headers, body: body, encoding: encoding));
66+
_withClient((client) => client.post(url, body,
67+
headers: headers, encoding: encoding));
7268

7369
/// Sends an HTTP PUT request with the given headers and body to the given URL,
7470
/// which can be a [Uri] or a [String].
@@ -89,10 +85,10 @@ Future<Response> post(url, {Map<String, String> headers, body,
8985
///
9086
/// For more fine-grained control over the request, use [Request] or
9187
/// [StreamedRequest] instead.
92-
Future<Response> put(url, {Map<String, String> headers, body,
88+
Future<Response> put(url, body, {Map<String, String> headers,
9389
Encoding encoding}) =>
94-
_withClient((client) => client.put(url,
95-
headers: headers, body: body, encoding: encoding));
90+
_withClient((client) => client.put(url, body,
91+
headers: headers, encoding: encoding));
9692

9793
/// Sends an HTTP PATCH request with the given headers and body to the given
9894
/// URL, which can be a [Uri] or a [String].
@@ -113,10 +109,10 @@ Future<Response> put(url, {Map<String, String> headers, body,
113109
///
114110
/// For more fine-grained control over the request, use [Request] or
115111
/// [StreamedRequest] instead.
116-
Future<Response> patch(url, {Map<String, String> headers, body,
112+
Future<Response> patch(url, body, {Map<String, String> headers,
117113
Encoding encoding}) =>
118-
_withClient((client) => client.patch(url,
119-
headers: headers, body: body, encoding: encoding));
114+
_withClient((client) => client.patch(url, body,
115+
headers: headers, encoding: encoding));
120116

121117
/// Sends an HTTP DELETE request with the given headers to the given URL, which
122118
/// can be a [Uri] or a [String].
@@ -161,7 +157,7 @@ Future<String> read(url, {Map<String, String> headers}) =>
161157
Future<Uint8List> readBytes(url, {Map<String, String> headers}) =>
162158
_withClient((client) => client.readBytes(url, headers: headers));
163159

164-
Future/*<T>*/ _withClient/*<T>*/(Future/*<T>*/ fn(Client client)) async {
160+
Future<T> _withClient<T>(Future<T> fn(Client client)) async {
165161
var client = new Client();
166162
try {
167163
return await fn(client);

lib/src/base_client.dart

+24-47
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ import 'dart:async';
66
import 'dart:convert';
77
import 'dart:typed_data';
88

9-
import 'package:collection/collection.dart';
9+
import 'package:async/async.dart';
1010

11-
import 'base_request.dart';
1211
import 'client.dart';
1312
import 'exception.dart';
1413
import 'request.dart';
1514
import 'response.dart';
16-
import 'streamed_response.dart';
1715

1816
/// The abstract base class for an HTTP client. This is a mixin-style class;
1917
/// subclasses only need to implement [send] and maybe [close], and then they
@@ -24,14 +22,14 @@ abstract class BaseClient implements Client {
2422
///
2523
/// For more fine-grained control over the request, use [send] instead.
2624
Future<Response> head(url, {Map<String, String> headers}) =>
27-
_sendUnstreamed("HEAD", url, headers);
25+
send(new Request.head(url, headers: headers));
2826

2927
/// Sends an HTTP GET request with the given headers to the given URL, which
3028
/// can be a [Uri] or a [String].
3129
///
3230
/// For more fine-grained control over the request, use [send] instead.
3331
Future<Response> get(url, {Map<String, String> headers}) =>
34-
_sendUnstreamed("GET", url, headers);
32+
send(new Request.get(url, headers: headers));
3533

3634
/// Sends an HTTP POST request with the given headers and body to the given
3735
/// URL, which can be a [Uri] or a [String].
@@ -51,9 +49,10 @@ abstract class BaseClient implements Client {
5149
/// [encoding] defaults to UTF-8.
5250
///
5351
/// 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,
5553
Encoding encoding}) =>
56-
_sendUnstreamed("POST", url, headers, body, encoding);
54+
send(new Request.post(url, body, headers: headers,
55+
encoding: encoding));
5756

5857
/// Sends an HTTP PUT request with the given headers and body to the given
5958
/// URL, which can be a [Uri] or a [String].
@@ -73,9 +72,10 @@ abstract class BaseClient implements Client {
7372
/// [encoding] defaults to UTF-8.
7473
///
7574
/// 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,
7776
Encoding encoding}) =>
78-
_sendUnstreamed("PUT", url, headers, body, encoding);
77+
send(new Request.put(url, body, headers: headers,
78+
encoding: encoding));
7979

8080
/// Sends an HTTP PATCH request with the given headers and body to the given
8181
/// URL, which can be a [Uri] or a [String].
@@ -95,16 +95,17 @@ abstract class BaseClient implements Client {
9595
/// [encoding] defaults to UTF-8.
9696
///
9797
/// 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,
9999
Encoding encoding}) =>
100-
_sendUnstreamed("PATCH", url, headers, body, encoding);
100+
send(new Request.patch(url, body, headers: headers,
101+
encoding: encoding));
101102

102103
/// Sends an HTTP DELETE request with the given headers to the given URL,
103104
/// which can be a [Uri] or a [String].
104105
///
105106
/// For more fine-grained control over the request, use [send] instead.
106107
Future<Response> delete(url, {Map<String, String> headers}) =>
107-
_sendUnstreamed("DELETE", url, headers);
108+
send(new Request.delete(url, headers: headers));
108109

109110
/// Sends an HTTP GET request with the given headers to the given URL, which
110111
/// can be a [Uri] or a [String], and returns a Future that completes to the
@@ -115,11 +116,11 @@ abstract class BaseClient implements Client {
115116
///
116117
/// For more fine-grained control over the request and response, use [send] or
117118
/// [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();
123124
}
124125

125126
/// Sends an HTTP GET request with the given headers to the given URL, which
@@ -131,11 +132,11 @@ abstract class BaseClient implements Client {
131132
///
132133
/// For more fine-grained control over the request and response, use [send] or
133134
/// [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());
139140
}
140141

141142
/// Sends an HTTP request and asynchronously returns the response.
@@ -145,31 +146,7 @@ abstract class BaseClient implements Client {
145146
/// state of the stream; it could have data written to it asynchronously at a
146147
/// later point, or it could already be closed when it's returned. Any
147148
/// 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);
173150

174151
/// Throws an error if [response] is not successful.
175152
void _checkResponseSuccess(url, Response response) {

lib/src/base_request.dart

-140
This file was deleted.

0 commit comments

Comments
 (0)