Skip to content

Commit

Permalink
feat: add ability to provide cancel token, send & receive progress ca…
Browse files Browse the repository at this point in the history
…llbacks (#2)

* feat: add ability to provide cancel token, send & receive progress callbacks

* metadata updates

* metadata updates (breaking change)

* constrain dart SDK version in CI

* constrain dart SDK version in CI (pana)

* address dio deprecations

* update CHANGELOG

* fix pubspec

* add cancelToken
  • Loading branch information
btrautmann authored Jul 18, 2023
1 parent faaa34d commit 12ba3aa
Show file tree
Hide file tree
Showing 9 changed files with 369 additions and 292 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0
- name: Setup dart
uses: dart-lang/setup-dart@d6a63dab3335f427404425de0fbfed4686d93c4f # v1.5.0
with:
sdk: 2.18.5
- name: Install dependencies
run: dart pub get
- name: Verify formatting
Expand All @@ -38,6 +40,8 @@ jobs:
uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0
- name: Setup dart
uses: dart-lang/setup-dart@d6a63dab3335f427404425de0fbfed4686d93c4f # v1.5.0
with:
sdk: 2.18.5
- name: Install dependencies
run: |
dart pub get
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.1.0

- Add ability to provide `CancelToken` as well as send and receive progress callbacks to `NetworkRequest`s
- Remove ability to provide `headers` to `NetworkRequest`s. Instead, pass `Options`.
- Migrate to `DioException` from the deprecated `DioError`.
- Remove proxy configuration on the underlying `Dio` instance during construction of `SturdyHttp`. If you want to configure proxy information, do it via an `Interceptor`.

## 0.0.2

- Correct library name
Expand Down
16 changes: 8 additions & 8 deletions lib/src/_dio_error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import 'dart:io';
import 'package:dio/dio.dart';

// ignore: public_member_api_docs
extension DioErrorX on DioError {
/// Indicates whether we have reason to believe a [DioError] occurred
extension DioExceptionX on DioException {
/// Indicates whether we have reason to believe a [DioException] occurred
/// because of an issue with the internet connection. These checks/values
/// are mostly curated through experience with Sentry issues and a sprinkle
/// of common sense. If we have reason to believe we're over or under
/// capturing [DioErrorType]s or [Exception] types we can adjust the logic.
/// capturing [DioExceptionType]s or [Exception] types we can adjust the logic.
bool isConnectionIssue() {
const knownBadConnectionTypes = [
DioErrorType.connectionError,
DioErrorType.connectionTimeout,
DioErrorType.receiveTimeout,
DioErrorType.sendTimeout,
DioErrorType.unknown,
DioExceptionType.connectionError,
DioExceptionType.connectionTimeout,
DioExceptionType.receiveTimeout,
DioExceptionType.sendTimeout,
DioExceptionType.unknown,
];
final isKnownExceptionType = error is HandshakeException ||
error is SocketException ||
Expand Down
39 changes: 32 additions & 7 deletions lib/src/network_request.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:dio/dio.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:sturdy_http/sturdy_http.dart';

Expand Down Expand Up @@ -25,7 +26,10 @@ abstract class NetworkRequest {
required this.data,
required this.shouldTriggerDataMutation,
this.queryParams,
this.headers,
this.options,
this.cancelToken,
this.onReceiveProgress,
this.onSendProgress,
});

/// {@macro network_request_type}
Expand All @@ -45,8 +49,17 @@ abstract class NetworkRequest {
/// Query parameters for this request
final Map<String, dynamic>? queryParams;

/// Headers for this request
final Map<String, String>? headers;
/// [Options] for this request
final Options? options;

/// [CancelToken] for this request
final CancelToken? cancelToken;

/// [ProgressCallback] for receive progress for this request
final ProgressCallback? onReceiveProgress;

/// [ProgressCallback] for send progress for this request
final ProgressCallback? onSendProgress;
}

/// {@template get_request}
Expand All @@ -58,7 +71,10 @@ class GetRequest extends NetworkRequest {
String path, {
super.data = const NetworkRequestBody.empty(),
Map<String, dynamic>? queryParameters,
super.headers,
super.options,
super.cancelToken,
super.onReceiveProgress,
super.onSendProgress,
}) : super(
type: NetworkRequestType.Get,
path: path,
Expand All @@ -77,7 +93,10 @@ class PostRequest extends NetworkRequest {
required super.data,
Map<String, dynamic>? queryParameters,
super.shouldTriggerDataMutation = true,
super.headers,
super.options,
super.cancelToken,
super.onReceiveProgress,
super.onSendProgress,
}) : super(
type: NetworkRequestType.Post,
path: path,
Expand All @@ -95,7 +114,10 @@ class PutRequest extends NetworkRequest {
required super.data,
Map<String, dynamic>? queryParameters,
super.shouldTriggerDataMutation = true,
super.headers,
super.options,
super.cancelToken,
super.onReceiveProgress,
super.onSendProgress,
}) : super(
type: NetworkRequestType.Put,
path: path,
Expand All @@ -113,7 +135,10 @@ class DeleteRequest extends NetworkRequest {
super.data = const NetworkRequestBody.empty(),
Map<String, dynamic>? queryParameters,
super.shouldTriggerDataMutation = true,
super.headers,
super.options,
super.cancelToken,
super.onReceiveProgress,
super.onSendProgress,
}) : super(
type: NetworkRequestType.Delete,
path: path,
Expand Down
17 changes: 9 additions & 8 deletions lib/src/network_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,40 @@ class NetworkResponse<R> with _$NetworkResponse<R> {
const factory NetworkResponse.okNoContent() = _OkNoContent;

/// 401 - for responses when the request was missing required authentication.
const factory NetworkResponse.unauthorized(DioError error) = _Unauthorized;
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(DioError error) = _Forbidden;
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(DioError error) = _NotFound;
const factory NetworkResponse.notFound(DioException error) = _NotFound;

/// 422 - for responses when the request inputs failed our validations.
const factory NetworkResponse.unprocessableEntity({
required DioError error,
required DioException error,
required R response,
}) = _UnprocessableEntity;

/// 500 - for responses where the service had an error while processing
/// the request.
const factory NetworkResponse.serverError(DioError error) = _ServerError;
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(DioError error) =
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
/// [DioError] will be present if it was available as a result of the request.
/// [DioException] will be present if it was available as a result of the request.
const factory NetworkResponse.genericError({
required String message,
required bool isConnectionIssue,
DioError? error,
DioException? error,
}) = _GenericError;
}

Expand Down
Loading

0 comments on commit 12ba3aa

Please sign in to comment.