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
2 changes: 1 addition & 1 deletion lib/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,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>(FutureOr<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
40 changes: 15 additions & 25 deletions lib/src/base_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ abstract class BaseClient implements Client {
/// can be a [Uri] or a [String].
///
/// For more fine-grained control over the request, use [send] instead.
FutureOr<Response> head(url, {Map<String, String> headers}) =>
send(new Request.head(_uri(url), headers: headers));
Future<Response> head(url, {Map<String, String> 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.
FutureOr<Response> get(url, {Map<String, String> headers}) =>
send(new Request.get(_uri(url), headers: headers));
Future<Response> get(url, {Map<String, String> 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 @@ -49,9 +49,9 @@ abstract class BaseClient implements Client {
/// [encoding] defaults to UTF-8.
///
/// For more fine-grained control over the request, use [send] instead.
FutureOr<Response> post(url, body, {Map<String, String> headers,
Future<Response> post(url, body, {Map<String, String> headers,
Encoding encoding}) =>
send(new Request.post(_uri(url), body, headers: headers,
send(new Request.post(url, body, headers: headers,
encoding: encoding));

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

/// Sends an HTTP PATCH request with the given headers and body to the given
Expand All @@ -95,17 +95,17 @@ abstract class BaseClient implements Client {
/// [encoding] defaults to UTF-8.
///
/// For more fine-grained control over the request, use [send] instead.
FutureOr<Response> patch(url, body, {Map<String, String> headers,
Future<Response> patch(url, body, {Map<String, String> headers,
Encoding encoding}) =>
send(new Request.patch(_uri(url), body, headers: headers,
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.
FutureOr<Response> delete(url, {Map<String, String> headers}) =>
send(new Request.delete(_uri(url), headers: headers));
Future<Response> delete(url, {Map<String, String> 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 @@ -116,7 +116,7 @@ abstract class BaseClient implements Client {
///
/// For more fine-grained control over the request and response, use [send] or
/// [get] instead.
FutureOr<String> read(url, {Map<String, String> headers}) async {
Future<String> read(url, {Map<String, String> headers}) async {
var response = await get(url, headers: headers);
_checkResponseSuccess(url, response);

Expand All @@ -132,7 +132,7 @@ abstract class BaseClient implements Client {
///
/// For more fine-grained control over the request and response, use [send] or
/// [get] instead.
FutureOr<Uint8List> readBytes(url, {Map<String, String> headers}) async {
Future<Uint8List> readBytes(url, {Map<String, String> headers}) async {
var response = await get(url, headers: headers);
_checkResponseSuccess(url, response);

Expand All @@ -146,7 +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.
FutureOr<Response> send(Request request);
Future<Response> send(Request request);

/// Throws an error if [response] is not successful.
void _checkResponseSuccess(url, Response response) {
Expand All @@ -164,13 +164,3 @@ abstract class BaseClient implements Client {
/// can cause the Dart process to hang.
void close() {}
}

Uri _uri(url) {
if (url is Uri) {
return url;
} else if (url is String) {
return Uri.parse(url);
} else {
throw new ArgumentError.value(url, 'url', 'Not a Uri or String');
}
}
18 changes: 9 additions & 9 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ abstract class Client {
/// can be a [Uri] or a [String].
///
/// For more fine-grained control over the request, use [send] instead.
FutureOr<Response> head(url, {Map<String, String> headers});
Future<Response> head(url, {Map<String, String> 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.
FutureOr<Response> get(url, {Map<String, String> headers});
Future<Response> get(url, {Map<String, String> 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 @@ -58,7 +58,7 @@ abstract class Client {
/// [encoding] defaults to [UTF8].
///
/// For more fine-grained control over the request, use [send] instead.
FutureOr<Response> post(url, body, {Map<String, String> headers,
Future<Response> post(url, body, {Map<String, String> headers,
Encoding encoding});

/// Sends an HTTP PUT request with the given headers and body to the given
Expand All @@ -79,7 +79,7 @@ abstract class Client {
/// [encoding] defaults to [UTF8].
///
/// For more fine-grained control over the request, use [send] instead.
FutureOr<Response> put(url, body, {Map<String, String> headers,
Future<Response> put(url, body, {Map<String, String> headers,
Encoding encoding});

/// Sends an HTTP PATCH request with the given headers and body to the given
Expand All @@ -100,14 +100,14 @@ abstract class Client {
/// [encoding] defaults to [UTF8].
///
/// For more fine-grained control over the request, use [send] instead.
FutureOr<Response> patch(url, body, {Map<String, String> headers,
Future<Response> patch(url, body, {Map<String, String> 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.
FutureOr<Response> delete(url, {Map<String, String> headers});
Future<Response> delete(url, {Map<String, String> 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 @@ -118,7 +118,7 @@ abstract class Client {
///
/// For more fine-grained control over the request and response, use [send] or
/// [get] instead.
FutureOr<String> read(url, {Map<String, String> headers});
Future<String> read(url, {Map<String, String> 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 @@ -129,10 +129,10 @@ abstract class Client {
///
/// For more fine-grained control over the request and response, use [send] or
/// [get] instead.
FutureOr<Uint8List> readBytes(url, {Map<String, String> headers});
Future<Uint8List> readBytes(url, {Map<String, String> headers});

/// Sends an HTTP request and asynchronously returns the response.
FutureOr<Response> send(Request request);
Future<Response> send(Request request);

/// Closes the client and cleans up any resources associated with it. It's
/// important to close each client when it's done being used; failing to do so
Expand Down
48 changes: 32 additions & 16 deletions lib/src/request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ class Request extends Message {
///
/// Extra [context] can be used to pass information between inner middleware
/// and handlers.
Request(this.method, this.url,
Request(String method, url,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document the acceptable types for url. You can probably just copy the docs from the top-level methods.

{body,
Encoding encoding,
Map<String, String> headers,
Map<String, Object> context})
: super(body, encoding: encoding, headers: headers, context: context);
: this._(method, _uri(url), body, encoding, headers, context);

/// Creates a new HEAD [Request] to [url].
///
Expand All @@ -44,7 +44,7 @@ class Request extends Message {
///
/// Extra [context] can be used to pass information between inner middleware
/// and handlers.
Request.head(Uri url,
Request.head(url,
{Map<String, String> headers, Map<String, Object> context})
: this('HEAD', url, headers: headers, context: context);

Expand All @@ -55,7 +55,7 @@ class Request extends Message {
///
/// Extra [context] can be used to pass information between inner middleware
/// and handlers.
Request.get(Uri url,
Request.get(url,
{Map<String, String> headers, Map<String, Object> context})
: this('GET', url, headers: headers, context: context);

Expand All @@ -70,8 +70,7 @@ class Request extends Message {
///
/// Extra [context] can be used to pass information between inner middleware
/// and handlers.
Request.post(Uri url,
body,
Request.post(url, body,
{Encoding encoding,
Map<String, String> headers,
Map<String, Object> context})
Expand All @@ -90,8 +89,7 @@ class Request extends Message {
///
/// Extra [context] can be used to pass information between inner middleware
/// and handlers.
Request.put(Uri url,
body,
Request.put(url, body,
{Encoding encoding,
Map<String, String> headers,
Map<String, Object> context})
Expand All @@ -110,8 +108,7 @@ class Request extends Message {
///
/// Extra [context] can be used to pass information between inner middleware
/// and handlers.
Request.patch(Uri url,
body,
Request.patch(url, body,
{Encoding encoding,
Map<String, String> headers,
Map<String, Object> context})
Expand All @@ -125,10 +122,17 @@ class Request extends Message {
///
/// Extra [context] can be used to pass information between inner middleware
/// and handlers.
Request.delete(Uri url,
Request.delete(url,
{Map<String, String> headers, Map<String, Object> context})
: this('DELETE', url, headers: headers, context: context);

Request._(this.method, this.url,
body,
Encoding encoding,
Map<String, String> headers,
Map<String, Object> context)
: super(body, encoding: encoding, headers: headers, context: context);

/// Creates a new [Request] by copying existing values and applying specified
/// changes.
///
Expand All @@ -147,10 +151,22 @@ class Request extends Message {
var updatedHeaders = updateMap(this.headers, headers);
var updatedContext = updateMap(this.context, context);

return new Request(this.method, this.url,
body: body ?? getBody(this),
encoding: this.encoding,
headers: updatedHeaders,
context: updatedContext);
return new Request._(
this.method,
this.url,
body ?? getBody(this),
this.encoding,
updatedHeaders,
updatedContext);
}
}

Uri _uri(url) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either make this a static method of Request, or put it in utils.

if (url is Uri) {
return url;
} else if (url is String) {
return Uri.parse(url);
} else {
throw new ArgumentError.value(url, 'url', 'Not a Uri or String');
}
}