| 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:io'; | 8 import 'dart:io'; |
| 9 import 'dart:typed_data'; | 9 import 'dart:typed_data'; |
| 10 | 10 |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 /// Sends a non-streaming [Request] and returns a non-streaming [Response]. | 105 /// Sends a non-streaming [Request] and returns a non-streaming [Response]. |
| 106 Future<Response> _sendUnstreamed( | 106 Future<Response> _sendUnstreamed( |
| 107 String method, url, Map<String, String> headers, | 107 String method, url, Map<String, String> headers, |
| 108 [Map<String, String> fields]) { | 108 [Map<String, String> fields]) { |
| 109 // Wrap everything in a Future block so that synchronous validation errors | 109 // Wrap everything in a Future block so that synchronous validation errors |
| 110 // are passed asynchronously through the Future chain. | 110 // are passed asynchronously through the Future chain. |
| 111 return async.then((_) { | 111 return async.then((_) { |
| 112 if (url is String) url = Uri.parse(url); | 112 if (url is String) url = Uri.parse(url); |
| 113 var request = new Request(method, url); | 113 var request = new Request(method, url); |
| 114 | 114 |
| 115 if (headers != null) mapAddAll(request.headers, headers); | 115 if (headers != null) request.headers.addAll(headers); |
| 116 if (fields != null && !fields.isEmpty) request.bodyFields = fields; | 116 if (fields != null && !fields.isEmpty) request.bodyFields = fields; |
| 117 | 117 |
| 118 return send(request); | 118 return send(request); |
| 119 }).then(Response.fromStream); | 119 }).then(Response.fromStream); |
| 120 } | 120 } |
| 121 | 121 |
| 122 /// Throws an error if [response] is not successful. | 122 /// Throws an error if [response] is not successful. |
| 123 void _checkResponseSuccess(url, Response response) { | 123 void _checkResponseSuccess(url, Response response) { |
| 124 if (response.statusCode < 400) return; | 124 if (response.statusCode < 400) return; |
| 125 var message = "Request to $url failed with status ${response.statusCode}"; | 125 var message = "Request to $url failed with status ${response.statusCode}"; |
| 126 if (response.reasonPhrase != null) { | 126 if (response.reasonPhrase != null) { |
| 127 message = "$message: ${response.reasonPhrase}"; | 127 message = "$message: ${response.reasonPhrase}"; |
| 128 } | 128 } |
| 129 throw new HttpException("$message."); | 129 throw new HttpException("$message."); |
| 130 } | 130 } |
| 131 | 131 |
| 132 /// Closes the client and cleans up any resources associated with it. It's | 132 /// 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 | 133 /// important to close each client when it's done being used; failing to do so |
| 134 /// can cause the Dart process to hang. | 134 /// can cause the Dart process to hang. |
| 135 void close() {} | 135 void close() {} |
| 136 } | 136 } |
| OLD | NEW |