Patcher#

Utilities for patching requests.

Warning

These functions are not thread-safe. Use CachedSession if you want to use caching in a multi-threaded environment.

clear

Clear the currently installed cache (if any)

disabled

Context manager for temporarily disabling caching for all requests functions

enabled

Context manager for temporarily enabling caching for all requests functions

get_cache

Get the internal cache object from the currently installed CachedSession (if any)

install_cache

Install the cache for all requests functions by monkey-patching requests.Session

is_installed

Indicate whether or not requests-cache is currently installed

remove_expired_responses

Remove expired responses from the cache, optionally with revalidation

uninstall_cache

Disable the cache by restoring the original requests.Session

requests_cache.patcher.clear()[source]#

Clear the currently installed cache (if any)

requests_cache.patcher.disabled()[source]#

Context manager for temporarily disabling caching for all requests functions

Example

>>> with requests_cache.disabled():
...     requests.get('http://httpbin.org/get')
requests_cache.patcher.enabled(*args, **kwargs)[source]#

Context manager for temporarily enabling caching for all requests functions

Accepts the same arguments as install_cache().

Example

>>> with requests_cache.enabled('cache_db'):
...     requests.get('http://httpbin.org/get')
requests_cache.patcher.get_cache()[source]#

Get the internal cache object from the currently installed CachedSession (if any)

Return type

Optional[BaseCache]

requests_cache.patcher.install_cache(cache_name='http_cache', backend=None, expire_after=-1, urls_expire_after=None, allowable_codes=(200, ), allowable_methods=('GET', 'HEAD'), filter_fn=None, stale_if_error=False, session_factory=<class 'requests_cache.session.CachedSession'>, **kwargs)[source]#

Install the cache for all requests functions by monkey-patching requests.Session

Example

>>> requests_cache.install_cache('demo_cache')

Accepts all the same parameters as CachedSession. Additional parameters:

Parameters
requests_cache.patcher.is_installed()[source]#

Indicate whether or not requests-cache is currently installed

Return type

bool

requests_cache.patcher.remove_expired_responses(expire_after=None)[source]#

Remove expired responses from the cache, optionally with revalidation

Parameters

expire_after (Union[None, int, float, str, datetime, timedelta]) – A new expiration time used to revalidate the cache

requests_cache.patcher.uninstall_cache()[source]#

Disable the cache by restoring the original requests.Session