Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_buildResponse() renamed to buildResponse(), and changed to protected… #92

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions lib/src/manager_dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:dio/dio.dart';
import 'package:dio_http_cache/src/core/config.dart';
import 'package:dio_http_cache/src/core/manager.dart';
import 'package:dio_http_cache/src/core/obj.dart';
import 'package:flutter/foundation.dart';

const DIO_CACHE_KEY_TRY_CACHE = "dio_cache_try_cache";
const DIO_CACHE_KEY_MAX_AGE = "dio_cache_max_age";
Expand Down Expand Up @@ -47,14 +48,17 @@ class DioCacheManager {
}
var responseDataFromCache = await _pullFromCacheBeforeMaxAge(options);
if (null != responseDataFromCache) {
return handler.resolve(_buildResponse(
responseDataFromCache, responseDataFromCache.statusCode, options), true);
return handler.resolve(
await buildResponse(
responseDataFromCache, responseDataFromCache.statusCode, options),
true);
}
return handler.next(options);
}

_onResponse(Response response, ResponseInterceptorHandler handler) async {
if ((response.requestOptions.extra[DIO_CACHE_KEY_TRY_CACHE] ?? false) == true &&
if ((response.requestOptions.extra[DIO_CACHE_KEY_TRY_CACHE] ?? false) ==
true &&
response.statusCode != null &&
response.statusCode! >= 200 &&
response.statusCode! < 300) {
Expand All @@ -65,9 +69,10 @@ class DioCacheManager {

_onError(DioError e, ErrorInterceptorHandler handler) async {
if ((e.requestOptions.extra[DIO_CACHE_KEY_TRY_CACHE] ?? false) == true) {
var responseDataFromCache = await _pullFromCacheBeforeMaxStale(e.requestOptions);
var responseDataFromCache =
await _pullFromCacheBeforeMaxStale(e.requestOptions);
if (null != responseDataFromCache) {
var response = _buildResponse(responseDataFromCache,
var response = await buildResponse(responseDataFromCache,
responseDataFromCache.statusCode, e.requestOptions);

return handler.resolve(response);
Expand All @@ -76,8 +81,9 @@ class DioCacheManager {
return handler.next(e);
}

Response _buildResponse(
CacheObj obj, int? statusCode, RequestOptions options) {
@protected
Future<Response> buildResponse(
CacheObj obj, int? statusCode, RequestOptions options) async {
Headers? headers;
if (null != obj.headers) {
headers = Headers.fromMap((Map<String, List<dynamic>>.from(
Expand All @@ -97,7 +103,8 @@ class DioCacheManager {
return Response(
data: data,
headers: headers,
requestOptions: options.copyWith(extra: options.extra..remove(DIO_CACHE_KEY_TRY_CACHE)),
requestOptions: options.copyWith(
extra: options.extra..remove(DIO_CACHE_KEY_TRY_CACHE)),
statusCode: statusCode ?? 200);
}

Expand Down Expand Up @@ -147,8 +154,10 @@ class DioCacheManager {
// try to get maxAge and maxStale from cacheControl
Map<String, String?> parameters;
try {
parameters = HeaderValue.parse("${HttpHeaders.cacheControlHeader}: $cacheControl",
parameterSeparator: ",", valueSeparator: "=")
parameters = HeaderValue.parse(
"${HttpHeaders.cacheControlHeader}: $cacheControl",
parameterSeparator: ",",
valueSeparator: "=")
.parameters;
_maxAge = _tryGetDurationFromMap(parameters, "s-maxage");
if (null == _maxAge) {
Expand Down Expand Up @@ -179,7 +188,8 @@ class DioCacheManager {
callback(_maxAge, maxStale);
}

Duration? _tryGetDurationFromMap(Map<String, String?> parameters, String key) {
Duration? _tryGetDurationFromMap(
Map<String, String?> parameters, String key) {
if (parameters.containsKey(key)) {
var value = int.tryParse(parameters[key]!);
if (null != value && value >= 0) {
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ authors:
homepage: https://github.com/hurshi/dio-http-cache

environment:
sdk: '>=2.12.0 <3.0.0'
sdk: '>=2.13.0 <3.0.0'

dependencies:
crypto: ^3.0.0
dio: ^4.0.0
flutter:
sdk: flutter
json_serializable: ^4.0.3
json_serializable: ^6.0.0
path: ^1.8.0
quiver: ^3.0.0
sqflite: ^2.0.0+3
Expand Down