summaryrefslogtreecommitdiff
path: root/requests_cache/models
diff options
context:
space:
mode:
authorJordan Cook <jordan.cook@pioneer.com>2022-03-18 21:05:21 -0500
committerJordan Cook <jordan.cook@pioneer.com>2022-03-29 12:17:43 -0500
commit01f4652b51c31f245f7d9d73da38b80f9c5b6688 (patch)
treee747b7c923c0a980f9bb94a6ea96f5af114defc2 /requests_cache/models
parent4e841eab1553bc9ba41000a9b4a85085836f1b1d (diff)
downloadrequests-cache-01f4652b51c31f245f7d9d73da38b80f9c5b6688.tar.gz
Refactor session-level settings into separate CacheSettings class
Diffstat (limited to 'requests_cache/models')
-rw-r--r--requests_cache/models/__init__.py1
-rw-r--r--requests_cache/models/settings.py56
2 files changed, 57 insertions, 0 deletions
diff --git a/requests_cache/models/__init__.py b/requests_cache/models/__init__.py
index 6ffc7ad..497ca3f 100644
--- a/requests_cache/models/__init__.py
+++ b/requests_cache/models/__init__.py
@@ -7,6 +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
AnyResponse = Union[Response, CachedResponse]
AnyRequest = Union[Request, PreparedRequest, CachedRequest]
diff --git a/requests_cache/models/settings.py b/requests_cache/models/settings.py
new file mode 100644
index 0000000..01ad937
--- /dev/null
+++ b/requests_cache/models/settings.py
@@ -0,0 +1,56 @@
+from typing import TYPE_CHECKING, Callable, Dict, Iterable, Union
+
+from attr import define, field
+
+from .._utils import get_valid_kwargs
+from ..cache_control import ExpirationTime
+
+if TYPE_CHECKING:
+ from . import AnyResponse
+
+# Signatures for user-provided callbacks
+FilterCallback = Callable[['AnyResponse'], bool]
+KeyCallback = Callable[..., str]
+
+
+@define(init=False)
+class CacheSettings:
+ """Class to store cache settings used by :py:class:`.CachedSession` and backends.
+
+ 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
+ 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.
+ ignored_parameters: List of request parameters to not match against, and exclude from the cache
+ key_fn: Request matching function for generating custom cache keys
+ match_headers: Match request headers when reading from the cache; may be either ``True`` or
+ a list of specific headers to match
+ stale_if_error: Return stale cache data if a new request raises an exception
+ urls_expire_after: Expiration times to apply for different URL patterns
+ """
+
+ 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)
+ 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)
+ stale_if_error: bool = field(default=False)
+ urls_expire_after: Dict[str, ExpirationTime] = field(factory=dict)
+
+ def __init__(self, **kwargs):
+ # Backwards-compatibility for old argument names
+ if 'old_data_on_error' in kwargs:
+ kwargs['stale_if_error'] = kwargs.pop('old_data_on_error')
+ if 'include_get_headers' in kwargs:
+ kwargs['match_headers'] = kwargs.pop('include_get_headers')
+
+ # Ignore invalid kwargs for easier initialization from mixed **kwargs
+ kwargs = get_valid_kwargs(self.__attrs_init__, kwargs)
+ self.__attrs_init__(**kwargs)