Session#
Main classes to add caching features to requests.Session
Session class that extends |
|
Mixin class that extends |
- class requests_cache.session.CachedSession(cache_name='http_cache', backend=None, expire_after=- 1, urls_expire_after=None, cache_control=False, allowable_codes=(200,), allowable_methods=('GET', 'HEAD'), filter_fn=None, stale_if_error=False, **kwargs)[source]#
Bases:
requests_cache.session.CacheMixin
,requests.sessions.Session
Session class that extends
requests.Session
with caching features.See individual
backend classes
for additional backend-specific arguments. Also see User Guide for more details and examples on how the following arguments affect cache behavior.- Parameters
cache_name (
str
) – Cache prefix or namespace, depending on backendbackend (
Union
[str
,BaseCache
,Type
[BaseCache
],None
]) – 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 (
Union
[None
,int
,float
,str
,datetime
,timedelta
]) – Time after which cached items will expireurls_expire_after (
Optional
[Dict
[str
,Union
[None
,int
,float
,str
,datetime
,timedelta
]]]) – Expiration times to apply for different URL patternscache_control (
bool
) – Use Cache-Control headers to set expirationallowable_codes (
Iterable
[int
]) – Only cache responses with one of these status codesallowable_methods (
Iterable
[str
]) – Cache only responses for one of these HTTP methodsmatch_headers – Match request headers when reading from the cache; may be either a boolean or a list of specific headers to match
ignored_parameters – List of request parameters to not match against, and exclude from the cache
filter_fn (
Optional
[Callable
[[Union
[Response
,CachedResponse
]],bool
]]) – Function that takes aResponse
object and returns a boolean indicating whether or not that response should be cached. Will be applied to both new and previously cached responses.key_fn – Function for generating custom cache keys based on request info
stale_if_error (
bool
) – Return stale cache data if a new request raises an exception
- auth#
Default Authentication tuple or object to attach to
Request
.
- cache_disabled()#
Context manager for temporary disabling the cache
Warning
This method is not thread-safe.
Example
>>> s = CachedSession() >>> with s.cache_disabled(): ... s.get('http://httpbin.org/ip')
- cert#
SSL client certificate default, if String, path to ssl client cert file (.pem). If Tuple, (‘cert’, ‘key’) pair.
- close()#
Closes all adapters and as such the session
- cookies#
A CookieJar containing all currently outstanding cookies set on this session. By default it is a
RequestsCookieJar
, but may be any othercookielib.CookieJar
compatible object.
- delete(url, **kwargs)#
Sends a DELETE request. Returns
Response
object.- Parameters
url – URL for the new
Request
object.**kwargs – Optional arguments that
request
takes.
- Return type
- get(url, **kwargs)#
Sends a GET request. Returns
Response
object.- Parameters
url – URL for the new
Request
object.**kwargs – Optional arguments that
request
takes.
- Return type
- get_adapter(url)#
Returns the appropriate connection adapter for the given URL.
- Return type
- get_redirect_target(resp)#
Receives a Response. Returns a redirect URI or
None
- head(url, **kwargs)#
Sends a HEAD request. Returns
Response
object.- Parameters
url – URL for the new
Request
object.**kwargs – Optional arguments that
request
takes.
- Return type
- headers#
A case-insensitive dictionary of headers to be sent on each
Request
sent from thisSession
.
- hooks#
Event-handling hooks.
- max_redirects#
Maximum number of redirects allowed. If the request exceeds this limit, a
TooManyRedirects
exception is raised. This defaults to requests.models.DEFAULT_REDIRECT_LIMIT, which is 30.
- merge_environment_settings(url, proxies, stream, verify, cert)#
Check the environment and merge it with some settings.
- Return type
- mount(prefix, adapter)#
Registers a connection adapter to a prefix.
Adapters are sorted in descending order by prefix length.
- options(url, **kwargs)#
Sends a OPTIONS request. Returns
Response
object.- Parameters
url – URL for the new
Request
object.**kwargs – Optional arguments that
request
takes.
- Return type
- params#
Dictionary of querystring data to attach to each
Request
. The dictionary values may be lists for representing multivalued query parameters.
- patch(url, data=None, **kwargs)#
Sends a PATCH request. Returns
Response
object.- Parameters
url – URL for the new
Request
object.data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the
Request
.**kwargs – Optional arguments that
request
takes.
- Return type
- post(url, data=None, json=None, **kwargs)#
Sends a POST request. Returns
Response
object.- Parameters
url – URL for the new
Request
object.data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the
Request
.json – (optional) json to send in the body of the
Request
.**kwargs – Optional arguments that
request
takes.
- Return type
- prepare_request(request)#
Constructs a
PreparedRequest
for transmission and returns it. ThePreparedRequest
has settings merged from theRequest
instance and those of theSession
.- Parameters
request –
Request
instance to prepare with this session’s settings.- Return type
- proxies#
Dictionary mapping protocol or protocol and host to the URL of the proxy (e.g. {‘http’: ‘foo.bar:3128’, ‘http://host.name’: ‘foo.bar:4012’}) to be used on each
Request
.
- put(url, data=None, **kwargs)#
Sends a PUT request. Returns
Response
object.- Parameters
url – URL for the new
Request
object.data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the
Request
.**kwargs – Optional arguments that
request
takes.
- Return type
- rebuild_auth(prepared_request, response)#
When being redirected we may want to strip authentication from the request to avoid leaking credentials. This method intelligently removes and reapplies authentication where possible to avoid credential loss.
- rebuild_method(prepared_request, response)#
When being redirected we may want to change the method of the request based on certain specs or browser behavior.
- rebuild_proxies(prepared_request, proxies)#
This method re-evaluates the proxy configuration by considering the environment variables. If we are redirected to a URL covered by NO_PROXY, we strip the proxy configuration. Otherwise, we set missing proxy keys for this URL (in case they were stripped by a previous redirect).
This method also replaces the Proxy-Authorization header where necessary.
- Return type
- request(method, url, *args, expire_after=None, **kwargs)#
This method prepares and sends a request while automatically performing any necessary caching operations. This will be called by any other method-specific
requests
functions (get, post, etc.). This does not include prepared requests, which will still be cached viasend()
.See
requests.Session.request()
for parameters. Additional parameters:- Parameters
expire_after (
Union
[None
,int
,float
,str
,datetime
,timedelta
]) – Expiration time to set only for this request; see details below. OverridesCachedSession.expire_after
. Accepts all the same values asCachedSession.expire_after
. Use-1
to disable expiration.- Return type
- Returns
Either a new or cached response
Order of operations: For reference, a request will pass through the following methods:
requests.get()
/requests.Session.get()
or other method-specific functions (optional)requests.Session.send()
(if not previously cached)BaseCache.save_response()
(if not previously cached)
- resolve_redirects(resp, req, stream=False, timeout=None, verify=True, cert=None, proxies=None, yield_requests=False, **adapter_kwargs)#
Receives a Response. Returns a generator of Responses or Requests.
- send(request, expire_after=None, **kwargs)#
Send a prepared request, with caching. See
request()
for notes on behavior, and seerequests.Session.send()
for parameters. Additional parameters:
- should_strip_auth(old_url, new_url)#
Decide whether Authorization header should be removed when redirecting
- stream#
Stream response content default.
- trust_env#
Trust environment settings for proxy configuration, default authentication and similar.
- verify#
SSL Verification default. Defaults to True, requiring requests to verify the TLS certificate at the remote end. If verify is set to False, requests will accept any TLS certificate presented by the server, and will ignore hostname mismatches and/or expired certificates, which will make your application vulnerable to man-in-the-middle (MitM) attacks. Only set this to False for testing.
- class requests_cache.session.CacheMixin(cache_name='http_cache', backend=None, expire_after=- 1, urls_expire_after=None, cache_control=False, allowable_codes=(200,), allowable_methods=('GET', 'HEAD'), filter_fn=None, stale_if_error=False, **kwargs)[source]#
Mixin class that extends
requests.Session
with caching features. SeeCachedSession
for usage details.- Parameters