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