| 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; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 /// .transform((response) => print(response.body)) | 30 /// .transform((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 /// [BaseClient] 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 HttpClient _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 /// | |
| 54 /// In turn, libraries using [Client] should take a [BaseClient] so that the | |
| 55 /// decorated clients can be used transparently. | |
| 56 | 53 |
| 57 library http; | 54 library http; |
| 58 | 55 |
| 59 import 'dart:scalarlist'; | 56 import 'dart:scalarlist'; |
| 60 import 'dart:uri'; | 57 import 'dart:uri'; |
| 61 | 58 |
| 62 import 'src/client.dart'; | 59 import 'src/client.dart'; |
| 63 import 'src/response.dart'; | 60 import 'src/response.dart'; |
| 64 | 61 |
| 65 export 'src/base_client.dart'; | 62 export 'src/base_client.dart'; |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 /// instead. | 164 /// instead. |
| 168 Future<Uint8List> readBytes(url, {Map<String, String> headers}) => | 165 Future<Uint8List> readBytes(url, {Map<String, String> headers}) => |
| 169 _withClient((client) => client.readBytes(url, headers: headers)); | 166 _withClient((client) => client.readBytes(url, headers: headers)); |
| 170 | 167 |
| 171 Future _withClient(Future fn(Client)) { | 168 Future _withClient(Future fn(Client)) { |
| 172 var client = new Client(); | 169 var client = new Client(); |
| 173 var future = fn(client); | 170 var future = fn(client); |
| 174 future.onComplete((_) => client.close()); | 171 future.onComplete((_) => client.close()); |
| 175 return future; | 172 return future; |
| 176 } | 173 } |
| OLD | NEW |