@@ -2,7 +2,10 @@ part of 'services.dart';
22
33class ApiProvider {
44 factory ApiProvider () => _apiProvider;
5- ApiProvider ._() {
5+ ApiProvider ._();
6+ static final ApiProvider _apiProvider = ApiProvider ._();
7+
8+ Future <void > initialize () async {
69 final auth = UserRepository ().currentAppAuthentication;
710
811 final client = Dio (
@@ -23,8 +26,42 @@ class ApiProvider {
2326 );
2427 }
2528
29+ final documentsDir = await getApplicationDocumentsDirectory ();
30+
31+ // Global options
32+ final options = CacheOptions (
33+ // A default store is required for interceptor.
34+ store: SembastCacheStore (storePath: documentsDir.path),
35+
36+ // All subsequent fields are optional.
37+
38+ // Default.
39+ policy: CachePolicy .noCache,
40+ // Returns a cached response on error but for statuses 401 & 403.
41+ // Also allows to return a cached response on network errors (e.g. offline usage).
42+ // Defaults to [null].
43+ hitCacheOnErrorExcept: [401 , 403 ],
44+ // Overrides any HTTP directive to delete entry past this duration.
45+ // Useful only when origin server has no cache config or custom behaviour is desired.
46+ // Defaults to [null].
47+ maxStale: const Duration (days: 7 ),
48+ // Default. Allows 3 cache sets and ease cleanup.
49+ priority: CachePriority .normal,
50+ // Default. Body and headers encryption with your own algorithm.
51+ cipher: null ,
52+ // Default. Key builder to retrieve requests.
53+ keyBuilder: CacheOptions .defaultCacheKeyBuilder,
54+ // Default. Allows to cache POST requests.
55+ // Overriding [keyBuilder] is strongly recommended when [true].
56+ allowPostMethod: false ,
57+ );
58+
2659 ncCookbookApi = NcCookbookApi (
27- dio: client,
60+ basePathOverride: '${auth .server }/apps/cookbook' ,
61+ interceptors: [
62+ BasicAuthInterceptor (),
63+ DioCacheInterceptor (options: options),
64+ ],
2865 );
2966
3067 ncCookbookApi.setBasicAuth (
@@ -37,7 +74,6 @@ class ApiProvider {
3774 miscApi = ncCookbookApi.getMiscApi ();
3875 tagsApi = ncCookbookApi.getTagsApi ();
3976 }
40- static final ApiProvider _apiProvider = ApiProvider ._();
4177
4278 late NcCookbookApi ncCookbookApi;
4379 late RecipesApi recipeApi;
0 commit comments