Skip to content

Commit

Permalink
s/BaseResposeV2/BaseResponseWithUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
brianquinlan committed Jan 10, 2024
1 parent 4ddd4b0 commit b8bed26
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pkgs/http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## 1.2.0

* Add `MockClient.pngResponse`, which makes it easier to fake image responses.
* Added the ability to fetch the URL of the response through `BaseResponseV2`.
* Added the ability to fetch the URL of the response through `BaseResponseWithUrl`.

## 1.1.2

Expand Down
4 changes: 2 additions & 2 deletions pkgs/http/lib/src/base_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ abstract class BaseRequest {
var response = await client.send(this);
var stream = onDone(response.stream, client.close);

if (response is BaseResponseV2) {
if (response is BaseResponseWithUrl) {
return StreamedResponseV2(ByteStream(stream), response.statusCode,
contentLength: response.contentLength,
request: response.request,
headers: response.headers,
isRedirect: response.isRedirect,
url: (response as BaseResponseV2).url,
url: (response as BaseResponseWithUrl).url,
persistentConnection: response.persistentConnection,
reasonPhrase: response.reasonPhrase);
} else {
Expand Down
14 changes: 8 additions & 6 deletions pkgs/http/lib/src/base_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,27 @@ abstract class BaseResponse {
}
}

/// Fields and methods that will be added to [BaseResponse] when `package:http`
/// version 2 is released.
/// A [BaseResponse] with a [url] field.
///
/// [Client] methods that return a [BaseResponse] subclass, such as [Response]
/// or [StreamedResponse], **may** return a [BaseResponseV2].
/// or [StreamedResponse], **may** return a [BaseResponseWithUrl].
///
/// For example:
///
/// ```dart
/// final client = Client();
/// final response = client.get(Uri.https('example.com', '/'));
/// Uri? finalUri;
/// if (response is BaseResponseV2) {
/// finalUri = (response as BaseResponseV2).uri;
/// if (response is BaseResponseWithUrl) {
/// finalUri = (response as BaseResponseWithUrl).url;
/// }
/// // Do something with `finalUri`.
/// client.close();
/// ```
mixin BaseResponseV2 on BaseResponse {
///
/// [url] will be added to [BaseResponse] when `package:http` version 2 is
/// released and this mixin will be deprecated.
mixin BaseResponseWithUrl on BaseResponse {
/// The [Uri] of the response returned by the server.
///
/// If no redirects were followed, [url] will be the same as the requested
Expand Down
3 changes: 2 additions & 1 deletion pkgs/http/lib/src/io_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class _ClientSocketException extends ClientException
String toString() => 'ClientException with $cause, uri=$uri';
}

class _IOStreamedResponseV2 extends IOStreamedResponse with BaseResponseV2 {
class _IOStreamedResponseV2 extends IOStreamedResponse
with BaseResponseWithUrl {
@override
final Uri? url;

Expand Down
2 changes: 1 addition & 1 deletion pkgs/http/lib/src/streamed_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class StreamedResponse extends BaseResponse {

/// This class is private to `package:http` and will be removed when
/// `package:http` v2 is released.
class StreamedResponseV2 extends StreamedResponse with BaseResponseV2 {
class StreamedResponseV2 extends StreamedResponse with BaseResponseWithUrl {
@override
final Uri? url;

Expand Down
6 changes: 3 additions & 3 deletions pkgs/http/test/io/request_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ void main() {
final response = await request.send();

expect(response.statusCode, equals(302));
expect(
(response as http.BaseResponseV2).url, serverUrl.resolve('/redirect'));
expect((response as http.BaseResponseWithUrl).url,
serverUrl.resolve('/redirect'));
});

test('with redirects', () async {
Expand All @@ -56,7 +56,7 @@ void main() {
expect(response.statusCode, equals(200));
final bytesString = await response.stream.bytesToString();
expect(bytesString, parse(containsPair('path', '/')));
expect((response as http.BaseResponseV2).url, serverUrl.resolve('/'));
expect((response as http.BaseResponseWithUrl).url, serverUrl.resolve('/'));
});

test('exceeding max redirects', () async {
Expand Down

0 comments on commit b8bed26

Please sign in to comment.