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

Side by Side Diff: pkg/http/lib/src/base_client.dart

Issue 810223002: Remove the http package from the repo. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years 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:async';
8 import 'dart:convert';
9 import 'dart:typed_data';
10
11 import 'base_request.dart';
12 import 'client.dart';
13 import 'exception.dart';
14 import 'request.dart';
15 import 'response.dart';
16 import 'streamed_response.dart';
17 import 'utils.dart';
18
19 /// The abstract base class for an HTTP client. This is a mixin-style class;
20 /// subclasses only need to implement [send] and maybe [close], and then they
21 /// get various convenience methods for free.
22 abstract class BaseClient implements Client {
23 /// Sends an HTTP HEAD request with the given headers to the given URL, which
24 /// can be a [Uri] or a [String].
25 ///
26 /// For more fine-grained control over the request, use [send] instead.
27 Future<Response> head(url, {Map<String, String> headers}) =>
28 _sendUnstreamed("HEAD", url, headers);
29
30 /// Sends an HTTP GET request with the given headers to the given URL, which
31 /// can be a [Uri] or a [String].
32 ///
33 /// For more fine-grained control over the request, use [send] instead.
34 Future<Response> get(url, {Map<String, String> headers}) =>
35 _sendUnstreamed("GET", url, headers);
36
37 /// Sends an HTTP POST request with the given headers and body to the given
38 /// URL, which can be a [Uri] or a [String].
39 ///
40 /// [body] sets the body of the request. It can be a [String], a [List<int>]
41 /// or a [Map<String, String>]. If it's a String, it's encoded using
42 /// [encoding] and used as the body of the request. The content-type of the
43 /// request will default to "text/plain".
44 ///
45 /// If [body] is a List, it's used as a list of bytes for the body of the
46 /// request.
47 ///
48 /// If [body] is a Map, it's encoded as form fields using [encoding]. The
49 /// content-type of the request will be set to
50 /// `"application/x-www-form-urlencoded"`; this cannot be overridden.
51 ///
52 /// [encoding] defaults to UTF-8.
53 ///
54 /// For more fine-grained control over the request, use [send] instead.
55 Future<Response> post(url, {Map<String, String> headers, body,
56 Encoding encoding}) =>
57 _sendUnstreamed("POST", url, headers, body, encoding);
58
59 /// Sends an HTTP PUT request with the given headers and body to the given
60 /// URL, which can be a [Uri] or a [String].
61 ///
62 /// [body] sets the body of the request. It can be a [String], a [List<int>]
63 /// or a [Map<String, String>]. If it's a String, it's encoded using
64 /// [encoding] and used as the body of the request. The content-type of the
65 /// request will default to "text/plain".
66 ///
67 /// If [body] is a List, it's used as a list of bytes for the body of the
68 /// request.
69 ///
70 /// If [body] is a Map, it's encoded as form fields using [encoding]. The
71 /// content-type of the request will be set to
72 /// `"application/x-www-form-urlencoded"`; this cannot be overridden.
73 ///
74 /// [encoding] defaults to UTF-8.
75 ///
76 /// For more fine-grained control over the request, use [send] instead.
77 Future<Response> put(url, {Map<String, String> headers, body,
78 Encoding encoding}) =>
79 _sendUnstreamed("PUT", url, headers, body, encoding);
80
81 /// Sends an HTTP DELETE request with the given headers to the given URL,
82 /// which can be a [Uri] or a [String].
83 ///
84 /// For more fine-grained control over the request, use [send] instead.
85 Future<Response> delete(url, {Map<String, String> headers}) =>
86 _sendUnstreamed("DELETE", url, headers);
87
88 /// Sends an HTTP GET request with the given headers to the given URL, which
89 /// can be a [Uri] or a [String], and returns a Future that completes to the
90 /// body of the response as a String.
91 ///
92 /// The Future will emit a [ClientException] if the response doesn't have a
93 /// success status code.
94 ///
95 /// For more fine-grained control over the request and response, use [send] or
96 /// [get] instead.
97 Future<String> read(url, {Map<String, String> headers}) {
98 return get(url, headers: headers).then((response) {
99 _checkResponseSuccess(url, response);
100 return response.body;
101 });
102 }
103
104 /// Sends an HTTP GET request with the given headers to the given URL, which
105 /// can be a [Uri] or a [String], and returns a Future that completes to the
106 /// body of the response as a list of bytes.
107 ///
108 /// The Future will emit an [ClientException] if the response doesn't have a
109 /// success status code.
110 ///
111 /// For more fine-grained control over the request and response, use [send] or
112 /// [get] instead.
113 Future<Uint8List> readBytes(url, {Map<String, String> headers}) {
114 return get(url, headers: headers).then((response) {
115 _checkResponseSuccess(url, response);
116 return response.bodyBytes;
117 });
118 }
119
120 /// Sends an HTTP request and asynchronously returns the response.
121 ///
122 /// Implementers should call [BaseRequest.finalize] to get the body of the
123 /// request as a [ByteStream]. They shouldn't make any assumptions about the
124 /// state of the stream; it could have data written to it asynchronously at a
125 /// later point, or it could already be closed when it's returned. Any
126 /// internal HTTP errors should be wrapped as [ClientException]s.
127 Future<StreamedResponse> send(BaseRequest request);
128
129 /// Sends a non-streaming [Request] and returns a non-streaming [Response].
130 Future<Response> _sendUnstreamed(String method, url,
131 Map<String, String> headers, [body, Encoding encoding]) {
132 return syncFuture(() {
133 if (url is String) url = Uri.parse(url);
134 var request = new Request(method, url);
135
136 if (headers != null) request.headers.addAll(headers);
137 if (encoding != null) request.encoding = encoding;
138 if (body != null) {
139 if (body is String) {
140 request.body = body;
141 } else if (body is List) {
142 request.bodyBytes = body;
143 } else if (body is Map) {
144 request.bodyFields = body;
145 } else {
146 throw new ArgumentError('Invalid request body "$body".');
147 }
148 }
149
150 return send(request);
151 }).then(Response.fromStream);
152 }
153
154 /// Throws an error if [response] is not successful.
155 void _checkResponseSuccess(url, Response response) {
156 if (response.statusCode < 400) return;
157 var message = "Request to $url failed with status ${response.statusCode}";
158 if (response.reasonPhrase != null) {
159 message = "$message: ${response.reasonPhrase}";
160 }
161 if (url is String) url = Uri.parse(url);
162 throw new ClientException("$message.", url);
163 }
164
165 /// Closes the client and cleans up any resources associated with it. It's
166 /// important to close each client when it's done being used; failing to do so
167 /// can cause the Dart process to hang.
168 void close() {}
169 }
OLDNEW
« no previous file with comments | « pkg/http/lib/http.dart ('k') | pkg/http/lib/src/base_request.dart » ('j') | pkg/pkg.gyp » ('J')

Powered by Google App Engine
This is Rietveld 408576698