From 95a9e0089ae0e93a564de195a66b43d7875ef4c6 Mon Sep 17 00:00:00 2001 From: Brandon Trautmann Date: Tue, 3 Sep 2024 17:11:31 -0400 Subject: [PATCH 01/10] wip: mostly working, may need to add generic response type back to execute invocation --- example/main.dart | 24 +- lib/src/deserializer.dart | 18 +- lib/src/network_response.dart | 152 +- lib/src/network_response.freezed.dart | 2035 ----------------- lib/src/sturdy_http.dart | 69 +- lib/src/sturdy_http_event_listener.dart | 1 - .../sturdy_http_event_listener.freezed.dart | 39 +- pubspec.yaml | 2 +- test/src/deserializer_test.dart | 39 +- test/src/sturdy_http_test.dart | 351 ++- test/src/sturdy_http_test.freezed.dart | 67 +- 11 files changed, 399 insertions(+), 2398 deletions(-) delete mode 100644 lib/src/network_response.freezed.dart diff --git a/example/main.dart b/example/main.dart index 642b80c..3fd3ac4 100644 --- a/example/main.dart +++ b/example/main.dart @@ -1,7 +1,5 @@ import 'package:charlatan/charlatan.dart'; -import 'package:sturdy_http/src/network_request.dart'; -import 'package:sturdy_http/src/sturdy_http.dart'; -import 'package:sturdy_http/src/sturdy_http_event_listener.dart'; +import 'package:sturdy_http/sturdy_http.dart'; void main(List args) async { // Set up some fake HTTP responses using Charlatan @@ -25,13 +23,13 @@ void main(List args) async { ); // A GetRequest. Prints 'Hello World!'. - await client.execute( + await client.execute( GetRequest('/foo'), onResponse: (r) { - r.maybeWhen( - ok: (message) => print(message), - orElse: () => print('GET /foo failed: $r'), - ); + return switch (r) { + Ok(:final response) => print(response), + _ => print('GET /foo failed: $r'), + }; }, ); @@ -40,13 +38,13 @@ void main(List args) async { // Prints: // 'mutative request success' <-- From ExampleEventListener // 'success!' - await client.execute( + await client.execute( PostRequest('/foo', data: NetworkRequestBody.empty()), onResponse: (r) { - r.maybeWhen( - okNoContent: () => print('success!'), - orElse: () => print('POST /foo failed: $r'), - ); + return switch (r) { + NoContent() => print('success!'), + _ => print('POST /foo failed: $r'), + }; }, ); } diff --git a/lib/src/deserializer.dart b/lib/src/deserializer.dart index c473468..93264ab 100644 --- a/lib/src/deserializer.dart +++ b/lib/src/deserializer.dart @@ -9,9 +9,9 @@ part of 'sturdy_http.dart'; /// {@endtemplate} abstract class Deserializer { /// {@macro deserializer} - Future deserialize({ - required NetworkResponse response, - required M Function(NetworkResponse) onResponse, + Future deserialize({ + required NetworkResponse response, + required M Function(NetworkResponse) onResponse, }); } @@ -25,9 +25,9 @@ class MainIsolateDeserializer implements Deserializer { /// {@macro main_isolate_deserializer} const MainIsolateDeserializer(); @override - Future deserialize({ - required NetworkResponse response, - required M Function(NetworkResponse response) onResponse, + Future deserialize({ + required NetworkResponse response, + required M Function(NetworkResponse response) onResponse, }) async { return onResponse(response); } @@ -57,9 +57,9 @@ class BackgroundDeserializer implements Deserializer { Stream? _results; @override - Future deserialize({ - required NetworkResponse response, - required M Function(NetworkResponse response) onResponse, + Future deserialize({ + required NetworkResponse response, + required M Function(NetworkResponse response) onResponse, }) async { await _initializeOnFirstRequest(); onResponseFunction() => onResponse(response); diff --git a/lib/src/network_response.dart b/lib/src/network_response.dart index 3ea7645..8d6ccaa 100644 --- a/lib/src/network_response.dart +++ b/lib/src/network_response.dart @@ -1,8 +1,4 @@ import 'package:dio/dio.dart'; -import 'package:freezed_annotation/freezed_annotation.dart'; -import 'package:sturdy_http/sturdy_http.dart'; - -part 'network_response.freezed.dart'; /// The produced object after [SturdyHttp] processes a [NetworkRequest]. /// @@ -12,58 +8,106 @@ part 'network_response.freezed.dart'; /// Most often the call sites executing a [NetworkRequest] will only be interested /// in a select few of these, and will resolve their error cases via a `maybeWhen` /// using the `orElse` clause. -@freezed -class NetworkResponse with _$NetworkResponse { - /// 200 - for successful responses that include a body. - const factory NetworkResponse.ok(R response) = _Ok; - - /// 204 - for successful responses that don't include a body. - const factory NetworkResponse.okNoContent() = _OkNoContent; - - /// 401 - for responses when the request was missing required authentication. - const factory NetworkResponse.unauthorized(DioException error) = - _Unauthorized; - - /// 403 - for responses when the request was authenticated but the - /// action is not authorized/allowed. - const factory NetworkResponse.forbidden(DioException error) = _Forbidden; - - /// 404 - for responses when we could not locate a resource, or when - /// someone would attempt to access a forbidden resource due to a bug. - const factory NetworkResponse.notFound(DioException error) = _NotFound; - - /// 422 - for responses when the request inputs failed our validations. - const factory NetworkResponse.unprocessableEntity({ - required DioException error, - 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; - - /// 503 - for responses when an underlying service issue prevents us from - /// fulfilling the request. - const factory NetworkResponse.serviceUnavailable(DioException error) = - _ServiceUnavailable; - - /// An error designated as a fallback in the event that we receive a status code - /// we don't explicitly handle *or* a request or response otherwise fails to meet - /// our expectations as "valid". The [message] will describe the condition and a - /// [DioException] will be present if it was available as a result of the request. - const factory NetworkResponse.genericError({ - required String message, - required bool isConnectionIssue, - DioException? error, - }) = _GenericError; +sealed class NetworkResponse { + const NetworkResponse(); +} + +sealed class NetworkResponseSuccess extends NetworkResponse { + const NetworkResponseSuccess(); +} + +final class NoContent extends NetworkResponseSuccess { + const NoContent(); +} + +final class Ok extends NetworkResponseSuccess { + final T response; + + const Ok(this.response); +} + +sealed class NetworkResponseFailure extends NetworkResponse { + final DioException? error; + + const NetworkResponseFailure({this.error}); +} + +/// 401 - for responses when the request was missing required authentication. +final class Unauthorized extends NetworkResponseFailure { + const Unauthorized({super.error}); +} + +/// 403 - for responses when the request was authenticated but the +/// action is not authorized/allowed. +final class Forbidden extends NetworkResponseFailure { + const Forbidden({super.error}); +} + +/// 404 - for responses when we could not locate a resource, or when +/// someone would attempt to access a forbidden resource due to a bug. +final class NotFound extends NetworkResponseFailure { + const NotFound({super.error}); +} + +/// 422 - for responses when the request inputs failed our validations. +final class UnprocessableEntity extends NetworkResponseFailure { + final R response; + + const UnprocessableEntity({super.error, required this.response}); +} + +/// 426 - for responses when a client version upgrade is required +final class UpgradeRequired extends NetworkResponseFailure { + const UpgradeRequired({super.error}); +} + +/// 500 - for responses where the service had an error while processing +/// the request. +final class ServerError extends NetworkResponseFailure { + const ServerError({super.error}); +} + +/// 503 - for responses when an underlying service issue prevents us from +/// fulfilling the request. +final class ServiceUnavailable extends NetworkResponseFailure { + const ServiceUnavailable({super.error}); +} + +final class GenericError extends NetworkResponseFailure { + const GenericError({ + super.error, + required this.message, + required this.isConnectionIssue, + }); + + final String message; + final bool isConnectionIssue; +} + +NetworkResponseSuccess? getSuccess(NetworkResponse response) { + return switch (response) { + NoContent() => NoContent(), + Ok(:final response) => Ok(response), + _ => null, + }; +} + +NetworkResponseFailure? getFail(NetworkResponse response) { + return switch (response) { + Unauthorized(:final error) => Unauthorized(error: error), + Forbidden(:final error) => Forbidden(error: error), + NotFound(:final error) => NotFound(error: error), + UnprocessableEntity(:final error, :final response) => + UnprocessableEntity(error: error, response: response), + UpgradeRequired(:final error) => UpgradeRequired(error: error), + ServerError(:final error) => ServerError(error: error), + ServiceUnavailable(:final error) => ServiceUnavailable(error: error), + _ => null, + }; } /// Extensions on the [NetworkResponse] type -extension NetworkResponseX on NetworkResponse { +extension NetworkResponseX on NetworkResponse { /// Whether this [NetworkResponse] should be considered successful - bool get isSuccess => this is _Ok || this is _OkNoContent; + bool get isSuccess => this is NetworkResponseSuccess; } diff --git a/lib/src/network_response.freezed.dart b/lib/src/network_response.freezed.dart deleted file mode 100644 index 45a7989..0000000 --- a/lib/src/network_response.freezed.dart +++ /dev/null @@ -1,2035 +0,0 @@ -// coverage:ignore-file -// GENERATED CODE - DO NOT MODIFY BY HAND -// ignore_for_file: type=lint -// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark - -part of 'network_response.dart'; - -// ************************************************************************** -// FreezedGenerator -// ************************************************************************** - -T _$identity(T value) => value; - -final _privateConstructorUsedError = UnsupportedError( - 'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models'); - -/// @nodoc -mixin _$NetworkResponse { - @optionalTypeArgs - TResult when({ - required TResult Function(R response) ok, - required TResult Function() okNoContent, - required TResult Function(DioException error) unauthorized, - required TResult Function(DioException error) forbidden, - required TResult Function(DioException error) notFound, - required TResult Function(DioException error, R response) - unprocessableEntity, - required TResult Function(DioException error) upgradeRequired, - required TResult Function(DioException error) serverError, - required TResult Function(DioException error) serviceUnavailable, - required TResult Function( - String message, bool isConnectionIssue, DioException? error) - genericError, - }) => - throw _privateConstructorUsedError; - @optionalTypeArgs - TResult? whenOrNull({ - TResult? Function(R response)? ok, - TResult? Function()? okNoContent, - TResult? Function(DioException error)? unauthorized, - TResult? Function(DioException error)? forbidden, - TResult? Function(DioException error)? notFound, - TResult? Function(DioException error, R response)? unprocessableEntity, - TResult? Function(DioException error)? upgradeRequired, - TResult? Function(DioException error)? serverError, - TResult? Function(DioException error)? serviceUnavailable, - TResult? Function( - String message, bool isConnectionIssue, DioException? error)? - genericError, - }) => - throw _privateConstructorUsedError; - @optionalTypeArgs - TResult maybeWhen({ - TResult Function(R response)? ok, - TResult Function()? okNoContent, - TResult Function(DioException error)? unauthorized, - TResult Function(DioException error)? forbidden, - TResult Function(DioException error)? notFound, - TResult Function(DioException error, R response)? unprocessableEntity, - TResult Function(DioException error)? upgradeRequired, - TResult Function(DioException error)? serverError, - TResult Function(DioException error)? serviceUnavailable, - TResult Function( - String message, bool isConnectionIssue, DioException? error)? - genericError, - required TResult orElse(), - }) => - throw _privateConstructorUsedError; - @optionalTypeArgs - TResult map({ - required TResult Function(_Ok value) ok, - required TResult Function(_OkNoContent value) okNoContent, - required TResult Function(_Unauthorized value) unauthorized, - required TResult Function(_Forbidden value) forbidden, - required TResult Function(_NotFound value) notFound, - required TResult Function(_UnprocessableEntity value) - unprocessableEntity, - required TResult Function(_UpgradeRequired value) upgradeRequired, - required TResult Function(_ServerError value) serverError, - required TResult Function(_ServiceUnavailable value) serviceUnavailable, - required TResult Function(_GenericError value) genericError, - }) => - throw _privateConstructorUsedError; - @optionalTypeArgs - TResult? mapOrNull({ - TResult? Function(_Ok value)? ok, - TResult? Function(_OkNoContent value)? okNoContent, - TResult? Function(_Unauthorized value)? unauthorized, - TResult? Function(_Forbidden value)? forbidden, - TResult? Function(_NotFound value)? notFound, - TResult? Function(_UnprocessableEntity value)? unprocessableEntity, - TResult? Function(_UpgradeRequired value)? upgradeRequired, - TResult? Function(_ServerError value)? serverError, - TResult? Function(_ServiceUnavailable value)? serviceUnavailable, - TResult? Function(_GenericError value)? genericError, - }) => - throw _privateConstructorUsedError; - @optionalTypeArgs - TResult maybeMap({ - TResult Function(_Ok value)? ok, - TResult Function(_OkNoContent value)? okNoContent, - TResult Function(_Unauthorized value)? unauthorized, - TResult Function(_Forbidden value)? forbidden, - TResult Function(_NotFound value)? notFound, - TResult Function(_UnprocessableEntity value)? unprocessableEntity, - TResult Function(_UpgradeRequired value)? upgradeRequired, - TResult Function(_ServerError value)? serverError, - TResult Function(_ServiceUnavailable value)? serviceUnavailable, - TResult Function(_GenericError value)? genericError, - required TResult orElse(), - }) => - throw _privateConstructorUsedError; -} - -/// @nodoc -abstract class $NetworkResponseCopyWith { - factory $NetworkResponseCopyWith( - NetworkResponse value, $Res Function(NetworkResponse) then) = - _$NetworkResponseCopyWithImpl>; -} - -/// @nodoc -class _$NetworkResponseCopyWithImpl> - implements $NetworkResponseCopyWith { - _$NetworkResponseCopyWithImpl(this._value, this._then); - - // ignore: unused_field - final $Val _value; - // ignore: unused_field - final $Res Function($Val) _then; -} - -/// @nodoc -abstract class _$$OkImplCopyWith { - factory _$$OkImplCopyWith( - _$OkImpl value, $Res Function(_$OkImpl) then) = - __$$OkImplCopyWithImpl; - @useResult - $Res call({R response}); -} - -/// @nodoc -class __$$OkImplCopyWithImpl - extends _$NetworkResponseCopyWithImpl> - implements _$$OkImplCopyWith { - __$$OkImplCopyWithImpl(_$OkImpl _value, $Res Function(_$OkImpl) _then) - : super(_value, _then); - - @pragma('vm:prefer-inline') - @override - $Res call({ - Object? response = freezed, - }) { - return _then(_$OkImpl( - freezed == response - ? _value.response - : response // ignore: cast_nullable_to_non_nullable - as R, - )); - } -} - -/// @nodoc - -class _$OkImpl implements _Ok { - const _$OkImpl(this.response); - - @override - final R response; - - @override - String toString() { - return 'NetworkResponse<$R>.ok(response: $response)'; - } - - @override - bool operator ==(Object other) { - return identical(this, other) || - (other.runtimeType == runtimeType && - other is _$OkImpl && - const DeepCollectionEquality().equals(other.response, response)); - } - - @override - int get hashCode => - Object.hash(runtimeType, const DeepCollectionEquality().hash(response)); - - @JsonKey(ignore: true) - @override - @pragma('vm:prefer-inline') - _$$OkImplCopyWith> get copyWith => - __$$OkImplCopyWithImpl>(this, _$identity); - - @override - @optionalTypeArgs - TResult when({ - required TResult Function(R response) ok, - required TResult Function() okNoContent, - required TResult Function(DioException error) unauthorized, - required TResult Function(DioException error) forbidden, - required TResult Function(DioException error) notFound, - required TResult Function(DioException error, R response) - unprocessableEntity, - required TResult Function(DioException error) upgradeRequired, - required TResult Function(DioException error) serverError, - required TResult Function(DioException error) serviceUnavailable, - required TResult Function( - String message, bool isConnectionIssue, DioException? error) - genericError, - }) { - return ok(response); - } - - @override - @optionalTypeArgs - TResult? whenOrNull({ - TResult? Function(R response)? ok, - TResult? Function()? okNoContent, - TResult? Function(DioException error)? unauthorized, - TResult? Function(DioException error)? forbidden, - TResult? Function(DioException error)? notFound, - TResult? Function(DioException error, R response)? unprocessableEntity, - TResult? Function(DioException error)? upgradeRequired, - TResult? Function(DioException error)? serverError, - TResult? Function(DioException error)? serviceUnavailable, - TResult? Function( - String message, bool isConnectionIssue, DioException? error)? - genericError, - }) { - return ok?.call(response); - } - - @override - @optionalTypeArgs - TResult maybeWhen({ - TResult Function(R response)? ok, - TResult Function()? okNoContent, - TResult Function(DioException error)? unauthorized, - TResult Function(DioException error)? forbidden, - TResult Function(DioException error)? notFound, - TResult Function(DioException error, R response)? unprocessableEntity, - TResult Function(DioException error)? upgradeRequired, - TResult Function(DioException error)? serverError, - TResult Function(DioException error)? serviceUnavailable, - TResult Function( - String message, bool isConnectionIssue, DioException? error)? - genericError, - required TResult orElse(), - }) { - if (ok != null) { - return ok(response); - } - return orElse(); - } - - @override - @optionalTypeArgs - TResult map({ - required TResult Function(_Ok value) ok, - required TResult Function(_OkNoContent value) okNoContent, - required TResult Function(_Unauthorized value) unauthorized, - required TResult Function(_Forbidden value) forbidden, - required TResult Function(_NotFound value) notFound, - required TResult Function(_UnprocessableEntity value) - unprocessableEntity, - required TResult Function(_UpgradeRequired value) upgradeRequired, - required TResult Function(_ServerError value) serverError, - required TResult Function(_ServiceUnavailable value) serviceUnavailable, - required TResult Function(_GenericError value) genericError, - }) { - return ok(this); - } - - @override - @optionalTypeArgs - TResult? mapOrNull({ - TResult? Function(_Ok value)? ok, - TResult? Function(_OkNoContent value)? okNoContent, - TResult? Function(_Unauthorized value)? unauthorized, - TResult? Function(_Forbidden value)? forbidden, - TResult? Function(_NotFound value)? notFound, - TResult? Function(_UnprocessableEntity value)? unprocessableEntity, - TResult? Function(_UpgradeRequired value)? upgradeRequired, - TResult? Function(_ServerError value)? serverError, - TResult? Function(_ServiceUnavailable value)? serviceUnavailable, - TResult? Function(_GenericError value)? genericError, - }) { - return ok?.call(this); - } - - @override - @optionalTypeArgs - TResult maybeMap({ - TResult Function(_Ok value)? ok, - TResult Function(_OkNoContent value)? okNoContent, - TResult Function(_Unauthorized value)? unauthorized, - TResult Function(_Forbidden value)? forbidden, - TResult Function(_NotFound value)? notFound, - TResult Function(_UnprocessableEntity value)? unprocessableEntity, - TResult Function(_UpgradeRequired value)? upgradeRequired, - TResult Function(_ServerError value)? serverError, - TResult Function(_ServiceUnavailable value)? serviceUnavailable, - TResult Function(_GenericError value)? genericError, - required TResult orElse(), - }) { - if (ok != null) { - return ok(this); - } - return orElse(); - } -} - -abstract class _Ok implements NetworkResponse { - const factory _Ok(final R response) = _$OkImpl; - - R get response; - @JsonKey(ignore: true) - _$$OkImplCopyWith> get copyWith => - throw _privateConstructorUsedError; -} - -/// @nodoc -abstract class _$$OkNoContentImplCopyWith { - factory _$$OkNoContentImplCopyWith(_$OkNoContentImpl value, - $Res Function(_$OkNoContentImpl) then) = - __$$OkNoContentImplCopyWithImpl; -} - -/// @nodoc -class __$$OkNoContentImplCopyWithImpl - extends _$NetworkResponseCopyWithImpl> - implements _$$OkNoContentImplCopyWith { - __$$OkNoContentImplCopyWithImpl( - _$OkNoContentImpl _value, $Res Function(_$OkNoContentImpl) _then) - : super(_value, _then); -} - -/// @nodoc - -class _$OkNoContentImpl implements _OkNoContent { - const _$OkNoContentImpl(); - - @override - String toString() { - return 'NetworkResponse<$R>.okNoContent()'; - } - - @override - bool operator ==(Object other) { - return identical(this, other) || - (other.runtimeType == runtimeType && other is _$OkNoContentImpl); - } - - @override - int get hashCode => runtimeType.hashCode; - - @override - @optionalTypeArgs - TResult when({ - required TResult Function(R response) ok, - required TResult Function() okNoContent, - required TResult Function(DioException error) unauthorized, - required TResult Function(DioException error) forbidden, - required TResult Function(DioException error) notFound, - required TResult Function(DioException error, R response) - unprocessableEntity, - required TResult Function(DioException error) upgradeRequired, - required TResult Function(DioException error) serverError, - required TResult Function(DioException error) serviceUnavailable, - required TResult Function( - String message, bool isConnectionIssue, DioException? error) - genericError, - }) { - return okNoContent(); - } - - @override - @optionalTypeArgs - TResult? whenOrNull({ - TResult? Function(R response)? ok, - TResult? Function()? okNoContent, - TResult? Function(DioException error)? unauthorized, - TResult? Function(DioException error)? forbidden, - TResult? Function(DioException error)? notFound, - TResult? Function(DioException error, R response)? unprocessableEntity, - TResult? Function(DioException error)? upgradeRequired, - TResult? Function(DioException error)? serverError, - TResult? Function(DioException error)? serviceUnavailable, - TResult? Function( - String message, bool isConnectionIssue, DioException? error)? - genericError, - }) { - return okNoContent?.call(); - } - - @override - @optionalTypeArgs - TResult maybeWhen({ - TResult Function(R response)? ok, - TResult Function()? okNoContent, - TResult Function(DioException error)? unauthorized, - TResult Function(DioException error)? forbidden, - TResult Function(DioException error)? notFound, - TResult Function(DioException error, R response)? unprocessableEntity, - TResult Function(DioException error)? upgradeRequired, - TResult Function(DioException error)? serverError, - TResult Function(DioException error)? serviceUnavailable, - TResult Function( - String message, bool isConnectionIssue, DioException? error)? - genericError, - required TResult orElse(), - }) { - if (okNoContent != null) { - return okNoContent(); - } - return orElse(); - } - - @override - @optionalTypeArgs - TResult map({ - required TResult Function(_Ok value) ok, - required TResult Function(_OkNoContent value) okNoContent, - required TResult Function(_Unauthorized value) unauthorized, - required TResult Function(_Forbidden value) forbidden, - required TResult Function(_NotFound value) notFound, - required TResult Function(_UnprocessableEntity value) - unprocessableEntity, - required TResult Function(_UpgradeRequired value) upgradeRequired, - required TResult Function(_ServerError value) serverError, - required TResult Function(_ServiceUnavailable value) serviceUnavailable, - required TResult Function(_GenericError value) genericError, - }) { - return okNoContent(this); - } - - @override - @optionalTypeArgs - TResult? mapOrNull({ - TResult? Function(_Ok value)? ok, - TResult? Function(_OkNoContent value)? okNoContent, - TResult? Function(_Unauthorized value)? unauthorized, - TResult? Function(_Forbidden value)? forbidden, - TResult? Function(_NotFound value)? notFound, - TResult? Function(_UnprocessableEntity value)? unprocessableEntity, - TResult? Function(_UpgradeRequired value)? upgradeRequired, - TResult? Function(_ServerError value)? serverError, - TResult? Function(_ServiceUnavailable value)? serviceUnavailable, - TResult? Function(_GenericError value)? genericError, - }) { - return okNoContent?.call(this); - } - - @override - @optionalTypeArgs - TResult maybeMap({ - TResult Function(_Ok value)? ok, - TResult Function(_OkNoContent value)? okNoContent, - TResult Function(_Unauthorized value)? unauthorized, - TResult Function(_Forbidden value)? forbidden, - TResult Function(_NotFound value)? notFound, - TResult Function(_UnprocessableEntity value)? unprocessableEntity, - TResult Function(_UpgradeRequired value)? upgradeRequired, - TResult Function(_ServerError value)? serverError, - TResult Function(_ServiceUnavailable value)? serviceUnavailable, - TResult Function(_GenericError value)? genericError, - required TResult orElse(), - }) { - if (okNoContent != null) { - return okNoContent(this); - } - return orElse(); - } -} - -abstract class _OkNoContent implements NetworkResponse { - const factory _OkNoContent() = _$OkNoContentImpl; -} - -/// @nodoc -abstract class _$$UnauthorizedImplCopyWith { - factory _$$UnauthorizedImplCopyWith(_$UnauthorizedImpl value, - $Res Function(_$UnauthorizedImpl) then) = - __$$UnauthorizedImplCopyWithImpl; - @useResult - $Res call({DioException error}); -} - -/// @nodoc -class __$$UnauthorizedImplCopyWithImpl - extends _$NetworkResponseCopyWithImpl> - implements _$$UnauthorizedImplCopyWith { - __$$UnauthorizedImplCopyWithImpl( - _$UnauthorizedImpl _value, $Res Function(_$UnauthorizedImpl) _then) - : super(_value, _then); - - @pragma('vm:prefer-inline') - @override - $Res call({ - Object? error = null, - }) { - return _then(_$UnauthorizedImpl( - null == error - ? _value.error - : error // ignore: cast_nullable_to_non_nullable - as DioException, - )); - } -} - -/// @nodoc - -class _$UnauthorizedImpl implements _Unauthorized { - const _$UnauthorizedImpl(this.error); - - @override - final DioException error; - - @override - String toString() { - return 'NetworkResponse<$R>.unauthorized(error: $error)'; - } - - @override - bool operator ==(Object other) { - return identical(this, other) || - (other.runtimeType == runtimeType && - other is _$UnauthorizedImpl && - (identical(other.error, error) || other.error == error)); - } - - @override - int get hashCode => Object.hash(runtimeType, error); - - @JsonKey(ignore: true) - @override - @pragma('vm:prefer-inline') - _$$UnauthorizedImplCopyWith> get copyWith => - __$$UnauthorizedImplCopyWithImpl>( - this, _$identity); - - @override - @optionalTypeArgs - TResult when({ - required TResult Function(R response) ok, - required TResult Function() okNoContent, - required TResult Function(DioException error) unauthorized, - required TResult Function(DioException error) forbidden, - required TResult Function(DioException error) notFound, - required TResult Function(DioException error, R response) - unprocessableEntity, - required TResult Function(DioException error) upgradeRequired, - required TResult Function(DioException error) serverError, - required TResult Function(DioException error) serviceUnavailable, - required TResult Function( - String message, bool isConnectionIssue, DioException? error) - genericError, - }) { - return unauthorized(error); - } - - @override - @optionalTypeArgs - TResult? whenOrNull({ - TResult? Function(R response)? ok, - TResult? Function()? okNoContent, - TResult? Function(DioException error)? unauthorized, - TResult? Function(DioException error)? forbidden, - TResult? Function(DioException error)? notFound, - TResult? Function(DioException error, R response)? unprocessableEntity, - TResult? Function(DioException error)? upgradeRequired, - TResult? Function(DioException error)? serverError, - TResult? Function(DioException error)? serviceUnavailable, - TResult? Function( - String message, bool isConnectionIssue, DioException? error)? - genericError, - }) { - return unauthorized?.call(error); - } - - @override - @optionalTypeArgs - TResult maybeWhen({ - TResult Function(R response)? ok, - TResult Function()? okNoContent, - TResult Function(DioException error)? unauthorized, - TResult Function(DioException error)? forbidden, - TResult Function(DioException error)? notFound, - TResult Function(DioException error, R response)? unprocessableEntity, - TResult Function(DioException error)? upgradeRequired, - TResult Function(DioException error)? serverError, - TResult Function(DioException error)? serviceUnavailable, - TResult Function( - String message, bool isConnectionIssue, DioException? error)? - genericError, - required TResult orElse(), - }) { - if (unauthorized != null) { - return unauthorized(error); - } - return orElse(); - } - - @override - @optionalTypeArgs - TResult map({ - required TResult Function(_Ok value) ok, - required TResult Function(_OkNoContent value) okNoContent, - required TResult Function(_Unauthorized value) unauthorized, - required TResult Function(_Forbidden value) forbidden, - required TResult Function(_NotFound value) notFound, - required TResult Function(_UnprocessableEntity value) - unprocessableEntity, - required TResult Function(_UpgradeRequired value) upgradeRequired, - required TResult Function(_ServerError value) serverError, - required TResult Function(_ServiceUnavailable value) serviceUnavailable, - required TResult Function(_GenericError value) genericError, - }) { - return unauthorized(this); - } - - @override - @optionalTypeArgs - TResult? mapOrNull({ - TResult? Function(_Ok value)? ok, - TResult? Function(_OkNoContent value)? okNoContent, - TResult? Function(_Unauthorized value)? unauthorized, - TResult? Function(_Forbidden value)? forbidden, - TResult? Function(_NotFound value)? notFound, - TResult? Function(_UnprocessableEntity value)? unprocessableEntity, - TResult? Function(_UpgradeRequired value)? upgradeRequired, - TResult? Function(_ServerError value)? serverError, - TResult? Function(_ServiceUnavailable value)? serviceUnavailable, - TResult? Function(_GenericError value)? genericError, - }) { - return unauthorized?.call(this); - } - - @override - @optionalTypeArgs - TResult maybeMap({ - TResult Function(_Ok value)? ok, - TResult Function(_OkNoContent value)? okNoContent, - TResult Function(_Unauthorized value)? unauthorized, - TResult Function(_Forbidden value)? forbidden, - TResult Function(_NotFound value)? notFound, - TResult Function(_UnprocessableEntity value)? unprocessableEntity, - TResult Function(_UpgradeRequired value)? upgradeRequired, - TResult Function(_ServerError value)? serverError, - TResult Function(_ServiceUnavailable value)? serviceUnavailable, - TResult Function(_GenericError value)? genericError, - required TResult orElse(), - }) { - if (unauthorized != null) { - return unauthorized(this); - } - return orElse(); - } -} - -abstract class _Unauthorized implements NetworkResponse { - const factory _Unauthorized(final DioException error) = _$UnauthorizedImpl; - - DioException get error; - @JsonKey(ignore: true) - _$$UnauthorizedImplCopyWith> get copyWith => - throw _privateConstructorUsedError; -} - -/// @nodoc -abstract class _$$ForbiddenImplCopyWith { - factory _$$ForbiddenImplCopyWith( - _$ForbiddenImpl value, $Res Function(_$ForbiddenImpl) then) = - __$$ForbiddenImplCopyWithImpl; - @useResult - $Res call({DioException error}); -} - -/// @nodoc -class __$$ForbiddenImplCopyWithImpl - extends _$NetworkResponseCopyWithImpl> - implements _$$ForbiddenImplCopyWith { - __$$ForbiddenImplCopyWithImpl( - _$ForbiddenImpl _value, $Res Function(_$ForbiddenImpl) _then) - : super(_value, _then); - - @pragma('vm:prefer-inline') - @override - $Res call({ - Object? error = null, - }) { - return _then(_$ForbiddenImpl( - null == error - ? _value.error - : error // ignore: cast_nullable_to_non_nullable - as DioException, - )); - } -} - -/// @nodoc - -class _$ForbiddenImpl implements _Forbidden { - const _$ForbiddenImpl(this.error); - - @override - final DioException error; - - @override - String toString() { - return 'NetworkResponse<$R>.forbidden(error: $error)'; - } - - @override - bool operator ==(Object other) { - return identical(this, other) || - (other.runtimeType == runtimeType && - other is _$ForbiddenImpl && - (identical(other.error, error) || other.error == error)); - } - - @override - int get hashCode => Object.hash(runtimeType, error); - - @JsonKey(ignore: true) - @override - @pragma('vm:prefer-inline') - _$$ForbiddenImplCopyWith> get copyWith => - __$$ForbiddenImplCopyWithImpl>(this, _$identity); - - @override - @optionalTypeArgs - TResult when({ - required TResult Function(R response) ok, - required TResult Function() okNoContent, - required TResult Function(DioException error) unauthorized, - required TResult Function(DioException error) forbidden, - required TResult Function(DioException error) notFound, - required TResult Function(DioException error, R response) - unprocessableEntity, - required TResult Function(DioException error) upgradeRequired, - required TResult Function(DioException error) serverError, - required TResult Function(DioException error) serviceUnavailable, - required TResult Function( - String message, bool isConnectionIssue, DioException? error) - genericError, - }) { - return forbidden(error); - } - - @override - @optionalTypeArgs - TResult? whenOrNull({ - TResult? Function(R response)? ok, - TResult? Function()? okNoContent, - TResult? Function(DioException error)? unauthorized, - TResult? Function(DioException error)? forbidden, - TResult? Function(DioException error)? notFound, - TResult? Function(DioException error, R response)? unprocessableEntity, - TResult? Function(DioException error)? upgradeRequired, - TResult? Function(DioException error)? serverError, - TResult? Function(DioException error)? serviceUnavailable, - TResult? Function( - String message, bool isConnectionIssue, DioException? error)? - genericError, - }) { - return forbidden?.call(error); - } - - @override - @optionalTypeArgs - TResult maybeWhen({ - TResult Function(R response)? ok, - TResult Function()? okNoContent, - TResult Function(DioException error)? unauthorized, - TResult Function(DioException error)? forbidden, - TResult Function(DioException error)? notFound, - TResult Function(DioException error, R response)? unprocessableEntity, - TResult Function(DioException error)? upgradeRequired, - TResult Function(DioException error)? serverError, - TResult Function(DioException error)? serviceUnavailable, - TResult Function( - String message, bool isConnectionIssue, DioException? error)? - genericError, - required TResult orElse(), - }) { - if (forbidden != null) { - return forbidden(error); - } - return orElse(); - } - - @override - @optionalTypeArgs - TResult map({ - required TResult Function(_Ok value) ok, - required TResult Function(_OkNoContent value) okNoContent, - required TResult Function(_Unauthorized value) unauthorized, - required TResult Function(_Forbidden value) forbidden, - required TResult Function(_NotFound value) notFound, - required TResult Function(_UnprocessableEntity value) - unprocessableEntity, - required TResult Function(_UpgradeRequired value) upgradeRequired, - required TResult Function(_ServerError value) serverError, - required TResult Function(_ServiceUnavailable value) serviceUnavailable, - required TResult Function(_GenericError value) genericError, - }) { - return forbidden(this); - } - - @override - @optionalTypeArgs - TResult? mapOrNull({ - TResult? Function(_Ok value)? ok, - TResult? Function(_OkNoContent value)? okNoContent, - TResult? Function(_Unauthorized value)? unauthorized, - TResult? Function(_Forbidden value)? forbidden, - TResult? Function(_NotFound value)? notFound, - TResult? Function(_UnprocessableEntity value)? unprocessableEntity, - TResult? Function(_UpgradeRequired value)? upgradeRequired, - TResult? Function(_ServerError value)? serverError, - TResult? Function(_ServiceUnavailable value)? serviceUnavailable, - TResult? Function(_GenericError value)? genericError, - }) { - return forbidden?.call(this); - } - - @override - @optionalTypeArgs - TResult maybeMap({ - TResult Function(_Ok value)? ok, - TResult Function(_OkNoContent value)? okNoContent, - TResult Function(_Unauthorized value)? unauthorized, - TResult Function(_Forbidden value)? forbidden, - TResult Function(_NotFound value)? notFound, - TResult Function(_UnprocessableEntity value)? unprocessableEntity, - TResult Function(_UpgradeRequired value)? upgradeRequired, - TResult Function(_ServerError value)? serverError, - TResult Function(_ServiceUnavailable value)? serviceUnavailable, - TResult Function(_GenericError value)? genericError, - required TResult orElse(), - }) { - if (forbidden != null) { - return forbidden(this); - } - return orElse(); - } -} - -abstract class _Forbidden implements NetworkResponse { - const factory _Forbidden(final DioException error) = _$ForbiddenImpl; - - DioException get error; - @JsonKey(ignore: true) - _$$ForbiddenImplCopyWith> get copyWith => - throw _privateConstructorUsedError; -} - -/// @nodoc -abstract class _$$NotFoundImplCopyWith { - factory _$$NotFoundImplCopyWith( - _$NotFoundImpl value, $Res Function(_$NotFoundImpl) then) = - __$$NotFoundImplCopyWithImpl; - @useResult - $Res call({DioException error}); -} - -/// @nodoc -class __$$NotFoundImplCopyWithImpl - extends _$NetworkResponseCopyWithImpl> - implements _$$NotFoundImplCopyWith { - __$$NotFoundImplCopyWithImpl( - _$NotFoundImpl _value, $Res Function(_$NotFoundImpl) _then) - : super(_value, _then); - - @pragma('vm:prefer-inline') - @override - $Res call({ - Object? error = null, - }) { - return _then(_$NotFoundImpl( - null == error - ? _value.error - : error // ignore: cast_nullable_to_non_nullable - as DioException, - )); - } -} - -/// @nodoc - -class _$NotFoundImpl implements _NotFound { - const _$NotFoundImpl(this.error); - - @override - final DioException error; - - @override - String toString() { - return 'NetworkResponse<$R>.notFound(error: $error)'; - } - - @override - bool operator ==(Object other) { - return identical(this, other) || - (other.runtimeType == runtimeType && - other is _$NotFoundImpl && - (identical(other.error, error) || other.error == error)); - } - - @override - int get hashCode => Object.hash(runtimeType, error); - - @JsonKey(ignore: true) - @override - @pragma('vm:prefer-inline') - _$$NotFoundImplCopyWith> get copyWith => - __$$NotFoundImplCopyWithImpl>(this, _$identity); - - @override - @optionalTypeArgs - TResult when({ - required TResult Function(R response) ok, - required TResult Function() okNoContent, - required TResult Function(DioException error) unauthorized, - required TResult Function(DioException error) forbidden, - required TResult Function(DioException error) notFound, - required TResult Function(DioException error, R response) - unprocessableEntity, - required TResult Function(DioException error) upgradeRequired, - required TResult Function(DioException error) serverError, - required TResult Function(DioException error) serviceUnavailable, - required TResult Function( - String message, bool isConnectionIssue, DioException? error) - genericError, - }) { - return notFound(error); - } - - @override - @optionalTypeArgs - TResult? whenOrNull({ - TResult? Function(R response)? ok, - TResult? Function()? okNoContent, - TResult? Function(DioException error)? unauthorized, - TResult? Function(DioException error)? forbidden, - TResult? Function(DioException error)? notFound, - TResult? Function(DioException error, R response)? unprocessableEntity, - TResult? Function(DioException error)? upgradeRequired, - TResult? Function(DioException error)? serverError, - TResult? Function(DioException error)? serviceUnavailable, - TResult? Function( - String message, bool isConnectionIssue, DioException? error)? - genericError, - }) { - return notFound?.call(error); - } - - @override - @optionalTypeArgs - TResult maybeWhen({ - TResult Function(R response)? ok, - TResult Function()? okNoContent, - TResult Function(DioException error)? unauthorized, - TResult Function(DioException error)? forbidden, - TResult Function(DioException error)? notFound, - TResult Function(DioException error, R response)? unprocessableEntity, - TResult Function(DioException error)? upgradeRequired, - TResult Function(DioException error)? serverError, - TResult Function(DioException error)? serviceUnavailable, - TResult Function( - String message, bool isConnectionIssue, DioException? error)? - genericError, - required TResult orElse(), - }) { - if (notFound != null) { - return notFound(error); - } - return orElse(); - } - - @override - @optionalTypeArgs - TResult map({ - required TResult Function(_Ok value) ok, - required TResult Function(_OkNoContent value) okNoContent, - required TResult Function(_Unauthorized value) unauthorized, - required TResult Function(_Forbidden value) forbidden, - required TResult Function(_NotFound value) notFound, - required TResult Function(_UnprocessableEntity value) - unprocessableEntity, - required TResult Function(_UpgradeRequired value) upgradeRequired, - required TResult Function(_ServerError value) serverError, - required TResult Function(_ServiceUnavailable value) serviceUnavailable, - required TResult Function(_GenericError value) genericError, - }) { - return notFound(this); - } - - @override - @optionalTypeArgs - TResult? mapOrNull({ - TResult? Function(_Ok value)? ok, - TResult? Function(_OkNoContent value)? okNoContent, - TResult? Function(_Unauthorized value)? unauthorized, - TResult? Function(_Forbidden value)? forbidden, - TResult? Function(_NotFound value)? notFound, - TResult? Function(_UnprocessableEntity value)? unprocessableEntity, - TResult? Function(_UpgradeRequired value)? upgradeRequired, - TResult? Function(_ServerError value)? serverError, - TResult? Function(_ServiceUnavailable value)? serviceUnavailable, - TResult? Function(_GenericError value)? genericError, - }) { - return notFound?.call(this); - } - - @override - @optionalTypeArgs - TResult maybeMap({ - TResult Function(_Ok value)? ok, - TResult Function(_OkNoContent value)? okNoContent, - TResult Function(_Unauthorized value)? unauthorized, - TResult Function(_Forbidden value)? forbidden, - TResult Function(_NotFound value)? notFound, - TResult Function(_UnprocessableEntity value)? unprocessableEntity, - TResult Function(_UpgradeRequired value)? upgradeRequired, - TResult Function(_ServerError value)? serverError, - TResult Function(_ServiceUnavailable value)? serviceUnavailable, - TResult Function(_GenericError value)? genericError, - required TResult orElse(), - }) { - if (notFound != null) { - return notFound(this); - } - return orElse(); - } -} - -abstract class _NotFound implements NetworkResponse { - const factory _NotFound(final DioException error) = _$NotFoundImpl; - - DioException get error; - @JsonKey(ignore: true) - _$$NotFoundImplCopyWith> get copyWith => - throw _privateConstructorUsedError; -} - -/// @nodoc -abstract class _$$UnprocessableEntityImplCopyWith { - factory _$$UnprocessableEntityImplCopyWith(_$UnprocessableEntityImpl value, - $Res Function(_$UnprocessableEntityImpl) then) = - __$$UnprocessableEntityImplCopyWithImpl; - @useResult - $Res call({DioException error, R response}); -} - -/// @nodoc -class __$$UnprocessableEntityImplCopyWithImpl - extends _$NetworkResponseCopyWithImpl> - implements _$$UnprocessableEntityImplCopyWith { - __$$UnprocessableEntityImplCopyWithImpl(_$UnprocessableEntityImpl _value, - $Res Function(_$UnprocessableEntityImpl) _then) - : super(_value, _then); - - @pragma('vm:prefer-inline') - @override - $Res call({ - Object? error = null, - Object? response = freezed, - }) { - return _then(_$UnprocessableEntityImpl( - error: null == error - ? _value.error - : error // ignore: cast_nullable_to_non_nullable - as DioException, - response: freezed == response - ? _value.response - : response // ignore: cast_nullable_to_non_nullable - as R, - )); - } -} - -/// @nodoc - -class _$UnprocessableEntityImpl implements _UnprocessableEntity { - const _$UnprocessableEntityImpl( - {required this.error, required this.response}); - - @override - final DioException error; - @override - final R response; - - @override - String toString() { - return 'NetworkResponse<$R>.unprocessableEntity(error: $error, response: $response)'; - } - - @override - bool operator ==(Object other) { - return identical(this, other) || - (other.runtimeType == runtimeType && - other is _$UnprocessableEntityImpl && - (identical(other.error, error) || other.error == error) && - const DeepCollectionEquality().equals(other.response, response)); - } - - @override - int get hashCode => Object.hash( - runtimeType, error, const DeepCollectionEquality().hash(response)); - - @JsonKey(ignore: true) - @override - @pragma('vm:prefer-inline') - _$$UnprocessableEntityImplCopyWith> - get copyWith => __$$UnprocessableEntityImplCopyWithImpl>(this, _$identity); - - @override - @optionalTypeArgs - TResult when({ - required TResult Function(R response) ok, - required TResult Function() okNoContent, - required TResult Function(DioException error) unauthorized, - required TResult Function(DioException error) forbidden, - required TResult Function(DioException error) notFound, - required TResult Function(DioException error, R response) - unprocessableEntity, - required TResult Function(DioException error) upgradeRequired, - required TResult Function(DioException error) serverError, - required TResult Function(DioException error) serviceUnavailable, - required TResult Function( - String message, bool isConnectionIssue, DioException? error) - genericError, - }) { - return unprocessableEntity(error, response); - } - - @override - @optionalTypeArgs - TResult? whenOrNull({ - TResult? Function(R response)? ok, - TResult? Function()? okNoContent, - TResult? Function(DioException error)? unauthorized, - TResult? Function(DioException error)? forbidden, - TResult? Function(DioException error)? notFound, - TResult? Function(DioException error, R response)? unprocessableEntity, - TResult? Function(DioException error)? upgradeRequired, - TResult? Function(DioException error)? serverError, - TResult? Function(DioException error)? serviceUnavailable, - TResult? Function( - String message, bool isConnectionIssue, DioException? error)? - genericError, - }) { - return unprocessableEntity?.call(error, response); - } - - @override - @optionalTypeArgs - TResult maybeWhen({ - TResult Function(R response)? ok, - TResult Function()? okNoContent, - TResult Function(DioException error)? unauthorized, - TResult Function(DioException error)? forbidden, - TResult Function(DioException error)? notFound, - TResult Function(DioException error, R response)? unprocessableEntity, - TResult Function(DioException error)? upgradeRequired, - TResult Function(DioException error)? serverError, - TResult Function(DioException error)? serviceUnavailable, - TResult Function( - String message, bool isConnectionIssue, DioException? error)? - genericError, - required TResult orElse(), - }) { - if (unprocessableEntity != null) { - return unprocessableEntity(error, response); - } - return orElse(); - } - - @override - @optionalTypeArgs - TResult map({ - required TResult Function(_Ok value) ok, - required TResult Function(_OkNoContent value) okNoContent, - required TResult Function(_Unauthorized value) unauthorized, - required TResult Function(_Forbidden value) forbidden, - required TResult Function(_NotFound value) notFound, - required TResult Function(_UnprocessableEntity value) - unprocessableEntity, - required TResult Function(_UpgradeRequired value) upgradeRequired, - required TResult Function(_ServerError value) serverError, - required TResult Function(_ServiceUnavailable value) serviceUnavailable, - required TResult Function(_GenericError value) genericError, - }) { - return unprocessableEntity(this); - } - - @override - @optionalTypeArgs - TResult? mapOrNull({ - TResult? Function(_Ok value)? ok, - TResult? Function(_OkNoContent value)? okNoContent, - TResult? Function(_Unauthorized value)? unauthorized, - TResult? Function(_Forbidden value)? forbidden, - TResult? Function(_NotFound value)? notFound, - TResult? Function(_UnprocessableEntity value)? unprocessableEntity, - TResult? Function(_UpgradeRequired value)? upgradeRequired, - TResult? Function(_ServerError value)? serverError, - TResult? Function(_ServiceUnavailable value)? serviceUnavailable, - TResult? Function(_GenericError value)? genericError, - }) { - return unprocessableEntity?.call(this); - } - - @override - @optionalTypeArgs - TResult maybeMap({ - TResult Function(_Ok value)? ok, - TResult Function(_OkNoContent value)? okNoContent, - TResult Function(_Unauthorized value)? unauthorized, - TResult Function(_Forbidden value)? forbidden, - TResult Function(_NotFound value)? notFound, - TResult Function(_UnprocessableEntity value)? unprocessableEntity, - TResult Function(_UpgradeRequired value)? upgradeRequired, - TResult Function(_ServerError value)? serverError, - TResult Function(_ServiceUnavailable value)? serviceUnavailable, - TResult Function(_GenericError value)? genericError, - required TResult orElse(), - }) { - if (unprocessableEntity != null) { - return unprocessableEntity(this); - } - return orElse(); - } -} - -abstract class _UnprocessableEntity implements NetworkResponse { - const factory _UnprocessableEntity( - {required final DioException error, - required final R response}) = _$UnprocessableEntityImpl; - - DioException get error; - R get response; - @JsonKey(ignore: true) - _$$UnprocessableEntityImplCopyWith> - get copyWith => throw _privateConstructorUsedError; -} - -/// @nodoc -abstract class _$$UpgradeRequiredImplCopyWith { - factory _$$UpgradeRequiredImplCopyWith(_$UpgradeRequiredImpl value, - $Res Function(_$UpgradeRequiredImpl) then) = - __$$UpgradeRequiredImplCopyWithImpl; - @useResult - $Res call({DioException error}); -} - -/// @nodoc -class __$$UpgradeRequiredImplCopyWithImpl - extends _$NetworkResponseCopyWithImpl> - implements _$$UpgradeRequiredImplCopyWith { - __$$UpgradeRequiredImplCopyWithImpl(_$UpgradeRequiredImpl _value, - $Res Function(_$UpgradeRequiredImpl) _then) - : super(_value, _then); - - @pragma('vm:prefer-inline') - @override - $Res call({ - Object? error = null, - }) { - return _then(_$UpgradeRequiredImpl( - null == error - ? _value.error - : error // ignore: cast_nullable_to_non_nullable - as DioException, - )); - } -} - -/// @nodoc - -class _$UpgradeRequiredImpl implements _UpgradeRequired { - const _$UpgradeRequiredImpl(this.error); - - @override - final DioException error; - - @override - String toString() { - return 'NetworkResponse<$R>.upgradeRequired(error: $error)'; - } - - @override - bool operator ==(Object other) { - return identical(this, other) || - (other.runtimeType == runtimeType && - other is _$UpgradeRequiredImpl && - (identical(other.error, error) || other.error == error)); - } - - @override - int get hashCode => Object.hash(runtimeType, error); - - @JsonKey(ignore: true) - @override - @pragma('vm:prefer-inline') - _$$UpgradeRequiredImplCopyWith> get copyWith => - __$$UpgradeRequiredImplCopyWithImpl>( - this, _$identity); - - @override - @optionalTypeArgs - TResult when({ - required TResult Function(R response) ok, - required TResult Function() okNoContent, - required TResult Function(DioException error) unauthorized, - required TResult Function(DioException error) forbidden, - required TResult Function(DioException error) notFound, - required TResult Function(DioException error, R response) - unprocessableEntity, - required TResult Function(DioException error) upgradeRequired, - required TResult Function(DioException error) serverError, - required TResult Function(DioException error) serviceUnavailable, - required TResult Function( - String message, bool isConnectionIssue, DioException? error) - genericError, - }) { - return upgradeRequired(error); - } - - @override - @optionalTypeArgs - TResult? whenOrNull({ - TResult? Function(R response)? ok, - TResult? Function()? okNoContent, - TResult? Function(DioException error)? unauthorized, - TResult? Function(DioException error)? forbidden, - TResult? Function(DioException error)? notFound, - TResult? Function(DioException error, R response)? unprocessableEntity, - TResult? Function(DioException error)? upgradeRequired, - TResult? Function(DioException error)? serverError, - TResult? Function(DioException error)? serviceUnavailable, - TResult? Function( - String message, bool isConnectionIssue, DioException? error)? - genericError, - }) { - return upgradeRequired?.call(error); - } - - @override - @optionalTypeArgs - TResult maybeWhen({ - TResult Function(R response)? ok, - TResult Function()? okNoContent, - TResult Function(DioException error)? unauthorized, - TResult Function(DioException error)? forbidden, - TResult Function(DioException error)? notFound, - TResult Function(DioException error, R response)? unprocessableEntity, - TResult Function(DioException error)? upgradeRequired, - TResult Function(DioException error)? serverError, - TResult Function(DioException error)? serviceUnavailable, - TResult Function( - String message, bool isConnectionIssue, DioException? error)? - genericError, - required TResult orElse(), - }) { - if (upgradeRequired != null) { - return upgradeRequired(error); - } - return orElse(); - } - - @override - @optionalTypeArgs - TResult map({ - required TResult Function(_Ok value) ok, - required TResult Function(_OkNoContent value) okNoContent, - required TResult Function(_Unauthorized value) unauthorized, - required TResult Function(_Forbidden value) forbidden, - required TResult Function(_NotFound value) notFound, - required TResult Function(_UnprocessableEntity value) - unprocessableEntity, - required TResult Function(_UpgradeRequired value) upgradeRequired, - required TResult Function(_ServerError value) serverError, - required TResult Function(_ServiceUnavailable value) serviceUnavailable, - required TResult Function(_GenericError value) genericError, - }) { - return upgradeRequired(this); - } - - @override - @optionalTypeArgs - TResult? mapOrNull({ - TResult? Function(_Ok value)? ok, - TResult? Function(_OkNoContent value)? okNoContent, - TResult? Function(_Unauthorized value)? unauthorized, - TResult? Function(_Forbidden value)? forbidden, - TResult? Function(_NotFound value)? notFound, - TResult? Function(_UnprocessableEntity value)? unprocessableEntity, - TResult? Function(_UpgradeRequired value)? upgradeRequired, - TResult? Function(_ServerError value)? serverError, - TResult? Function(_ServiceUnavailable value)? serviceUnavailable, - TResult? Function(_GenericError value)? genericError, - }) { - return upgradeRequired?.call(this); - } - - @override - @optionalTypeArgs - TResult maybeMap({ - TResult Function(_Ok value)? ok, - TResult Function(_OkNoContent value)? okNoContent, - TResult Function(_Unauthorized value)? unauthorized, - TResult Function(_Forbidden value)? forbidden, - TResult Function(_NotFound value)? notFound, - TResult Function(_UnprocessableEntity value)? unprocessableEntity, - TResult Function(_UpgradeRequired value)? upgradeRequired, - TResult Function(_ServerError value)? serverError, - TResult Function(_ServiceUnavailable value)? serviceUnavailable, - TResult Function(_GenericError value)? genericError, - required TResult orElse(), - }) { - if (upgradeRequired != null) { - return upgradeRequired(this); - } - return orElse(); - } -} - -abstract class _UpgradeRequired implements NetworkResponse { - const factory _UpgradeRequired(final DioException error) = - _$UpgradeRequiredImpl; - - DioException get error; - @JsonKey(ignore: true) - _$$UpgradeRequiredImplCopyWith> get copyWith => - throw _privateConstructorUsedError; -} - -/// @nodoc -abstract class _$$ServerErrorImplCopyWith { - factory _$$ServerErrorImplCopyWith(_$ServerErrorImpl value, - $Res Function(_$ServerErrorImpl) then) = - __$$ServerErrorImplCopyWithImpl; - @useResult - $Res call({DioException error}); -} - -/// @nodoc -class __$$ServerErrorImplCopyWithImpl - extends _$NetworkResponseCopyWithImpl> - implements _$$ServerErrorImplCopyWith { - __$$ServerErrorImplCopyWithImpl( - _$ServerErrorImpl _value, $Res Function(_$ServerErrorImpl) _then) - : super(_value, _then); - - @pragma('vm:prefer-inline') - @override - $Res call({ - Object? error = null, - }) { - return _then(_$ServerErrorImpl( - null == error - ? _value.error - : error // ignore: cast_nullable_to_non_nullable - as DioException, - )); - } -} - -/// @nodoc - -class _$ServerErrorImpl implements _ServerError { - const _$ServerErrorImpl(this.error); - - @override - final DioException error; - - @override - String toString() { - return 'NetworkResponse<$R>.serverError(error: $error)'; - } - - @override - bool operator ==(Object other) { - return identical(this, other) || - (other.runtimeType == runtimeType && - other is _$ServerErrorImpl && - (identical(other.error, error) || other.error == error)); - } - - @override - int get hashCode => Object.hash(runtimeType, error); - - @JsonKey(ignore: true) - @override - @pragma('vm:prefer-inline') - _$$ServerErrorImplCopyWith> get copyWith => - __$$ServerErrorImplCopyWithImpl>( - this, _$identity); - - @override - @optionalTypeArgs - TResult when({ - required TResult Function(R response) ok, - required TResult Function() okNoContent, - required TResult Function(DioException error) unauthorized, - required TResult Function(DioException error) forbidden, - required TResult Function(DioException error) notFound, - required TResult Function(DioException error, R response) - unprocessableEntity, - required TResult Function(DioException error) upgradeRequired, - required TResult Function(DioException error) serverError, - required TResult Function(DioException error) serviceUnavailable, - required TResult Function( - String message, bool isConnectionIssue, DioException? error) - genericError, - }) { - return serverError(error); - } - - @override - @optionalTypeArgs - TResult? whenOrNull({ - TResult? Function(R response)? ok, - TResult? Function()? okNoContent, - TResult? Function(DioException error)? unauthorized, - TResult? Function(DioException error)? forbidden, - TResult? Function(DioException error)? notFound, - TResult? Function(DioException error, R response)? unprocessableEntity, - TResult? Function(DioException error)? upgradeRequired, - TResult? Function(DioException error)? serverError, - TResult? Function(DioException error)? serviceUnavailable, - TResult? Function( - String message, bool isConnectionIssue, DioException? error)? - genericError, - }) { - return serverError?.call(error); - } - - @override - @optionalTypeArgs - TResult maybeWhen({ - TResult Function(R response)? ok, - TResult Function()? okNoContent, - TResult Function(DioException error)? unauthorized, - TResult Function(DioException error)? forbidden, - TResult Function(DioException error)? notFound, - TResult Function(DioException error, R response)? unprocessableEntity, - TResult Function(DioException error)? upgradeRequired, - TResult Function(DioException error)? serverError, - TResult Function(DioException error)? serviceUnavailable, - TResult Function( - String message, bool isConnectionIssue, DioException? error)? - genericError, - required TResult orElse(), - }) { - if (serverError != null) { - return serverError(error); - } - return orElse(); - } - - @override - @optionalTypeArgs - TResult map({ - required TResult Function(_Ok value) ok, - required TResult Function(_OkNoContent value) okNoContent, - required TResult Function(_Unauthorized value) unauthorized, - required TResult Function(_Forbidden value) forbidden, - required TResult Function(_NotFound value) notFound, - required TResult Function(_UnprocessableEntity value) - unprocessableEntity, - required TResult Function(_UpgradeRequired value) upgradeRequired, - required TResult Function(_ServerError value) serverError, - required TResult Function(_ServiceUnavailable value) serviceUnavailable, - required TResult Function(_GenericError value) genericError, - }) { - return serverError(this); - } - - @override - @optionalTypeArgs - TResult? mapOrNull({ - TResult? Function(_Ok value)? ok, - TResult? Function(_OkNoContent value)? okNoContent, - TResult? Function(_Unauthorized value)? unauthorized, - TResult? Function(_Forbidden value)? forbidden, - TResult? Function(_NotFound value)? notFound, - TResult? Function(_UnprocessableEntity value)? unprocessableEntity, - TResult? Function(_UpgradeRequired value)? upgradeRequired, - TResult? Function(_ServerError value)? serverError, - TResult? Function(_ServiceUnavailable value)? serviceUnavailable, - TResult? Function(_GenericError value)? genericError, - }) { - return serverError?.call(this); - } - - @override - @optionalTypeArgs - TResult maybeMap({ - TResult Function(_Ok value)? ok, - TResult Function(_OkNoContent value)? okNoContent, - TResult Function(_Unauthorized value)? unauthorized, - TResult Function(_Forbidden value)? forbidden, - TResult Function(_NotFound value)? notFound, - TResult Function(_UnprocessableEntity value)? unprocessableEntity, - TResult Function(_UpgradeRequired value)? upgradeRequired, - TResult Function(_ServerError value)? serverError, - TResult Function(_ServiceUnavailable value)? serviceUnavailable, - TResult Function(_GenericError value)? genericError, - required TResult orElse(), - }) { - if (serverError != null) { - return serverError(this); - } - return orElse(); - } -} - -abstract class _ServerError implements NetworkResponse { - const factory _ServerError(final DioException error) = _$ServerErrorImpl; - - DioException get error; - @JsonKey(ignore: true) - _$$ServerErrorImplCopyWith> get copyWith => - throw _privateConstructorUsedError; -} - -/// @nodoc -abstract class _$$ServiceUnavailableImplCopyWith { - factory _$$ServiceUnavailableImplCopyWith(_$ServiceUnavailableImpl value, - $Res Function(_$ServiceUnavailableImpl) then) = - __$$ServiceUnavailableImplCopyWithImpl; - @useResult - $Res call({DioException error}); -} - -/// @nodoc -class __$$ServiceUnavailableImplCopyWithImpl - extends _$NetworkResponseCopyWithImpl> - implements _$$ServiceUnavailableImplCopyWith { - __$$ServiceUnavailableImplCopyWithImpl(_$ServiceUnavailableImpl _value, - $Res Function(_$ServiceUnavailableImpl) _then) - : super(_value, _then); - - @pragma('vm:prefer-inline') - @override - $Res call({ - Object? error = null, - }) { - return _then(_$ServiceUnavailableImpl( - null == error - ? _value.error - : error // ignore: cast_nullable_to_non_nullable - as DioException, - )); - } -} - -/// @nodoc - -class _$ServiceUnavailableImpl implements _ServiceUnavailable { - const _$ServiceUnavailableImpl(this.error); - - @override - final DioException error; - - @override - String toString() { - return 'NetworkResponse<$R>.serviceUnavailable(error: $error)'; - } - - @override - bool operator ==(Object other) { - return identical(this, other) || - (other.runtimeType == runtimeType && - other is _$ServiceUnavailableImpl && - (identical(other.error, error) || other.error == error)); - } - - @override - int get hashCode => Object.hash(runtimeType, error); - - @JsonKey(ignore: true) - @override - @pragma('vm:prefer-inline') - _$$ServiceUnavailableImplCopyWith> - get copyWith => __$$ServiceUnavailableImplCopyWithImpl>(this, _$identity); - - @override - @optionalTypeArgs - TResult when({ - required TResult Function(R response) ok, - required TResult Function() okNoContent, - required TResult Function(DioException error) unauthorized, - required TResult Function(DioException error) forbidden, - required TResult Function(DioException error) notFound, - required TResult Function(DioException error, R response) - unprocessableEntity, - required TResult Function(DioException error) upgradeRequired, - required TResult Function(DioException error) serverError, - required TResult Function(DioException error) serviceUnavailable, - required TResult Function( - String message, bool isConnectionIssue, DioException? error) - genericError, - }) { - return serviceUnavailable(error); - } - - @override - @optionalTypeArgs - TResult? whenOrNull({ - TResult? Function(R response)? ok, - TResult? Function()? okNoContent, - TResult? Function(DioException error)? unauthorized, - TResult? Function(DioException error)? forbidden, - TResult? Function(DioException error)? notFound, - TResult? Function(DioException error, R response)? unprocessableEntity, - TResult? Function(DioException error)? upgradeRequired, - TResult? Function(DioException error)? serverError, - TResult? Function(DioException error)? serviceUnavailable, - TResult? Function( - String message, bool isConnectionIssue, DioException? error)? - genericError, - }) { - return serviceUnavailable?.call(error); - } - - @override - @optionalTypeArgs - TResult maybeWhen({ - TResult Function(R response)? ok, - TResult Function()? okNoContent, - TResult Function(DioException error)? unauthorized, - TResult Function(DioException error)? forbidden, - TResult Function(DioException error)? notFound, - TResult Function(DioException error, R response)? unprocessableEntity, - TResult Function(DioException error)? upgradeRequired, - TResult Function(DioException error)? serverError, - TResult Function(DioException error)? serviceUnavailable, - TResult Function( - String message, bool isConnectionIssue, DioException? error)? - genericError, - required TResult orElse(), - }) { - if (serviceUnavailable != null) { - return serviceUnavailable(error); - } - return orElse(); - } - - @override - @optionalTypeArgs - TResult map({ - required TResult Function(_Ok value) ok, - required TResult Function(_OkNoContent value) okNoContent, - required TResult Function(_Unauthorized value) unauthorized, - required TResult Function(_Forbidden value) forbidden, - required TResult Function(_NotFound value) notFound, - required TResult Function(_UnprocessableEntity value) - unprocessableEntity, - required TResult Function(_UpgradeRequired value) upgradeRequired, - required TResult Function(_ServerError value) serverError, - required TResult Function(_ServiceUnavailable value) serviceUnavailable, - required TResult Function(_GenericError value) genericError, - }) { - return serviceUnavailable(this); - } - - @override - @optionalTypeArgs - TResult? mapOrNull({ - TResult? Function(_Ok value)? ok, - TResult? Function(_OkNoContent value)? okNoContent, - TResult? Function(_Unauthorized value)? unauthorized, - TResult? Function(_Forbidden value)? forbidden, - TResult? Function(_NotFound value)? notFound, - TResult? Function(_UnprocessableEntity value)? unprocessableEntity, - TResult? Function(_UpgradeRequired value)? upgradeRequired, - TResult? Function(_ServerError value)? serverError, - TResult? Function(_ServiceUnavailable value)? serviceUnavailable, - TResult? Function(_GenericError value)? genericError, - }) { - return serviceUnavailable?.call(this); - } - - @override - @optionalTypeArgs - TResult maybeMap({ - TResult Function(_Ok value)? ok, - TResult Function(_OkNoContent value)? okNoContent, - TResult Function(_Unauthorized value)? unauthorized, - TResult Function(_Forbidden value)? forbidden, - TResult Function(_NotFound value)? notFound, - TResult Function(_UnprocessableEntity value)? unprocessableEntity, - TResult Function(_UpgradeRequired value)? upgradeRequired, - TResult Function(_ServerError value)? serverError, - TResult Function(_ServiceUnavailable value)? serviceUnavailable, - TResult Function(_GenericError value)? genericError, - required TResult orElse(), - }) { - if (serviceUnavailable != null) { - return serviceUnavailable(this); - } - return orElse(); - } -} - -abstract class _ServiceUnavailable implements NetworkResponse { - const factory _ServiceUnavailable(final DioException error) = - _$ServiceUnavailableImpl; - - DioException get error; - @JsonKey(ignore: true) - _$$ServiceUnavailableImplCopyWith> - get copyWith => throw _privateConstructorUsedError; -} - -/// @nodoc -abstract class _$$GenericErrorImplCopyWith { - factory _$$GenericErrorImplCopyWith(_$GenericErrorImpl value, - $Res Function(_$GenericErrorImpl) then) = - __$$GenericErrorImplCopyWithImpl; - @useResult - $Res call({String message, bool isConnectionIssue, DioException? error}); -} - -/// @nodoc -class __$$GenericErrorImplCopyWithImpl - extends _$NetworkResponseCopyWithImpl> - implements _$$GenericErrorImplCopyWith { - __$$GenericErrorImplCopyWithImpl( - _$GenericErrorImpl _value, $Res Function(_$GenericErrorImpl) _then) - : super(_value, _then); - - @pragma('vm:prefer-inline') - @override - $Res call({ - Object? message = null, - Object? isConnectionIssue = null, - Object? error = freezed, - }) { - return _then(_$GenericErrorImpl( - message: null == message - ? _value.message - : message // ignore: cast_nullable_to_non_nullable - as String, - isConnectionIssue: null == isConnectionIssue - ? _value.isConnectionIssue - : isConnectionIssue // ignore: cast_nullable_to_non_nullable - as bool, - error: freezed == error - ? _value.error - : error // ignore: cast_nullable_to_non_nullable - as DioException?, - )); - } -} - -/// @nodoc - -class _$GenericErrorImpl implements _GenericError { - const _$GenericErrorImpl( - {required this.message, required this.isConnectionIssue, this.error}); - - @override - final String message; - @override - final bool isConnectionIssue; - @override - final DioException? error; - - @override - String toString() { - return 'NetworkResponse<$R>.genericError(message: $message, isConnectionIssue: $isConnectionIssue, error: $error)'; - } - - @override - bool operator ==(Object other) { - return identical(this, other) || - (other.runtimeType == runtimeType && - other is _$GenericErrorImpl && - (identical(other.message, message) || other.message == message) && - (identical(other.isConnectionIssue, isConnectionIssue) || - other.isConnectionIssue == isConnectionIssue) && - (identical(other.error, error) || other.error == error)); - } - - @override - int get hashCode => - Object.hash(runtimeType, message, isConnectionIssue, error); - - @JsonKey(ignore: true) - @override - @pragma('vm:prefer-inline') - _$$GenericErrorImplCopyWith> get copyWith => - __$$GenericErrorImplCopyWithImpl>( - this, _$identity); - - @override - @optionalTypeArgs - TResult when({ - required TResult Function(R response) ok, - required TResult Function() okNoContent, - required TResult Function(DioException error) unauthorized, - required TResult Function(DioException error) forbidden, - required TResult Function(DioException error) notFound, - required TResult Function(DioException error, R response) - unprocessableEntity, - required TResult Function(DioException error) upgradeRequired, - required TResult Function(DioException error) serverError, - required TResult Function(DioException error) serviceUnavailable, - required TResult Function( - String message, bool isConnectionIssue, DioException? error) - genericError, - }) { - return genericError(message, isConnectionIssue, error); - } - - @override - @optionalTypeArgs - TResult? whenOrNull({ - TResult? Function(R response)? ok, - TResult? Function()? okNoContent, - TResult? Function(DioException error)? unauthorized, - TResult? Function(DioException error)? forbidden, - TResult? Function(DioException error)? notFound, - TResult? Function(DioException error, R response)? unprocessableEntity, - TResult? Function(DioException error)? upgradeRequired, - TResult? Function(DioException error)? serverError, - TResult? Function(DioException error)? serviceUnavailable, - TResult? Function( - String message, bool isConnectionIssue, DioException? error)? - genericError, - }) { - return genericError?.call(message, isConnectionIssue, error); - } - - @override - @optionalTypeArgs - TResult maybeWhen({ - TResult Function(R response)? ok, - TResult Function()? okNoContent, - TResult Function(DioException error)? unauthorized, - TResult Function(DioException error)? forbidden, - TResult Function(DioException error)? notFound, - TResult Function(DioException error, R response)? unprocessableEntity, - TResult Function(DioException error)? upgradeRequired, - TResult Function(DioException error)? serverError, - TResult Function(DioException error)? serviceUnavailable, - TResult Function( - String message, bool isConnectionIssue, DioException? error)? - genericError, - required TResult orElse(), - }) { - if (genericError != null) { - return genericError(message, isConnectionIssue, error); - } - return orElse(); - } - - @override - @optionalTypeArgs - TResult map({ - required TResult Function(_Ok value) ok, - required TResult Function(_OkNoContent value) okNoContent, - required TResult Function(_Unauthorized value) unauthorized, - required TResult Function(_Forbidden value) forbidden, - required TResult Function(_NotFound value) notFound, - required TResult Function(_UnprocessableEntity value) - unprocessableEntity, - required TResult Function(_UpgradeRequired value) upgradeRequired, - required TResult Function(_ServerError value) serverError, - required TResult Function(_ServiceUnavailable value) serviceUnavailable, - required TResult Function(_GenericError value) genericError, - }) { - return genericError(this); - } - - @override - @optionalTypeArgs - TResult? mapOrNull({ - TResult? Function(_Ok value)? ok, - TResult? Function(_OkNoContent value)? okNoContent, - TResult? Function(_Unauthorized value)? unauthorized, - TResult? Function(_Forbidden value)? forbidden, - TResult? Function(_NotFound value)? notFound, - TResult? Function(_UnprocessableEntity value)? unprocessableEntity, - TResult? Function(_UpgradeRequired value)? upgradeRequired, - TResult? Function(_ServerError value)? serverError, - TResult? Function(_ServiceUnavailable value)? serviceUnavailable, - TResult? Function(_GenericError value)? genericError, - }) { - return genericError?.call(this); - } - - @override - @optionalTypeArgs - TResult maybeMap({ - TResult Function(_Ok value)? ok, - TResult Function(_OkNoContent value)? okNoContent, - TResult Function(_Unauthorized value)? unauthorized, - TResult Function(_Forbidden value)? forbidden, - TResult Function(_NotFound value)? notFound, - TResult Function(_UnprocessableEntity value)? unprocessableEntity, - TResult Function(_UpgradeRequired value)? upgradeRequired, - TResult Function(_ServerError value)? serverError, - TResult Function(_ServiceUnavailable value)? serviceUnavailable, - TResult Function(_GenericError value)? genericError, - required TResult orElse(), - }) { - if (genericError != null) { - return genericError(this); - } - return orElse(); - } -} - -abstract class _GenericError implements NetworkResponse { - const factory _GenericError( - {required final String message, - required final bool isConnectionIssue, - final DioException? error}) = _$GenericErrorImpl; - - String get message; - bool get isConnectionIssue; - DioException? get error; - @JsonKey(ignore: true) - _$$GenericErrorImplCopyWith> get copyWith => - throw _privateConstructorUsedError; -} diff --git a/lib/src/sturdy_http.dart b/lib/src/sturdy_http.dart index 7f48491..32ab613 100644 --- a/lib/src/sturdy_http.dart +++ b/lib/src/sturdy_http.dart @@ -31,8 +31,7 @@ class SturdyHttp { final RetryBehavior _retryBehavior; /// The interceptors provided when this [SturdyHttp] was constructed. - UnmodifiableListView get interceptors => - UnmodifiableListView(_dio.interceptors); + UnmodifiableListView get interceptors => UnmodifiableListView(_dio.interceptors); /// The base URL of the underlying [Dio] instance. String get baseUrl => _dio.options.baseUrl; @@ -100,11 +99,11 @@ class SturdyHttp { /// If [onResponse] fails to produce an [M] and instead throws an [Exception], /// some known failure reasons are emitted via [SturdyHttpEvent]s and the /// [Exception] is re-thrown. - Future execute( + Future execute( NetworkRequest request, { - required M Function(NetworkResponse response) onResponse, + required M Function(NetworkResponse response) onResponse, }) async { - final responsePayload = await _handleRequest(request); + final responsePayload = await _handleRequest(request); try { return await _deserializer.deserialize( @@ -125,13 +124,13 @@ class SturdyHttp { } } - Future<_ResponsePayload> _handleRequest( + Future<_ResponsePayload> _handleRequest( NetworkRequest request, ) async { - Future<(Response?, NetworkResponse)> send( + Future<(Response?, NetworkResponse)> send( NetworkRequest request, ) async { - late final NetworkResponse resolvedResponse; + late final NetworkResponse resolvedResponse; Response? dioResponse; try { // By expecting `Object?` we allow for cases where an API will return @@ -154,59 +153,48 @@ class SturdyHttp { onSendProgress: request.onSendProgress, ); if (dioResponse.statusCode == 204) { - resolvedResponse = const NetworkResponse.okNoContent(); + resolvedResponse = const NoContent(); } else { final data = dioResponse.data; - if (data == null || data is! R) { - String buildErrorMessage() { - final messageSuffix = data == null - // Disallow empty responses when status code is non-204 - ? 'was null and status code was ${dioResponse!.statusCode}' - // Enforce that response data matches expected, otherwise we'll run into casting - // issues below - : 'was of type ${data.runtimeType} when it should have been of type $R'; - return 'Request to ${request.path} was successful but response data $messageSuffix'; - } - - resolvedResponse = NetworkResponse.genericError( - message: buildErrorMessage(), + if (data == null) { + resolvedResponse = GenericError( + message: 'Request to ${request.path} was successful but response data was null', isConnectionIssue: false, ); } else { - resolvedResponse = NetworkResponse.ok(data as R); + resolvedResponse = Ok(data); } } } on DioException catch (error) { switch (error.response?.statusCode) { case 401: await _onEvent(SturdyHttpEvent.authFailure(error.requestOptions)); - resolvedResponse = NetworkResponse.unauthorized(error); + resolvedResponse = Unauthorized(error: error); break; case 403: - resolvedResponse = NetworkResponse.forbidden(error); + resolvedResponse = Forbidden(error: error); break; case 404: - resolvedResponse = NetworkResponse.notFound(error); + resolvedResponse = NotFound(error: error); break; case 422: - resolvedResponse = NetworkResponse.unprocessableEntity( + resolvedResponse = UnprocessableEntity( error: error, - response: error.response?.data as R, + response: error.response?.data, ); break; case 426: - resolvedResponse = NetworkResponse.upgradeRequired(error); + resolvedResponse = UpgradeRequired(error: error); break; case 500: - resolvedResponse = NetworkResponse.serverError(error); + resolvedResponse = ServerError(error: error); break; case 503: - resolvedResponse = NetworkResponse.serviceUnavailable(error); + resolvedResponse = ServiceUnavailable(error: error); break; default: - resolvedResponse = NetworkResponse.genericError( - message: - 'Unexpected status code ${error.response?.statusCode} returned for ${request.path}', + resolvedResponse = GenericError( + message: 'Unexpected status code ${error.response?.statusCode} returned for ${request.path}', isConnectionIssue: error.isConnectionIssue(), error: error, ); @@ -227,8 +215,7 @@ class SturdyHttp { final retryBehavior = determineRetryBehavior(); var response = await send(request); var retryCount = 0; - while (!response.$2.isSuccess && - retryBehavior.shouldRetry(response.$1, retryCount)) { + while (!response.$2.isSuccess && retryBehavior.shouldRetry(response.$1, retryCount)) { // `retryBehavior` must be a `Retry`, otherwise we wouldn't be here. await Future.delayed((retryBehavior as Retry).retryInterval); retryCount++; @@ -241,7 +228,7 @@ class SturdyHttp { ); } - return _ResponsePayload( + return _ResponsePayload( request: request, dioResponse: response.$1, resolvedResponse: response.$2, @@ -249,10 +236,10 @@ class SturdyHttp { } } -class _ResponsePayload { +class _ResponsePayload { final NetworkRequest request; final Response? dioResponse; - final NetworkResponse resolvedResponse; + final NetworkResponse resolvedResponse; _ResponsePayload({ required this.request, @@ -271,9 +258,7 @@ Dio _configureDio({ }) { return Dio() // Instruct Dio to use the same Isolate approach as requested of SturdyHttp - ..transformer = deserializer is MainIsolateDeserializer - ? SyncTransformer() - : BackgroundTransformer() + ..transformer = deserializer is MainIsolateDeserializer ? SyncTransformer() : BackgroundTransformer() ..options.baseUrl = baseUrl ..options.listFormat = ListFormat.multiCompatible ..interceptors.addAll(interceptors) diff --git a/lib/src/sturdy_http_event_listener.dart b/lib/src/sturdy_http_event_listener.dart index 735199f..2e996b7 100644 --- a/lib/src/sturdy_http_event_listener.dart +++ b/lib/src/sturdy_http_event_listener.dart @@ -1,6 +1,5 @@ import 'package:dio/dio.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; -import 'package:sturdy_http/sturdy_http.dart'; part 'sturdy_http_event_listener.freezed.dart'; diff --git a/lib/src/sturdy_http_event_listener.freezed.dart b/lib/src/sturdy_http_event_listener.freezed.dart index 9a12143..dab4ba9 100644 --- a/lib/src/sturdy_http_event_listener.freezed.dart +++ b/lib/src/sturdy_http_event_listener.freezed.dart @@ -69,7 +69,9 @@ mixin _$SturdyHttpEvent { }) => throw _privateConstructorUsedError; - @JsonKey(ignore: true) + /// Create a copy of SturdyHttpEvent + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) $SturdyHttpEventCopyWith get copyWith => throw _privateConstructorUsedError; } @@ -93,6 +95,8 @@ class _$SturdyHttpEventCopyWithImpl<$Res, $Val extends SturdyHttpEvent> // ignore: unused_field final $Res Function($Val) _then; + /// Create a copy of SturdyHttpEvent + /// with the given fields replaced by the non-null parameter values. @pragma('vm:prefer-inline') @override $Res call({ @@ -127,6 +131,8 @@ class __$$JsonDecodingErrorImplCopyWithImpl<$Res> $Res Function(_$JsonDecodingErrorImpl) _then) : super(_value, _then); + /// Create a copy of SturdyHttpEvent + /// with the given fields replaced by the non-null parameter values. @pragma('vm:prefer-inline') @override $Res call({ @@ -183,7 +189,9 @@ class _$JsonDecodingErrorImpl implements _JsonDecodingError { @override int get hashCode => Object.hash(runtimeType, request, exception, stackTrace); - @JsonKey(ignore: true) + /// Create a copy of SturdyHttpEvent + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) @override @pragma('vm:prefer-inline') _$$JsonDecodingErrorImplCopyWith<_$JsonDecodingErrorImpl> get copyWith => @@ -276,8 +284,11 @@ abstract class _JsonDecodingError implements SturdyHttpEvent { RequestOptions get request; Exception get exception; StackTrace? get stackTrace; + + /// Create a copy of SturdyHttpEvent + /// with the given fields replaced by the non-null parameter values. @override - @JsonKey(ignore: true) + @JsonKey(includeFromJson: false, includeToJson: false) _$$JsonDecodingErrorImplCopyWith<_$JsonDecodingErrorImpl> get copyWith => throw _privateConstructorUsedError; } @@ -301,6 +312,8 @@ class __$$AuthFailureImplCopyWithImpl<$Res> _$AuthFailureImpl _value, $Res Function(_$AuthFailureImpl) _then) : super(_value, _then); + /// Create a copy of SturdyHttpEvent + /// with the given fields replaced by the non-null parameter values. @pragma('vm:prefer-inline') @override $Res call({ @@ -339,7 +352,9 @@ class _$AuthFailureImpl implements _AuthFailure { @override int get hashCode => Object.hash(runtimeType, request); - @JsonKey(ignore: true) + /// Create a copy of SturdyHttpEvent + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) @override @pragma('vm:prefer-inline') _$$AuthFailureImplCopyWith<_$AuthFailureImpl> get copyWith => @@ -426,8 +441,11 @@ abstract class _AuthFailure implements SturdyHttpEvent { @override RequestOptions get request; + + /// Create a copy of SturdyHttpEvent + /// with the given fields replaced by the non-null parameter values. @override - @JsonKey(ignore: true) + @JsonKey(includeFromJson: false, includeToJson: false) _$$AuthFailureImplCopyWith<_$AuthFailureImpl> get copyWith => throw _privateConstructorUsedError; } @@ -453,6 +471,8 @@ class __$$OnMutativeRequestSuccessImplCopyWithImpl<$Res> $Res Function(_$OnMutativeRequestSuccessImpl) _then) : super(_value, _then); + /// Create a copy of SturdyHttpEvent + /// with the given fields replaced by the non-null parameter values. @pragma('vm:prefer-inline') @override $Res call({ @@ -491,7 +511,9 @@ class _$OnMutativeRequestSuccessImpl implements _OnMutativeRequestSuccess { @override int get hashCode => Object.hash(runtimeType, request); - @JsonKey(ignore: true) + /// Create a copy of SturdyHttpEvent + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) @override @pragma('vm:prefer-inline') _$$OnMutativeRequestSuccessImplCopyWith<_$OnMutativeRequestSuccessImpl> @@ -580,8 +602,11 @@ abstract class _OnMutativeRequestSuccess implements SturdyHttpEvent { @override RequestOptions get request; + + /// Create a copy of SturdyHttpEvent + /// with the given fields replaced by the non-null parameter values. @override - @JsonKey(ignore: true) + @JsonKey(includeFromJson: false, includeToJson: false) _$$OnMutativeRequestSuccessImplCopyWith<_$OnMutativeRequestSuccessImpl> get copyWith => throw _privateConstructorUsedError; } diff --git a/pubspec.yaml b/pubspec.yaml index 2ce21af..00c2823 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: sturdy_http description: A strongly typed, event-based, reliable HTTP client that wraps `Dio`. -version: 0.4.0 +version: 0.5.0-dev1 homepage: https://github.com/Betterment/sturdy_http repository: https://github.com/Betterment/sturdy_http diff --git a/test/src/deserializer_test.dart b/test/src/deserializer_test.dart index f9452a8..326b952 100644 --- a/test/src/deserializer_test.dart +++ b/test/src/deserializer_test.dart @@ -10,9 +10,8 @@ import 'sturdy_http_test.dart'; void main() { group('BackgroundDeserializer', () { - test('it invokes onResponse on a non-main Isolate and sends result back', - () async { - onResponse(NetworkResponse response) { + test('it invokes onResponse on a non-main Isolate and sends result back', () async { + onResponse(NetworkResponse response) { final isolateName = Isolate.current.debugName; // Hijack `Foo` to send over the `IsolateName` since // isolates don't share memory (so we can't set a late @@ -21,7 +20,7 @@ void main() { return Foo(message: isolateName!); } - final response = NetworkResponse.ok(const Foo(message: '--').toJson()); + final response = Ok(const Foo(message: '--').toJson()); final subject = BackgroundDeserializer(); final result = await subject.deserialize( response: response, @@ -31,15 +30,15 @@ void main() { }); test('it handles multiple requests for deserialization', () async { - onResponse(NetworkResponse response) { - return response.maybeWhen( - ok: Foo.fromJson, - orElse: () => fail('orElse not expected'), - ); + Foo onResponse(NetworkResponse response) { + return switch (response) { + Ok(:final response) => Foo.fromJson(response), + _ => fail('Not expected: orElse'), + }; } - final responseOne = NetworkResponse.ok(const Foo(message: '1').toJson()); - final responseTwo = NetworkResponse.ok(const Foo(message: '2').toJson()); + final responseOne = Ok(const Foo(message: '1').toJson()); + final responseTwo = Ok(const Foo(message: '2').toJson()); final subject = BackgroundDeserializer(); final resultOne = await subject.deserialize( response: responseOne, @@ -53,19 +52,15 @@ void main() { expect(resultTwo.message, '2'); }); - test( - 'it throws CheckedFromJsonExceptions when deserialization issues occur', - () async { - onResponse(NetworkResponse response) { - return response.maybeWhen( - // Attempt to deserialize will fail because the response - // payload is a `Foo`, not a `NotFoo` - ok: NotFoo.fromJson, - orElse: () => fail('orElse not expected'), - ); + test('it throws CheckedFromJsonExceptions when deserialization issues occur', () async { + onResponse(NetworkResponse response) { + return switch (response) { + Ok(:final response) => NotFoo.fromJson(response), + _ => fail('orElse not expected'), + }; } - final response = NetworkResponse.ok(const Foo(message: 'Nope').toJson()); + final response = Ok(const Foo(message: 'Nope').toJson()); final subject = BackgroundDeserializer(); try { await subject.deserialize( diff --git a/test/src/sturdy_http_test.dart b/test/src/sturdy_http_test.dart index 9136678..5cb9d67 100644 --- a/test/src/sturdy_http_test.dart +++ b/test/src/sturdy_http_test.dart @@ -73,8 +73,7 @@ void main() { interceptors: [ _FakeInterceptor( onRequestInvoked: (options) { - contentType = - options.headers[Headers.contentTypeHeader] as String?; + contentType = options.headers[Headers.contentTypeHeader] as String?; }, ), ], @@ -97,8 +96,7 @@ void main() { interceptors: [ _FakeInterceptor( onRequestInvoked: (options) { - contentType = - options.headers[Headers.contentTypeHeader] as String?; + contentType = options.headers[Headers.contentTypeHeader] as String?; }, ), ], @@ -133,9 +131,7 @@ void main() { }); group('withBaseUrl', () { - test( - 'it returns a new instance with correct baseUrl and pre-configured settings', - () { + test('it returns a new instance with correct baseUrl and pre-configured settings', () { final oldInstance = buildSubject( interceptors: [_FakeInterceptor()], ); @@ -168,13 +164,13 @@ void main() { group('data', () { group('when NetworkRequestBody is empty', () { test('request options contain null data', () async { - await buildSubject().execute( + await buildSubject().execute( const GetRequest('/foo'), onResponse: (response) { - return response.maybeWhen( - ok: (response) => true, - orElse: () => false, - ); + return switch (response) { + Ok() => true, + _ => false, + }; }, ); expect(options.data, isNull); @@ -183,7 +179,7 @@ void main() { group('when NetworkRequestBody is json', () { test('request options contain json data', () async { - await buildSubject().execute( + await buildSubject().execute( const GetRequest( '/foo', data: NetworkRequestBody.json( @@ -193,7 +189,7 @@ void main() { ), ), onResponse: (response) { - return response.maybeWhen(orElse: () => null); + return null; }, ); expect( @@ -207,7 +203,7 @@ void main() { group('when manual options are provided', () { test('request options contain manually provided values', () async { - await buildSubject().execute( + await buildSubject().execute( GetRequest( '/foo', data: NetworkRequestBody.json( @@ -218,7 +214,7 @@ void main() { options: Options(extra: {'foo': 'bar'}), ), onResponse: (response) { - return response.maybeWhen(orElse: () => null); + return null; }, ); expect( @@ -233,14 +229,12 @@ void main() { group('queryParameters', () { test('request options contain correct query parameters ', () async { - await buildSubject().execute( + await buildSubject().execute( const GetRequest( '/foo', queryParameters: {'foo': 'bar'}, ), - onResponse: (response) => response.maybeWhen( - orElse: () => null, - ), + onResponse: (response) {}, ); expect( options.queryParameters, @@ -253,21 +247,17 @@ void main() { group('method', () { test('request options contain correct method ', () async { - await buildSubject().execute( + await buildSubject().execute( const GetRequest('/foo'), - onResponse: (response) => response.maybeWhen( - orElse: () => null, - ), + onResponse: (response) {}, ); expect(options.method, 'GET'); - await buildSubject().execute( + await buildSubject().execute( const PostRequest( '/bar', data: NetworkRequestBody.empty(), ), - onResponse: (response) => response.maybeWhen( - orElse: () => null, - ), + onResponse: (response) {}, ); expect(options.method, 'POST'); }); @@ -300,15 +290,13 @@ void main() { group('when deserialization succeeds', () { test('it returns parsed model', () async { - final response = - await buildSubject().execute>( + final response = await buildSubject().execute>( const GetRequest('/foo'), onResponse: (response) { - return response.maybeWhen( - ok: (json) => Result.success(Foo.fromJson(json)), - orElse: () => - const Result.failure('Not expected: orElse'), - ); + return switch (response) { + Ok(:final response) => Result.success(Foo.fromJson(response)), + _ => const Result.failure('Not expected: orElse'), + }; }, ); @@ -320,18 +308,14 @@ void main() { }); group('when deserialization fails', () { - test( - 'it emits a decodingError event and rethrows the Exception', - () async { - final request = - buildSubject().execute>( + test('it emits a decodingError event and rethrows the Exception', () async { + final request = buildSubject().execute>( const GetRequest('/not-foo'), onResponse: (response) { - return response.maybeWhen( - ok: (json) => Result.success(Foo.fromJson(json)), - orElse: () => - const Result.failure('Not expected: orElse'), - ); + return switch (response) { + Ok(:final response) => Result.success(Foo.fromJson(response)), + _ => const Result.failure('Not expected: orElse'), + }; }, ); @@ -360,18 +344,16 @@ void main() { ); }); test('it returns okNoContent', () async { - final response = - await buildSubject().execute>( + final response = await buildSubject().execute>( const PostRequest( '/foo', data: NetworkRequestBody.empty(), ), onResponse: (response) { - return response.maybeWhen( - okNoContent: () => const Result.success(true), - orElse: () => - const Result.failure('Not expected: orElse'), - ); + return switch (response) { + NoContent() => const Result.success(true), + _ => const Result.failure('Not expected: orElse'), + }; }, ); @@ -395,21 +377,21 @@ void main() { test( 'it returns genericError and isConnectionIssue is false', () async { - final response = await buildSubject() - .execute>( + final response = await buildSubject().execute>( const PostRequest( '/foo', data: NetworkRequestBody.empty(), ), onResponse: (response) { - return response.maybeWhen( - genericError: (message, isConnectionIssue, error) { - expect(isConnectionIssue, isFalse); - return const Result.success(true); - }, - orElse: () => - const Result.failure('Not expected: orElse'), - ); + return switch (response) { + GenericError(:final isConnectionIssue) => () { + { + expect(isConnectionIssue, isFalse); + return const Result.success(true); + } + }(), + _ => const Result.failure('Not expected: orElse'), + }; }, ); @@ -451,48 +433,43 @@ void main() { ); }); - test( - 'it emits a MutativeRequestSuccess event with correct path', - () async { + test('it emits a MutativeRequestSuccess event with correct path', () async { final subject = buildSubject(); await Future.wait([ - subject.execute>( + subject.execute>( const PostRequest( '/foo', data: NetworkRequestBody.empty(), ), onResponse: (response) { - return response.maybeWhen( - ok: (json) => Result.success(json['foo'] as String), - orElse: () => - const Result.failure('Not expected: orElse'), - ); + return switch (response) { + Ok(:final response) => Result.success(response['foo'] as String), + _ => const Result.failure('Not expected: orElse'), + }; }, ), - subject.execute>( + subject.execute>( const PutRequest( '/bar', data: NetworkRequestBody.empty(), ), onResponse: (response) { - return response.maybeWhen( - ok: (json) => Result.success(json['foo'] as String), - orElse: () => - const Result.failure('Not expected: orElse'), - ); + return switch (response) { + Ok(:final response) => Result.success(response['foo'] as String), + _ => const Result.failure('Not expected: orElse'), + }; }, ), - subject.execute>( + subject.execute>( const DeleteRequest( '/baz', data: NetworkRequestBody.empty(), ), onResponse: (response) { - return response.maybeWhen( - ok: (json) => Result.success(json['foo'] as String), - orElse: () => - const Result.failure('Not expected: orElse'), - ); + return switch (response) { + Ok(:final response) => Result.success(response['foo'] as String), + _ => const Result.failure('Not expected: orElse'), + }; }, ), ]); @@ -512,8 +489,7 @@ void main() { }); }); - group('and the response has status codes other than 200 or 204', - () { + group('and the response has status codes other than 200 or 204', () { setUp(() { charlatan ..whenPost( @@ -539,47 +515,43 @@ void main() { ); }); - test('it does not emit a MutativeRequestSuccess event', - () async { + test('it does not emit a MutativeRequestSuccess event', () async { final subject = buildSubject(); await Future.wait([ - subject.execute>( + subject.execute>( const PostRequest( '/foo', data: NetworkRequestBody.empty(), ), onResponse: (response) { - return response.maybeWhen( - ok: (json) => Result.success(json['foo'] as String), - orElse: () => - const Result.failure('Not expected: orElse'), - ); + return switch (response) { + Ok(:final response) => Result.success(response['foo'] as String), + _ => const Result.failure('Not expected: orElse'), + }; }, ), - subject.execute>( + subject.execute>( const PutRequest( '/bar', data: NetworkRequestBody.empty(), ), onResponse: (response) { - return response.maybeWhen( - ok: (json) => Result.success(json['foo'] as String), - orElse: () => - const Result.failure('Not expected: orElse'), - ); + return switch (response) { + Ok(:final response) => Result.success(response['foo'] as String), + _ => const Result.failure('Not expected: orElse'), + }; }, ), - subject.execute>( + subject.execute>( const DeleteRequest( '/baz', data: NetworkRequestBody.empty(), ), onResponse: (response) { - return response.maybeWhen( - ok: (json) => Result.success(json['foo'] as String), - orElse: () => - const Result.failure('Not expected: orElse'), - ); + return switch (response) { + Ok(:final response) => Result.success(response['foo'] as String), + _ => const Result.failure('Not expected: orElse'), + }; }, ), ]); @@ -612,16 +584,14 @@ void main() { setupErrorResponse(statusCode: 401); }); - test('it emits an authFailure event and invokes unauthorized', - () async { - final response = - await buildSubject().execute>( + test('it emits an authFailure event and invokes unauthorized', () async { + final response = await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { - return response.maybeMap( - unauthorized: (request) => const Result.success(true), - orElse: () => const Result.failure('Not expected: orElse'), - ); + return switch (response) { + Unauthorized() => const Result.success(true), + _ => const Result.failure('Not expected: orElse'), + }; }, ); @@ -640,14 +610,13 @@ void main() { }); test('it returns forbidden', () async { - final response = - await buildSubject().execute>( + final response = await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { - return response.maybeWhen( - forbidden: (request) => const Result.success(true), - orElse: () => const Result.failure('Not expected: orElse'), - ); + return switch (response) { + Forbidden() => const Result.success(true), + _ => const Result.failure('Not expected: orElse'), + }; }, ); @@ -664,14 +633,13 @@ void main() { }); test('it returns notFound', () async { - final response = - await buildSubject().execute>( + final response = await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { - return response.maybeWhen( - notFound: (request) => const Result.success(true), - orElse: () => const Result.failure('Not expected: orElse'), - ); + return switch (response) { + NotFound() => const Result.success(true), + _ => const Result.failure('Not expected: orElse'), + }; }, ); @@ -691,15 +659,13 @@ void main() { }); test('it returns unprocessableEntity', () async { - final response = - await buildSubject().execute>( + final response = await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { - return response.maybeWhen( - unprocessableEntity: (error, response) => - const Result.success(true), - orElse: () => const Result.failure('Not expected: orElse'), - ); + return switch (response) { + UnprocessableEntity() => const Result.success(true), + _ => const Result.failure('Not expected: orElse'), + }; }, ); @@ -719,14 +685,13 @@ void main() { }); test('it returns upgradeRequired', () async { - final response = - await buildSubject().execute>( + final response = await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { - return response.maybeWhen( - upgradeRequired: (error) => const Result.success(true), - orElse: () => const Result.failure('Not expected: orElse'), - ); + return switch (response) { + UpgradeRequired() => const Result.success(true), + _ => const Result.failure('Not expected: orElse'), + }; }, ); @@ -743,14 +708,13 @@ void main() { }); test('it returns serverError', () async { - final response = - await buildSubject().execute>( + final response = await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { - return response.maybeWhen( - serverError: (request) => const Result.success(true), - orElse: () => const Result.failure('Not expected: orElse'), - ); + return switch (response) { + ServerError() => const Result.success(true), + _ => const Result.failure('Not expected: orElse'), + }; }, ); @@ -767,14 +731,13 @@ void main() { }); test('it returns service unavailable', () async { - final response = - await buildSubject().execute>( + final response = await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { - return response.maybeWhen( - serviceUnavailable: (request) => const Result.success(true), - orElse: () => const Result.failure('Not expected: orElse'), - ); + return switch (response) { + ServiceUnavailable() => const Result.success(true), + _ => const Result.failure('Not expected: orElse'), + }; }, ); @@ -791,15 +754,13 @@ void main() { }); test('it returns genericError', () async { - final response = - await buildSubject().execute>( + final response = await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { - return response.maybeWhen( - genericError: (message, _, error) => - const Result.success(true), - orElse: () => const Result.failure('Not expected: orElse'), - ); + return switch (response) { + GenericError() => const Result.success(true), + _ => const Result.failure('Not expected: orElse'), + }; }, ); @@ -818,19 +779,19 @@ void main() { ); }); - test('it returns genericError and isConnectionIssue is true', - () async { - final response = - await buildSubject().execute>( + test('it returns genericError and isConnectionIssue is true', () async { + final response = await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { - return response.maybeWhen( - genericError: (message, isConnectionIssue, error) { - expect(isConnectionIssue, isTrue); - return const Result.success(true); - }, - orElse: () => const Result.failure('Not expected: orElse'), - ); + return switch (response) { + GenericError(:final isConnectionIssue) => () { + { + expect(isConnectionIssue, isTrue); + return const Result.success(true); + } + }(), + _ => const Result.failure('Not expected: orElse'), + }; }, ); @@ -853,8 +814,7 @@ void main() { }, ); - final response = - await buildSubject().execute>( + final response = await buildSubject().execute>( const GetRequest( defaultPath, retryBehavior: Retry( @@ -863,13 +823,10 @@ void main() { ), ), onResponse: (response) { - return response.maybeWhen( - genericError: (_, __, ___) { - return const Result.success(true); - }, - orElse: () => - const Result.failure('Not expected: orElse'), - ); + return switch (response) { + GenericError() => const Result.success(true), + _ => const Result.failure('Not expected: orElse'), + }; }, ); @@ -896,20 +853,16 @@ void main() { }, ); - final response = - await buildSubject().execute>( + final response = await buildSubject().execute>( const GetRequest( defaultPath, retryBehavior: NeverRetry(), ), onResponse: (response) { - return response.maybeWhen( - genericError: (_, __, ___) { - return const Result.success(true); - }, - orElse: () => - const Result.failure('Not expected: orElse'), - ); + return switch (response) { + GenericError() => const Result.success(true), + _ => const Result.failure('Not expected: orElse'), + }; }, ); @@ -937,7 +890,7 @@ void main() { final response = await buildSubject( retryBehavior: NeverRetry(), - ).execute>( + ).execute>( const GetRequest( defaultPath, retryBehavior: Retry( @@ -946,13 +899,10 @@ void main() { ), ), onResponse: (response) { - return response.maybeWhen( - genericError: (_, __, ___) { - return const Result.success(true); - }, - orElse: () => - const Result.failure('Not expected: orElse'), - ); + return switch (response) { + GenericError() => const Result.success(true), + _ => const Result.failure('Not expected: orElse'), + }; }, ); @@ -981,7 +931,7 @@ void main() { final response = await buildSubject( retryBehavior: NeverRetry(), - ).execute>( + ).execute>( GetRequest( defaultPath, retryBehavior: Retry( @@ -994,13 +944,10 @@ void main() { ), ), onResponse: (response) { - return response.maybeWhen( - genericError: (_, __, ___) { - return const Result.success(true); - }, - orElse: () => - const Result.failure('Not expected: orElse'), - ); + return switch (response) { + GenericError() => const Result.success(true), + _ => const Result.failure('Not expected: orElse'), + }; }, ); diff --git a/test/src/sturdy_http_test.freezed.dart b/test/src/sturdy_http_test.freezed.dart index 14704cd..c0d3331 100644 --- a/test/src/sturdy_http_test.freezed.dart +++ b/test/src/sturdy_http_test.freezed.dart @@ -72,6 +72,9 @@ class _$ResultCopyWithImpl> final $Val _value; // ignore: unused_field final $Res Function($Val) _then; + + /// Create a copy of Result + /// with the given fields replaced by the non-null parameter values. } /// @nodoc @@ -91,6 +94,8 @@ class __$$SuccessImplCopyWithImpl _$SuccessImpl _value, $Res Function(_$SuccessImpl) _then) : super(_value, _then); + /// Create a copy of Result + /// with the given fields replaced by the non-null parameter values. @pragma('vm:prefer-inline') @override $Res call({ @@ -130,7 +135,9 @@ class _$SuccessImpl implements _Success { int get hashCode => Object.hash(runtimeType, const DeepCollectionEquality().hash(success)); - @JsonKey(ignore: true) + /// Create a copy of Result + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) @override @pragma('vm:prefer-inline') _$$SuccessImplCopyWith> get copyWith => @@ -203,7 +210,10 @@ abstract class _Success implements Result { const factory _Success(final S success) = _$SuccessImpl; S get success; - @JsonKey(ignore: true) + + /// Create a copy of Result + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) _$$SuccessImplCopyWith> get copyWith => throw _privateConstructorUsedError; } @@ -225,6 +235,8 @@ class __$$FailureImplCopyWithImpl _$FailureImpl _value, $Res Function(_$FailureImpl) _then) : super(_value, _then); + /// Create a copy of Result + /// with the given fields replaced by the non-null parameter values. @pragma('vm:prefer-inline') @override $Res call({ @@ -264,7 +276,9 @@ class _$FailureImpl implements _Failure { int get hashCode => Object.hash(runtimeType, const DeepCollectionEquality().hash(failure)); - @JsonKey(ignore: true) + /// Create a copy of Result + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) @override @pragma('vm:prefer-inline') _$$FailureImplCopyWith> get copyWith => @@ -337,7 +351,10 @@ abstract class _Failure implements Result { const factory _Failure(final F failure) = _$FailureImpl; F get failure; - @JsonKey(ignore: true) + + /// Create a copy of Result + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) _$$FailureImplCopyWith> get copyWith => throw _privateConstructorUsedError; } @@ -350,8 +367,12 @@ Foo _$FooFromJson(Map json) { mixin _$Foo { String get message => throw _privateConstructorUsedError; + /// Serializes this Foo to a JSON map. Map toJson() => throw _privateConstructorUsedError; - @JsonKey(ignore: true) + + /// Create a copy of Foo + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) $FooCopyWith get copyWith => throw _privateConstructorUsedError; } @@ -372,6 +393,8 @@ class _$FooCopyWithImpl<$Res, $Val extends Foo> implements $FooCopyWith<$Res> { // ignore: unused_field final $Res Function($Val) _then; + /// Create a copy of Foo + /// with the given fields replaced by the non-null parameter values. @pragma('vm:prefer-inline') @override $Res call({ @@ -401,6 +424,8 @@ class __$$FooImplCopyWithImpl<$Res> extends _$FooCopyWithImpl<$Res, _$FooImpl> __$$FooImplCopyWithImpl(_$FooImpl _value, $Res Function(_$FooImpl) _then) : super(_value, _then); + /// Create a copy of Foo + /// with the given fields replaced by the non-null parameter values. @pragma('vm:prefer-inline') @override $Res call({ @@ -439,11 +464,13 @@ class _$FooImpl implements _Foo { (identical(other.message, message) || other.message == message)); } - @JsonKey(ignore: true) + @JsonKey(includeFromJson: false, includeToJson: false) @override int get hashCode => Object.hash(runtimeType, message); - @JsonKey(ignore: true) + /// Create a copy of Foo + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) @override @pragma('vm:prefer-inline') _$$FooImplCopyWith<_$FooImpl> get copyWith => @@ -464,8 +491,11 @@ abstract class _Foo implements Foo { @override String get message; + + /// Create a copy of Foo + /// with the given fields replaced by the non-null parameter values. @override - @JsonKey(ignore: true) + @JsonKey(includeFromJson: false, includeToJson: false) _$$FooImplCopyWith<_$FooImpl> get copyWith => throw _privateConstructorUsedError; } @@ -478,8 +508,12 @@ NotFoo _$NotFooFromJson(Map json) { mixin _$NotFoo { String get notMessage => throw _privateConstructorUsedError; + /// Serializes this NotFoo to a JSON map. Map toJson() => throw _privateConstructorUsedError; - @JsonKey(ignore: true) + + /// Create a copy of NotFoo + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) $NotFooCopyWith get copyWith => throw _privateConstructorUsedError; } @@ -501,6 +535,8 @@ class _$NotFooCopyWithImpl<$Res, $Val extends NotFoo> // ignore: unused_field final $Res Function($Val) _then; + /// Create a copy of NotFoo + /// with the given fields replaced by the non-null parameter values. @pragma('vm:prefer-inline') @override $Res call({ @@ -533,6 +569,8 @@ class __$$NotFooImplCopyWithImpl<$Res> _$NotFooImpl _value, $Res Function(_$NotFooImpl) _then) : super(_value, _then); + /// Create a copy of NotFoo + /// with the given fields replaced by the non-null parameter values. @pragma('vm:prefer-inline') @override $Res call({ @@ -572,11 +610,13 @@ class _$NotFooImpl implements _NotFoo { other.notMessage == notMessage)); } - @JsonKey(ignore: true) + @JsonKey(includeFromJson: false, includeToJson: false) @override int get hashCode => Object.hash(runtimeType, notMessage); - @JsonKey(ignore: true) + /// Create a copy of NotFoo + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) @override @pragma('vm:prefer-inline') _$$NotFooImplCopyWith<_$NotFooImpl> get copyWith => @@ -597,8 +637,11 @@ abstract class _NotFoo implements NotFoo { @override String get notMessage; + + /// Create a copy of NotFoo + /// with the given fields replaced by the non-null parameter values. @override - @JsonKey(ignore: true) + @JsonKey(includeFromJson: false, includeToJson: false) _$$NotFooImplCopyWith<_$NotFooImpl> get copyWith => throw _privateConstructorUsedError; } From 7595a86784ac75b4254e74ff45775f94db3eb08d Mon Sep 17 00:00:00 2001 From: Brandon Trautmann Date: Wed, 4 Sep 2024 09:40:01 -0400 Subject: [PATCH 02/10] add type back to top level response types --- example/main.dart | 4 +-- lib/src/deserializer.dart | 18 +++++----- lib/src/network_response.dart | 39 +++++++++++----------- lib/src/sturdy_http.dart | 36 ++++++++++++-------- test/src/deserializer_test.dart | 6 ++-- test/src/sturdy_http_test.dart | 58 ++++++++++++++++----------------- 6 files changed, 86 insertions(+), 75 deletions(-) diff --git a/example/main.dart b/example/main.dart index 3fd3ac4..c1b5c70 100644 --- a/example/main.dart +++ b/example/main.dart @@ -23,7 +23,7 @@ void main(List args) async { ); // A GetRequest. Prints 'Hello World!'. - await client.execute( + await client.execute( GetRequest('/foo'), onResponse: (r) { return switch (r) { @@ -38,7 +38,7 @@ void main(List args) async { // Prints: // 'mutative request success' <-- From ExampleEventListener // 'success!' - await client.execute( + await client.execute( PostRequest('/foo', data: NetworkRequestBody.empty()), onResponse: (r) { return switch (r) { diff --git a/lib/src/deserializer.dart b/lib/src/deserializer.dart index 93264ab..c473468 100644 --- a/lib/src/deserializer.dart +++ b/lib/src/deserializer.dart @@ -9,9 +9,9 @@ part of 'sturdy_http.dart'; /// {@endtemplate} abstract class Deserializer { /// {@macro deserializer} - Future deserialize({ - required NetworkResponse response, - required M Function(NetworkResponse) onResponse, + Future deserialize({ + required NetworkResponse response, + required M Function(NetworkResponse) onResponse, }); } @@ -25,9 +25,9 @@ class MainIsolateDeserializer implements Deserializer { /// {@macro main_isolate_deserializer} const MainIsolateDeserializer(); @override - Future deserialize({ - required NetworkResponse response, - required M Function(NetworkResponse response) onResponse, + Future deserialize({ + required NetworkResponse response, + required M Function(NetworkResponse response) onResponse, }) async { return onResponse(response); } @@ -57,9 +57,9 @@ class BackgroundDeserializer implements Deserializer { Stream? _results; @override - Future deserialize({ - required NetworkResponse response, - required M Function(NetworkResponse response) onResponse, + Future deserialize({ + required NetworkResponse response, + required M Function(NetworkResponse response) onResponse, }) async { await _initializeOnFirstRequest(); onResponseFunction() => onResponse(response); diff --git a/lib/src/network_response.dart b/lib/src/network_response.dart index 8d6ccaa..a27194c 100644 --- a/lib/src/network_response.dart +++ b/lib/src/network_response.dart @@ -1,3 +1,5 @@ +// ignore_for_file: public_member_api_docs + import 'package:dio/dio.dart'; /// The produced object after [SturdyHttp] processes a [NetworkRequest]. @@ -8,72 +10,72 @@ import 'package:dio/dio.dart'; /// Most often the call sites executing a [NetworkRequest] will only be interested /// in a select few of these, and will resolve their error cases via a `maybeWhen` /// using the `orElse` clause. -sealed class NetworkResponse { +sealed class NetworkResponse { const NetworkResponse(); } -sealed class NetworkResponseSuccess extends NetworkResponse { +sealed class NetworkResponseSuccess extends NetworkResponse { const NetworkResponseSuccess(); } -final class NoContent extends NetworkResponseSuccess { +final class NoContent extends NetworkResponseSuccess { const NoContent(); } -final class Ok extends NetworkResponseSuccess { +final class Ok extends NetworkResponseSuccess { final T response; const Ok(this.response); } -sealed class NetworkResponseFailure extends NetworkResponse { +sealed class NetworkResponseFailure extends NetworkResponse { final DioException? error; const NetworkResponseFailure({this.error}); } /// 401 - for responses when the request was missing required authentication. -final class Unauthorized extends NetworkResponseFailure { +final class Unauthorized extends NetworkResponseFailure { const Unauthorized({super.error}); } /// 403 - for responses when the request was authenticated but the /// action is not authorized/allowed. -final class Forbidden extends NetworkResponseFailure { +final class Forbidden extends NetworkResponseFailure { const Forbidden({super.error}); } /// 404 - for responses when we could not locate a resource, or when /// someone would attempt to access a forbidden resource due to a bug. -final class NotFound extends NetworkResponseFailure { +final class NotFound extends NetworkResponseFailure { const NotFound({super.error}); } /// 422 - for responses when the request inputs failed our validations. -final class UnprocessableEntity extends NetworkResponseFailure { +final class UnprocessableEntity extends NetworkResponseFailure { final R response; const UnprocessableEntity({super.error, required this.response}); } /// 426 - for responses when a client version upgrade is required -final class UpgradeRequired extends NetworkResponseFailure { +final class UpgradeRequired extends NetworkResponseFailure { const UpgradeRequired({super.error}); } /// 500 - for responses where the service had an error while processing /// the request. -final class ServerError extends NetworkResponseFailure { +final class ServerError extends NetworkResponseFailure { const ServerError({super.error}); } /// 503 - for responses when an underlying service issue prevents us from /// fulfilling the request. -final class ServiceUnavailable extends NetworkResponseFailure { +final class ServiceUnavailable extends NetworkResponseFailure { const ServiceUnavailable({super.error}); } -final class GenericError extends NetworkResponseFailure { +final class GenericError extends NetworkResponseFailure { const GenericError({ super.error, required this.message, @@ -84,21 +86,20 @@ final class GenericError extends NetworkResponseFailure { final bool isConnectionIssue; } -NetworkResponseSuccess? getSuccess(NetworkResponse response) { +NetworkResponseSuccess? getSuccess(NetworkResponse response) { return switch (response) { NoContent() => NoContent(), - Ok(:final response) => Ok(response), + Ok(:final response) => Ok(response), _ => null, }; } -NetworkResponseFailure? getFail(NetworkResponse response) { +NetworkResponseFailure? getFail(NetworkResponse response) { return switch (response) { Unauthorized(:final error) => Unauthorized(error: error), Forbidden(:final error) => Forbidden(error: error), NotFound(:final error) => NotFound(error: error), - UnprocessableEntity(:final error, :final response) => - UnprocessableEntity(error: error, response: response), + UnprocessableEntity(:final error, :final response) => UnprocessableEntity(error: error, response: response), UpgradeRequired(:final error) => UpgradeRequired(error: error), ServerError(:final error) => ServerError(error: error), ServiceUnavailable(:final error) => ServiceUnavailable(error: error), @@ -107,7 +108,7 @@ NetworkResponseFailure? getFail(NetworkResponse response) { } /// Extensions on the [NetworkResponse] type -extension NetworkResponseX on NetworkResponse { +extension NetworkResponseX on NetworkResponse { /// Whether this [NetworkResponse] should be considered successful bool get isSuccess => this is NetworkResponseSuccess; } diff --git a/lib/src/sturdy_http.dart b/lib/src/sturdy_http.dart index 32ab613..a6008ca 100644 --- a/lib/src/sturdy_http.dart +++ b/lib/src/sturdy_http.dart @@ -99,11 +99,11 @@ class SturdyHttp { /// If [onResponse] fails to produce an [M] and instead throws an [Exception], /// some known failure reasons are emitted via [SturdyHttpEvent]s and the /// [Exception] is re-thrown. - Future execute( + Future execute( NetworkRequest request, { - required M Function(NetworkResponse response) onResponse, + required M Function(NetworkResponse response) onResponse, }) async { - final responsePayload = await _handleRequest(request); + final responsePayload = await _handleRequest(request); try { return await _deserializer.deserialize( @@ -124,13 +124,13 @@ class SturdyHttp { } } - Future<_ResponsePayload> _handleRequest( + Future<_ResponsePayload> _handleRequest( NetworkRequest request, ) async { - Future<(Response?, NetworkResponse)> send( + Future<(Response?, NetworkResponse)> send( NetworkRequest request, ) async { - late final NetworkResponse resolvedResponse; + late final NetworkResponse resolvedResponse; Response? dioResponse; try { // By expecting `Object?` we allow for cases where an API will return @@ -156,13 +156,23 @@ class SturdyHttp { resolvedResponse = const NoContent(); } else { final data = dioResponse.data; - if (data == null) { + if (data == null || data is! R) { + String buildErrorMessage() { + final messageSuffix = data == null + // Disallow empty responses when status code is non-204 + ? 'was null and status code was ${dioResponse!.statusCode}' + // Enforce that response data matches expected, otherwise we'll run into casting + // issues below + : 'was of type ${data.runtimeType} when it should have been of type $R'; + return 'Request to ${request.path} was successful but response data $messageSuffix'; + } + resolvedResponse = GenericError( - message: 'Request to ${request.path} was successful but response data was null', + message: buildErrorMessage(), isConnectionIssue: false, ); } else { - resolvedResponse = Ok(data); + resolvedResponse = Ok(data as R); } } } on DioException catch (error) { @@ -178,9 +188,9 @@ class SturdyHttp { resolvedResponse = NotFound(error: error); break; case 422: - resolvedResponse = UnprocessableEntity( + resolvedResponse = UnprocessableEntity( error: error, - response: error.response?.data, + response: error.response?.data as R, ); break; case 426: @@ -236,10 +246,10 @@ class SturdyHttp { } } -class _ResponsePayload { +class _ResponsePayload { final NetworkRequest request; final Response? dioResponse; - final NetworkResponse resolvedResponse; + final NetworkResponse resolvedResponse; _ResponsePayload({ required this.request, diff --git a/test/src/deserializer_test.dart b/test/src/deserializer_test.dart index 326b952..e8b1a36 100644 --- a/test/src/deserializer_test.dart +++ b/test/src/deserializer_test.dart @@ -11,7 +11,7 @@ import 'sturdy_http_test.dart'; void main() { group('BackgroundDeserializer', () { test('it invokes onResponse on a non-main Isolate and sends result back', () async { - onResponse(NetworkResponse response) { + onResponse(NetworkResponse response) { final isolateName = Isolate.current.debugName; // Hijack `Foo` to send over the `IsolateName` since // isolates don't share memory (so we can't set a late @@ -30,7 +30,7 @@ void main() { }); test('it handles multiple requests for deserialization', () async { - Foo onResponse(NetworkResponse response) { + Foo onResponse(NetworkResponse response) { return switch (response) { Ok(:final response) => Foo.fromJson(response), _ => fail('Not expected: orElse'), @@ -53,7 +53,7 @@ void main() { }); test('it throws CheckedFromJsonExceptions when deserialization issues occur', () async { - onResponse(NetworkResponse response) { + onResponse(NetworkResponse response) { return switch (response) { Ok(:final response) => NotFoo.fromJson(response), _ => fail('orElse not expected'), diff --git a/test/src/sturdy_http_test.dart b/test/src/sturdy_http_test.dart index 5cb9d67..2ac703b 100644 --- a/test/src/sturdy_http_test.dart +++ b/test/src/sturdy_http_test.dart @@ -164,7 +164,7 @@ void main() { group('data', () { group('when NetworkRequestBody is empty', () { test('request options contain null data', () async { - await buildSubject().execute( + await buildSubject().execute( const GetRequest('/foo'), onResponse: (response) { return switch (response) { @@ -179,7 +179,7 @@ void main() { group('when NetworkRequestBody is json', () { test('request options contain json data', () async { - await buildSubject().execute( + await buildSubject().execute( const GetRequest( '/foo', data: NetworkRequestBody.json( @@ -203,7 +203,7 @@ void main() { group('when manual options are provided', () { test('request options contain manually provided values', () async { - await buildSubject().execute( + await buildSubject().execute( GetRequest( '/foo', data: NetworkRequestBody.json( @@ -229,7 +229,7 @@ void main() { group('queryParameters', () { test('request options contain correct query parameters ', () async { - await buildSubject().execute( + await buildSubject().execute( const GetRequest( '/foo', queryParameters: {'foo': 'bar'}, @@ -247,12 +247,12 @@ void main() { group('method', () { test('request options contain correct method ', () async { - await buildSubject().execute( + await buildSubject().execute( const GetRequest('/foo'), onResponse: (response) {}, ); expect(options.method, 'GET'); - await buildSubject().execute( + await buildSubject().execute( const PostRequest( '/bar', data: NetworkRequestBody.empty(), @@ -290,7 +290,7 @@ void main() { group('when deserialization succeeds', () { test('it returns parsed model', () async { - final response = await buildSubject().execute>( + final response = await buildSubject().execute>( const GetRequest('/foo'), onResponse: (response) { return switch (response) { @@ -309,7 +309,7 @@ void main() { group('when deserialization fails', () { test('it emits a decodingError event and rethrows the Exception', () async { - final request = buildSubject().execute>( + final request = buildSubject().execute>( const GetRequest('/not-foo'), onResponse: (response) { return switch (response) { @@ -344,7 +344,7 @@ void main() { ); }); test('it returns okNoContent', () async { - final response = await buildSubject().execute>( + final response = await buildSubject().execute>( const PostRequest( '/foo', data: NetworkRequestBody.empty(), @@ -377,7 +377,7 @@ void main() { test( 'it returns genericError and isConnectionIssue is false', () async { - final response = await buildSubject().execute>( + final response = await buildSubject().execute>( const PostRequest( '/foo', data: NetworkRequestBody.empty(), @@ -436,7 +436,7 @@ void main() { test('it emits a MutativeRequestSuccess event with correct path', () async { final subject = buildSubject(); await Future.wait([ - subject.execute>( + subject.execute>( const PostRequest( '/foo', data: NetworkRequestBody.empty(), @@ -448,7 +448,7 @@ void main() { }; }, ), - subject.execute>( + subject.execute>( const PutRequest( '/bar', data: NetworkRequestBody.empty(), @@ -460,7 +460,7 @@ void main() { }; }, ), - subject.execute>( + subject.execute>( const DeleteRequest( '/baz', data: NetworkRequestBody.empty(), @@ -518,7 +518,7 @@ void main() { test('it does not emit a MutativeRequestSuccess event', () async { final subject = buildSubject(); await Future.wait([ - subject.execute>( + subject.execute>( const PostRequest( '/foo', data: NetworkRequestBody.empty(), @@ -530,7 +530,7 @@ void main() { }; }, ), - subject.execute>( + subject.execute>( const PutRequest( '/bar', data: NetworkRequestBody.empty(), @@ -542,7 +542,7 @@ void main() { }; }, ), - subject.execute>( + subject.execute>( const DeleteRequest( '/baz', data: NetworkRequestBody.empty(), @@ -585,7 +585,7 @@ void main() { }); test('it emits an authFailure event and invokes unauthorized', () async { - final response = await buildSubject().execute>( + final response = await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { return switch (response) { @@ -610,7 +610,7 @@ void main() { }); test('it returns forbidden', () async { - final response = await buildSubject().execute>( + final response = await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { return switch (response) { @@ -633,7 +633,7 @@ void main() { }); test('it returns notFound', () async { - final response = await buildSubject().execute>( + final response = await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { return switch (response) { @@ -659,7 +659,7 @@ void main() { }); test('it returns unprocessableEntity', () async { - final response = await buildSubject().execute>( + final response = await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { return switch (response) { @@ -685,7 +685,7 @@ void main() { }); test('it returns upgradeRequired', () async { - final response = await buildSubject().execute>( + final response = await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { return switch (response) { @@ -708,7 +708,7 @@ void main() { }); test('it returns serverError', () async { - final response = await buildSubject().execute>( + final response = await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { return switch (response) { @@ -731,7 +731,7 @@ void main() { }); test('it returns service unavailable', () async { - final response = await buildSubject().execute>( + final response = await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { return switch (response) { @@ -754,7 +754,7 @@ void main() { }); test('it returns genericError', () async { - final response = await buildSubject().execute>( + final response = await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { return switch (response) { @@ -780,7 +780,7 @@ void main() { }); test('it returns genericError and isConnectionIssue is true', () async { - final response = await buildSubject().execute>( + final response = await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { return switch (response) { @@ -814,7 +814,7 @@ void main() { }, ); - final response = await buildSubject().execute>( + final response = await buildSubject().execute>( const GetRequest( defaultPath, retryBehavior: Retry( @@ -853,7 +853,7 @@ void main() { }, ); - final response = await buildSubject().execute>( + final response = await buildSubject().execute>( const GetRequest( defaultPath, retryBehavior: NeverRetry(), @@ -890,7 +890,7 @@ void main() { final response = await buildSubject( retryBehavior: NeverRetry(), - ).execute>( + ).execute>( const GetRequest( defaultPath, retryBehavior: Retry( @@ -931,7 +931,7 @@ void main() { final response = await buildSubject( retryBehavior: NeverRetry(), - ).execute>( + ).execute>( GetRequest( defaultPath, retryBehavior: Retry( From 6e066cef412e963911599490dc5448b47b3e5e88 Mon Sep 17 00:00:00 2001 From: nhannah Date: Thu, 12 Sep 2024 08:05:01 -0600 Subject: [PATCH 03/10] Make errors required on all non generic errors --- lib/src/network_response.dart | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/lib/src/network_response.dart b/lib/src/network_response.dart index a27194c..cb56d78 100644 --- a/lib/src/network_response.dart +++ b/lib/src/network_response.dart @@ -29,59 +29,71 @@ final class Ok extends NetworkResponseSuccess { } sealed class NetworkResponseFailure extends NetworkResponse { - final DioException? error; - - const NetworkResponseFailure({this.error}); + const NetworkResponseFailure(); } /// 401 - for responses when the request was missing required authentication. final class Unauthorized extends NetworkResponseFailure { - const Unauthorized({super.error}); + final DioException error; + + const Unauthorized({required this.error}); } /// 403 - for responses when the request was authenticated but the /// action is not authorized/allowed. final class Forbidden extends NetworkResponseFailure { - const Forbidden({super.error}); + final DioException error; + + const Forbidden({required this.error}); } /// 404 - for responses when we could not locate a resource, or when /// someone would attempt to access a forbidden resource due to a bug. final class NotFound extends NetworkResponseFailure { - const NotFound({super.error}); + final DioException error; + + const NotFound({required this.error}); } /// 422 - for responses when the request inputs failed our validations. final class UnprocessableEntity extends NetworkResponseFailure { + final DioException error; final R response; - const UnprocessableEntity({super.error, required this.response}); + const UnprocessableEntity({required this.error, required this.response}); } /// 426 - for responses when a client version upgrade is required final class UpgradeRequired extends NetworkResponseFailure { - const UpgradeRequired({super.error}); + final DioException error; + + const UpgradeRequired({required this.error}); } /// 500 - for responses where the service had an error while processing /// the request. final class ServerError extends NetworkResponseFailure { - const ServerError({super.error}); + final DioException error; + + const ServerError({required this.error}); } /// 503 - for responses when an underlying service issue prevents us from /// fulfilling the request. final class ServiceUnavailable extends NetworkResponseFailure { - const ServiceUnavailable({super.error}); + final DioException error; + + const ServiceUnavailable({required this.error}); } final class GenericError extends NetworkResponseFailure { const GenericError({ - super.error, + this.error, required this.message, required this.isConnectionIssue, }); + final DioException? error; final String message; final bool isConnectionIssue; } From d9d644dfec8bcd2914642c893197e4f35d668130 Mon Sep 17 00:00:00 2001 From: nhannah Date: Thu, 12 Sep 2024 11:19:02 -0600 Subject: [PATCH 04/10] rename Ok and NoContent --- example/main.dart | 4 ++-- lib/src/network_response.dart | 12 ++++++------ lib/src/sturdy_http.dart | 4 ++-- test/src/deserializer_test.dart | 12 ++++++------ test/src/sturdy_http_test.dart | 20 ++++++++++---------- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/example/main.dart b/example/main.dart index c1b5c70..b1b55db 100644 --- a/example/main.dart +++ b/example/main.dart @@ -27,7 +27,7 @@ void main(List args) async { GetRequest('/foo'), onResponse: (r) { return switch (r) { - Ok(:final response) => print(response), + OkResponse(:final response) => print(response), _ => print('GET /foo failed: $r'), }; }, @@ -42,7 +42,7 @@ void main(List args) async { PostRequest('/foo', data: NetworkRequestBody.empty()), onResponse: (r) { return switch (r) { - NoContent() => print('success!'), + OkNoContent() => print('success!'), _ => print('POST /foo failed: $r'), }; }, diff --git a/lib/src/network_response.dart b/lib/src/network_response.dart index cb56d78..a7e5300 100644 --- a/lib/src/network_response.dart +++ b/lib/src/network_response.dart @@ -18,14 +18,14 @@ sealed class NetworkResponseSuccess extends NetworkResponse { const NetworkResponseSuccess(); } -final class NoContent extends NetworkResponseSuccess { - const NoContent(); +final class OkNoContent extends NetworkResponseSuccess { + const OkNoContent(); } -final class Ok extends NetworkResponseSuccess { +final class OkResponse extends NetworkResponseSuccess { final T response; - const Ok(this.response); + const OkResponse(this.response); } sealed class NetworkResponseFailure extends NetworkResponse { @@ -100,8 +100,8 @@ final class GenericError extends NetworkResponseFailure { NetworkResponseSuccess? getSuccess(NetworkResponse response) { return switch (response) { - NoContent() => NoContent(), - Ok(:final response) => Ok(response), + OkNoContent() => OkNoContent(), + OkResponse(:final response) => OkResponse(response), _ => null, }; } diff --git a/lib/src/sturdy_http.dart b/lib/src/sturdy_http.dart index a6008ca..8d28a50 100644 --- a/lib/src/sturdy_http.dart +++ b/lib/src/sturdy_http.dart @@ -153,7 +153,7 @@ class SturdyHttp { onSendProgress: request.onSendProgress, ); if (dioResponse.statusCode == 204) { - resolvedResponse = const NoContent(); + resolvedResponse = const OkNoContent(); } else { final data = dioResponse.data; if (data == null || data is! R) { @@ -172,7 +172,7 @@ class SturdyHttp { isConnectionIssue: false, ); } else { - resolvedResponse = Ok(data as R); + resolvedResponse = OkResponse(data as R); } } } on DioException catch (error) { diff --git a/test/src/deserializer_test.dart b/test/src/deserializer_test.dart index e8b1a36..543006c 100644 --- a/test/src/deserializer_test.dart +++ b/test/src/deserializer_test.dart @@ -20,7 +20,7 @@ void main() { return Foo(message: isolateName!); } - final response = Ok(const Foo(message: '--').toJson()); + final response = OkResponse(const Foo(message: '--').toJson()); final subject = BackgroundDeserializer(); final result = await subject.deserialize( response: response, @@ -32,13 +32,13 @@ void main() { test('it handles multiple requests for deserialization', () async { Foo onResponse(NetworkResponse response) { return switch (response) { - Ok(:final response) => Foo.fromJson(response), + OkResponse(:final response) => Foo.fromJson(response), _ => fail('Not expected: orElse'), }; } - final responseOne = Ok(const Foo(message: '1').toJson()); - final responseTwo = Ok(const Foo(message: '2').toJson()); + final responseOne = OkResponse(const Foo(message: '1').toJson()); + final responseTwo = OkResponse(const Foo(message: '2').toJson()); final subject = BackgroundDeserializer(); final resultOne = await subject.deserialize( response: responseOne, @@ -55,12 +55,12 @@ void main() { test('it throws CheckedFromJsonExceptions when deserialization issues occur', () async { onResponse(NetworkResponse response) { return switch (response) { - Ok(:final response) => NotFoo.fromJson(response), + OkResponse(:final response) => NotFoo.fromJson(response), _ => fail('orElse not expected'), }; } - final response = Ok(const Foo(message: 'Nope').toJson()); + final response = OkResponse(const Foo(message: 'Nope').toJson()); final subject = BackgroundDeserializer(); try { await subject.deserialize( diff --git a/test/src/sturdy_http_test.dart b/test/src/sturdy_http_test.dart index 2ac703b..6fc9964 100644 --- a/test/src/sturdy_http_test.dart +++ b/test/src/sturdy_http_test.dart @@ -168,7 +168,7 @@ void main() { const GetRequest('/foo'), onResponse: (response) { return switch (response) { - Ok() => true, + OkResponse() => true, _ => false, }; }, @@ -294,7 +294,7 @@ void main() { const GetRequest('/foo'), onResponse: (response) { return switch (response) { - Ok(:final response) => Result.success(Foo.fromJson(response)), + OkResponse(:final response) => Result.success(Foo.fromJson(response)), _ => const Result.failure('Not expected: orElse'), }; }, @@ -313,7 +313,7 @@ void main() { const GetRequest('/not-foo'), onResponse: (response) { return switch (response) { - Ok(:final response) => Result.success(Foo.fromJson(response)), + OkResponse(:final response) => Result.success(Foo.fromJson(response)), _ => const Result.failure('Not expected: orElse'), }; }, @@ -351,7 +351,7 @@ void main() { ), onResponse: (response) { return switch (response) { - NoContent() => const Result.success(true), + OkNoContent() => const Result.success(true), _ => const Result.failure('Not expected: orElse'), }; }, @@ -443,7 +443,7 @@ void main() { ), onResponse: (response) { return switch (response) { - Ok(:final response) => Result.success(response['foo'] as String), + OkResponse(:final response) => Result.success(response['foo'] as String), _ => const Result.failure('Not expected: orElse'), }; }, @@ -455,7 +455,7 @@ void main() { ), onResponse: (response) { return switch (response) { - Ok(:final response) => Result.success(response['foo'] as String), + OkResponse(:final response) => Result.success(response['foo'] as String), _ => const Result.failure('Not expected: orElse'), }; }, @@ -467,7 +467,7 @@ void main() { ), onResponse: (response) { return switch (response) { - Ok(:final response) => Result.success(response['foo'] as String), + OkResponse(:final response) => Result.success(response['foo'] as String), _ => const Result.failure('Not expected: orElse'), }; }, @@ -525,7 +525,7 @@ void main() { ), onResponse: (response) { return switch (response) { - Ok(:final response) => Result.success(response['foo'] as String), + OkResponse(:final response) => Result.success(response['foo'] as String), _ => const Result.failure('Not expected: orElse'), }; }, @@ -537,7 +537,7 @@ void main() { ), onResponse: (response) { return switch (response) { - Ok(:final response) => Result.success(response['foo'] as String), + OkResponse(:final response) => Result.success(response['foo'] as String), _ => const Result.failure('Not expected: orElse'), }; }, @@ -549,7 +549,7 @@ void main() { ), onResponse: (response) { return switch (response) { - Ok(:final response) => Result.success(response['foo'] as String), + OkResponse(:final response) => Result.success(response['foo'] as String), _ => const Result.failure('Not expected: orElse'), }; }, From 9346b1630e603f54b461404c4e6248fd2b42a258 Mon Sep 17 00:00:00 2001 From: Brandon Trautmann Date: Tue, 17 Sep 2024 15:05:18 -0400 Subject: [PATCH 05/10] cleanup --- lib/src/network_response.dart | 24 +------- lib/src/sturdy_http.dart | 13 +++-- test/src/deserializer_test.dart | 7 ++- test/src/sturdy_http_test.dart | 99 ++++++++++++++++++++++----------- 4 files changed, 84 insertions(+), 59 deletions(-) diff --git a/lib/src/network_response.dart b/lib/src/network_response.dart index a7e5300..0479c2a 100644 --- a/lib/src/network_response.dart +++ b/lib/src/network_response.dart @@ -86,6 +86,9 @@ final class ServiceUnavailable extends NetworkResponseFailure { const ServiceUnavailable({required this.error}); } +/// Any "other" error not covered by the above cases. If a [DioException] is present, +/// it has an error status we don't handle. Often this error will occur when no +/// response was received from the server. final class GenericError extends NetworkResponseFailure { const GenericError({ this.error, @@ -98,27 +101,6 @@ final class GenericError extends NetworkResponseFailure { final bool isConnectionIssue; } -NetworkResponseSuccess? getSuccess(NetworkResponse response) { - return switch (response) { - OkNoContent() => OkNoContent(), - OkResponse(:final response) => OkResponse(response), - _ => null, - }; -} - -NetworkResponseFailure? getFail(NetworkResponse response) { - return switch (response) { - Unauthorized(:final error) => Unauthorized(error: error), - Forbidden(:final error) => Forbidden(error: error), - NotFound(:final error) => NotFound(error: error), - UnprocessableEntity(:final error, :final response) => UnprocessableEntity(error: error, response: response), - UpgradeRequired(:final error) => UpgradeRequired(error: error), - ServerError(:final error) => ServerError(error: error), - ServiceUnavailable(:final error) => ServiceUnavailable(error: error), - _ => null, - }; -} - /// Extensions on the [NetworkResponse] type extension NetworkResponseX on NetworkResponse { /// Whether this [NetworkResponse] should be considered successful diff --git a/lib/src/sturdy_http.dart b/lib/src/sturdy_http.dart index 8d28a50..0c62f7e 100644 --- a/lib/src/sturdy_http.dart +++ b/lib/src/sturdy_http.dart @@ -31,7 +31,8 @@ class SturdyHttp { final RetryBehavior _retryBehavior; /// The interceptors provided when this [SturdyHttp] was constructed. - UnmodifiableListView get interceptors => UnmodifiableListView(_dio.interceptors); + UnmodifiableListView get interceptors => + UnmodifiableListView(_dio.interceptors); /// The base URL of the underlying [Dio] instance. String get baseUrl => _dio.options.baseUrl; @@ -204,7 +205,8 @@ class SturdyHttp { break; default: resolvedResponse = GenericError( - message: 'Unexpected status code ${error.response?.statusCode} returned for ${request.path}', + message: + 'Unexpected status code ${error.response?.statusCode} returned for ${request.path}', isConnectionIssue: error.isConnectionIssue(), error: error, ); @@ -225,7 +227,8 @@ class SturdyHttp { final retryBehavior = determineRetryBehavior(); var response = await send(request); var retryCount = 0; - while (!response.$2.isSuccess && retryBehavior.shouldRetry(response.$1, retryCount)) { + while (!response.$2.isSuccess && + retryBehavior.shouldRetry(response.$1, retryCount)) { // `retryBehavior` must be a `Retry`, otherwise we wouldn't be here. await Future.delayed((retryBehavior as Retry).retryInterval); retryCount++; @@ -268,7 +271,9 @@ Dio _configureDio({ }) { return Dio() // Instruct Dio to use the same Isolate approach as requested of SturdyHttp - ..transformer = deserializer is MainIsolateDeserializer ? SyncTransformer() : BackgroundTransformer() + ..transformer = deserializer is MainIsolateDeserializer + ? SyncTransformer() + : BackgroundTransformer() ..options.baseUrl = baseUrl ..options.listFormat = ListFormat.multiCompatible ..interceptors.addAll(interceptors) diff --git a/test/src/deserializer_test.dart b/test/src/deserializer_test.dart index 543006c..6c6f858 100644 --- a/test/src/deserializer_test.dart +++ b/test/src/deserializer_test.dart @@ -10,7 +10,8 @@ import 'sturdy_http_test.dart'; void main() { group('BackgroundDeserializer', () { - test('it invokes onResponse on a non-main Isolate and sends result back', () async { + test('it invokes onResponse on a non-main Isolate and sends result back', + () async { onResponse(NetworkResponse response) { final isolateName = Isolate.current.debugName; // Hijack `Foo` to send over the `IsolateName` since @@ -52,7 +53,9 @@ void main() { expect(resultTwo.message, '2'); }); - test('it throws CheckedFromJsonExceptions when deserialization issues occur', () async { + test( + 'it throws CheckedFromJsonExceptions when deserialization issues occur', + () async { onResponse(NetworkResponse response) { return switch (response) { OkResponse(:final response) => NotFoo.fromJson(response), diff --git a/test/src/sturdy_http_test.dart b/test/src/sturdy_http_test.dart index 6fc9964..de2a328 100644 --- a/test/src/sturdy_http_test.dart +++ b/test/src/sturdy_http_test.dart @@ -73,7 +73,8 @@ void main() { interceptors: [ _FakeInterceptor( onRequestInvoked: (options) { - contentType = options.headers[Headers.contentTypeHeader] as String?; + contentType = + options.headers[Headers.contentTypeHeader] as String?; }, ), ], @@ -96,7 +97,8 @@ void main() { interceptors: [ _FakeInterceptor( onRequestInvoked: (options) { - contentType = options.headers[Headers.contentTypeHeader] as String?; + contentType = + options.headers[Headers.contentTypeHeader] as String?; }, ), ], @@ -131,7 +133,9 @@ void main() { }); group('withBaseUrl', () { - test('it returns a new instance with correct baseUrl and pre-configured settings', () { + test( + 'it returns a new instance with correct baseUrl and pre-configured settings', + () { final oldInstance = buildSubject( interceptors: [_FakeInterceptor()], ); @@ -290,11 +294,13 @@ void main() { group('when deserialization succeeds', () { test('it returns parsed model', () async { - final response = await buildSubject().execute>( + final response = + await buildSubject().execute>( const GetRequest('/foo'), onResponse: (response) { return switch (response) { - OkResponse(:final response) => Result.success(Foo.fromJson(response)), + OkResponse(:final response) => + Result.success(Foo.fromJson(response)), _ => const Result.failure('Not expected: orElse'), }; }, @@ -308,12 +314,16 @@ void main() { }); group('when deserialization fails', () { - test('it emits a decodingError event and rethrows the Exception', () async { - final request = buildSubject().execute>( + test( + 'it emits a decodingError event and rethrows the Exception', + () async { + final request = + buildSubject().execute>( const GetRequest('/not-foo'), onResponse: (response) { return switch (response) { - OkResponse(:final response) => Result.success(Foo.fromJson(response)), + OkResponse(:final response) => + Result.success(Foo.fromJson(response)), _ => const Result.failure('Not expected: orElse'), }; }, @@ -344,7 +354,8 @@ void main() { ); }); test('it returns okNoContent', () async { - final response = await buildSubject().execute>( + final response = + await buildSubject().execute>( const PostRequest( '/foo', data: NetworkRequestBody.empty(), @@ -377,7 +388,8 @@ void main() { test( 'it returns genericError and isConnectionIssue is false', () async { - final response = await buildSubject().execute>( + final response = await buildSubject() + .execute>( const PostRequest( '/foo', data: NetworkRequestBody.empty(), @@ -433,7 +445,9 @@ void main() { ); }); - test('it emits a MutativeRequestSuccess event with correct path', () async { + test( + 'it emits a MutativeRequestSuccess event with correct path', + () async { final subject = buildSubject(); await Future.wait([ subject.execute>( @@ -443,7 +457,8 @@ void main() { ), onResponse: (response) { return switch (response) { - OkResponse(:final response) => Result.success(response['foo'] as String), + OkResponse(:final response) => + Result.success(response['foo'] as String), _ => const Result.failure('Not expected: orElse'), }; }, @@ -455,7 +470,8 @@ void main() { ), onResponse: (response) { return switch (response) { - OkResponse(:final response) => Result.success(response['foo'] as String), + OkResponse(:final response) => + Result.success(response['foo'] as String), _ => const Result.failure('Not expected: orElse'), }; }, @@ -467,7 +483,8 @@ void main() { ), onResponse: (response) { return switch (response) { - OkResponse(:final response) => Result.success(response['foo'] as String), + OkResponse(:final response) => + Result.success(response['foo'] as String), _ => const Result.failure('Not expected: orElse'), }; }, @@ -489,7 +506,8 @@ void main() { }); }); - group('and the response has status codes other than 200 or 204', () { + group('and the response has status codes other than 200 or 204', + () { setUp(() { charlatan ..whenPost( @@ -515,7 +533,8 @@ void main() { ); }); - test('it does not emit a MutativeRequestSuccess event', () async { + test('it does not emit a MutativeRequestSuccess event', + () async { final subject = buildSubject(); await Future.wait([ subject.execute>( @@ -525,7 +544,8 @@ void main() { ), onResponse: (response) { return switch (response) { - OkResponse(:final response) => Result.success(response['foo'] as String), + OkResponse(:final response) => + Result.success(response['foo'] as String), _ => const Result.failure('Not expected: orElse'), }; }, @@ -537,7 +557,8 @@ void main() { ), onResponse: (response) { return switch (response) { - OkResponse(:final response) => Result.success(response['foo'] as String), + OkResponse(:final response) => + Result.success(response['foo'] as String), _ => const Result.failure('Not expected: orElse'), }; }, @@ -549,7 +570,8 @@ void main() { ), onResponse: (response) { return switch (response) { - OkResponse(:final response) => Result.success(response['foo'] as String), + OkResponse(:final response) => + Result.success(response['foo'] as String), _ => const Result.failure('Not expected: orElse'), }; }, @@ -584,8 +606,10 @@ void main() { setupErrorResponse(statusCode: 401); }); - test('it emits an authFailure event and invokes unauthorized', () async { - final response = await buildSubject().execute>( + test('it emits an authFailure event and invokes unauthorized', + () async { + final response = + await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { return switch (response) { @@ -610,7 +634,8 @@ void main() { }); test('it returns forbidden', () async { - final response = await buildSubject().execute>( + final response = + await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { return switch (response) { @@ -633,7 +658,8 @@ void main() { }); test('it returns notFound', () async { - final response = await buildSubject().execute>( + final response = + await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { return switch (response) { @@ -659,7 +685,8 @@ void main() { }); test('it returns unprocessableEntity', () async { - final response = await buildSubject().execute>( + final response = + await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { return switch (response) { @@ -685,7 +712,8 @@ void main() { }); test('it returns upgradeRequired', () async { - final response = await buildSubject().execute>( + final response = + await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { return switch (response) { @@ -708,7 +736,8 @@ void main() { }); test('it returns serverError', () async { - final response = await buildSubject().execute>( + final response = + await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { return switch (response) { @@ -731,7 +760,8 @@ void main() { }); test('it returns service unavailable', () async { - final response = await buildSubject().execute>( + final response = + await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { return switch (response) { @@ -754,7 +784,8 @@ void main() { }); test('it returns genericError', () async { - final response = await buildSubject().execute>( + final response = + await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { return switch (response) { @@ -779,8 +810,10 @@ void main() { ); }); - test('it returns genericError and isConnectionIssue is true', () async { - final response = await buildSubject().execute>( + test('it returns genericError and isConnectionIssue is true', + () async { + final response = + await buildSubject().execute>( const GetRequest(defaultPath), onResponse: (response) { return switch (response) { @@ -814,7 +847,8 @@ void main() { }, ); - final response = await buildSubject().execute>( + final response = + await buildSubject().execute>( const GetRequest( defaultPath, retryBehavior: Retry( @@ -853,7 +887,8 @@ void main() { }, ); - final response = await buildSubject().execute>( + final response = + await buildSubject().execute>( const GetRequest( defaultPath, retryBehavior: NeverRetry(), From d0b688df751009f976339c7e3720490afbdf6dee Mon Sep 17 00:00:00 2001 From: Brandon Trautmann Date: Tue, 17 Sep 2024 15:42:50 -0400 Subject: [PATCH 06/10] bump version constraints on most deps --- pubspec.yaml | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/pubspec.yaml b/pubspec.yaml index 00c2823..be67d9d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -8,16 +8,16 @@ environment: sdk: ">=3.0.7 <4.0.0" dependencies: - collection: ^1.17.1 - dio: ^5.2.0 - freezed_annotation: ^2.2.0 - uuid: ^4.1.0 + collection: ^1.19.0 + dio: ^5.7.0 + freezed_annotation: ^2.4.4 + uuid: ^4.5.0 dev_dependencies: - build_runner: ^2.1.8 - charlatan: 0.4.0 - freezed: ^2.2.0 - json_annotation: ^4.4.0 - json_serializable: ^6.1.5 - lints: ^3.0.0 - test: ^1.16.0 + build_runner: ^2.4.12 + charlatan: ^0.4.0 + freezed: ^2.5.7 + json_annotation: ^4.9.0 + json_serializable: ^6.8.0 + lints: ^4.0.0 + test: ^1.25.8 From f57bffede7d0cd3e7eb71ce689bb928b343b7351 Mon Sep 17 00:00:00 2001 From: Brandon Trautmann Date: Tue, 17 Sep 2024 15:44:11 -0400 Subject: [PATCH 07/10] bump dart constraint --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index be67d9d..8edd5d3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -5,7 +5,7 @@ homepage: https://github.com/Betterment/sturdy_http repository: https://github.com/Betterment/sturdy_http environment: - sdk: ">=3.0.7 <4.0.0" + sdk: ">=3.4.0 <4.0.0" dependencies: collection: ^1.19.0 From fafb54f8fbfd656887e2c09902e3917fbc3a47f9 Mon Sep 17 00:00:00 2001 From: Brandon Trautmann Date: Tue, 17 Sep 2024 15:44:32 -0400 Subject: [PATCH 08/10] install correct dart version on CI --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2254628..716f2e3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,7 +22,7 @@ jobs: - name: Setup dart uses: dart-lang/setup-dart@d6a63dab3335f427404425de0fbfed4686d93c4f # v1.5.0 with: - sdk: 3.3.1 + sdk: 3.4.0 - name: Install dependencies run: dart pub get @@ -49,7 +49,7 @@ jobs: - name: Setup dart uses: dart-lang/setup-dart@d6a63dab3335f427404425de0fbfed4686d93c4f # v1.5.0 with: - sdk: 3.3.1 + sdk: 3.4.0 - name: Install dependencies run: | From fee98719307e52f4b52d32e3202c3b663a1e150c Mon Sep 17 00:00:00 2001 From: Brandon Trautmann Date: Tue, 17 Sep 2024 15:47:36 -0400 Subject: [PATCH 09/10] bump dart constraint --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index 8edd5d3..6560da4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -5,7 +5,7 @@ homepage: https://github.com/Betterment/sturdy_http repository: https://github.com/Betterment/sturdy_http environment: - sdk: ">=3.4.0 <4.0.0" + sdk: ">=3.5.0 <4.0.0" dependencies: collection: ^1.19.0 From c9c9c4a03971d6d37cd6186ff9097297b06d6bc0 Mon Sep 17 00:00:00 2001 From: Brandon Trautmann Date: Tue, 17 Sep 2024 15:48:43 -0400 Subject: [PATCH 10/10] bump CI dart version --- .github/workflows/ci.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 716f2e3..a52bfc7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,6 +11,9 @@ on: pull_request: branches: [ main ] +env: + DART_VERSION: 3.5.0 + jobs: ci: runs-on: ubuntu-latest @@ -22,7 +25,7 @@ jobs: - name: Setup dart uses: dart-lang/setup-dart@d6a63dab3335f427404425de0fbfed4686d93c4f # v1.5.0 with: - sdk: 3.4.0 + sdk: ${{ env.DART_VERSION }} - name: Install dependencies run: dart pub get @@ -49,7 +52,7 @@ jobs: - name: Setup dart uses: dart-lang/setup-dart@d6a63dab3335f427404425de0fbfed4686d93c4f # v1.5.0 with: - sdk: 3.4.0 + sdk: ${{ env.DART_VERSION }} - name: Install dependencies run: |