Skip to content

Commit

Permalink
feat(caching): add apiOptions parameter to api service
Browse files Browse the repository at this point in the history
Signed-off-by: Vatsal Gandhi <[email protected]>
  • Loading branch information
vatsal201 authored and TusharFA committed May 23, 2024
1 parent de80225 commit 9eff30b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
3 changes: 3 additions & 0 deletions lib/src/api_options/api_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ApiOptions {
this.receiveTimeout,
this.sendTimeout,
this.refreshCache,
this.cacheResponse,
this.expireDuration = const Duration(days: 1),
this.cancelToken,
this.ignoreAutoRefresh = false,
Expand All @@ -19,6 +20,8 @@ class ApiOptions {

bool? refreshCache;

bool? cacheResponse;

Duration? expireDuration;

CancelToken? cancelToken;
Expand Down
18 changes: 13 additions & 5 deletions lib/src/api_service_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ApiServiceImpl implements ApiService {
ApiServiceImpl({
required this.baseUrl,
this.interceptors,
this.apiOptions,
}) {
_dio = Dio()
..options.contentType = Headers.jsonContentType
Expand All @@ -34,6 +35,7 @@ class ApiServiceImpl implements ApiService {

String baseUrl;
Dio? _dio;
ApiOptions? apiOptions;

Dio? _dioFile;

Expand Down Expand Up @@ -187,13 +189,19 @@ class ApiServiceImpl implements ApiService {
seconds: 1,
),
);
final expireDuration =
options?.expireDuration ?? apiOptions?.expireDuration;
headers['appSpecificHeaders'] = {
"forceRefreshCache": options?.refreshCache ?? false,
"expirationTime": options?.expireDuration != null
? _now.add(options!.expireDuration!).toString()
"forceRefreshCache":
options?.refreshCache ?? apiOptions?.refreshCache ?? false,
"expirationTime": expireDuration != null
? _now.add(expireDuration).toString()
: _midnightTime.toString(),
"expireDuration": options?.expireDuration,
"ignoreAutoRefresh": options?.ignoreAutoRefresh ?? false,
"expireDuration": options?.expireDuration ?? apiOptions?.expireDuration,
"ignoreAutoRefresh":
options?.ignoreAutoRefresh ?? apiOptions?.ignoreAutoRefresh ?? false,
"cacheResponse":
options?.cacheResponse ?? apiOptions?.cacheResponse ?? true
};
return headers;
}
Expand Down
5 changes: 3 additions & 2 deletions lib/src/interceptors/cache_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ abstract class ApiCacheInterceptor extends Interceptor {
),
);
final status = response.statusCode ?? 0;
final cacheResponse =
response.requestOptions.headers['cacheResponse'] ?? true;
final cacheResponse = response.requestOptions.headers['appSpecificHeaders']
?['cacheResponse'] ??
true;

if ((status == 200 || status == 201 || status == 202) && cacheResponse) {
// await sembastHelper.put(sembastHelper.record(_dataStore, id), response);
Expand Down

0 comments on commit 9eff30b

Please sign in to comment.