Utility Modules#
Cache Keys#
- requests_cache.cache_keys.create_key(request, ignored_params=None, include_get_headers=False, **kwargs)[source]#
Create a normalized cache key from a request object
- Return type
- requests_cache.cache_keys.decode(value, encoding='utf-8')[source]#
Decode a value from bytes, if hasn’t already been. Note: PreparedRequest.body is always encoded in utf-8.
- Return type
- requests_cache.cache_keys.encode(value, encoding='utf-8')[source]#
Encode a value to bytes, if it hasn’t already been
- Return type
Cache-Control#
Utilities for determining cache expiration and other cache actions
- class requests_cache.cache_control.CacheActions(cache_key, request, cache_control=False, **kwargs)[source]#
A dataclass that contains info on specific actions to take for a given cache item. This is determined by a combination of cache settings and request + response headers. If multiple sources are provided, they will be used in the following order of precedence:
Cache-Control request headers (if enabled)
Cache-Control response headers (if enabled)
Per-request expiration
Per-URL expiration
Per-session expiration
- property expires: Optional[datetime.datetime]#
Convert the user/header-provided expiration value to a datetime
- requests_cache.cache_control.coalesce(*values, default=None)[source]#
Get the first non-
None
value in a list of values- Return type
- requests_cache.cache_control.get_cache_directives(headers)[source]#
Get all Cache-Control directives, and handle multiple headers and comma-separated lists
- Return type
- requests_cache.cache_control.get_expiration_datetime(expire_after)[source]#
Convert an expiration value in any supported format to an absolute datetime
- requests_cache.cache_control.get_url_expiration(url, urls_expire_after=None)[source]#
Check for a matching per-URL expiration, if any
- requests_cache.cache_control.has_cache_headers(headers)[source]#
Determine if headers contain cache directives that we currently support
- Return type
- requests_cache.cache_control.parse_http_date(value)[source]#
Attempt to parse an HTTP (RFC 5322-compatible) timestamp
- requests_cache.cache_control.split_kv_directive(header_value)[source]#
Split a cache directive into a
(header_value, int)
key-value pair, if possible; otherwise just(header_value, True)
.
- requests_cache.cache_control.to_utc(dt)[source]#
All internal datetimes are UTC and timezone-naive. Convert any user/header-provided datetimes to the same format.
- requests_cache.cache_control.try_int(value)[source]#
Convert a string value to an int, if possible, otherwise
None
- requests_cache.cache_control.url_match(url, pattern)[source]#
Determine if a URL matches a pattern
- Parameters
Example
>>> url_match('https://httpbin.org/delay/1', 'httpbin.org/delay') True >>> url_match('https://httpbin.org/stream/1', 'httpbin.org/*/1') True >>> url_match('https://httpbin.org/stream/2', 'httpbin.org/*/1') False
- Return type