diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/dio_retry.iml b/.idea/dio_retry.iml new file mode 100644 index 0000000..000ea5f --- /dev/null +++ b/.idea/dio_retry.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6a1064f --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..51a6b7d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index c078e2b..712445f 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,7 +6,7 @@ "configurations": [ { "name": "Dart", - "program": "bin/main.dart", + "program": "dio_retry/example/example.dart", "request": "launch", "type": "dart" } diff --git a/dio_retry/example/example.dart b/dio_retry/example/example.dart index 70e2a0f..60a02d2 100644 --- a/dio_retry/example/example.dart +++ b/dio_retry/example/example.dart @@ -14,16 +14,16 @@ main() async { // Add the interceptor with optional options dio.interceptors.add(RetryInterceptor( dio: dio, - logger: Logger("Retry"), + logger: Logger('Retry'), options: const RetryOptions( - retryInterval: const Duration(seconds: 5), + retryInterval: Duration(seconds: 5), ), )); /// Sending a failing request for 3 times with a 5s interval try { - await dio.get("http://www.mqldkfjmdisljfmlksqdjfmlkj.dev"); + await dio.get('http://www.apple111.com'); } catch (e) { - print("End error : $e"); + print('End error : $e'); } } diff --git a/dio_retry/lib/src/options.dart b/dio_retry/lib/src/options.dart index ef8543b..56f4180 100644 --- a/dio_retry/lib/src/options.dart +++ b/dio_retry/lib/src/options.dart @@ -11,14 +11,14 @@ class RetryOptions { /// The interval before a retry. final Duration retryInterval; - /// Evaluating if a retry is necessary.regarding the error. - /// - /// It can be a good candidate for additional operations too, like - /// updating authentication token in case of a unauthorized error (be careful - /// with concurrency though). - /// + /// Evaluating if a retry is necessary.regarding the error. + /// + /// It can be a good candidate for additional operations too, like + /// updating authentication token in case of a unauthorized error (be careful + /// with concurrency though). + /// /// Defaults to [defaultRetryEvaluator]. - RetryEvaluator get retryEvaluator => this._retryEvaluator ?? defaultRetryEvaluator; + RetryEvaluator get retryEvaluator => _retryEvaluator ?? defaultRetryEvaluator; final RetryEvaluator _retryEvaluator; @@ -28,7 +28,7 @@ class RetryOptions { this.retryInterval = const Duration(seconds: 1)}) : assert(retries != null), assert(retryInterval != null), - this._retryEvaluator = retryEvaluator; + _retryEvaluator = retryEvaluator; factory RetryOptions.noRetry() { return RetryOptions( @@ -36,12 +36,13 @@ class RetryOptions { ); } - static const extraKey = "cache_retry_request"; + static const extraKey = 'cache_retry_request'; /// Returns [true] only if the response hasn't been cancelled or got /// a bas status code. static FutureOr defaultRetryEvaluator(DioError error) { - return error.type != DioErrorType.CANCEL && error.type != DioErrorType.RESPONSE; + return error.type != DioErrorType.cancel && + error.type != DioErrorType.response; } factory RetryOptions.fromExtra(RequestOptions request) { @@ -64,16 +65,13 @@ class RetryOptions { } Options toOptions() { - return Options( - extra: this.toExtra() - ); + return Options(extra: toExtra()); } - Options mergeIn(Options options) { - return options.merge( - extra: {} - ..addAll(options.extra ?? {}) - ..addAll(this.toExtra()) - ); - } + // Options mergeIn(Options options) { + // return options.merge( + // extra: {} + // ..addAll(options.extra ?? {}) + // ..addAll(toExtra())); + // } } diff --git a/dio_retry/lib/src/retry_interceptor.dart b/dio_retry/lib/src/retry_interceptor.dart index 7a58275..fd571f1 100644 --- a/dio_retry/lib/src/retry_interceptor.dart +++ b/dio_retry/lib/src/retry_interceptor.dart @@ -11,11 +11,11 @@ class RetryInterceptor extends Interceptor { final RetryOptions options; RetryInterceptor({@required this.dio, this.logger, RetryOptions options}) - : this.options = options ?? const RetryOptions(); + : options = options ?? const RetryOptions(); @override - onError(DioError err) async { - var extra = RetryOptions.fromExtra(err.request) ?? this.options; + Future onError(DioError err, ErrorInterceptorHandler handler) async { + var extra = RetryOptions.fromExtra(err.requestOptions) ?? options; // var shouldRetry = extra.retries > 0 && await extra.retryEvaluator(err); (bugged, as per https://github.com/aloisdeniel/dio_retry/pull/5) var shouldRetry = extra.retries > 0 && await options.retryEvaluator(err); @@ -26,26 +26,27 @@ class RetryInterceptor extends Interceptor { // Update options to decrease retry count before new try extra = extra.copyWith(retries: extra.retries - 1); - err.request.extra = err.request.extra..addAll(extra.toExtra()); + err.requestOptions.extra = err.requestOptions.extra + ..addAll(extra.toExtra()); try { logger?.warning( - "[${err.request.uri}] An error occured during request, trying a again (remaining tries: ${extra.retries}, error: ${err.error})"); + '[${err.requestOptions.path}] An error occured during request, trying a again (remaining tries: ${extra.retries}, error: ${err.error})'); // We retry with the updated options - return await this.dio.request( - err.request.path, - cancelToken: err.request.cancelToken, - data: err.request.data, - onReceiveProgress: err.request.onReceiveProgress, - onSendProgress: err.request.onSendProgress, - queryParameters: err.request.queryParameters, - options: err.request, - ); + return await dio.request( + err.requestOptions.path, + cancelToken: err.requestOptions.cancelToken, + data: err.requestOptions.data, + onReceiveProgress: err.requestOptions.onReceiveProgress, + onSendProgress: err.requestOptions.onSendProgress, + queryParameters: err.requestOptions.queryParameters, + options: extra.toOptions(), + ); } catch (e) { return e; } } - return super.onError(err); + return super.onError(err, handler); } } diff --git a/dio_retry/pubspec.yaml b/dio_retry/pubspec.yaml index 6d52320..e89d321 100644 --- a/dio_retry/pubspec.yaml +++ b/dio_retry/pubspec.yaml @@ -1,16 +1,16 @@ name: dio_retry description: A starting point for Dart libraries or applications. -version: 0.1.10-beta +version: 0.1.10-beta3 homepage: https://github.com/aloisdeniel/dio_retry author: aloisdeniel environment: - sdk: '>=2.2.0 <3.0.0' + sdk: '>=2.10.0 <3.0.0' dependencies: - meta: ^1.1.6 - logging: ^0.11.3 - dio: ^3.0.0 + meta: ^1.3.0 + logging: ^1.0.1 + dio: ^4.0.0 dev_dependencies: pedantic: ^1.0.0