| 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 import 'dart:async'; | 6 import 'dart:async'; |
| 7 import 'dart:convert'; | 7 import 'dart:convert'; |
| 8 import 'dart:typed_data'; | 8 import 'dart:typed_data'; |
| 9 | 9 |
| 10 import 'src/client.dart'; | 10 import 'src/client.dart'; |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 /// | 154 /// |
| 155 /// This automatically initializes a new [Client] and closes that client once | 155 /// This automatically initializes a new [Client] and closes that client once |
| 156 /// the request is complete. If you're planning on making multiple requests to | 156 /// the request is complete. If you're planning on making multiple requests to |
| 157 /// the same server, you should use a single [Client] for all of those requests. | 157 /// the same server, you should use a single [Client] for all of those requests. |
| 158 /// | 158 /// |
| 159 /// For more fine-grained control over the request and response, use [Request] | 159 /// For more fine-grained control over the request and response, use [Request] |
| 160 /// instead. | 160 /// instead. |
| 161 Future<Uint8List> readBytes(url, {Map<String, String> headers}) => | 161 Future<Uint8List> readBytes(url, {Map<String, String> headers}) => |
| 162 _withClient((client) => client.readBytes(url, headers: headers)); | 162 _withClient((client) => client.readBytes(url, headers: headers)); |
| 163 | 163 |
| 164 Future _withClient(Future fn(Client)) { | 164 Future/*<T>*/ _withClient/*<T>*/(Future/*<T>*/ fn(Client client)) async { |
| 165 var client = new Client(); | 165 var client = new Client(); |
| 166 var future = fn(client); | 166 try { |
| 167 return future.whenComplete(client.close); | 167 return await fn(client); |
| 168 } finally { |
| 169 client.close(); |
| 170 } |
| 168 } | 171 } |
| OLD | NEW |