hurshi / dio-http-cache

http cache lib for Flutter dio like RxCache
Apache License 2.0
274 stars 223 forks source link

Use with other interceptors and offline testing #57

Closed ghenry closed 3 years ago

ghenry commented 4 years ago

Hi all,

I can't seem to get offline mode to work. I have the following and I see some sql logs.

Logs:

W/SQLiteLog(20310): (28) double-quoted string literal: "09204E34E6BB9B48A29FD8F8008D725D"
W/SQLiteLog(20310): (28) double-quoted string literal: "D7CF0CC9A790EF556B05A20DBD38F479"

My setup and code:

      logger.d('Fetching calls from API...');

      String _surevoipAPI = SUREVOIP_API_PROD;
      if (isInDebugMode) {
        _surevoipAPI = SUREVOIP_API_DEV;
      }

      // Serve up data on device for offline usage.
      dio.interceptors.add(DioCacheManager(CacheConfig()).interceptor);
      Options _cacheOptions =
          buildCacheOptions(Duration(days: 7), maxStale: Duration(days: 14));

      dio.interceptors.add(handleExpiredTokenInterceptor);

      dio.options.connectTimeout = 5000; //5s
      dio.options.receiveTimeout = 3000;
      dio.options.headers = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer $accessToken',
        'Accept': 'application/json'
      };

      // TODO: Allow user to chose Today, 7 Days etc.
      String _sevenDays = DateTime.now()
          .subtract(Duration(days: 7))
          .toUtc()
          .toIso8601String();

      dio.options.queryParameters = {
        'hypermedia': 'no',
        'startdate': _sevenDays,
      };
      //dio.interceptors.add(LogInterceptor(responseBody: false));

      final AuthController ac = Get.find();
      final _customerNumber = ac.accountNumber;

      final response = await dio.get(
          _surevoipAPI + '/customers/' + _customerNumber + '/calls',
          options: _cacheOptions);
      logger.d("Dio response: ");
      logger.d(response.data);

Above works, but when I go into airplane mode I get:

DioError [DioErrorType.DEFAULT]: SocketException: Failed host lookup: 'xxxxx' (OS Error: No address associated with hostname, errno = 7)
ghenry commented 3 years ago

Would the cache initially, before going offline, be filled only if the API/website had the correct cache headers?

ghenry commented 3 years ago

Anyone any ideas? I'm just digging into the source code to see what I should expect.

Thanks.

kauaicreative commented 3 years ago

This is probably the solution https://github.com/hurshi/dio-http-cache/issues/2

ghenry commented 3 years ago

That's in my example code already though?

Thanks.

On Sun, 11 Oct 2020, 17:35 Sunny, notifications@github.com wrote:

This is probably the solution #2 https://github.com/hurshi/dio-http-cache/issues/2

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/hurshi/dio-http-cache/issues/57#issuecomment-706731264, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABG66AGEYK52Y7ZDCCQIPTSKHNDNANCNFSM4RQREWNA .

tiagocdr commented 1 year ago

Having the same issue I don't understand how to work this

TianyongWang commented 1 year ago

来信已收到