onestudio-co / flutter-bond

Your next Flutter project template!
178 stars 49 forks source link

Api Cache #19

Open devmsh opened 2 years ago

devmsh commented 2 years ago

https://parseplatform.org/Parse-SDK-Android/api/com/parse/ParseQuery.CachePolicy.html

salahamassi commented 2 years ago

Api Cache Proposal:

Suggested Cache Policies:

I skipped these two policies:

and I and I added one optional policy for the (get post, put and delete) requests:

This is just a pseudo code


enum CachePolicy {
  cacheElseNetwork,
  cacheThenNetwork,
  networkElseCache,
  networkOnly
}

class CachableApiClient {

 final HttpClient _client;
 final CacheDriver _cache;

 CachableApiClient(this._client, this._cache);

  Stream<T> get<T>(
        String path, {
        Map<String, dynamic>? queryParameters,
        Options? options,
        CachePolicy? policy = null,
       bool smartRequestPolicyOnConnectivityFailed = false,
      });

  Stream<T> post<T>(
          String path, {
          Map<String, dynamic>? queryParameters,
          Options? options,
          bool smartRequestPolicyOnConnectivityFailed = false,
        });

  Stream<T> put<T>(
          String path, {
          Map<String, dynamic>? queryParameters,
          Options? options,
         bool smartRequestPolicyOnConnectivityFailed = false,
        }) ;

  Future<T> delete<T>(
          String path, {
          Map<String, dynamic>? queryParameters,
          Options? options,
         bool smartRequestPolicyOnConnectivityFailed = false,
        });
}

most of the methods return stream due to the fact it emits more than one result from multi-data sources (cache and remote) and on else cache policy we can emit an error with the stream to tell the client about the failure of fetch operation from the first data source, then emit the results from the fallback data source.

smartRequestPolicyOnConnectivityFailed, will try to perform the request again directly when the device back to online mode.

How to use:

WIP