| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library base_client; |
| 6 |
| 7 import 'dart:io'; |
| 8 import 'dart:scalarlist'; |
| 9 import 'dart:uri'; |
| 10 |
| 11 import 'base_request.dart'; |
| 12 import 'request.dart'; |
| 13 import 'response.dart'; |
| 14 import 'streamed_response.dart'; |
| 15 import 'utils.dart'; |
| 16 |
| 17 /// The abstract base class for an HTTP client. This is a mixin-style class; |
| 18 /// subclasses only need to implement [send] and maybe [close], and then they |
| 19 /// get various convenience methods for free. |
| 20 abstract class BaseClient { |
| 21 /// Sends an HTTP HEAD request with the given headers to the given URL, which |
| 22 /// can be a [Uri] or a [String]. |
| 23 /// |
| 24 /// For more fine-grained control over the request, use [send] instead. |
| 25 Future<Response> head(url, {Map<String, String> headers}) => |
| 26 _sendUnstreamed("HEAD", url, headers); |
| 27 |
| 28 /// Sends an HTTP GET request with the given headers to the given URL, which |
| 29 /// can be a [Uri] or a [String]. |
| 30 /// |
| 31 /// For more fine-grained control over the request, use [send] instead. |
| 32 Future<Response> get(url, {Map<String, String> headers}) => |
| 33 _sendUnstreamed("GET", url, headers); |
| 34 |
| 35 /// Sends an HTTP POST request with the given headers and fields to the given |
| 36 /// URL, which can be a [Uri] or a [String]. If any fields are specified, the |
| 37 /// content-type is automatically set to |
| 38 /// `"application/x-www-form-urlencoded"`. |
| 39 /// |
| 40 /// For more fine-grained control over the request, use [send] instead. |
| 41 Future<Response> post(url, |
| 42 {Map<String, String> headers, |
| 43 Map<String, String> fields}) => |
| 44 _sendUnstreamed("POST", url, headers, fields); |
| 45 |
| 46 /// Sends an HTTP PUT request with the given headers and fields to the given |
| 47 /// URL, which can be a [Uri] or a [String]. If any fields are specified, the |
| 48 /// content-type is automatically set to |
| 49 /// `"application/x-www-form-urlencoded"`. |
| 50 /// |
| 51 /// For more fine-grained control over the request, use [send] instead. |
| 52 Future<Response> put(url, |
| 53 {Map<String, String> headers, |
| 54 Map<String, String> fields}) => |
| 55 _sendUnstreamed("PUT", url, headers, fields); |
| 56 |
| 57 /// Sends an HTTP DELETE request with the given headers to the given URL, |
| 58 /// which can be a [Uri] or a [String]. |
| 59 /// |
| 60 /// For more fine-grained control over the request, use [send] instead. |
| 61 Future<Response> delete(url, {Map<String, String> headers}) => |
| 62 _sendUnstreamed("DELETE", url, headers); |
| 63 |
| 64 /// Sends an HTTP GET request with the given headers to the given URL, which |
| 65 /// can be a [Uri] or a [String], and returns a Future that completes to the |
| 66 /// body of the response as a String. |
| 67 /// |
| 68 /// The Future will emit an [HttpException] if the response doesn't have a |
| 69 /// success status code. |
| 70 /// |
| 71 /// For more fine-grained control over the request and response, use [send] or |
| 72 /// [get] instead. |
| 73 Future<String> read(url, {Map<String, String> headers}) { |
| 74 return get(url, headers: headers).transform((response) { |
| 75 _checkResponseSuccess(url, response); |
| 76 return response.body; |
| 77 }); |
| 78 } |
| 79 |
| 80 /// Sends an HTTP GET request with the given headers to the given URL, which |
| 81 /// can be a [Uri] or a [String], and returns a Future that completes to the |
| 82 /// body of the response as a list of bytes. |
| 83 /// |
| 84 /// The Future will emit an [HttpException] if the response doesn't have a |
| 85 /// success status code. |
| 86 /// |
| 87 /// For more fine-grained control over the request and response, use [send] or |
| 88 /// [get] instead. |
| 89 Future<Uint8List> readBytes(url, {Map<String, String> headers}) { |
| 90 return get(url, headers: headers).transform((response) { |
| 91 _checkResponseSuccess(url, response); |
| 92 return response.bodyBytes; |
| 93 }); |
| 94 } |
| 95 |
| 96 /// Sends an HTTP request and asynchronously returns the response. |
| 97 /// |
| 98 /// Implementers should call [BaseRequest.finalize] to get the body of the |
| 99 /// request as an [InputStream]. They shouldn't make any assumptions about the |
| 100 /// state of the stream; it could have data written to it asynchronously at a |
| 101 /// later point, or it could already be closed when it's returned. |
| 102 Future<StreamedResponse> send(BaseRequest request); |
| 103 |
| 104 /// Sends a non-streaming [Request] and returns a non-streaming [Response]. |
| 105 Future<Response> _sendUnstreamed( |
| 106 String method, url, Map<String, String> headers, |
| 107 [Map<String, String> fields]) { |
| 108 // Wrap everything in a Future block so that synchronous validation errors |
| 109 // are passed asynchronously through the Future chain. |
| 110 return async.chain((_) { |
| 111 if (url is String) url = new Uri.fromString(url); |
| 112 var request = new Request(method, url); |
| 113 |
| 114 if (headers != null) mapAddAll(request.headers, headers); |
| 115 if (fields != null && !fields.isEmpty) request.bodyFields = fields; |
| 116 |
| 117 return send(request); |
| 118 }).chain(Response.fromStream); |
| 119 } |
| 120 |
| 121 /// Throws an error if [response] is not successful. |
| 122 void _checkResponseSuccess(url, Response response) { |
| 123 if (response.statusCode < 400) return; |
| 124 var message = "Request to $url failed with status ${response.statusCode}"; |
| 125 if (response.reasonPhrase != null) { |
| 126 message = "$message: ${response.reasonPhrase}"; |
| 127 } |
| 128 throw new HttpException("$message."); |
| 129 } |
| 130 |
| 131 /// Closes the client and cleans up any resources associated with it. It's |
| 132 /// important to close each client when it's done being used; failing to do so |
| 133 /// can cause the Dart process to hang. |
| 134 void close() {} |
| 135 } |
| OLD | NEW |