diff options
| author | Jordan Cook <jordan.cook@pioneer.com> | 2022-03-19 00:13:06 -0500 |
|---|---|---|
| committer | Jordan Cook <jordan.cook@pioneer.com> | 2022-03-29 12:17:43 -0500 |
| commit | 4b524f7298093bb4c27cb3419d4ddafc7e42891e (patch) | |
| tree | c2c2a5f532c28bb4944efa43813deedbf2e2efb4 /requests_cache/models | |
| parent | 01f4652b51c31f245f7d9d73da38b80f9c5b6688 (diff) | |
| download | requests-cache-4b524f7298093bb4c27cb3419d4ddafc7e42891e.tar.gz | |
Refactor request-level settings into separate RequestSettings class
Diffstat (limited to 'requests_cache/models')
| -rw-r--r-- | requests_cache/models/__init__.py | 2 | ||||
| -rw-r--r-- | requests_cache/models/settings.py | 34 |
2 files changed, 31 insertions, 5 deletions
diff --git a/requests_cache/models/__init__.py b/requests_cache/models/__init__.py index 497ca3f..1931ea2 100644 --- a/requests_cache/models/__init__.py +++ b/requests_cache/models/__init__.py @@ -7,7 +7,7 @@ from requests import PreparedRequest, Request, Response from .raw_response import CachedHTTPResponse from .request import CachedRequest from .response import CachedResponse, set_response_defaults -from .settings import CacheSettings +from .settings import CacheSettings, RequestSettings AnyResponse = Union[Response, CachedResponse] AnyRequest = Union[Request, PreparedRequest, CachedRequest] diff --git a/requests_cache/models/settings.py b/requests_cache/models/settings.py index 01ad937..b32df3d 100644 --- a/requests_cache/models/settings.py +++ b/requests_cache/models/settings.py @@ -1,6 +1,6 @@ from typing import TYPE_CHECKING, Callable, Dict, Iterable, Union -from attr import define, field +from attr import asdict, define, field from .._utils import get_valid_kwargs from ..cache_control import ExpirationTime @@ -13,14 +13,24 @@ FilterCallback = Callable[['AnyResponse'], bool] KeyCallback = Callable[..., str] +# TODO: RequestSettings subclass? +# This would work by merging settings for an individual request on top of session settings +# This would also allow for *any* session setting to be passed as a per-request setting +# ...But only for send(), not request() +# TODO: HeaderSettings class? +# This would encapsulate settings from Cache-Control directives and other cache headers +# Downside: that logic would then live in this moduel instead of in cache_control (which may be a better fit) + + @define(init=False) class CacheSettings: - """Class to store cache settings used by :py:class:`.CachedSession` and backends. + """Settings that affect caching behavior, used by :py:class:`.CachedSession` and + :py:class:`.BaseCache`. Args: allowable_codes: Only cache responses with one of these status codes allowable_methods: Cache only responses for one of these HTTP methods - cache_control: Use Cache-Control headers to set expiration + cache_control: Use Cache-Control and other response headers to set expiration expire_after: Time after which cached items will expire filter_fn: Response filtering function that indicates whether or not a given response should be cached. @@ -35,12 +45,13 @@ class CacheSettings: allowable_codes: Iterable[int] = field(default=(200,)) allowable_methods: Iterable[str] = field(default=('GET', 'HEAD')) cache_control: bool = field(default=False) - # cache_disabled: bool = field(default=False) + disabled: bool = field(default=False) expire_after: ExpirationTime = field(default=-1) filter_fn: FilterCallback = field(default=None) ignored_parameters: Iterable[str] = field(default=None) key_fn: KeyCallback = field(default=None) match_headers: Union[Iterable[str], bool] = field(default=False) + only_if_cached: bool = field(default=False) stale_if_error: bool = field(default=False) urls_expire_after: Dict[str, ExpirationTime] = field(factory=dict) @@ -54,3 +65,18 @@ class CacheSettings: # Ignore invalid kwargs for easier initialization from mixed **kwargs kwargs = get_valid_kwargs(self.__attrs_init__, kwargs) self.__attrs_init__(**kwargs) + + +@define(init=False) +class RequestSettings(CacheSettings): + """Cache settings that may be set for an individual request""" + + refresh: bool = field(default=False) + revalidate: bool = field(default=False) + request_expire_after: ExpirationTime = field(default=-1) + + def __init__(self, session_settings: CacheSettings, **kwargs): + # Start with session-level cache settings and add/override with request-level settings + kwargs['request_expire_after'] = kwargs.pop('expire_after', None) + kwargs = {**asdict(session_settings), **kwargs} + super().__init__(**kwargs) |
