Skip to content

Commit

Permalink
feat: Add support for 426: Upgrade Required (#12)
Browse files Browse the repository at this point in the history
* feat: Add support for 426: Upgrade Required

* Reformat

* Add library-level doc comment and re-order library exports

* Upgrade min dio version to 5.2.0, which has DioException
  • Loading branch information
willlockwood authored Jul 22, 2024
1 parent 7354000 commit 296cc2c
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 3 deletions.
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

0 comments on commit 296cc2c

Please sign in to comment.