Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: add maxRetryCount param #114

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/src/mixin/network_manager_core_operation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ mixin NetworkManagerCoreOperation<E extends INetworkModel<E>> {
CancelToken? cancelToken,
bool? forceUpdateDecode,
}) async {
if (!isErrorDialog ||
_noNetworkTryCount == NetworkManagerParameters.maxRetryCount) {
if (!isErrorDialog || _noNetworkTryCount == parameters.maxRetryCount) {
return onError.call(error);
}

Expand Down Expand Up @@ -89,8 +88,7 @@ mixin NetworkManagerCoreOperation<E extends INetworkModel<E>> {
CancelToken? cancelToken,
bool? forceUpdateDecode,
}) async {
if (!isErrorDialog ||
_noNetworkTryCount == NetworkManagerParameters.maxRetryCount) {
if (!isErrorDialog || _noNetworkTryCount == parameters.maxRetryCount) {
return onError.call(error);
}

Expand Down
3 changes: 2 additions & 1 deletion lib/src/mixin/network_manager_error_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ mixin NetworkManagerErrorInterceptor {
() => _createNewRequest(error),
onRetry: (_) async =>
error = await _createError(parameters, exception),
maxAttempts: NetworkManagerParameters.maxRetryCount,
maxAttempts: parameters.maxRetryCount,
retryIf: _retryIf,
);
// onResponseParse is null, then return response
Expand Down Expand Up @@ -78,6 +78,7 @@ mixin NetworkManagerErrorInterceptor {
isEnableLogger: params.isEnableLogger,
isEnableTest: params.isEnableTest,
options: parameters.baseOptions,
maxRetryCount: params.maxRetryCount,
),
);
}
Expand Down
5 changes: 3 additions & 2 deletions lib/src/mixin/network_manager_parameters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ typedef OnReply = Response<dynamic> Function(
class NetworkManagerParameters extends Equatable {
final VoidCallback? onRefreshFail;

static const int maxRetryCount = 3;

final IFileManager? fileManager;

final bool? isEnableTest;
Expand All @@ -38,6 +36,8 @@ class NetworkManagerParameters extends Equatable {

final OnReply? onResponseParse;

final int maxRetryCount;

const NetworkManagerParameters({
required BaseOptions options,
this.onRefreshFail,
Expand All @@ -50,6 +50,7 @@ class NetworkManagerParameters extends Equatable {
this.interceptor,
this.onRefreshToken,
this.onResponseParse,
this.maxRetryCount = 3,
}) : baseOptions = options;

@override
Expand Down
2 changes: 2 additions & 0 deletions lib/src/network_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class NetworkManager<E extends INetworkModel<E>> extends dio.DioMixin
IFileManager? fileManager,
Interceptor? interceptor,
OnReply? onReply,
int maxRetryCount = 3,
}) {
parameters = NetworkManagerParameters(
options: options,
Expand All @@ -54,6 +55,7 @@ class NetworkManager<E extends INetworkModel<E>> extends dio.DioMixin
onRefreshToken: onRefreshToken,
onRefreshFail: onRefreshFail,
onResponseParse: onReply,
maxRetryCount: maxRetryCount,
);
_setup();
}
Expand Down
1 change: 1 addition & 0 deletions test/unit/common/mock_network_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ final class MockErrorCustomNetworkManager extends NetworkManager<EmptyModel> {
: super(
options: BaseOptions(baseUrl: baseUrl),
isEnableTest: true,
maxRetryCount: 5,
onRefreshFail: () => print('onRefreshFail Triggered'),
onRefreshToken: (e, networkManager) async {
await onRefresh.call();
Expand Down
4 changes: 2 additions & 2 deletions test/unit/network_manager_error_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void main() {
method: RequestType.GET,
);
stopServer();
expect(retryCount, 3);
expect(retryCount, 5);
expect(response.error?.statusCode, 401);
});

Expand Down Expand Up @@ -57,7 +57,7 @@ void main() {
),
]);
stopServer();
expect(retryCount, 3);
expect(retryCount, 5);
expect(cancelToken.isCancelled, true);
});

Expand Down
Loading