Chromium Code Reviews| 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 /// A composable, [Future]-based library for making HTTP requests. | |
| 6 /// | |
| 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: | |
| 9 /// | |
| 10 /// import 'package:http/http.dart' as http; | |
| 11 /// | |
| 12 /// var uri = new Uri.fromString("http://example.com/whatsit/create"); | |
|
Bob Nystrom
2012/10/31 01:17:44
This makes me wonder if the top-level functions sh
nweiz
2012/10/31 18:20:59
That makes the type signature pretty ugly, but I g
Bob Nystrom
2012/11/01 19:53:59
I don't think we can use typedefs for this, at lea
nweiz
2012/11/02 19:29:12
By "leave it typed" do you mean "leave it untyped"
Bob Nystrom
2012/11/02 19:35:03
Yup, sorry.
| |
| 13 /// http.post(uri, fields: {"name": "doodle", "color": "blue"}) | |
| 14 /// .then((response) { | |
| 15 /// print("Response status: ${response.statusCode}"); | |
| 16 /// print("Response body: ${response.body}"); | |
| 17 /// }); | |
| 18 /// | |
| 19 /// http.read(new Uri.fromString("http://example.com/foobar.txt")) | |
| 20 /// .then((body) { | |
|
Bob Nystrom
2012/10/31 01:17:44
For maximum terseness:
.then(print);
:)
nweiz
2012/10/31 18:20:59
Done.
| |
| 21 /// print("Response body: $body"); | |
| 22 /// }); | |
| 23 /// | |
| 24 /// If you're making multiple requests to the same server, you can keep open a | |
| 25 /// persistent connection by using a [Client] rather than making one-off | |
| 26 /// requests: | |
|
Bob Nystrom
2012/10/31 01:17:44
Add, "If you do this, make sure to close the clien
nweiz
2012/10/31 18:20:59
Done.
| |
| 27 /// | |
| 28 /// var client = new http.Client(); | |
| 29 /// var baseUri = new Uri.fromString("http://example.com/"); | |
| 30 /// client.post( | |
| 31 /// baseUri.relative("whatsit/create", | |
| 32 /// fields: {"name": "doodle", "color": "blue"}) | |
| 33 /// .chain((response) => client.get(response.bodyFields['uri'])); | |
| 34 /// | |
| 35 /// You can also exert more fine-grained control over your requests and | |
| 36 /// responses by creating [Request] or [StreamRequest] objects yourself and | |
| 37 /// passing them to [Client.send]. | |
| 38 /// | |
| 39 /// This package is designed to be composable. This makes it easy for external | |
| 40 /// libraries to work with one another to add behavior to it. Libraries wishing | |
| 41 /// to add behavior should create a subclass of [BaseClient] that wraps another | |
| 42 /// [BaseClient] and adds the desired behavior: | |
| 43 /// | |
| 44 /// class UserAgentClient extends http.BaseClient { | |
| 45 /// final String userAgent; | |
| 46 /// final HttpClient _inner; | |
| 47 /// | |
| 48 /// UserAgentClient(this.userAgent, this._inner); | |
| 49 /// | |
| 50 /// Future<StreamResponse> send(BaseRequest request) { | |
| 51 /// request.headers[HttpHeaders.USER_AGENT] = userAgent; | |
| 52 /// return _inner.send(request); | |
| 53 /// } | |
| 54 /// } | |
| 55 /// | |
| 56 /// In turn, libraries using [Client] should take a [BaseClient] so that the | |
| 57 /// decorated clients can be used transparently. | |
| 58 | |
| 59 library http; | |
| 60 | |
| 61 import 'dart:scalarlist'; | |
| 62 import 'dart:uri'; | |
| 63 | |
| 64 import 'src/client.dart'; | |
| 65 import 'src/response.dart'; | |
| 66 | |
| 67 export 'src/base_client.dart'; | |
| 68 export 'src/base_request.dart'; | |
| 69 export 'src/base_response.dart'; | |
| 70 export 'src/client.dart'; | |
| 71 export 'src/request.dart'; | |
| 72 export 'src/response.dart'; | |
| 73 export 'src/stream_request.dart'; | |
| 74 export 'src/stream_response.dart'; | |
|
Bob Nystrom
2012/10/31 01:17:44
This is really awesome.
| |
| 75 | |
| 76 /// Send an HTTP HEAD request with the given headers to the given URI. | |
| 77 /// | |
| 78 /// This automatically initializes a new [Client] and closes that client once | |
| 79 /// the request is complete. If you're planning on making multiple requests to | |
| 80 /// the same server, you should use a single [Client] for all of those requests. | |
| 81 /// | |
| 82 /// For more fine-grained control over the request, use [Request] instead. | |
| 83 Future<Response> head(Uri uri, {Map<String, String> headers: null}) => | |
| 84 _withClient((client) => client.head(uri, headers: headers)); | |
| 85 | |
| 86 /// Send an HTTP GET request with the given headers to the given URI. | |
| 87 /// | |
| 88 /// This automatically initializes a new [Client] and closes that client once | |
| 89 /// the request is complete. If you're planning on making multiple requests to | |
| 90 /// the same server, you should use a single [Client] for all of those requests. | |
| 91 /// | |
| 92 /// For more fine-grained control over the request, use [Request] instead. | |
| 93 Future<Response> get(Uri uri, {Map<String, String> headers: null}) => | |
| 94 _withClient((client) => client.get(uri, headers: headers)); | |
| 95 | |
| 96 /// Send an HTTP POST request with the given headers and fields to the given | |
| 97 /// URI. If any fields are specified, the content-type is automatically set to | |
| 98 /// `"application/x-www-form-urlencoded"`. | |
| 99 /// | |
| 100 /// This automatically initializes a new [Client] and closes that client once | |
| 101 /// the request is complete. If you're planning on making multiple requests to | |
| 102 /// the same server, you should use a single [Client] for all of those requests. | |
| 103 /// | |
| 104 /// For more fine-grained control over the request, use [Request] or | |
| 105 /// [StreamRequest] instead. | |
| 106 Future<Response> post(Uri uri, | |
| 107 {Map<String, String> headers: null, | |
| 108 Map<String, String> fields: null}) => | |
| 109 _withClient((client) => client.post(uri, headers: headers, fields: fields)); | |
| 110 | |
| 111 /// Send an HTTP POST request with the given headers and fields to the given | |
| 112 /// URI. If any fields are specified, the content-type is automatically set to | |
| 113 /// `"application/x-www-form-urlencoded"`. | |
| 114 /// | |
| 115 /// This automatically initializes a new [Client] and closes that client once | |
| 116 /// the request is complete. If you're planning on making multiple requests to | |
| 117 /// the same server, you should use a single [Client] for all of those requests. | |
| 118 /// | |
| 119 /// For more fine-grained control over the request, use [Request] or | |
| 120 /// [StreamRequest] instead. | |
| 121 Future<Response> put(Uri uri, | |
| 122 {Map<String, String> headers: null, | |
| 123 Map<String, String> fields: null}) => | |
| 124 _withClient((client) => client.put(uri, headers: headers, fields: fields)); | |
| 125 | |
| 126 /// Send an HTTP DELETE request with the given headers to the given URI. | |
| 127 /// | |
| 128 /// This automatically initializes a new [Client] and closes that client once | |
| 129 /// the request is complete. If you're planning on making multiple requests to | |
| 130 /// the same server, you should use a single [Client] for all of those requests. | |
| 131 /// | |
| 132 /// For more fine-grained control over the request, use [Request] instead. | |
| 133 Future<Response> delete(Uri uri, {Map<String, String> headers: null}) => | |
| 134 _withClient((client) => client.delete(uri, headers: headers)); | |
| 135 | |
| 136 /// Send an HTTP GET request with the given headers to the given URI, and return | |
| 137 /// a Future that completes to the body of the response as a String. | |
| 138 /// | |
| 139 /// The Future will emit an [HttpException] if the response doesn't have a | |
| 140 /// success status code. | |
| 141 /// | |
| 142 /// This automatically initializes a new [Client] and closes that client once | |
| 143 /// the request is complete. If you're planning on making multiple requests to | |
| 144 /// the same server, you should use a single [Client] for all of those requests. | |
| 145 /// | |
| 146 /// For more fine-grained control over the request and response, use [Request] | |
| 147 /// instead. | |
| 148 Future<String> read(Uri uri, {Map<String, String> headers: null}) => | |
| 149 _withClient((client) => client.read(uri, headers: headers)); | |
| 150 | |
| 151 /// Send an HTTP GET request with the given headers to the given URI, and | |
| 152 /// return a Future that completes to the body of the response as a list of | |
| 153 /// bytes. | |
| 154 /// | |
| 155 /// The Future will emit an [HttpException] if the response doesn't have a | |
| 156 /// success status code. | |
| 157 /// | |
| 158 /// This automatically initializes a new [Client] and closes that client once | |
| 159 /// the request is complete. If you're planning on making multiple requests to | |
| 160 /// the same server, you should use a single [Client] for all of those requests. | |
| 161 /// | |
| 162 /// For more fine-grained control over the request and response, use [Request] | |
| 163 /// instead. | |
| 164 Future<Uint8List> readBytes(Uri uri, {Map<String, String> headers: null}) => | |
| 165 _withClient((client) => client.readBytes(uri, headers: headers)); | |
| 166 | |
| 167 Future _withClient(Future fn(Client)) { | |
| 168 var client = new Client(); | |
| 169 var future = fn(client); | |
| 170 future.onComplete((_) => client.close()); | |
| 171 return future; | |
| 172 } | |
| OLD | NEW |