| 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 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. |
| 40 /// | 53 /// |
| 41 /// For more fine-grained control over the request, use [send] instead. | 54 /// For more fine-grained control over the request, use [send] instead. |
| 42 Future<Response> post(url, | 55 Future<Response> post(url, {Map<String, String> headers, body, |
| 43 {Map<String, String> headers, | 56 Encoding encoding}) => |
| 44 Map<String, String> fields}) => | 57 _sendUnstreamed("POST", url, headers, body, encoding); |
| 45 _sendUnstreamed("POST", url, headers, fields); | |
| 46 | 58 |
| 47 /// Sends an HTTP PUT request with the given headers and fields to the given | 59 /// 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 | 60 /// URL, which can be a [Uri] or a [String]. |
| 49 /// content-type is automatically set to | 61 /// |
| 50 /// `"application/x-www-form-urlencoded"`. | 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. |
| 51 /// | 75 /// |
| 52 /// For more fine-grained control over the request, use [send] instead. | 76 /// For more fine-grained control over the request, use [send] instead. |
| 53 Future<Response> put(url, | 77 Future<Response> put(url, {Map<String, String> headers, body, |
| 54 {Map<String, String> headers, | 78 Encoding encoding}) => |
| 55 Map<String, String> fields}) => | 79 _sendUnstreamed("PUT", url, headers, body, encoding); |
| 56 _sendUnstreamed("PUT", url, headers, fields); | |
| 57 | 80 |
| 58 /// Sends an HTTP DELETE request with the given headers to the given URL, | 81 /// Sends an HTTP DELETE request with the given headers to the given URL, |
| 59 /// which can be a [Uri] or a [String]. | 82 /// which can be a [Uri] or a [String]. |
| 60 /// | 83 /// |
| 61 /// For more fine-grained control over the request, use [send] instead. | 84 /// For more fine-grained control over the request, use [send] instead. |
| 62 Future<Response> delete(url, {Map<String, String> headers}) => | 85 Future<Response> delete(url, {Map<String, String> headers}) => |
| 63 _sendUnstreamed("DELETE", url, headers); | 86 _sendUnstreamed("DELETE", url, headers); |
| 64 | 87 |
| 65 /// Sends an HTTP GET request with the given headers to the given URL, which | 88 /// 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 | 89 /// can be a [Uri] or a [String], and returns a Future that completes to the |
| (...skipping 29 matching lines...) Expand all Loading... |
| 96 | 119 |
| 97 /// Sends an HTTP request and asynchronously returns the response. | 120 /// Sends an HTTP request and asynchronously returns the response. |
| 98 /// | 121 /// |
| 99 /// Implementers should call [BaseRequest.finalize] to get the body of the | 122 /// Implementers should call [BaseRequest.finalize] to get the body of the |
| 100 /// request as a [ByteStream]. They shouldn't make any assumptions about the | 123 /// 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 | 124 /// 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. | 125 /// later point, or it could already be closed when it's returned. |
| 103 Future<StreamedResponse> send(BaseRequest request); | 126 Future<StreamedResponse> send(BaseRequest request); |
| 104 | 127 |
| 105 /// Sends a non-streaming [Request] and returns a non-streaming [Response]. | 128 /// Sends a non-streaming [Request] and returns a non-streaming [Response]. |
| 106 Future<Response> _sendUnstreamed( | 129 Future<Response> _sendUnstreamed(String method, url, |
| 107 String method, url, Map<String, String> headers, | 130 Map<String, String> headers, [body, Encoding encoding]) { |
| 108 [Map<String, String> fields]) { | 131 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); | 132 if (url is String) url = Uri.parse(url); |
| 113 var request = new Request(method, url); | 133 var request = new Request(method, url); |
| 114 | 134 |
| 115 if (headers != null) request.headers.addAll(headers); | 135 if (headers != null) request.headers.addAll(headers); |
| 116 if (fields != null && !fields.isEmpty) request.bodyFields = fields; | 136 if (encoding != null) request.encoding = encoding; |
| 137 if (body != null) { |
| 138 if (body is String) { |
| 139 request.body = body; |
| 140 } else if (body is List) { |
| 141 request.bodyBytes = body; |
| 142 } else if (body is Map) { |
| 143 request.bodyFields = body; |
| 144 } else { |
| 145 throw new ArgumentError('Invalid request body "$body".'); |
| 146 } |
| 147 } |
| 117 | 148 |
| 118 return send(request); | 149 return send(request); |
| 119 }).then(Response.fromStream); | 150 }).then(Response.fromStream); |
| 120 } | 151 } |
| 121 | 152 |
| 122 /// Throws an error if [response] is not successful. | 153 /// Throws an error if [response] is not successful. |
| 123 void _checkResponseSuccess(url, Response response) { | 154 void _checkResponseSuccess(url, Response response) { |
| 124 if (response.statusCode < 400) return; | 155 if (response.statusCode < 400) return; |
| 125 var message = "Request to $url failed with status ${response.statusCode}"; | 156 var message = "Request to $url failed with status ${response.statusCode}"; |
| 126 if (response.reasonPhrase != null) { | 157 if (response.reasonPhrase != null) { |
| 127 message = "$message: ${response.reasonPhrase}"; | 158 message = "$message: ${response.reasonPhrase}"; |
| 128 } | 159 } |
| 129 throw new HttpException("$message."); | 160 throw new HttpException("$message."); |
| 130 } | 161 } |
| 131 | 162 |
| 132 /// Closes the client and cleans up any resources associated with it. It's | 163 /// 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 | 164 /// important to close each client when it's done being used; failing to do so |
| 134 /// can cause the Dart process to hang. | 165 /// can cause the Dart process to hang. |
| 135 void close() {} | 166 void close() {} |
| 136 } | 167 } |
| OLD | NEW |