OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library base_client; | 5 library base_client; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:typed_data'; | 9 import 'dart:typed_data'; |
10 | 10 |
11 import 'base_request.dart'; | 11 import 'base_request.dart'; |
12 import 'client.dart'; | 12 import 'client.dart'; |
13 import 'exception.dart'; | 13 import 'exception.dart'; |
14 import 'request.dart'; | 14 import 'request.dart'; |
15 import 'response.dart'; | 15 import 'response.dart'; |
16 import 'streamed_response.dart'; | 16 import 'streamed_response.dart'; |
17 import 'utils.dart'; | |
18 | 17 |
19 /// The abstract base class for an HTTP client. This is a mixin-style class; | 18 /// 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 | 19 /// subclasses only need to implement [send] and maybe [close], and then they |
21 /// get various convenience methods for free. | 20 /// get various convenience methods for free. |
22 abstract class BaseClient implements Client { | 21 abstract class BaseClient implements Client { |
23 /// Sends an HTTP HEAD request with the given headers to the given URL, which | 22 /// Sends an HTTP HEAD request with the given headers to the given URL, which |
24 /// can be a [Uri] or a [String]. | 23 /// can be a [Uri] or a [String]. |
25 /// | 24 /// |
26 /// For more fine-grained control over the request, use [send] instead. | 25 /// For more fine-grained control over the request, use [send] instead. |
27 Future<Response> head(url, {Map<String, String> headers}) => | 26 Future<Response> head(url, {Map<String, String> headers}) => |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
144 /// Implementers should call [BaseRequest.finalize] to get the body of the | 143 /// Implementers should call [BaseRequest.finalize] to get the body of the |
145 /// request as a [ByteStream]. They shouldn't make any assumptions about the | 144 /// request as a [ByteStream]. They shouldn't make any assumptions about the |
146 /// state of the stream; it could have data written to it asynchronously at a | 145 /// state of the stream; it could have data written to it asynchronously at a |
147 /// later point, or it could already be closed when it's returned. Any | 146 /// later point, or it could already be closed when it's returned. Any |
148 /// internal HTTP errors should be wrapped as [ClientException]s. | 147 /// internal HTTP errors should be wrapped as [ClientException]s. |
149 Future<StreamedResponse> send(BaseRequest request); | 148 Future<StreamedResponse> send(BaseRequest request); |
150 | 149 |
151 /// Sends a non-streaming [Request] and returns a non-streaming [Response]. | 150 /// Sends a non-streaming [Request] and returns a non-streaming [Response]. |
152 Future<Response> _sendUnstreamed(String method, url, | 151 Future<Response> _sendUnstreamed(String method, url, |
153 Map<String, String> headers, [body, Encoding encoding]) { | 152 Map<String, String> headers, [body, Encoding encoding]) { |
154 return syncFuture(() { | |
nweiz
2015/08/25 23:25:00
If you're going to remove this, make the function
kevmoo
2015/08/27 21:14:20
Done - updated min SDK to 1.9
| |
155 if (url is String) url = Uri.parse(url); | |
156 var request = new Request(method, url); | |
157 | 153 |
158 if (headers != null) request.headers.addAll(headers); | 154 if (url is String) url = Uri.parse(url); |
159 if (encoding != null) request.encoding = encoding; | 155 var request = new Request(method, url); |
160 if (body != null) { | 156 |
161 if (body is String) { | 157 if (headers != null) request.headers.addAll(headers); |
162 request.body = body; | 158 if (encoding != null) request.encoding = encoding; |
163 } else if (body is List) { | 159 if (body != null) { |
164 request.bodyBytes = body; | 160 if (body is String) { |
165 } else if (body is Map) { | 161 request.body = body; |
166 request.bodyFields = body; | 162 } else if (body is List) { |
167 } else { | 163 request.bodyBytes = body; |
168 throw new ArgumentError('Invalid request body "$body".'); | 164 } else if (body is Map) { |
169 } | 165 request.bodyFields = body; |
166 } else { | |
167 throw new ArgumentError('Invalid request body "$body".'); | |
170 } | 168 } |
169 } | |
171 | 170 |
172 return send(request); | 171 return send(request).then(Response.fromStream); |
173 }).then(Response.fromStream); | |
174 } | 172 } |
175 | 173 |
176 /// Throws an error if [response] is not successful. | 174 /// Throws an error if [response] is not successful. |
177 void _checkResponseSuccess(url, Response response) { | 175 void _checkResponseSuccess(url, Response response) { |
178 if (response.statusCode < 400) return; | 176 if (response.statusCode < 400) return; |
179 var message = "Request to $url failed with status ${response.statusCode}"; | 177 var message = "Request to $url failed with status ${response.statusCode}"; |
180 if (response.reasonPhrase != null) { | 178 if (response.reasonPhrase != null) { |
181 message = "$message: ${response.reasonPhrase}"; | 179 message = "$message: ${response.reasonPhrase}"; |
182 } | 180 } |
183 if (url is String) url = Uri.parse(url); | 181 if (url is String) url = Uri.parse(url); |
184 throw new ClientException("$message.", url); | 182 throw new ClientException("$message.", url); |
185 } | 183 } |
186 | 184 |
187 /// Closes the client and cleans up any resources associated with it. It's | 185 /// Closes the client and cleans up any resources associated with it. It's |
188 /// important to close each client when it's done being used; failing to do so | 186 /// important to close each client when it's done being used; failing to do so |
189 /// can cause the Dart process to hang. | 187 /// can cause the Dart process to hang. |
190 void close() {} | 188 void close() {} |
191 } | 189 } |
OLD | NEW |