dio_cache_interceptor icon indicating copy to clipboard operation
dio_cache_interceptor copied to clipboard

Extend supported methods

Open FixThisWorld opened this issue 1 year ago • 4 comments

Currently, only GET requests seem to be handled and optionally POST methods, if allowPostMethod == true. I suggest replacing allowPostMethod with allowedMethods: Set<String>, so the user can configure which methods should be cached. As HTTP is used e.g. for WebDAV, that set may even include non-standard HTTP methods like PROPFIND, so just hardcoding all HTTP methods seems too restrictive to me. This should be a minor change in the _shouldSkip() function.

FixThisWorld avatar Nov 20 '23 15:11 FixThisWorld

In addition to the above, and I'm not sure if the feature already exists, but anyway, it is good if we have an option to skip the caching for a specific route/endpoint. Basically, I need to be able to cache all the GET requests of the app except for a list of defined endpoints. This can be added to _shouldSkip() function as well, so you can accept a list like final List<String> skipForRoutes, and use this list to see which request to skip the caching for. Thank you!

mahmoud-othmane avatar Nov 30 '23 14:11 mahmoud-othmane

In addition to the above, and I'm not sure if the feature already exists, but anyway, it is good if we have an option to skip the caching for a specific route/endpoint. Basically, I need to be able to cache all the GET requests of the app except for a list of defined endpoints. This can be added to _shouldSkip() function as well, so you can accept a list like final List<String> skipForRoutes, and use this list to see which request to skip the caching for. Thank you!

I might have found a way to go around this, but it seems like hack and not a straightforward solution, but I think it works. Basically, in the extra pass the policy you need for the specific request with a key @cache_options@, which is defined as follows in the package:

  // Key to retrieve options from request
  static const _extraKey = '@cache_options@';

In my app I do this:

   extra: {
        "@cache_options@": currentCacheOptions.copyWith(
          policy: CachePolicy.noCache,
        ),
      },

mahmoud-othmane avatar Nov 30 '23 14:11 mahmoud-othmane

@mahmoud-othmane you can do this explicitly for each route. This option is available like shown in the readme.

response = await dio.get('https://www.foo.com',
  options: options.copyWith(policy: CachePolicy.noCache).toOptions(),
);

This way allows to cache every routes but those you've chosen or you can invert it by specifying CachePolicy.noCache by default.

llfbandit avatar Nov 30 '23 14:11 llfbandit

Oh okay got it. Thank you! @llfbandit

mahmoud-othmane avatar Nov 30 '23 14:11 mahmoud-othmane