diff --git a/lib/http.dart b/lib/http.dart index 0976043195..8b5887f3b3 100644 --- a/lib/http.dart +++ b/lib/http.dart @@ -25,30 +25,27 @@ export 'src/response.dart'; export 'src/streamed_request.dart'; export 'src/streamed_response.dart'; -/// Sends an HTTP HEAD request with the given headers to the given URL, which -/// can be a [Uri] or a [String]. +/// Sends an HTTP HEAD request with the given headers to the given URL. /// /// This automatically initializes a new [Client] and closes that client once /// the request is complete. If you're planning on making multiple requests to /// the same server, you should use a single [Client] for all of those requests. /// /// For more fine-grained control over the request, use [Request] instead. -Future head(Object url, {Map? headers}) => +Future head(Uri url, {Map? headers}) => _withClient((client) => client.head(url, headers: headers)); -/// Sends an HTTP GET request with the given headers to the given URL, which can -/// be a [Uri] or a [String]. +/// Sends an HTTP GET request with the given headers to the given URL. /// /// This automatically initializes a new [Client] and closes that client once /// the request is complete. If you're planning on making multiple requests to /// the same server, you should use a single [Client] for all of those requests. /// /// For more fine-grained control over the request, use [Request] instead. -Future get(Object url, {Map? headers}) => +Future get(Uri url, {Map? headers}) => _withClient((client) => client.get(url, headers: headers)); -/// Sends an HTTP POST request with the given headers and body to the given URL, -/// which can be a [Uri] or a [String]. +/// Sends an HTTP POST request with the given headers and body to the given URL. /// /// [body] sets the body of the request. It can be a [String], a [List] or /// a [Map]. If it's a String, it's encoded using [encoding] and @@ -66,13 +63,12 @@ Future get(Object url, {Map? headers}) => /// /// For more fine-grained control over the request, use [Request] or /// [StreamedRequest] instead. -Future post(Object url, +Future post(Uri url, {Map? headers, Object? body, Encoding? encoding}) => _withClient((client) => client.post(url, headers: headers, body: body, encoding: encoding)); -/// Sends an HTTP PUT request with the given headers and body to the given URL, -/// which can be a [Uri] or a [String]. +/// Sends an HTTP PUT request with the given headers and body to the given URL. /// /// [body] sets the body of the request. It can be a [String], a [List] or /// a [Map]. If it's a String, it's encoded using [encoding] and @@ -90,13 +86,13 @@ Future post(Object url, /// /// For more fine-grained control over the request, use [Request] or /// [StreamedRequest] instead. -Future put(Object url, +Future put(Uri url, {Map? headers, Object? body, Encoding? encoding}) => _withClient((client) => client.put(url, headers: headers, body: body, encoding: encoding)); /// Sends an HTTP PATCH request with the given headers and body to the given -/// URL, which can be a [Uri] or a [String]. +/// URL. /// /// [body] sets the body of the request. It can be a [String], a [List] or /// a [Map]. If it's a String, it's encoded using [encoding] and @@ -114,25 +110,23 @@ Future put(Object url, /// /// For more fine-grained control over the request, use [Request] or /// [StreamedRequest] instead. -Future patch(Object url, +Future patch(Uri url, {Map? headers, Object? body, Encoding? encoding}) => _withClient((client) => client.patch(url, headers: headers, body: body, encoding: encoding)); -/// Sends an HTTP DELETE request with the given headers to the given URL, which -/// can be a [Uri] or a [String]. +/// Sends an HTTP DELETE request with the given headers to the given URL. /// /// This automatically initializes a new [Client] and closes that client once /// the request is complete. If you're planning on making multiple requests to /// the same server, you should use a single [Client] for all of those requests. /// /// For more fine-grained control over the request, use [Request] instead. -Future delete(Object url, {Map? headers}) => +Future delete(Uri url, {Map? headers}) => _withClient((client) => client.delete(url, headers: headers)); -/// Sends an HTTP GET request with the given headers to the given URL, which can -/// be a [Uri] or a [String], and returns a Future that completes to the body of -/// the response as a [String]. +/// Sends an HTTP GET request with the given headers to the given URL and +/// returns a Future that completes to the body of the response as a [String]. /// /// The Future will emit a [ClientException] if the response doesn't have a /// success status code. @@ -143,12 +137,12 @@ Future delete(Object url, {Map? headers}) => /// /// For more fine-grained control over the request and response, use [Request] /// instead. -Future read(Object url, {Map? headers}) => +Future read(Uri url, {Map? headers}) => _withClient((client) => client.read(url, headers: headers)); -/// Sends an HTTP GET request with the given headers to the given URL, which can -/// be a [Uri] or a [String], and returns a Future that completes to the body of -/// the response as a list of bytes. +/// Sends an HTTP GET request with the given headers to the given URL and +/// returns a Future that completes to the body of the response as a list of +/// bytes. /// /// The Future will emit a [ClientException] if the response doesn't have a /// success status code. @@ -159,7 +153,7 @@ Future read(Object url, {Map? headers}) => /// /// For more fine-grained control over the request and response, use [Request] /// instead. -Future readBytes(Object url, {Map? headers}) => +Future readBytes(Uri url, {Map? headers}) => _withClient((client) => client.readBytes(url, headers: headers)); Future _withClient(Future Function(Client) fn) async { diff --git a/lib/src/base_client.dart b/lib/src/base_client.dart index db70215304..c541114f4f 100644 --- a/lib/src/base_client.dart +++ b/lib/src/base_client.dart @@ -19,42 +19,41 @@ import 'streamed_response.dart'; /// maybe [close], and then they get various convenience methods for free. abstract class BaseClient implements Client { @override - Future head(Object url, {Map? headers}) => + Future head(Uri url, {Map? headers}) => _sendUnstreamed('HEAD', url, headers); @override - Future get(Object url, {Map? headers}) => + Future get(Uri url, {Map? headers}) => _sendUnstreamed('GET', url, headers); @override - Future post(Object url, + Future post(Uri url, {Map? headers, Object? body, Encoding? encoding}) => _sendUnstreamed('POST', url, headers, body, encoding); @override - Future put(Object url, + Future put(Uri url, {Map? headers, Object? body, Encoding? encoding}) => _sendUnstreamed('PUT', url, headers, body, encoding); @override - Future patch(Object url, + Future patch(Uri url, {Map? headers, Object? body, Encoding? encoding}) => _sendUnstreamed('PATCH', url, headers, body, encoding); @override - Future delete(Object url, {Map? headers}) => + Future delete(Uri url, {Map? headers}) => _sendUnstreamed('DELETE', url, headers); @override - Future read(Object url, {Map? headers}) async { + Future read(Uri url, {Map? headers}) async { final response = await get(url, headers: headers); _checkResponseSuccess(url, response); return response.body; } @override - Future readBytes(Object url, - {Map? headers}) async { + Future readBytes(Uri url, {Map? headers}) async { final response = await get(url, headers: headers); _checkResponseSuccess(url, response); return response.bodyBytes; @@ -72,9 +71,9 @@ abstract class BaseClient implements Client { /// Sends a non-streaming [Request] and returns a non-streaming [Response]. Future _sendUnstreamed( - String method, url, Map? headers, + String method, Uri url, Map? headers, [body, Encoding? encoding]) async { - var request = Request(method, _fromUriOrString(url)); + var request = Request(method, url); if (headers != null) request.headers.addAll(headers); if (encoding != null) request.encoding = encoding; @@ -94,17 +93,15 @@ abstract class BaseClient implements Client { } /// Throws an error if [response] is not successful. - void _checkResponseSuccess(url, Response response) { + void _checkResponseSuccess(Uri url, Response response) { if (response.statusCode < 400) return; var message = 'Request to $url failed with status ${response.statusCode}'; if (response.reasonPhrase != null) { message = '$message: ${response.reasonPhrase}'; } - throw ClientException('$message.', _fromUriOrString(url)); + throw ClientException('$message.', url); } @override void close() {} } - -Uri _fromUriOrString(uri) => uri is String ? Uri.parse(uri) : uri as Uri; diff --git a/lib/src/client.dart b/lib/src/client.dart index d289699f4c..9958d4c92c 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -31,20 +31,18 @@ abstract class Client { /// `dart:html` is available, otherwise it will throw an unsupported error. factory Client() => createClient(); - /// Sends an HTTP HEAD request with the given headers to the given URL, which - /// can be a [Uri] or a [String]. + /// Sends an HTTP HEAD request with the given headers to the given URL. /// /// For more fine-grained control over the request, use [send] instead. - Future head(Object url, {Map? headers}); + Future head(Uri url, {Map? headers}); - /// Sends an HTTP GET request with the given headers to the given URL, which - /// can be a [Uri] or a [String]. + /// Sends an HTTP GET request with the given headers to the given URL. /// /// For more fine-grained control over the request, use [send] instead. - Future get(Object url, {Map? headers}); + Future get(Uri url, {Map? headers}); /// Sends an HTTP POST request with the given headers and body to the given - /// URL, which can be a [Uri] or a [String]. + /// URL. /// /// [body] sets the body of the request. It can be a [String], a [List] /// or a [Map]. If it's a String, it's encoded using @@ -61,11 +59,11 @@ abstract class Client { /// [encoding] defaults to [utf8]. /// /// For more fine-grained control over the request, use [send] instead. - Future post(Object url, + Future post(Uri url, {Map? headers, Object? body, Encoding? encoding}); /// Sends an HTTP PUT request with the given headers and body to the given - /// URL, which can be a [Uri] or a [String]. + /// URL. /// /// [body] sets the body of the request. It can be a [String], a [List] /// or a [Map]. If it's a String, it's encoded using @@ -82,11 +80,11 @@ abstract class Client { /// [encoding] defaults to [utf8]. /// /// For more fine-grained control over the request, use [send] instead. - Future put(Object url, + Future put(Uri url, {Map? headers, Object? body, Encoding? encoding}); /// Sends an HTTP PATCH request with the given headers and body to the given - /// URL, which can be a [Uri] or a [String]. + /// URL. /// /// [body] sets the body of the request. It can be a [String], a [List] /// or a [Map]. If it's a String, it's encoded using @@ -103,36 +101,34 @@ abstract class Client { /// [encoding] defaults to [utf8]. /// /// For more fine-grained control over the request, use [send] instead. - Future patch(Object url, + Future patch(Uri url, {Map? headers, Object? body, Encoding? encoding}); - /// Sends an HTTP DELETE request with the given headers to the given URL, - /// which can be a [Uri] or a [String]. + /// Sends an HTTP DELETE request with the given headers to the given URL. /// /// For more fine-grained control over the request, use [send] instead. - Future delete(Object url, {Map? headers}); + Future delete(Uri url, {Map? headers}); - /// Sends an HTTP GET request with the given headers to the given URL, which - /// can be a [Uri] or a [String], and returns a Future that completes to the - /// body of the response as a String. + /// Sends an HTTP GET request with the given headers to the given URL and + /// returns a Future that completes to the body of the response as a String. /// /// The Future will emit a [ClientException] if the response doesn't have a /// success status code. /// /// For more fine-grained control over the request and response, use [send] or /// [get] instead. - Future read(Object url, {Map? headers}); + Future read(Uri url, {Map? headers}); - /// Sends an HTTP GET request with the given headers to the given URL, which - /// can be a [Uri] or a [String], and returns a Future that completes to the - /// body of the response as a list of bytes. + /// Sends an HTTP GET request with the given headers to the given URL and + /// returns a Future that completes to the body of the response as a list of + /// bytes. /// /// The Future will emit a [ClientException] if the response doesn't have a /// success status code. /// /// For more fine-grained control over the request and response, use [send] or /// [get] instead. - Future readBytes(Object url, {Map? headers}); + Future readBytes(Uri url, {Map? headers}); /// Sends an HTTP request and asynchronously returns the response. Future send(BaseRequest request); diff --git a/test/mock_client_test.dart b/test/mock_client_test.dart index 3a18e34b55..db561c51d6 100644 --- a/test/mock_client_test.dart +++ b/test/mock_client_test.dart @@ -16,7 +16,7 @@ void main() { json.encode(request.bodyFields), 200, request: request, headers: {'content-type': 'application/json'})); - var response = await client.post('http://example.com/foo', + var response = await client.post(Uri.http('example.com', '/foo'), body: {'field1': 'value1', 'field2': 'value2'}); expect( response.body, parse(equals({'field1': 'value1', 'field2': 'value2'}))); @@ -30,7 +30,7 @@ void main() { return http.StreamedResponse(stream, 200); }); - var uri = Uri.parse('http://example.com/foo'); + var uri = Uri.http('example.com', '/foo'); var request = http.Request('POST', uri)..body = 'hello, world'; var streamedResponse = await client.send(request); var response = await http.Response.fromStream(streamedResponse); @@ -40,6 +40,7 @@ void main() { test('handles a request with no body', () async { var client = MockClient((_) async => http.Response('you did it', 200)); - expect(await client.read('http://example.com/foo'), equals('you did it')); + expect(await client.read(Uri.http('example.com', '/foo')), + equals('you did it')); }); }