Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(604)

Side by Side Diff: pkg/http/lib/src/base_client.dart

Issue 216603010: Rip out dart:io from pkg/http wherever possible. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/http/lib/http.dart ('k') | pkg/http/lib/src/byte_stream.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 base_client; 5 library base_client;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:convert'; 8 import 'dart:convert';
9 import 'dart:io';
10 import 'dart:typed_data'; 9 import 'dart:typed_data';
11 10
12 import 'base_request.dart'; 11 import 'base_request.dart';
13 import 'client.dart'; 12 import 'client.dart';
13 import 'exception.dart';
14 import 'request.dart'; 14 import 'request.dart';
15 import 'response.dart'; 15 import 'response.dart';
16 import 'streamed_response.dart'; 16 import 'streamed_response.dart';
17 import 'utils.dart'; 17 import 'utils.dart';
18 18
19 /// The abstract base class for an HTTP client. This is a mixin-style class; 19 /// The abstract base class for an HTTP client. This is a mixin-style class;
20 /// subclasses only need to implement [send] and maybe [close], and then they 20 /// subclasses only need to implement [send] and maybe [close], and then they
21 /// get various convenience methods for free. 21 /// get various convenience methods for free.
22 abstract class BaseClient implements Client { 22 abstract class BaseClient implements Client {
23 /// Sends an HTTP HEAD request with the given headers to the given URL, which 23 /// Sends an HTTP HEAD request with the given headers to the given URL, which
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 /// which can be a [Uri] or a [String]. 82 /// which can be a [Uri] or a [String].
83 /// 83 ///
84 /// For more fine-grained control over the request, use [send] instead. 84 /// For more fine-grained control over the request, use [send] instead.
85 Future<Response> delete(url, {Map<String, String> headers}) => 85 Future<Response> delete(url, {Map<String, String> headers}) =>
86 _sendUnstreamed("DELETE", url, headers); 86 _sendUnstreamed("DELETE", url, headers);
87 87
88 /// Sends an HTTP GET request with the given headers to the given URL, which 88 /// Sends an HTTP GET request with the given headers to the given URL, which
89 /// can be a [Uri] or a [String], and returns a Future that completes to the 89 /// can be a [Uri] or a [String], and returns a Future that completes to the
90 /// body of the response as a String. 90 /// body of the response as a String.
91 /// 91 ///
92 /// The Future will emit an [HttpException] if the response doesn't have a 92 /// The Future will emit a [ClientException] if the response doesn't have a
93 /// success status code. 93 /// success status code.
94 /// 94 ///
95 /// For more fine-grained control over the request and response, use [send] or 95 /// For more fine-grained control over the request and response, use [send] or
96 /// [get] instead. 96 /// [get] instead.
97 Future<String> read(url, {Map<String, String> headers}) { 97 Future<String> read(url, {Map<String, String> headers}) {
98 return get(url, headers: headers).then((response) { 98 return get(url, headers: headers).then((response) {
99 _checkResponseSuccess(url, response); 99 _checkResponseSuccess(url, response);
100 return response.body; 100 return response.body;
101 }); 101 });
102 } 102 }
103 103
104 /// Sends an HTTP GET request with the given headers to the given URL, which 104 /// Sends an HTTP GET request with the given headers to the given URL, which
105 /// can be a [Uri] or a [String], and returns a Future that completes to the 105 /// can be a [Uri] or a [String], and returns a Future that completes to the
106 /// body of the response as a list of bytes. 106 /// body of the response as a list of bytes.
107 /// 107 ///
108 /// The Future will emit an [HttpException] if the response doesn't have a 108 /// The Future will emit an [ClientException] if the response doesn't have a
109 /// success status code. 109 /// success status code.
110 /// 110 ///
111 /// For more fine-grained control over the request and response, use [send] or 111 /// For more fine-grained control over the request and response, use [send] or
112 /// [get] instead. 112 /// [get] instead.
113 Future<Uint8List> readBytes(url, {Map<String, String> headers}) { 113 Future<Uint8List> readBytes(url, {Map<String, String> headers}) {
114 return get(url, headers: headers).then((response) { 114 return get(url, headers: headers).then((response) {
115 _checkResponseSuccess(url, response); 115 _checkResponseSuccess(url, response);
116 return response.bodyBytes; 116 return response.bodyBytes;
117 }); 117 });
118 } 118 }
119 119
120 /// Sends an HTTP request and asynchronously returns the response. 120 /// Sends an HTTP request and asynchronously returns the response.
121 /// 121 ///
122 /// Implementers should call [BaseRequest.finalize] to get the body of the 122 /// Implementers should call [BaseRequest.finalize] to get the body of the
123 /// request as a [ByteStream]. They shouldn't make any assumptions about the 123 /// request as a [ByteStream]. They shouldn't make any assumptions about the
124 /// state of the stream; it could have data written to it asynchronously at a 124 /// state of the stream; it could have data written to it asynchronously at a
125 /// later point, or it could already be closed when it's returned. 125 /// later point, or it could already be closed when it's returned. Any
126 /// internal HTTP errors should be wrapped as [ClientException]s.
126 Future<StreamedResponse> send(BaseRequest request); 127 Future<StreamedResponse> send(BaseRequest request);
127 128
128 /// Sends a non-streaming [Request] and returns a non-streaming [Response]. 129 /// Sends a non-streaming [Request] and returns a non-streaming [Response].
129 Future<Response> _sendUnstreamed(String method, url, 130 Future<Response> _sendUnstreamed(String method, url,
130 Map<String, String> headers, [body, Encoding encoding]) { 131 Map<String, String> headers, [body, Encoding encoding]) {
131 return syncFuture(() { 132 return syncFuture(() {
132 if (url is String) url = Uri.parse(url); 133 if (url is String) url = Uri.parse(url);
133 var request = new Request(method, url); 134 var request = new Request(method, url);
134 135
135 if (headers != null) request.headers.addAll(headers); 136 if (headers != null) request.headers.addAll(headers);
(...skipping 14 matching lines...) Expand all
150 }).then(Response.fromStream); 151 }).then(Response.fromStream);
151 } 152 }
152 153
153 /// Throws an error if [response] is not successful. 154 /// Throws an error if [response] is not successful.
154 void _checkResponseSuccess(url, Response response) { 155 void _checkResponseSuccess(url, Response response) {
155 if (response.statusCode < 400) return; 156 if (response.statusCode < 400) return;
156 var message = "Request to $url failed with status ${response.statusCode}"; 157 var message = "Request to $url failed with status ${response.statusCode}";
157 if (response.reasonPhrase != null) { 158 if (response.reasonPhrase != null) {
158 message = "$message: ${response.reasonPhrase}"; 159 message = "$message: ${response.reasonPhrase}";
159 } 160 }
160 throw new HttpException("$message."); 161 if (url is String) url = Uri.parse(url);
162 throw new ClientException("$message.", url);
161 } 163 }
162 164
163 /// Closes the client and cleans up any resources associated with it. It's 165 /// Closes the client and cleans up any resources associated with it. It's
164 /// important to close each client when it's done being used; failing to do so 166 /// important to close each client when it's done being used; failing to do so
165 /// can cause the Dart process to hang. 167 /// can cause the Dart process to hang.
166 void close() {} 168 void close() {}
167 } 169 }
OLDNEW
« no previous file with comments | « pkg/http/lib/http.dart ('k') | pkg/http/lib/src/byte_stream.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698