From 910eeeeda1e7e8ec88bd865aa2d38e61f97611b4 Mon Sep 17 00:00:00 2001 From: Dmitry Gaimaldinov Date: Thu, 19 Sep 2024 13:08:31 +0500 Subject: [PATCH] Fix file not found error --- lib/dio_smart_retry.dart | 1 - lib/src/multipart_file_recreatable.dart | 67 ---------------------- lib/src/retry_interceptor.dart | 54 +---------------- lib/src/retry_not_supported_exception.dart | 11 ---- test/multipart_retry_tests.dart | 29 +--------- 5 files changed, 4 insertions(+), 158 deletions(-) delete mode 100644 lib/src/multipart_file_recreatable.dart delete mode 100644 lib/src/retry_not_supported_exception.dart diff --git a/lib/dio_smart_retry.dart b/lib/dio_smart_retry.dart index 128a86b..a7c8e06 100644 --- a/lib/dio_smart_retry.dart +++ b/lib/dio_smart_retry.dart @@ -3,5 +3,4 @@ library dio_smart_retry; export 'src/default_retry_evaluator.dart'; export 'src/http_status_codes.dart'; -export 'src/multipart_file_recreatable.dart'; export 'src/retry_interceptor.dart'; diff --git a/lib/src/multipart_file_recreatable.dart b/lib/src/multipart_file_recreatable.dart deleted file mode 100644 index 768380e..0000000 --- a/lib/src/multipart_file_recreatable.dart +++ /dev/null @@ -1,67 +0,0 @@ -import 'dart:async'; -import 'dart:io'; -import 'package:dio/dio.dart'; -import 'package:http_parser/http_parser.dart'; -import 'package:path/path.dart' as p; - -/// Creates an instance of [MultipartFile] that can be recreated and reused. -class MultipartFileRecreatable extends MultipartFile { - /// Default constructor. - MultipartFileRecreatable( - super.stream, - super.length, { - super.filename, - super.contentType, - super.headers, - }) : data = stream; - - /// Creates a [MultipartFileRecreatable] object with [bytes]. - factory MultipartFileRecreatable.fromBytes( - List bytes, { - String? filename, - MediaType? contentType, - Map>? headers, - }) { - return MultipartFileRecreatable( - Stream.fromIterable(>[bytes]), - bytes.length, - filename: filename, - contentType: contentType, - headers: headers, - ); - } - - /// Creates a [MultipartFileRecreatable] object from a [File] in [filePath]. - factory MultipartFileRecreatable.fromFileSync( - String filePath, { - String? filename, - MediaType? contentType, - Map>? headers, - }) { - filename ??= p.basename(filePath); - final file = File(filePath); - final length = file.lengthSync(); - final stream = file.openRead(); - return MultipartFileRecreatable( - stream, - length, - filename: filename, - contentType: contentType, - headers: headers, - ); - } - - /// The stream that will emit the file's contents. - final Stream> data; - - /// Recreates the [MultipartFileRecreatable] object. - MultipartFileRecreatable recreate() { - return MultipartFileRecreatable( - data, - length, - filename: filename, - contentType: contentType, - headers: headers, - ); - } -} diff --git a/lib/src/retry_interceptor.dart b/lib/src/retry_interceptor.dart index d977fd2..ef91cc8 100644 --- a/lib/src/retry_interceptor.dart +++ b/lib/src/retry_interceptor.dart @@ -3,8 +3,6 @@ import 'dart:async'; import 'package:dio/dio.dart'; import 'package:dio_smart_retry/src/default_retry_evaluator.dart'; import 'package:dio_smart_retry/src/http_status_codes.dart'; -import 'package:dio_smart_retry/src/multipart_file_recreatable.dart'; -import 'package:dio_smart_retry/src/retry_not_supported_exception.dart'; typedef RetryEvaluator = FutureOr Function( DioException error, @@ -46,9 +44,6 @@ class RetryInterceptor extends Interceptor { } } - static const _multipartRetryHelpLink = - 'https://github.com/rodion-m/dio_smart_retry#retry-requests-with-multipartform-data'; - /// The original dio final Dio dio; @@ -101,12 +96,6 @@ class RetryInterceptor extends Interceptor { return true; } - @override - void onRequest(RequestOptions options, RequestInterceptorHandler handler) { - _printErrorIfRequestHasMultipartFile(options); - super.onRequest(options, handler); - } - @override Future onError( DioException err, @@ -137,14 +126,7 @@ class RetryInterceptor extends Interceptor { var requestOptions = err.requestOptions; if (requestOptions.data is FormData) { - try { - requestOptions = _recreateOptions(err.requestOptions); - } on RetryNotSupportedException catch (e) { - return super.onError( - DioException(requestOptions: requestOptions, error: e), - handler, - ); - } + requestOptions = _recreateOptions(err.requestOptions); } if (delay != Duration.zero) { @@ -179,41 +161,9 @@ class RetryInterceptor extends Interceptor { ); } final formData = options.data as FormData; - final newFormData = FormData(); - newFormData.fields.addAll(formData.fields); - for (final pair in formData.files) { - final file = pair.value; - if (file is MultipartFileRecreatable) { - newFormData.files.add(MapEntry(pair.key, file.recreate())); - } else { - throw RetryNotSupportedException( - 'Use MultipartFileRecreatable class ' - 'instead of MultipartFile to make retry available. ' - 'See: $_multipartRetryHelpLink', - ); - } - } + final newFormData = formData.clone(); return options.copyWith(data: newFormData); } - - var _multipartFileChecked = false; - - void _printErrorIfRequestHasMultipartFile(RequestOptions options) { - if (_multipartFileChecked) return; - if (options.data is FormData) { - final data = options.data as FormData; - if (data.files.any((pair) => pair.value is! MultipartFileRecreatable)) { - final printer = logPrint ?? print; - printer( - 'WARNING: Retry is not supported for MultipartFile class. ' - 'Use MultipartFileRecreatable class ' - 'instead of MultipartFile to make retry available. ' - 'See: $_multipartRetryHelpLink', - ); - } - } - _multipartFileChecked = true; - } } const _kDisableRetryKey = 'ro_disable_retry'; diff --git a/lib/src/retry_not_supported_exception.dart b/lib/src/retry_not_supported_exception.dart deleted file mode 100644 index 5192193..0000000 --- a/lib/src/retry_not_supported_exception.dart +++ /dev/null @@ -1,11 +0,0 @@ -class RetryNotSupportedException implements Exception { - RetryNotSupportedException([this.message]); - - final String? message; - - @override - String toString() { - if (message == null) return 'RetryNotSupportedException'; - return 'RetryNotSupportedException: $message'; - } -} diff --git a/test/multipart_retry_tests.dart b/test/multipart_retry_tests.dart index dd31293..26b71da 100644 --- a/test/multipart_retry_tests.dart +++ b/test/multipart_retry_tests.dart @@ -1,35 +1,10 @@ @TestOn('vm') import 'package:dio/dio.dart'; import 'package:dio_smart_retry/dio_smart_retry.dart'; -import 'package:dio_smart_retry/src/retry_not_supported_exception.dart'; import 'package:test/test.dart'; void main() { - test('Retry for MultipartFile throws RetryNotSupportedException', () async { - final dio = Dio(); - dynamic exception; - dio.interceptors.add( - RetryInterceptor( - dio: dio, - logPrint: print, - ), - ); - - final formData = - FormData.fromMap({'file': MultipartFile.fromFileSync('README.md')}); - try { - await dio.post( - 'https://rodion-m.ru/mock/post500.php', - data: formData, - ); - } on DioException catch (error) { - exception = error.error; - } - - expect(exception is RetryNotSupportedException, true); - }); - - test('Retry for MultipartFileRecreatable is retrying', () async { + test('Retry for MultipartFile is retrying', () async { final dio = Dio(); const retries = 2; final evaluator = DefaultRetryEvaluator(defaultRetryableStatuses); @@ -44,7 +19,7 @@ void main() { ); final formData = FormData.fromMap({ - 'file': MultipartFileRecreatable.fromFileSync('README.md'), + 'file': MultipartFile.fromFileSync('README.md'), }); try { await dio.post(