Skip to content

Commit

Permalink
feat: add maxRetryCount param
Browse files Browse the repository at this point in the history
  • Loading branch information
bedirhanssaglam committed Sep 3, 2024
1 parent 04a2019 commit 81ed2fb
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 9 deletions.
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

0 comments on commit 81ed2fb

Please sign in to comment.