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 library client; | 5 library io_client; |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 | 8 |
9 import 'base_client.dart'; | 9 import 'base_client.dart'; |
10 import 'base_request.dart'; | 10 import 'base_request.dart'; |
11 import 'streamed_response.dart'; | 11 import 'streamed_response.dart'; |
12 import 'utils.dart'; | 12 import 'utils.dart'; |
13 | 13 |
14 /// An HTTP client which takes care of maintaining persistent connections across | 14 /// A `dart:io`-based HTTP client. This is the default client. |
15 /// multiple requests to the same server. If you only need to send a single | 15 class IOClient extends BaseClient { |
16 /// request, it's usually easier to use [head], [get], [post], | |
17 /// [put], or [delete] instead. | |
18 /// | |
19 /// When creating an HTTP client class with additional functionality, it's | |
20 /// recommended that you subclass [BaseClient] and wrap another instance of | |
21 /// [BaseClient] rather than subclassing [Client] directly. This allows all | |
22 /// subclasses of [BaseClient] to be mutually composable. | |
23 class Client extends BaseClient { | |
24 /// The underlying `dart:io` HTTP client. | 16 /// The underlying `dart:io` HTTP client. |
25 HttpClient _inner; | 17 HttpClient _inner; |
26 | 18 |
27 /// Creates a new HTTP client. | 19 /// Creates a new HTTP client. |
28 Client() : _inner = new HttpClient(); | 20 IOClient() : _inner = new HttpClient(); |
29 | 21 |
30 /// Sends an HTTP request and asynchronously returns the response. | 22 /// Sends an HTTP request and asynchronously returns the response. |
31 Future<StreamedResponse> send(BaseRequest request) { | 23 Future<StreamedResponse> send(BaseRequest request) { |
32 var stream = request.finalize(); | 24 var stream = request.finalize(); |
33 | 25 |
34 var completer = new Completer<StreamedResponse>(); | 26 var completer = new Completer<StreamedResponse>(); |
35 var connection = _inner.openUrl(request.method, request.url); | 27 var connection = _inner.openUrl(request.method, request.url); |
36 connection.onError = (e) { | 28 connection.onError = (e) { |
37 async.then((_) { | 29 async.then((_) { |
38 // TODO(nweiz): remove this when issue 4974 is fixed | 30 // TODO(nweiz): remove this when issue 4974 is fixed |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 return completer.future; | 65 return completer.future; |
74 } | 66 } |
75 | 67 |
76 /// Closes the client. This terminates all active connections. If a client | 68 /// Closes the client. This terminates all active connections. If a client |
77 /// remains unclosed, the Dart process may not terminate. | 69 /// remains unclosed, the Dart process may not terminate. |
78 void close() { | 70 void close() { |
79 if (_inner != null) _inner.shutdown(); | 71 if (_inner != null) _inner.shutdown(); |
80 _inner = null; | 72 _inner = null; |
81 } | 73 } |
82 } | 74 } |
OLD | NEW |