From b80a47b37a52ec1f859192eba48ea1336f69db60 Mon Sep 17 00:00:00 2001 From: vipin9821 <50833659+Vipin9821@users.noreply.github.com> Date: Thu, 4 Jan 2024 12:03:55 +0530 Subject: [PATCH] [ApiOptions] Add cancel token to base methods --- lib/src/api_options/api_options.dart | 5 +++++ lib/src/api_service_impl.dart | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/lib/src/api_options/api_options.dart b/lib/src/api_options/api_options.dart index ccee724..98e2f30 100644 --- a/lib/src/api_options/api_options.dart +++ b/lib/src/api_options/api_options.dart @@ -1,3 +1,5 @@ +import 'package:dio/dio.dart'; + class ApiOptions { ApiOptions({ this.headers = const {}, @@ -5,6 +7,7 @@ class ApiOptions { this.sendTimeout, this.refreshCache, this.expireDuration = const Duration(days: 1), + this.cancelToken, }); Map? headers; @@ -16,4 +19,6 @@ class ApiOptions { bool? refreshCache; Duration? expireDuration; + + CancelToken? cancelToken; } diff --git a/lib/src/api_service_impl.dart b/lib/src/api_service_impl.dart index 4cfbf70..f814a04 100644 --- a/lib/src/api_service_impl.dart +++ b/lib/src/api_service_impl.dart @@ -46,6 +46,7 @@ class ApiServiceImpl implements ApiService { ApiOptions? options, }) async { return _dio!.get(checkIfNotEmpty(url) ? '$url' : '$baseUrl$endpoint', + cancelToken: options?.cancelToken, options: Options( headers: _formatHeaders(options), receiveTimeout: options?.receiveTimeout, @@ -62,6 +63,7 @@ class ApiServiceImpl implements ApiService { }) async { return _dio!.post(url ?? '$baseUrl$endpoint', data: body, + cancelToken: options?.cancelToken, options: Options( headers: _formatHeaders(options), receiveTimeout: options?.receiveTimeout, @@ -75,6 +77,7 @@ class ApiServiceImpl implements ApiService { ApiOptions? options, }) async { return _dio!.delete('$baseUrl$endpoint', + cancelToken: options?.cancelToken, options: Options( headers: _formatHeaders(options), receiveTimeout: options?.receiveTimeout, @@ -89,6 +92,7 @@ class ApiServiceImpl implements ApiService { ApiOptions? options, }) async { return _dio!.put('$baseUrl$endpoint', + cancelToken: options?.cancelToken, data: body, options: Options( headers: _formatHeaders(options), @@ -103,6 +107,7 @@ class ApiServiceImpl implements ApiService { String? keyName, File? file, ProgressCallback? onSendProgress, + ApiOptions? options, }) async { endpoint = endpoint != null ? "$baseUrl$endpoint" : getFileUploadUrl(); keyName = keyName ?? 'asset'; @@ -123,6 +128,7 @@ class ApiServiceImpl implements ApiService { }); return _dioFile!.post( endpoint, + cancelToken: options?.cancelToken, data: formData, onSendProgress: onSendProgress, );