Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(221)

Unified Diff: pkg/http/lib/src/base_client.dart

Issue 11338054: Add an HTTP library that wraps dart:io. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/http/lib/src/base_client.dart
diff --git a/pkg/http/lib/src/base_client.dart b/pkg/http/lib/src/base_client.dart
new file mode 100644
index 0000000000000000000000000000000000000000..bdcf52c39fd85c73f6c151ae013eab63aaf578e9
--- /dev/null
+++ b/pkg/http/lib/src/base_client.dart
@@ -0,0 +1,130 @@
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library base_client;
+
+import 'dart:io';
+import 'dart:scalarlist';
+import 'dart:uri';
+
+import 'base_request.dart';
+import 'request.dart';
+import 'response.dart';
+import 'stream_response.dart';
+import 'utils.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
+/// get various convenience methods for free.
+abstract class BaseClient {
+ /// Send an HTTP HEAD request with the given headers to the given URI.
Bob Nystrom 2012/10/31 01:17:44 "Send" -> "Sends" here and elsewhere. I think we u
nweiz 2012/10/31 18:20:59 Done. Guess I got too used to Python's style.
+ ///
+ /// For more fine-grained control over the request, use [send] instead.
+ Future<Response> head(Uri uri, {Map<String, String> headers: null}) =>
Bob Nystrom 2012/10/31 01:17:44 The ": null" shouldn't be required here and elsewh
nweiz 2012/10/31 18:20:59 Done.
+ _sendNoStream("HEAD", uri, headers);
Bob Nystrom 2012/10/31 01:17:44 Since this is a line continuation, it should be in
nweiz 2012/10/31 18:20:59 I like indenting this +2, since it reads more like
+
+ /// Send an HTTP GET request with the given headers to the given URI.
+ ///
+ /// For more fine-grained control over the request, use [send] instead.
+ Future<Response> get(Uri uri, {Map<String, String> headers: null}) =>
+ _sendNoStream("GET", uri, headers);
+
+ /// Send an HTTP POST request with the given headers and fields to the given
+ /// URI. If any fields are specified, the content-type is automatically set to
+ /// `"application/x-www-form-urlencoded"`.
+ ///
+ /// For more fine-grained control over the request, use [send] instead.
+ Future<Response> post(Uri uri,
+ {Map<String, String> headers: null,
+ Map<String, String> fields: null}) =>
+ _sendNoStream("POST", uri, headers, fields);
+
+ /// Send an HTTP PUT request with the given headers and fields to the given
+ /// URI. If any fields are specified, the content-type is automatically set to
+ /// `"application/x-www-form-urlencoded"`.
+ ///
+ /// For more fine-grained control over the request, use [send] instead.
+ Future<Response> put(Uri uri,
+ {Map<String, String> headers: null,
+ Map<String, String> fields: null}) =>
+ _sendNoStream("PUT", uri, headers, fields);
+
+ /// Send an HTTP DELETE request with the given headers to the given URI.
+ ///
+ /// For more fine-grained control over the request, use [send] instead.
+ Future<Response> delete(Uri uri, {Map<String, String> headers: null}) =>
+ _sendNoStream("DELETE", uri, headers);
+
+ /// Send an HTTP GET request with the given headers to the given URI, and
+ /// return a Future that completes to the body of the response as a String.
+ ///
+ /// The Future will emit an [HttpException] if the response doesn't have a
+ /// success status code.
+ ///
+ /// For more fine-grained control over the request and response, use [send] or
+ /// [get] instead.
+ Future<String> read(Uri uri, {Map<String, String> headers: null}) {
+ return get(uri, headers: headers).transform((response) {
+ _checkResponseSuccess(response);
+ return response.body;
+ });
+ }
+
+ /// Send an HTTP GET request with the given headers to the given URI, and
+ /// return a Future that completes to the body of the response as a list of
+ /// bytes.
+ ///
+ /// The Future will emit an [HttpException] if the response doesn't have a
+ /// success status code.
+ ///
+ /// For more fine-grained control over the request and response, use [send] or
+ /// [get] instead.
+ Future<Uint8List> readBytes(Uri uri, {Map<String, String> headers: null}) {
+ return get(uri, headers: headers).transform((response) {
+ _checkResponseSuccess(response);
+ return response.bodyBytes;
+ });
+ }
+
+ /// Send an HTTP request and asynchronously return the response.
+ ///
+ /// Implementers should call [BaseRequest.finalize] to get the body of the
+ /// request as an [InputStream]. They shouldn't make any assumptions about the
+ /// 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.
+ Future<StreamResponse> send(BaseRequest request);
+
+ /// Send a non-streaming [Request] and return a non-streaming [Response].
+ Future<Response> _sendNoStream(
Bob Nystrom 2012/10/31 01:17:44 This name feels a bit strange to me. Maybe "_sendU
nweiz 2012/10/31 18:20:59 Done.
+ String method,
+ Uri uri,
+ Map<String, String> headers,
Bob Nystrom 2012/10/31 01:17:44 Nit, but how about having the required params all
nweiz 2012/10/31 18:20:59 Done.
+ [Map<String, String> fields]) {
+ // Wrap everything in a Future block so that synchronous validation errors
+ // are passed through the Future chain.
+ return new Future.immediate(null).chain((_) {
Bob Nystrom 2012/10/31 01:17:44 This can still cause errors to be sent synchronous
nweiz 2012/10/31 18:20:59 Done.
+ var request = new Request(method, uri);
+
+ if (headers != null) mapAddAll(request.headers, headers);
+ if (fields != null && !fields.isEmpty) request.bodyFields = fields;
+
+ return send(request);
+ }).chain(Response.fromStream);
+ }
+
+ /// Throw an error if [response] is not successful.
+ void _checkResponseSuccess(Response response) {
+ if (response.statusCode < 400) return;
+ var message = "Request to $uri failed with status ${response.statusCode}";
+ if (response.reasonPhrase != null) {
+ message = "$message: ${response.reasonPhrase}";
+ }
+ throw new HttpException("$message.");
Bob Nystrom 2012/10/31 01:17:44 Pub has its own PubHttpException class specificall
nweiz 2012/10/31 18:20:59 I feel like that would be more confusing than it w
Bob Nystrom 2012/11/01 19:53:59 Yeah, I definitely wouldn't want to mix and match
nweiz 2012/11/02 19:29:12 The thing is, most HTTP errors happen because of t
Bob Nystrom 2012/11/02 19:35:03 SGTM.
+ }
+
+ /// Close the client and clean up any resources associated with it. It's
+ /// important to close each client when it's done being used; failing to do so
+ /// can cause the Dart process to hang.
+ void close() {}
+}

Powered by Google App Engine
This is Rietveld 408576698