summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--HISTORY.md9
-rw-r--r--README.md2
-rw-r--r--docs/user_guide/matching.md8
-rw-r--r--requests_cache/backends/__init__.py2
-rw-r--r--requests_cache/backends/base.py6
-rw-r--r--requests_cache/cache_keys.py4
-rw-r--r--requests_cache/session.py8
-rw-r--r--tests/unit/test_session.py18
8 files changed, 35 insertions, 22 deletions
diff --git a/HISTORY.md b/HISTORY.md
index 739ae74..70e69b7 100644
--- a/HISTORY.md
+++ b/HISTORY.md
@@ -104,7 +104,14 @@
* Add support for HTTP timestamps (RFC 5322) in ``expire_after`` parameters
* Add support for bypassing the cache if `expire_after=0`
* Add support for making a cache allowlist using URL patterns
-
+GET https://iam.amazonaws.com?Action=ListUsers&Version=2010-05-08&
+X-Amz-Algorithm=AWS4-HMAC-SHA256&
+X-Amz-Credential=AKIDEXAMPLE%2F20150830%2Fus-east-1%2Fiam%2Faws4_request&
+X-Amz-Date=20150830T123600Z&X-Amz-Expires=60&
+X-Amz-SignedHeaders=content-type%3Bhost&
+X-Amz-Signature=37ac2f4fde00b0ac9bd9eadeb459b1bbee224158d66e7ae5fcadb70b2d181d02 HTTP/1.1
+content-type: application/x-www-form-urlencoded; charset=utf-8
+host: iam.amazonaws.com
**Serialization:**
* Add data models for all serialized objects
* Add a JSON serializer
diff --git a/README.md b/README.md
index 0caa1cd..3f8c5ff 100644
--- a/README.md
+++ b/README.md
@@ -102,7 +102,7 @@ session = CachedSession(
expire_after=timedelta(days=1), # Otherwise expire responses after one day
allowable_methods=['GET', 'POST'] # Cache POST requests to avoid sending the same data twice
allowable_codes=[200, 400] # Cache 400 responses as a solemn reminder of your failures
- include_get_headers=True, # Match all request headers
+ match_headers=True, # Match all request headers
ignored_parameters=['api_key'], # Don't match this param or save it in the cache
old_data_on_error=True, # In case of request errors, use stale cache data if possible
)
diff --git a/docs/user_guide/matching.md b/docs/user_guide/matching.md
index 9670be2..5756161 100644
--- a/docs/user_guide/matching.md
+++ b/docs/user_guide/matching.md
@@ -8,9 +8,9 @@ parameters, or create your own custom request matcher.
## Matching Request Headers
In some cases, different headers may result in different response data, so you may want to cache
-them separately. To enable this, use `include_get_headers`:
+them separately. To enable this, use `match_headers`:
```python
->>> session = CachedSession(include_get_headers=True)
+>>> session = CachedSession(match_headers=True)
>>> # Both of these requests will be sent and cached separately
>>> session.get('http://httpbin.org/headers', {'Accept': 'text/plain'})
>>> session.get('http://httpbin.org/headers', {'Accept': 'application/json'})
@@ -45,9 +45,9 @@ This also applies to parameters in a JSON-formatted request body:
**Request Headers:**
-As well as headers, if `include_get_headers` is also used:
+As well as headers, if `match_headers` is also used:
```python
->>> session = CachedSession(ignored_parameters=['auth-token'], include_get_headers=True)
+>>> session = CachedSession(ignored_parameters=['auth-token'], match_headers=True)
>>> session.get('http://httpbin.org/get', headers={'auth-token': '2F63E5DF4F44'})
>>> r = session.get('http://httpbin.org/get', headers={'auth-token': 'D9FAEB3449D3'})
>>> assert r.from_cache is True
diff --git a/requests_cache/backends/__init__.py b/requests_cache/backends/__init__.py
index 4a8f82c..2162551 100644
--- a/requests_cache/backends/__init__.py
+++ b/requests_cache/backends/__init__.py
@@ -16,7 +16,7 @@ BACKEND_KWARGS = CACHE_NAME_KWARGS + [
'endpoint_url',
'fast_save',
'ignored_parameters',
- 'include_get_headers',
+ 'match_headers',
'name',
'read_capacity_units',
'region_name',
diff --git a/requests_cache/backends/base.py b/requests_cache/backends/base.py
index 67ef5ba..0562a0a 100644
--- a/requests_cache/backends/base.py
+++ b/requests_cache/backends/base.py
@@ -45,14 +45,14 @@ class BaseCache:
def __init__(
self,
*args,
- include_get_headers: bool = False,
+ match_headers: bool = False,
ignored_parameters: Iterable[str] = None,
key_fn: KEY_FN = None,
**kwargs,
):
self.responses: BaseStorage = DictStorage()
self.redirects: BaseStorage = DictStorage()
- self.include_get_headers = include_get_headers
+ self.match_headers = match_headers or kwargs.get('include_get_headers')
self.ignored_parameters = ignored_parameters
self.key_fn = key_fn or create_key
self.name: str = kwargs.get('cache_name', '')
@@ -117,7 +117,7 @@ class BaseCache:
return self.key_fn(
request=request,
ignored_parameters=self.ignored_parameters,
- include_get_headers=self.include_get_headers,
+ match_headers=self.match_headers,
**kwargs,
)
diff --git a/requests_cache/cache_keys.py b/requests_cache/cache_keys.py
index e58bb14..b83aa9a 100644
--- a/requests_cache/cache_keys.py
+++ b/requests_cache/cache_keys.py
@@ -30,7 +30,7 @@ RequestContent = Union[Mapping, str, bytes]
def create_key(
request: AnyRequest = None,
ignored_parameters: Iterable[str] = None,
- include_get_headers: bool = False,
+ match_headers: bool = False,
**kwargs,
) -> str:
"""Create a normalized cache key from a request object or :py:class:`~requests.Request`
@@ -51,7 +51,7 @@ def create_key(
body = remove_ignored_body_params(request, ignored_parameters)
if body:
key.update(body)
- if include_get_headers and request.headers != DEFAULT_HEADERS:
+ if match_headers and request.headers != DEFAULT_HEADERS:
exclude_headers = list(ignored_parameters or []) + DEFAULT_EXCLUDE_HEADERS
headers = normalize_dict(remove_ignored_headers(request, exclude_headers))
if TYPE_CHECKING:
diff --git a/requests_cache/session.py b/requests_cache/session.py
index 97cdacc..2231532 100644
--- a/requests_cache/session.py
+++ b/requests_cache/session.py
@@ -291,16 +291,16 @@ class CachedSession(CacheMixin, OriginalSession):
Args:
cache_name: Cache prefix or namespace, depending on backend
- backend: Cache backend name, or instance; name may be one of
- ``['sqlite', 'mongodb', 'gridfs', 'redis', 'dynamodb', 'memory']``
+ backend: Cache backend name or instance; name may be one of
+ ``['sqlite', 'filesystem', 'mongodb', 'gridfs', 'redis', 'dynamodb', 'memory']``
serializer: Serializer name or instance; name may be one of
``['pickle', 'json', 'yaml', 'bson']``.
expire_after: Time after which cached items will expire
urls_expire_after: Expiration times to apply for different URL patterns
allowable_codes: Only cache responses with one of these status codes
allowable_methods: Cache only responses for one of these HTTP methods
- include_get_headers: Match request headers when reading from the cache
- ignored_parameters: List of request parameters to exclude from the cache and not match
+ match_headers: Match request headers when reading from the cache
+ ignored_parameters: List of request parameters to not match against, and exclude from the cache
filter_fn: Function that takes a :py:class:`~requests.Response` object and returns a boolean
indicating whether or not that response should be cached. Will be applied to both new
and previously cached responses.
diff --git a/tests/unit/test_session.py b/tests/unit/test_session.py
index 4a86c6e..d98c574 100644
--- a/tests/unit/test_session.py
+++ b/tests/unit/test_session.py
@@ -400,24 +400,30 @@ def test_response_defaults(mock_session):
assert response_2.is_expired is response_3.is_expired is False
-def test_include_get_headers(mock_session):
- """With include_get_headers, requests with different headers should have different cache keys"""
- mock_session.cache.include_get_headers = True
+def test_match_headers(mock_session):
+ """With match_headers, requests with different headers should have different cache keys"""
+ mock_session.cache.match_headers = True
headers_list = [{'Accept': 'text/json'}, {'Accept': 'text/xml'}, {'Accept': 'custom'}, None]
for headers in headers_list:
assert mock_session.get(MOCKED_URL, headers=headers).from_cache is False
assert mock_session.get(MOCKED_URL, headers=headers).from_cache is True
-def test_include_get_headers_normalize(mock_session):
- """With include_get_headers, the same headers (in any order) should have the same cache key"""
- mock_session.cache.include_get_headers = True
+def test_match_headers_normalize(mock_session):
+ """With match_headers, the same headers (in any order) should have the same cache key"""
+ mock_session.cache.match_headers = True
headers = {'Accept': 'text/json', 'Custom': 'abc'}
reversed_headers = {'Custom': 'abc', 'Accept': 'text/json'}
assert mock_session.get(MOCKED_URL, headers=headers).from_cache is False
assert mock_session.get(MOCKED_URL, headers=reversed_headers).from_cache is True
+def test_include_get_headers():
+ """include_get_headers is aliased to match_headers for backwards-compatibility"""
+ session = CachedSession(include_get_headers=True, backend='memory')
+ assert session.cache.match_headers is True
+
+
@pytest.mark.parametrize('exception_cls', DESERIALIZE_ERRORS)
def test_cache_error(exception_cls, mock_session):
"""If there is an error while fetching a cached response, a new one should be fetched"""