Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a feature allowing to specify extra retryable status codes (param…
…eter `retryableExtraStatuses`) (#11)
- Loading branch information
Showing
8 changed files
with
116 additions
and
44 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,27 @@ | ||
import 'package:dio/dio.dart'; | ||
import 'package:dio_smart_retry/dio_smart_retry.dart'; | ||
import 'package:dio_smart_retry/src/http_status_codes.dart'; | ||
|
||
Future main() async { | ||
Future<dynamic> main() async { | ||
final dio = Dio(); | ||
|
||
// Add the interceptor | ||
dio.interceptors.add( | ||
RetryInterceptor( | ||
dio: dio, | ||
logPrint: print, // specify log function (optional) | ||
retries: 3, // retry count (optional) | ||
retries: 4, // retry count (optional) | ||
retryDelays: const [ | ||
// set delays between retries (optional) | ||
Duration(seconds: 1), // wait 1 sec before first retry | ||
Duration(seconds: 2), // wait 2 sec before second retry | ||
Duration(seconds: 3), // wait 3 sec before third retry | ||
Duration(seconds: 1), // wait 1 sec before the first retry | ||
Duration(seconds: 2), // wait 2 sec before the second retry | ||
Duration(seconds: 3), // wait 3 sec before the third retry | ||
Duration(seconds: 4), // wait 4 sec before the fourth retry | ||
], | ||
retryableExtraStatuses: { status401Unauthorized }, | ||
), | ||
); | ||
|
||
/// Sending a failing request for 3 times with 1s, then 2s, then 3s interval | ||
await dio.get<dynamic>('https://mock.codes/500'); | ||
await dio.get<dynamic>('https://mock.codes/401'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:dio/dio.dart'; | ||
|
||
class DefaultRetryEvaluator { | ||
DefaultRetryEvaluator(this._retryableStatuses); | ||
|
||
final Set<int> _retryableStatuses; | ||
|
||
/// Returns true only if the response hasn't been cancelled | ||
/// or got a bad status code. | ||
// ignore: avoid-unused-parameters | ||
FutureOr<bool> evaluate(DioError error, int attempt) { | ||
bool shouldRetry; | ||
if (error.type == DioErrorType.response) { | ||
final statusCode = error.response?.statusCode; | ||
if (statusCode != null) { | ||
shouldRetry = isRetryable(statusCode); | ||
} else { | ||
shouldRetry = true; | ||
} | ||
} else { | ||
shouldRetry = | ||
error.type != DioErrorType.cancel && error.error is! FormatException; | ||
} | ||
return shouldRetry; | ||
} | ||
|
||
bool isRetryable(int statusCode) => _retryableStatuses.contains(statusCode); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters