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 /// A composable, [Future]-based library for making HTTP requests. | 5 /// A composable, [Future]-based library for making HTTP requests. |
6 /// | 6 /// |
7 /// The easiest way to use this library is via the top-level functions. They | 7 /// The easiest way to use this library is via the top-level functions. They |
8 /// allow you to make individual HTTP requests with minimal hassle: | 8 /// allow you to make individual HTTP requests with minimal hassle: |
9 /// | 9 /// |
10 /// import 'package:http/http.dart' as http; | 10 /// import 'package:http/http.dart' as http; |
11 /// | 11 /// |
12 /// var url = "http://example.com/whatsit/create"; | 12 /// var url = "http://example.com/whatsit/create"; |
13 /// http.post(url, fields: {"name": "doodle", "color": "blue"}) | 13 /// http.post(url, fields: {"name": "doodle", "color": "blue"}) |
14 /// .then((response) { | 14 /// .then((response) { |
15 /// print("Response status: ${response.statusCode}"); | 15 /// print("Response status: ${response.statusCode}"); |
16 /// print("Response body: ${response.body}"); | 16 /// print("Response body: ${response.body}"); |
17 /// }); | 17 /// }); |
18 /// | 18 /// |
19 /// http.read("http://example.com/foobar.txt").then(print); | 19 /// http.read("http://example.com/foobar.txt").then(print); |
20 /// | 20 /// |
21 /// If you're making multiple requests to the same server, you can keep open a | 21 /// If you're making multiple requests to the same server, you can keep open a |
22 /// persistent connection by using a [Client] rather than making one-off | 22 /// persistent connection by using a [Client] rather than making one-off |
23 /// requests. If you do this, make sure to close the client when you're done: | 23 /// requests. If you do this, make sure to close the client when you're done: |
24 /// | 24 /// |
25 /// var client = new http.Client(); | 25 /// var client = new http.Client(); |
26 /// client.post( | 26 /// client.post( |
27 /// "http://example.com/whatsit/create", | 27 /// "http://example.com/whatsit/create", |
28 /// fields: {"name": "doodle", "color": "blue"}) | 28 /// fields: {"name": "doodle", "color": "blue"}) |
29 /// .chain((response) => client.get(response.bodyFields['uri'])) | 29 /// .chain((response) => client.get(response.bodyFields['uri'])) |
30 /// .transform((response) => print(response.body)) | 30 /// .then((response) => print(response.body)) |
31 /// .onComplete((_) => client.close()); | 31 /// .onComplete((_) => client.close()); |
32 /// | 32 /// |
33 /// You can also exert more fine-grained control over your requests and | 33 /// You can also exert more fine-grained control over your requests and |
34 /// responses by creating [Request] or [StreamedRequest] objects yourself and | 34 /// responses by creating [Request] or [StreamedRequest] objects yourself and |
35 /// passing them to [Client.send]. | 35 /// passing them to [Client.send]. |
36 /// | 36 /// |
37 /// This package is designed to be composable. This makes it easy for external | 37 /// This package is designed to be composable. This makes it easy for external |
38 /// libraries to work with one another to add behavior to it. Libraries wishing | 38 /// libraries to work with one another to add behavior to it. Libraries wishing |
39 /// to add behavior should create a subclass of [BaseClient] that wraps another | 39 /// to add behavior should create a subclass of [BaseClient] that wraps another |
40 /// [Client] and adds the desired behavior: | 40 /// [Client] and adds the desired behavior: |
41 /// | 41 /// |
42 /// class UserAgentClient extends http.BaseClient { | 42 /// class UserAgentClient extends http.BaseClient { |
43 /// final String userAgent; | 43 /// final String userAgent; |
44 /// final http.Client _inner; | 44 /// final http.Client _inner; |
45 /// | 45 /// |
46 /// UserAgentClient(this.userAgent, this._inner); | 46 /// UserAgentClient(this.userAgent, this._inner); |
47 /// | 47 /// |
48 /// Future<StreamedResponse> send(BaseRequest request) { | 48 /// Future<StreamedResponse> send(BaseRequest request) { |
49 /// request.headers[HttpHeaders.USER_AGENT] = userAgent; | 49 /// request.headers[HttpHeaders.USER_AGENT] = userAgent; |
50 /// return _inner.send(request); | 50 /// return _inner.send(request); |
51 /// } | 51 /// } |
52 /// } | 52 /// } |
53 | 53 |
54 library http; | 54 library http; |
55 | 55 |
| 56 import 'dart:async'; |
56 import 'dart:scalarlist'; | 57 import 'dart:scalarlist'; |
57 import 'dart:uri'; | 58 import 'dart:uri'; |
58 | 59 |
59 import 'src/client.dart'; | 60 import 'src/client.dart'; |
60 import 'src/response.dart'; | 61 import 'src/response.dart'; |
61 | 62 |
62 export 'src/base_client.dart'; | 63 export 'src/base_client.dart'; |
63 export 'src/base_request.dart'; | 64 export 'src/base_request.dart'; |
64 export 'src/base_response.dart'; | 65 export 'src/base_response.dart'; |
65 export 'src/client.dart'; | 66 export 'src/client.dart'; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 /// the same server, you should use a single [Client] for all of those requests. | 162 /// the same server, you should use a single [Client] for all of those requests. |
162 /// | 163 /// |
163 /// For more fine-grained control over the request and response, use [Request] | 164 /// For more fine-grained control over the request and response, use [Request] |
164 /// instead. | 165 /// instead. |
165 Future<Uint8List> readBytes(url, {Map<String, String> headers}) => | 166 Future<Uint8List> readBytes(url, {Map<String, String> headers}) => |
166 _withClient((client) => client.readBytes(url, headers: headers)); | 167 _withClient((client) => client.readBytes(url, headers: headers)); |
167 | 168 |
168 Future _withClient(Future fn(Client)) { | 169 Future _withClient(Future fn(Client)) { |
169 var client = new Client(); | 170 var client = new Client(); |
170 var future = fn(client); | 171 var future = fn(client); |
171 future.onComplete((_) => client.close()); | 172 future.catchError((_) {}).then((_) => client.close()); |
172 return future; | 173 return future; |
173 } | 174 } |
OLD | NEW |