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

Update Client interface #56

Merged
merged 13 commits into from
Mar 6, 2017
24 changes: 10 additions & 14 deletions lib/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import 'src/client.dart';
import 'src/response.dart';

export 'src/base_client.dart';
export 'src/base_request.dart';
export 'src/base_response.dart';
export 'src/byte_stream.dart';
export 'src/client.dart';
export 'src/exception.dart';
Expand All @@ -21,8 +19,6 @@ export 'src/multipart_file.dart';
export 'src/multipart_request.dart';
export 'src/request.dart';
export 'src/response.dart';
export 'src/streamed_request.dart';
export 'src/streamed_response.dart';

/// Sends an HTTP HEAD request with the given headers to the given URL, which
/// can be a [Uri] or a [String].
Expand Down Expand Up @@ -65,10 +61,10 @@ Future<Response> get(url, {Map<String, String> headers}) =>
///
/// For more fine-grained control over the request, use [Request] or
/// [StreamedRequest] instead.
Future<Response> post(url, {Map<String, String> headers, body,
Future<Response> post(url, body, {Map<String, String> headers,
Encoding encoding}) =>
_withClient((client) => client.post(url,
headers: headers, body: body, encoding: encoding));
_withClient((client) => client.post(url, body,
headers: headers, encoding: encoding));

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

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

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

Future/*<T>*/ _withClient/*<T>*/(Future/*<T>*/ fn(Client client)) async {
Future<T> _withClient<T>(Future<T> fn(Client client)) async {
var client = new Client();
try {
return await fn(client);
Expand Down
71 changes: 24 additions & 47 deletions lib/src/base_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';

import 'package:collection/collection.dart';
import 'package:async/async.dart';

import 'base_request.dart';
import 'client.dart';
import 'exception.dart';
import 'request.dart';
import 'response.dart';
import 'streamed_response.dart';

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

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

/// Sends an HTTP POST request with the given headers and body to the given
/// URL, which can be a [Uri] or a [String].
Expand All @@ -51,9 +49,10 @@ abstract class BaseClient implements Client {
/// [encoding] defaults to UTF-8.
///
/// For more fine-grained control over the request, use [send] instead.
Future<Response> post(url, {Map<String, String> headers, body,
Future<Response> post(url, body, {Map<String, String> headers,
Encoding encoding}) =>
_sendUnstreamed("POST", url, headers, body, encoding);
send(new Request.post(url, body, headers: headers,
encoding: encoding));

/// Sends an HTTP PUT request with the given headers and body to the given
/// URL, which can be a [Uri] or a [String].
Expand All @@ -73,9 +72,10 @@ abstract class BaseClient implements Client {
/// [encoding] defaults to UTF-8.
///
/// For more fine-grained control over the request, use [send] instead.
Future<Response> put(url, {Map<String, String> headers, body,
Future<Response> put(url, body, {Map<String, String> headers,
Encoding encoding}) =>
_sendUnstreamed("PUT", url, headers, body, encoding);
send(new Request.put(url, body, headers: headers,
encoding: encoding));

/// Sends an HTTP PATCH request with the given headers and body to the given
/// URL, which can be a [Uri] or a [String].
Expand All @@ -95,16 +95,17 @@ abstract class BaseClient implements Client {
/// [encoding] defaults to UTF-8.
///
/// For more fine-grained control over the request, use [send] instead.
Future<Response> patch(url, {Map<String, String> headers, body,
Future<Response> patch(url, body, {Map<String, String> headers,
Encoding encoding}) =>
_sendUnstreamed("PATCH", url, headers, body, encoding);
send(new Request.patch(url, body, headers: headers,
encoding: encoding));

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

/// Sends an HTTP GET request with the given headers to the given URL, which
/// can be a [Uri] or a [String], and returns a Future that completes to the
Expand All @@ -115,11 +116,11 @@ abstract class BaseClient implements Client {
///
/// For more fine-grained control over the request and response, use [send] or
/// [get] instead.
Future<String> read(url, {Map<String, String> headers}) {
return get(url, headers: headers).then((response) {
_checkResponseSuccess(url, response);
return response.body;
});
Future<String> read(url, {Map<String, String> headers}) async {
var response = await get(url, headers: headers);
_checkResponseSuccess(url, response);

return await response.readAsString();
}

/// Sends an HTTP GET request with the given headers to the given URL, which
Expand All @@ -131,11 +132,11 @@ abstract class BaseClient implements Client {
///
/// For more fine-grained control over the request and response, use [send] or
/// [get] instead.
Future<Uint8List> readBytes(url, {Map<String, String> headers}) {
return get(url, headers: headers).then((response) {
_checkResponseSuccess(url, response);
return response.bodyBytes;
});
Future<Uint8List> readBytes(url, {Map<String, String> headers}) async {
var response = await get(url, headers: headers);
_checkResponseSuccess(url, response);

return await collectBytes(response.read());
}

/// Sends an HTTP request and asynchronously returns the response.
Expand All @@ -145,31 +146,7 @@ abstract class BaseClient implements Client {
/// state of the stream; it could have data written to it asynchronously at a
/// later point, or it could already be closed when it's returned. Any
/// internal HTTP errors should be wrapped as [ClientException]s.
Future<StreamedResponse> send(BaseRequest request);

/// Sends a non-streaming [Request] and returns a non-streaming [Response].
Future<Response> _sendUnstreamed(String method, url,
Map<String, String> headers, [body, Encoding encoding]) async {

if (url is String) url = Uri.parse(url);
var request = new Request(method, url);

if (headers != null) request.headers.addAll(headers);
if (encoding != null) request.encoding = encoding;
if (body != null) {
if (body is String) {
request.body = body;
} else if (body is List) {
request.bodyBytes = DelegatingList.typed(body);
} else if (body is Map) {
request.bodyFields = DelegatingMap.typed(body);
} else {
throw new ArgumentError('Invalid request body "$body".');
}
}

return Response.fromStream(await send(request));
}
Future<Response> send(Request request);

/// Throws an error if [response] is not successful.
void _checkResponseSuccess(url, Response response) {
Expand Down
140 changes: 0 additions & 140 deletions lib/src/base_request.dart

This file was deleted.

Loading