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, serializer=None, expire_after=-1, urls_expire_after=None, cache_control=False, allowable_codes=(200,), allowable_methods=('GET', 'HEAD'), always_revalidate=False, ignored_parameters=('Authorization', 'X-API-KEY', 'access_token', 'api_key'), match_headers=False, filter_fn=None, key_fn=None, stale_if_error=False, **kwargs)[source]#
Bases:
CacheMixin
,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
) – Used as a cache path, prefix, or namespace, depending on the backendbackend (
Union
[str
,BaseCache
,None
]) – Cache backend name or instance; name may be one of['sqlite', 'filesystem', 'mongodb', 'gridfs', 'redis', 'dynamodb', 'memory']
serializer (
Union
[str
,SerializerPipeline
,Stage
,None
]) – 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 expire. See Expiration for details.urls_expire_after (
Optional
[Dict
[Union
[str
,Pattern
],Union
[None
,int
,float
,str
,datetime
,timedelta
]]]) – Expiration times to apply for different URL patternscache_control (
bool
) – Use Cache-Control and other response 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 methodsalways_revalidate (
bool
) – Revalidate with the server for every request, even if the cached response is not expiredmatch_headers (
Union
[Iterable
[str
],bool
]) – Request headers to match, when Vary response header is not available. May be a list of headers, orTrue
to match all.ignored_parameters (
Iterable
[str
]) – Request paramters, headers, and/or JSON body params to exclude from both request matching and cached request datastale_if_error (
Union
[bool
,int
]) – Return a stale response if a new request raises an exception. Optionally accepts a time value representing maximum staleness to accept.stale_while_revalidate – Return a stale response initially, while a non-blocking request is sent to refresh the response for the next time it’s requested
filter_fn (
Optional
[Callable
[[Response
],bool
]]) – Response filtering function that indicates whether or not a given response should be cached. See Custom Cache Filtering for details.key_fn (
Optional
[Callable
[...
,str
]]) – Request matching function for generating custom cache keys. See Custom Request Matching for details.
- 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('https://httpbin.org/ip')
- cert#
SSL client certificate default, if String, path to ssl client cert file (.pem). If Tuple, (‘cert’, ‘key’) pair.
- close()#
Close the session and any open backend connections
- 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 (
str
) – URL for the newRequest
object.**kwargs – Optional arguments that
request
takes.
- Return type:
- get(url, params=None, **kwargs)#
Sends a GET request. Returns
Response
object.- Parameters:
url (
str
) – URL for the newRequest
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 (
str
) – URL for the newRequest
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 (
str
) – URL for the newRequest
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 (
str
) – URL for the newRequest
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, **kwargs)#
Sends a POST request. Returns
Response
object.- Parameters:
url (
str
) – URL for the newRequest
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 (
str
) – URL for the newRequest
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, headers=None, expire_after=None, only_if_cached=False, refresh=False, force_refresh=False, **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 is not used byPreparedRequest
objects, which are handled bysend()
.See
requests.Session.request()
for base parameters. Additional parameters:- Parameters:
expire_after (
Union
[None
,int
,float
,str
,datetime
,timedelta
]) – Expiration time to set only for this request. See Expiration for details.only_if_cached (
bool
) – Only return results from the cache. If not cached, return a 504 response instead of sending a new request.refresh (
bool
) – Revalidate with the server before using a cached response, and refresh if needed (e.g., a “soft refresh,” like F5 in a browser)force_refresh (
bool
) – Always make a new request, and overwrite any previously cached response (e.g., a “hard refresh”, like Ctrl-F5 in a browser))method (
str
) –url (
str
) –headers (
Optional
[MutableMapping
[str
,str
]]) –
- Return type:
- Returns:
Either a new or cached response
- 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, only_if_cached=False, refresh=False, force_refresh=False, **kwargs)#
Send a prepared request, with caching. See
requests.Session.send()
for base parameters, and seerequest()
for extra parameters.Order of operations: For reference, a request will pass through the following methods:
requests.get()
,CachedSession.get()
, etc. (optional)requests.Session.send()
(if not using a cached response)BaseCache.save_response()
(if not using a cached response)
- Parameters:
- Return type:
- property settings: CacheSettings#
Settings that affect cache behavior
- 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, serializer=None, expire_after=-1, urls_expire_after=None, cache_control=False, allowable_codes=(200,), allowable_methods=('GET', 'HEAD'), always_revalidate=False, ignored_parameters=('Authorization', 'X-API-KEY', 'access_token', 'api_key'), match_headers=False, filter_fn=None, key_fn=None, stale_if_error=False, **kwargs)[source]#
Mixin class that extends
requests.Session
with caching features. SeeCachedSession
for usage details.- Parameters:
cache_name (
str
) –serializer (
Union
[str
,SerializerPipeline
,Stage
,None
]) –expire_after (
Union
[None
,int
,float
,str
,datetime
,timedelta
]) –urls_expire_after (
Optional
[Dict
[Union
[str
,Pattern
],Union
[None
,int
,float
,str
,datetime
,timedelta
]]]) –cache_control (
bool
) –always_revalidate (
bool
) –