| 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:io'; | 8 import 'dart:io'; |
| 8 import 'dart:scalarlist'; | 9 import 'dart:scalarlist'; |
| 9 import 'dart:uri'; | 10 import 'dart:uri'; |
| 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'; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 /// Sends an HTTP GET request with the given headers to the given URL, which | 66 /// 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 | 67 /// can be a [Uri] or a [String], and returns a Future that completes to the |
| 67 /// body of the response as a String. | 68 /// body of the response as a String. |
| 68 /// | 69 /// |
| 69 /// The Future will emit an [HttpException] if the response doesn't have a | 70 /// The Future will emit an [HttpException] if the response doesn't have a |
| 70 /// success status code. | 71 /// success status code. |
| 71 /// | 72 /// |
| 72 /// For more fine-grained control over the request and response, use [send] or | 73 /// For more fine-grained control over the request and response, use [send] or |
| 73 /// [get] instead. | 74 /// [get] instead. |
| 74 Future<String> read(url, {Map<String, String> headers}) { | 75 Future<String> read(url, {Map<String, String> headers}) { |
| 75 return get(url, headers: headers).transform((response) { | 76 return get(url, headers: headers).then((response) { |
| 76 _checkResponseSuccess(url, response); | 77 _checkResponseSuccess(url, response); |
| 77 return response.body; | 78 return response.body; |
| 78 }); | 79 }); |
| 79 } | 80 } |
| 80 | 81 |
| 81 /// Sends an HTTP GET request with the given headers to the given URL, which | 82 /// Sends an HTTP GET request with the given headers to the given URL, which |
| 82 /// can be a [Uri] or a [String], and returns a Future that completes to the | 83 /// can be a [Uri] or a [String], and returns a Future that completes to the |
| 83 /// body of the response as a list of bytes. | 84 /// body of the response as a list of bytes. |
| 84 /// | 85 /// |
| 85 /// The Future will emit an [HttpException] if the response doesn't have a | 86 /// The Future will emit an [HttpException] if the response doesn't have a |
| 86 /// success status code. | 87 /// success status code. |
| 87 /// | 88 /// |
| 88 /// For more fine-grained control over the request and response, use [send] or | 89 /// For more fine-grained control over the request and response, use [send] or |
| 89 /// [get] instead. | 90 /// [get] instead. |
| 90 Future<Uint8List> readBytes(url, {Map<String, String> headers}) { | 91 Future<Uint8List> readBytes(url, {Map<String, String> headers}) { |
| 91 return get(url, headers: headers).transform((response) { | 92 return get(url, headers: headers).then((response) { |
| 92 _checkResponseSuccess(url, response); | 93 _checkResponseSuccess(url, response); |
| 93 return response.bodyBytes; | 94 return response.bodyBytes; |
| 94 }); | 95 }); |
| 95 } | 96 } |
| 96 | 97 |
| 97 /// Sends an HTTP request and asynchronously returns the response. | 98 /// Sends an HTTP request and asynchronously returns the response. |
| 98 /// | 99 /// |
| 99 /// Implementers should call [BaseRequest.finalize] to get the body of the | 100 /// Implementers should call [BaseRequest.finalize] to get the body of the |
| 100 /// request as an [InputStream]. They shouldn't make any assumptions about the | 101 /// request as an [InputStream]. They shouldn't make any assumptions about the |
| 101 /// state of the stream; it could have data written to it asynchronously at a | 102 /// 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. | 103 /// later point, or it could already be closed when it's returned. |
| 103 Future<StreamedResponse> send(BaseRequest request); | 104 Future<StreamedResponse> send(BaseRequest request); |
| 104 | 105 |
| 105 /// Sends a non-streaming [Request] and returns a non-streaming [Response]. | 106 /// Sends a non-streaming [Request] and returns a non-streaming [Response]. |
| 106 Future<Response> _sendUnstreamed( | 107 Future<Response> _sendUnstreamed( |
| 107 String method, url, Map<String, String> headers, | 108 String method, url, Map<String, String> headers, |
| 108 [Map<String, String> fields]) { | 109 [Map<String, String> fields]) { |
| 109 // Wrap everything in a Future block so that synchronous validation errors | 110 // Wrap everything in a Future block so that synchronous validation errors |
| 110 // are passed asynchronously through the Future chain. | 111 // are passed asynchronously through the Future chain. |
| 111 return async.chain((_) { | 112 return async.then((_) { |
| 112 if (url is String) url = new Uri.fromString(url); | 113 if (url is String) url = new Uri.fromString(url); |
| 113 var request = new Request(method, url); | 114 var request = new Request(method, url); |
| 114 | 115 |
| 115 if (headers != null) mapAddAll(request.headers, headers); | 116 if (headers != null) mapAddAll(request.headers, headers); |
| 116 if (fields != null && !fields.isEmpty) request.bodyFields = fields; | 117 if (fields != null && !fields.isEmpty) request.bodyFields = fields; |
| 117 | 118 |
| 118 return send(request); | 119 return send(request); |
| 119 }).chain(Response.fromStream); | 120 }).then(Response.fromStream); |
| 120 } | 121 } |
| 121 | 122 |
| 122 /// Throws an error if [response] is not successful. | 123 /// Throws an error if [response] is not successful. |
| 123 void _checkResponseSuccess(url, Response response) { | 124 void _checkResponseSuccess(url, Response response) { |
| 124 if (response.statusCode < 400) return; | 125 if (response.statusCode < 400) return; |
| 125 var message = "Request to $url failed with status ${response.statusCode}"; | 126 var message = "Request to $url failed with status ${response.statusCode}"; |
| 126 if (response.reasonPhrase != null) { | 127 if (response.reasonPhrase != null) { |
| 127 message = "$message: ${response.reasonPhrase}"; | 128 message = "$message: ${response.reasonPhrase}"; |
| 128 } | 129 } |
| 129 throw new HttpException("$message."); | 130 throw new HttpException("$message."); |
| 130 } | 131 } |
| 131 | 132 |
| 132 /// Closes the client and cleans up any resources associated with it. It's | 133 /// 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 | 134 /// important to close each client when it's done being used; failing to do so |
| 134 /// can cause the Dart process to hang. | 135 /// can cause the Dart process to hang. |
| 135 void close() {} | 136 void close() {} |
| 136 } | 137 } |
| OLD | NEW |