Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for 426: Upgrade Required #12

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/src/network_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class NetworkResponse<R> with _$NetworkResponse<R> {
required R response,
}) = _UnprocessableEntity;

/// 426 - for responses when access to a resource requires a client upgrade.
const factory NetworkResponse.upgradeRequired(DioException error) =
_UpgradeRequired;

/// 500 - for responses where the service had an error while processing
/// the request.
const factory NetworkResponse.serverError(DioException error) = _ServerError;
Expand Down
251 changes: 251 additions & 0 deletions lib/src/network_response.freezed.dart

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions lib/src/sturdy_http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ class SturdyHttp {
response: error.response?.data as R,
);
break;
case 426:
resolvedResponse = NetworkResponse.upgradeRequired(error);
break;
case 500:
resolvedResponse = NetworkResponse.serverError(error);
break;
Expand Down
5 changes: 3 additions & 2 deletions lib/sturdy_http.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/// A strongly typed, event-based, reliable HTTP client that wraps Dio.
library sturdy_http;

export 'src/_dio_error.dart';
export 'src/sturdy_http.dart';
export 'src/sturdy_http_event_listener.dart';
export 'src/network_request.dart';
export 'src/network_response.dart';
export 'src/retry_behavior.dart';
export 'src/sturdy_http.dart';
export 'src/sturdy_http_event_listener.dart';
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ environment:

dependencies:
collection: ^1.17.1
dio: ^5.1.1
dio: ^5.2.0
freezed_annotation: ^2.2.0
uuid: ^4.1.0

Expand Down
27 changes: 27 additions & 0 deletions test/src/sturdy_http_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,33 @@ void main() {
});
});

group('when status code is 426', () {
setUp(() {
setupErrorResponse(
statusCode: 426,
body: const Foo(message: 'error').toJson(),
);
});

test('it returns upgradeRequired', () async {
final response =
await buildSubject().execute<Json, Result<bool, String>>(
const GetRequest(defaultPath),
onResponse: (response) {
return response.maybeWhen(
upgradeRequired: (error) => const Result.success(true),
orElse: () => const Result.failure('Not expected: orElse'),
);
},
);

response.when(
success: (s) => expect(s, isTrue),
failure: fail,
);
});
});

group('when status code is 500', () {
setUp(() {
setupErrorResponse(statusCode: 500);
Expand Down
Loading