Skip to content

Commit

Permalink
Add wrapper properties for settings for partial forwards-compatibilit…
Browse files Browse the repository at this point in the history
…y with CacheSettings in 1.0
  • Loading branch information
JWCook committed Oct 20, 2022
1 parent 864afeb commit eee1c2f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
8 changes: 8 additions & 0 deletions HISTORY.md
Expand Up @@ -8,6 +8,14 @@ Backport fixes from 1.0:
* Add support for header values as bytes for compatibility with OAuth1 features of `requests-oauthlib`
* Update to cattrs 22.2

Add the following for forwards-compatibility with 1.0:
* `requests_cache.policy` subpackage
* `BaseCache.contains()`
* `BaseCache.delete()`
* `BaseCache.filter()`
* `CachedSession.settings`
* `DeprecationWarnings` for items to give an earlier notice for upcoming changes in 1.0

## 0.9.6 (2022-08-24)
Backport fixes from 1.0:
* Remove potentially problematic row count from `BaseCache.__str__()`
Expand Down
2 changes: 1 addition & 1 deletion requests_cache/backends/__init__.py
Expand Up @@ -4,7 +4,7 @@
from typing import Callable, Dict, Iterable, Optional, Type, Union

from .._utils import get_placeholder_class, get_valid_kwargs
from .base import BaseCache, BaseStorage, DictStorage
from .base import KEY_FN, BaseCache, BaseStorage, DictStorage

# Backend-specific keyword arguments equivalent to 'cache_name'
CACHE_NAME_KWARGS = ['db_path', 'db_name', 'namespace', 'table_name']
Expand Down
35 changes: 33 additions & 2 deletions requests_cache/session.py
Expand Up @@ -16,15 +16,15 @@
from contextlib import contextmanager
from logging import getLogger
from threading import RLock
from typing import TYPE_CHECKING, Callable, Dict, Iterable, Optional
from typing import TYPE_CHECKING, Callable, Dict, Iterable, Optional, Union

from requests import PreparedRequest, Response
from requests import Session as OriginalSession
from requests.hooks import dispatch_hook
from urllib3 import filepost

from ._utils import get_valid_kwargs
from .backends import BackendSpecifier, init_backend
from .backends import KEY_FN, BackendSpecifier, init_backend
from .models import AnyResponse, CachedResponse, set_response_defaults
from .policy import CacheActions, ExpirationTime, get_expiration_seconds

Expand Down Expand Up @@ -295,6 +295,37 @@ def __repr__(self):
attr_strs = [f'{k}={repr(getattr(self, k))}' for k in repr_attrs]
return f'<CachedSession({", ".join(attr_strs)})>'

# The following properties exist for partial forwards-compatibility with CacheSettings in 1.0
# All settings will be settable via CachedSession.settings, instead of being split between
# CachedSession and BaseCache.
@property
def settings(self):
return self

@property
def ignored_parameters(self) -> Iterable[str]:
return self.cache.ignored_parameters or []

@ignored_parameters.setter
def ignored_parameters(self, value: Iterable[str]):
self.cache.ignored_parameters = value

@property
def match_headers(self) -> Union[Iterable[str], bool]:
return self.cache.match_headers or []

@match_headers.setter
def match_headers(self, value: Union[Iterable[str], bool]):
self.cache.match_headers = value

@property
def key_fn(self) -> KEY_FN:
return self.cache.key_fn or []

@key_fn.setter
def key_fn(self, value: KEY_FN):
self.cache.key_fn = value


class CachedSession(CacheMixin, OriginalSession):
"""Session class that extends :py:class:`requests.Session` with caching features.
Expand Down

0 comments on commit eee1c2f

Please sign in to comment.