Skip to content

Commit

Permalink
[ApiOptions] Add cancel token to base methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Vipin9821 authored and TusharFA committed Jan 4, 2024
1 parent fd87957 commit b80a47b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/src/api_options/api_options.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import 'package:dio/dio.dart';

class ApiOptions {
ApiOptions({
this.headers = const {},
this.receiveTimeout,
this.sendTimeout,
this.refreshCache,
this.expireDuration = const Duration(days: 1),
this.cancelToken,
});

Map<String, dynamic>? headers;
Expand All @@ -16,4 +19,6 @@ class ApiOptions {
bool? refreshCache;

Duration? expireDuration;

CancelToken? cancelToken;
}
6 changes: 6 additions & 0 deletions lib/src/api_service_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class ApiServiceImpl implements ApiService {
ApiOptions? options,
}) async {
return _dio!.get<T>(checkIfNotEmpty(url) ? '$url' : '$baseUrl$endpoint',
cancelToken: options?.cancelToken,
options: Options(
headers: _formatHeaders(options),
receiveTimeout: options?.receiveTimeout,
Expand All @@ -62,6 +63,7 @@ class ApiServiceImpl implements ApiService {
}) async {
return _dio!.post<T>(url ?? '$baseUrl$endpoint',
data: body,
cancelToken: options?.cancelToken,
options: Options(
headers: _formatHeaders(options),
receiveTimeout: options?.receiveTimeout,
Expand All @@ -75,6 +77,7 @@ class ApiServiceImpl implements ApiService {
ApiOptions? options,
}) async {
return _dio!.delete<T>('$baseUrl$endpoint',
cancelToken: options?.cancelToken,
options: Options(
headers: _formatHeaders(options),
receiveTimeout: options?.receiveTimeout,
Expand All @@ -89,6 +92,7 @@ class ApiServiceImpl implements ApiService {
ApiOptions? options,
}) async {
return _dio!.put<T>('$baseUrl$endpoint',
cancelToken: options?.cancelToken,
data: body,
options: Options(
headers: _formatHeaders(options),
Expand All @@ -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';
Expand All @@ -123,6 +128,7 @@ class ApiServiceImpl implements ApiService {
});
return _dioFile!.post<T>(
endpoint,
cancelToken: options?.cancelToken,
data: formData,
onSendProgress: onSendProgress,
);
Expand Down

0 comments on commit b80a47b

Please sign in to comment.