1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
"""Utilities for patching ``requests``. See :ref:`patching` for general usage info.
.. warning:: These functions are not thread-safe. Use :py:class:`.CachedSession` if you want to use
caching in a multi-threaded environment.
.. automodsumm:: requests_cache.patcher
:functions-only:
:nosignatures:
"""
import inspect
from contextlib import contextmanager
from logging import getLogger
from typing import TYPE_CHECKING, List, Optional, Type
from warnings import warn
import requests
from .backends import BackendSpecifier, BaseCache, init_backend
from .session import CachedSession, OriginalSession
# TODO: This is going to require thinking through many more edge cases.
# Lots of ways this could potentially go wrong.
# TODO: What if we want to use module_only but patch an external library? Options:
# 1. Check if enabled module is anywhere in the call stack (e.g., it called some other module that
# made the request)
# 2. Provide target module to patch (e.g., 'github.Requester')
logger = getLogger(__name__)
if TYPE_CHECKING:
MIXIN_BASE = CachedSession
else:
MIXIN_BASE = object
def install_cache(
cache_name: str = 'http_cache',
backend: Optional[BackendSpecifier] = None,
module_only: bool = False,
session: CachedSession = None,
session_factory: Type[OriginalSession] = CachedSession,
**kwargs,
):
"""
Install the cache for all ``requests`` functions by monkey-patching :py:class:`requests.Session`
Example:
>>> requests_cache.install_cache('demo_cache')
Accepts all parameters for :py:class:`.CachedSession`. Additional parameters:
Args:
module_only: Only install the cache for the current module
session: An existing session object to use
session_factory: Session class to use. It must inherit from either
:py:class:`.CachedSession` or :py:class:`.CacheMixin`
"""
# Patch with an existing session object
if session:
cls = _get_session_wrapper(session)
_patch_session_factory(cls)
# Patch only for the current module
elif module_only:
if not is_installed:
session = ModuleCachedSession(
cache_name=cache_name,
backend=init_backend(cache_name, backend, **kwargs),
**kwargs,
)
cls = _get_session_wrapper(session)
_patch_session_factory(cls)
requests.Session._session.enable_module()
# modules = get_installed_modules() + [_calling_module()]
# _enable_module(
# cache_name,
# init_backend(cache_name, backend, **kwargs),
# modules,
# **kwargs,
# )
# cls = _get_configured_session(
# ModuleCachedSession,
# cache_name=cache_name,
# backend=backend,
# include_modules=modules,
# **kwargs,
# )
# _patch_session_factory(cls)
# Patch with CachedSession or session_factory
else:
cls = _get_configured_session(
session_factory,
cache_name=cache_name,
backend=init_backend(cache_name, backend, **kwargs),
**kwargs,
)
_patch_session_factory(cls)
def uninstall_cache(module_only: bool = False):
"""Disable the cache by restoring the original :py:class:`requests.Session`
Args:
module_only: Only uninstall the cache for the current module
"""
if not is_installed():
return
elif module_only:
requests.Session._session.disable_module()
else:
_patch_session_factory(OriginalSession)
@contextmanager
def disabled():
"""
Context manager for temporarily disabling caching for all ``requests`` functions
Example:
>>> with requests_cache.disabled():
... requests.get('https://httpbin.org/get')
"""
previous = requests.Session
uninstall_cache()
try:
yield
finally:
_patch_session_factory(previous)
@contextmanager
def enabled(*args, **kwargs):
"""
Context manager for temporarily enabling caching for all ``requests`` functions
Example:
>>> with requests_cache.enabled('cache.db'):
... requests.get('https://httpbin.org/get')
Accepts the same arguments as :py:class:`.CachedSession` and :py:func:`.install_cache`.
"""
install_cache(*args, **kwargs)
try:
yield
finally:
uninstall_cache()
def get_cache() -> Optional[BaseCache]:
"""Get the internal cache object from the currently installed ``CachedSession`` (if any)"""
return getattr(requests.Session(), 'cache', None)
def get_installed_modules() -> List[str]:
"""Get all modules that have caching installed"""
session = requests.Session()
if isinstance(session, ModuleCachedSession):
return list(session.include_modules)
else:
return []
def is_installed() -> bool:
"""Indicate whether or not requests-cache is currently installed"""
session = requests.Session()
if isinstance(session, ModuleCachedSession):
return session.is_module_enabled()
else:
return isinstance(session, CachedSession)
def clear():
"""Clear the currently installed cache (if any)"""
if get_cache():
get_cache().clear()
def delete(*args, **kwargs):
"""Remove responses from the cache according one or more conditions.
See :py:meth:`.BaseCache.delete for usage details.
"""
session = requests.Session()
if isinstance(session, CachedSession):
session.cache.delete(*args, **kwargs)
def remove_expired_responses():
"""Remove expired responses from the cache"""
warn(
'remove_expired_responses() is deprecated; please use delete() instead',
DeprecationWarning,
)
delete(expired=True)
class ModuleCachedSession(CachedSession):
"""Session mixin that only caches requests sent from specific modules. May be used in one of two
modes:
* Opt-in: caching is disabled by default, and enabled for modules in ``include_modules``
* Opt-out: caching is enabled by default, and disabled for modules in ``exclude_modules``
Args:
include_modules: List of modules to enable caching for
exclude_modules: List of modules to disable caching for
opt_in: Whether to use opt-in mode (``True``) or opt-out mode (``False``)
"""
def __init__(
self,
*args,
include_modules: List[str] = None,
exclude_modules: List[str] = None,
opt_in: bool = True,
**kwargs,
):
super().__init__(*args, **kwargs)
self.include_modules = set(include_modules or [])
self.exclude_modules = set(exclude_modules or [])
self.opt_in = opt_in
def close(self):
"""Only close adapter(s) and skip closing backend connections in CachedSession.close(), as
the object may be reused after closing. :py:func:`requests.request` will create a session
for the request and close it after use. We will keep this behavior to avoid memory leaks.
Adapter(s) will create new connections if re-used after closing.
"""
for v in self.adapters.values():
v.close()
def disable_module(self):
module = _calling_module()
if self.opt_in:
self.include_modules -= {module}
else:
self.exclude_modules |= {module}
logger.info(f'Caching disabled for {module}')
def enable_module(self):
module = _calling_module()
if self.opt_in:
self.include_modules |= {module}
else:
self.exclude_modules -= {module}
logger.info(f'Caching enabled for {module}')
def is_module_enabled(self, back: int = 2) -> bool:
module = _calling_module(back=back)
if self.opt_in:
return module in self.include_modules
else:
return module not in self.exclude_modules
def request(self, *args, **kwargs):
if self.is_module_enabled(back=3):
return super().request(*args, **kwargs)
else:
return OriginalSession.request(self, *args, **kwargs)
def _calling_module(back: int = 2) -> str:
"""Get the name of the module ``back`` frames up in the call stack"""
frame = inspect.stack()[back].frame
module = inspect.getmodule(frame)
return getattr(module, '__name__', '')
# testing
def _calling_module_2() -> str:
"""Get the name of the calling module (first module outside of requests-cache)"""
for module in _stack_modules():
if not module.startswith('requests_cache'):
return module
raise RuntimeError('Could not determine calling module')
def _stack_modules() -> Iterator[str]:
for frame_info in inspect.stack():
module = inspect.getmodule(frame_info.frame)
yield getattr(module, '__name__', '')
# def _enable_module(
# cache_name: str,
# backend: BackendSpecifier,
# modules: List[str],
# **kwargs,
# ):
# """Install the cache for specific modules"""
# if not is_installed():
# return
# requests.Session._session.enable_module()
# class _ConfiguredCachedSession(ModuleCachedSession): # type: ignore # See mypy issue #5865
# def __init__(self):
# super().__init__(
# cache_name=cache_name, backend=backend, include_modules=modules, **kwargs
# )
# _patch_session_factory(_ConfiguredCachedSession)
# def _disable_module():
# """Uninstall the cache for the current module"""
# session = requests.Session()
# if not isinstance(session, ModuleCachedSession):
# return
# modules = get_installed_modules()
# modules.remove(_calling_module())
# # No enabled modules remaining; restore the original Session
# if not modules:
# uninstall_cache()
# # Reinstall cache with updated modules
# else:
# _enable_module(
# session.cache.cache_name,
# session.cache,
# CachedSession,
# modules,
# **session.settings.to_dict(),
# )
def _get_configured_session(
session_factory: Type[OriginalSession], *args, **kwargs
) -> Type[OriginalSession]:
class _ConfiguredCachedSession(session_factory): # type: ignore # See mypy issue #5865
def __init__(self):
super().__init__(*args, **kwargs)
return _ConfiguredCachedSession
def _get_session_wrapper(session: CachedSession):
"""Create a wrapper Session class that returns an existing session object when created"""
class _SessionWrapper(OriginalSession):
"""Wrapper for an existing session object, as a mutable class attribute"""
_session = session
def __new__(cls, *args, **kwargs):
return cls._session
return _SessionWrapper
def _patch_session_factory(session_factory: Type[OriginalSession]):
logger.debug(f'Patching requests.Session with class: {session_factory.__name__}')
requests.Session = requests.sessions.Session = session_factory # type: ignore
|