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

Side by Side 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, 1 month 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library base_client;
6
7 import 'dart:io';
8 import 'dart:scalarlist';
9 import 'dart:uri';
10
11 import 'base_request.dart';
12 import 'request.dart';
13 import 'response.dart';
14 import 'stream_response.dart';
15 import 'utils.dart';
16
17 /// The abstract base class for an HTTP client. This is a mixin-style class;
18 /// subclasses only need to implement [send] and maybe [close], and then they
19 /// get various convenience methods for free.
20 abstract class BaseClient {
21 /// 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.
22 ///
23 /// For more fine-grained control over the request, use [send] instead.
24 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.
25 _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
26
27 /// Send an HTTP GET request with the given headers to the given URI.
28 ///
29 /// For more fine-grained control over the request, use [send] instead.
30 Future<Response> get(Uri uri, {Map<String, String> headers: null}) =>
31 _sendNoStream("GET", uri, headers);
32
33 /// Send an HTTP POST request with the given headers and fields to the given
34 /// URI. If any fields are specified, the content-type is automatically set to
35 /// `"application/x-www-form-urlencoded"`.
36 ///
37 /// For more fine-grained control over the request, use [send] instead.
38 Future<Response> post(Uri uri,
39 {Map<String, String> headers: null,
40 Map<String, String> fields: null}) =>
41 _sendNoStream("POST", uri, headers, fields);
42
43 /// Send an HTTP PUT request with the given headers and fields to the given
44 /// URI. If any fields are specified, the content-type is automatically set to
45 /// `"application/x-www-form-urlencoded"`.
46 ///
47 /// For more fine-grained control over the request, use [send] instead.
48 Future<Response> put(Uri uri,
49 {Map<String, String> headers: null,
50 Map<String, String> fields: null}) =>
51 _sendNoStream("PUT", uri, headers, fields);
52
53 /// Send an HTTP DELETE request with the given headers to the given URI.
54 ///
55 /// For more fine-grained control over the request, use [send] instead.
56 Future<Response> delete(Uri uri, {Map<String, String> headers: null}) =>
57 _sendNoStream("DELETE", uri, headers);
58
59 /// Send an HTTP GET request with the given headers to the given URI, and
60 /// return a Future that completes to the body of the response as a String.
61 ///
62 /// The Future will emit an [HttpException] if the response doesn't have a
63 /// success status code.
64 ///
65 /// For more fine-grained control over the request and response, use [send] or
66 /// [get] instead.
67 Future<String> read(Uri uri, {Map<String, String> headers: null}) {
68 return get(uri, headers: headers).transform((response) {
69 _checkResponseSuccess(response);
70 return response.body;
71 });
72 }
73
74 /// Send an HTTP GET request with the given headers to the given URI, and
75 /// return a Future that completes to the body of the response as a list of
76 /// bytes.
77 ///
78 /// The Future will emit an [HttpException] if the response doesn't have a
79 /// success status code.
80 ///
81 /// For more fine-grained control over the request and response, use [send] or
82 /// [get] instead.
83 Future<Uint8List> readBytes(Uri uri, {Map<String, String> headers: null}) {
84 return get(uri, headers: headers).transform((response) {
85 _checkResponseSuccess(response);
86 return response.bodyBytes;
87 });
88 }
89
90 /// Send an HTTP request and asynchronously return the response.
91 ///
92 /// Implementers should call [BaseRequest.finalize] to get the body of the
93 /// request as an [InputStream]. They shouldn't make any assumptions about the
94 /// state of the stream; it could have data written to it asynchronously at a
95 /// later point, or it could already be closed when it's returned.
96 Future<StreamResponse> send(BaseRequest request);
97
98 /// Send a non-streaming [Request] and return a non-streaming [Response].
99 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.
100 String method,
101 Uri uri,
102 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.
103 [Map<String, String> fields]) {
104 // Wrap everything in a Future block so that synchronous validation errors
105 // are passed through the Future chain.
106 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.
107 var request = new Request(method, uri);
108
109 if (headers != null) mapAddAll(request.headers, headers);
110 if (fields != null && !fields.isEmpty) request.bodyFields = fields;
111
112 return send(request);
113 }).chain(Response.fromStream);
114 }
115
116 /// Throw an error if [response] is not successful.
117 void _checkResponseSuccess(Response response) {
118 if (response.statusCode < 400) return;
119 var message = "Request to $uri failed with status ${response.statusCode}";
120 if (response.reasonPhrase != null) {
121 message = "$message: ${response.reasonPhrase}";
122 }
123 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.
124 }
125
126 /// Close the client and clean up any resources associated with it. It's
127 /// important to close each client when it's done being used; failing to do so
128 /// can cause the Dart process to hang.
129 void close() {}
130 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698