OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 /// A composable, [Future]-based library for making HTTP requests. | 5 /// A composable, [Future]-based library for making HTTP requests. |
6 /// | 6 /// |
7 /// ## Installing ## | 7 /// ## Installing ## |
8 /// | 8 /// |
9 /// Use [pub][] to install this package. Add the following to your | 9 /// Use [pub][] to install this package. Add the following to your |
10 /// `pubspec.yaml` file. | 10 /// `pubspec.yaml` file. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 /// to add behavior should create a subclass of [BaseClient] that wraps another | 52 /// to add behavior should create a subclass of [BaseClient] that wraps another |
53 /// [Client] and adds the desired behavior: | 53 /// [Client] and adds the desired behavior: |
54 /// | 54 /// |
55 /// class UserAgentClient extends http.BaseClient { | 55 /// class UserAgentClient extends http.BaseClient { |
56 /// final String userAgent; | 56 /// final String userAgent; |
57 /// final http.Client _inner; | 57 /// final http.Client _inner; |
58 /// | 58 /// |
59 /// UserAgentClient(this.userAgent, this._inner); | 59 /// UserAgentClient(this.userAgent, this._inner); |
60 /// | 60 /// |
61 /// Future<StreamedResponse> send(BaseRequest request) { | 61 /// Future<StreamedResponse> send(BaseRequest request) { |
62 /// request.headers[HttpHeaders.USER_AGENT] = userAgent; | 62 /// request.headers['user-agent'] = userAgent; |
63 /// return _inner.send(request); | 63 /// return _inner.send(request); |
64 /// } | 64 /// } |
65 /// } | 65 /// } |
66 /// | 66 /// |
67 /// [pub]: http://pub.dartlang.org | 67 /// [pub]: http://pub.dartlang.org |
68 library http; | 68 library http; |
69 | 69 |
70 import 'dart:async'; | 70 import 'dart:async'; |
71 import 'dart:convert'; | 71 import 'dart:convert'; |
72 import 'dart:typed_data'; | 72 import 'dart:typed_data'; |
73 | 73 |
74 import 'src/client.dart'; | 74 import 'src/client.dart'; |
75 import 'src/response.dart'; | 75 import 'src/response.dart'; |
76 | 76 |
77 export 'src/base_client.dart'; | 77 export 'src/base_client.dart'; |
78 export 'src/base_request.dart'; | 78 export 'src/base_request.dart'; |
79 export 'src/base_response.dart'; | 79 export 'src/base_response.dart'; |
80 export 'src/byte_stream.dart'; | 80 export 'src/byte_stream.dart'; |
81 export 'src/client.dart'; | 81 export 'src/client.dart'; |
| 82 export 'src/exception.dart'; |
82 export 'src/multipart_file.dart'; | 83 export 'src/multipart_file.dart'; |
83 export 'src/multipart_request.dart'; | 84 export 'src/multipart_request.dart'; |
84 export 'src/request.dart'; | 85 export 'src/request.dart'; |
85 export 'src/response.dart'; | 86 export 'src/response.dart'; |
86 export 'src/streamed_request.dart'; | 87 export 'src/streamed_request.dart'; |
87 export 'src/streamed_response.dart'; | 88 export 'src/streamed_response.dart'; |
88 | 89 |
89 /// Sends an HTTP HEAD request with the given headers to the given URL, which | 90 /// Sends an HTTP HEAD request with the given headers to the given URL, which |
90 /// can be a [Uri] or a [String]. | 91 /// can be a [Uri] or a [String]. |
91 /// | 92 /// |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 /// the same server, you should use a single [Client] for all of those requests. | 165 /// the same server, you should use a single [Client] for all of those requests. |
165 /// | 166 /// |
166 /// For more fine-grained control over the request, use [Request] instead. | 167 /// For more fine-grained control over the request, use [Request] instead. |
167 Future<Response> delete(url, {Map<String, String> headers}) => | 168 Future<Response> delete(url, {Map<String, String> headers}) => |
168 _withClient((client) => client.delete(url, headers: headers)); | 169 _withClient((client) => client.delete(url, headers: headers)); |
169 | 170 |
170 /// Sends an HTTP GET request with the given headers to the given URL, which can | 171 /// Sends an HTTP GET request with the given headers to the given URL, which can |
171 /// be a [Uri] or a [String], and returns a Future that completes to the body of | 172 /// be a [Uri] or a [String], and returns a Future that completes to the body of |
172 /// the response as a [String]. | 173 /// the response as a [String]. |
173 /// | 174 /// |
174 /// The Future will emit an [HttpException] if the response doesn't have a | 175 /// The Future will emit a [ClientException] if the response doesn't have a |
175 /// success status code. | 176 /// success status code. |
176 /// | 177 /// |
177 /// This automatically initializes a new [Client] and closes that client once | 178 /// This automatically initializes a new [Client] and closes that client once |
178 /// the request is complete. If you're planning on making multiple requests to | 179 /// the request is complete. If you're planning on making multiple requests to |
179 /// the same server, you should use a single [Client] for all of those requests. | 180 /// the same server, you should use a single [Client] for all of those requests. |
180 /// | 181 /// |
181 /// For more fine-grained control over the request and response, use [Request] | 182 /// For more fine-grained control over the request and response, use [Request] |
182 /// instead. | 183 /// instead. |
183 Future<String> read(url, {Map<String, String> headers}) => | 184 Future<String> read(url, {Map<String, String> headers}) => |
184 _withClient((client) => client.read(url, headers: headers)); | 185 _withClient((client) => client.read(url, headers: headers)); |
185 | 186 |
186 /// Sends an HTTP GET request with the given headers to the given URL, which can | 187 /// Sends an HTTP GET request with the given headers to the given URL, which can |
187 /// be a [Uri] or a [String], and returns a Future that completes to the body of | 188 /// be a [Uri] or a [String], and returns a Future that completes to the body of |
188 /// the response as a list of bytes. | 189 /// the response as a list of bytes. |
189 /// | 190 /// |
190 /// The Future will emit an [HttpException] if the response doesn't have a | 191 /// The Future will emit a [ClientException] if the response doesn't have a |
191 /// success status code. | 192 /// success status code. |
192 /// | 193 /// |
193 /// This automatically initializes a new [Client] and closes that client once | 194 /// This automatically initializes a new [Client] and closes that client once |
194 /// the request is complete. If you're planning on making multiple requests to | 195 /// the request is complete. If you're planning on making multiple requests to |
195 /// the same server, you should use a single [Client] for all of those requests. | 196 /// the same server, you should use a single [Client] for all of those requests. |
196 /// | 197 /// |
197 /// For more fine-grained control over the request and response, use [Request] | 198 /// For more fine-grained control over the request and response, use [Request] |
198 /// instead. | 199 /// instead. |
199 Future<Uint8List> readBytes(url, {Map<String, String> headers}) => | 200 Future<Uint8List> readBytes(url, {Map<String, String> headers}) => |
200 _withClient((client) => client.readBytes(url, headers: headers)); | 201 _withClient((client) => client.readBytes(url, headers: headers)); |
201 | 202 |
202 Future _withClient(Future fn(Client)) { | 203 Future _withClient(Future fn(Client)) { |
203 var client = new Client(); | 204 var client = new Client(); |
204 var future = fn(client); | 205 var future = fn(client); |
205 return future.whenComplete(client.close); | 206 return future.whenComplete(client.close); |
206 } | 207 } |
OLD | NEW |