API Reference¶
Session that adds rate-limiting behavior to requests. |
|
Transport adapter that adds rate-limiting behavior to requests. |
|
Mixin class that adds rate-limiting behavior to requests. |
- class requests_ratelimiter.LimiterSession(per_second=0, per_minute=0, per_hour=0, per_day=0, per_month=0, burst=1, bucket_class=<class 'pyrate_limiter.buckets.in_memory_bucket.InMemoryBucket'>, bucket_kwargs=None, time_function=None, limiter=None, per_host=True, limit_statuses=(429, ), bucket_name=None, **kwargs)¶
Bases:
LimiterMixin,SessionSession that adds rate-limiting behavior to requests.
The following parameters also apply to
LimiterMixinandLimiterAdapter.Note
The
per_*params are aliases for the most common rate limit intervals; for more complex rate limits, you can provide aLimiterobject instead.- Parameters:
per_second (
float) – Max requests per secondper_minute (
float) – Max requests per minuteper_hour (
float) – Max requests per hourper_day (
float) – Max requests per dayper_month (
float) – Max requests per monthburst (
float) – Max number of consecutive requests allowed before applying per-second rate-limitingbucket_class (
Type[AbstractBucket]) – Bucket backend class; may be one ofInMemoryBucket(default),SQLiteBucket, orRedisBucketbucket_kwargs (
Optional[Dict]) – Bucket backend keyword argumentslimiter (
Optional[Limiter]) – An existing Limiter object to use instead of the above paramsper_host (
bool) – Track request rate limits separately for each hostlimit_statuses (
Iterable[int]) – Alternative HTTP status codes that indicate a rate limit was exceeded
- auth¶
Default Authentication tuple or object to attach to
Request.
- cert¶
SSL client certificate default, if String, path to ssl client cert file (.pem). If Tuple, (‘cert’, ‘key’) pair.
- close()¶
Close the session or adapter and release all rate-limiter resources.
Calls the parent
close()to tear down connection pools, then stops the backgroundLeakerthread owned by the internalBucketFactory. The thread is only stopped when the limiter was created internally (i.e. via theper_second/per_minute/ … parameters); if a customLimiterwas supplied by the caller its lifecycle is the caller’s responsibility.- Return type:
- cookies¶
A CookieJar containing all currently outstanding cookies set on this session. By default it is a
RequestsCookieJar, but may be any othercookielib.CookieJarcompatible object.
- delete(url, **kwargs)¶
Sends a DELETE request. Returns
Responseobject.- Parameters:
url – URL for the new
Requestobject.**kwargs – Optional arguments that
requesttakes.
- Return type:
- get(url, **kwargs)¶
Sends a GET request. Returns
Responseobject.- Parameters:
url – URL for the new
Requestobject.**kwargs – Optional arguments that
requesttakes.
- 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
Responseobject.- Parameters:
url – URL for the new
Requestobject.**kwargs – Optional arguments that
requesttakes.
- Return type:
- headers¶
A case-insensitive dictionary of headers to be sent on each
Requestsent from thisSession.
- hooks¶
Event-handling hooks.
- max_redirects¶
Maximum number of redirects allowed. If the request exceeds this limit, a
TooManyRedirectsexception 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
Responseobject.- Parameters:
url – URL for the new
Requestobject.**kwargs – Optional arguments that
requesttakes.
- 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
Responseobject.- Parameters:
url – URL for the new
Requestobject.data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the
Request.**kwargs – Optional arguments that
requesttakes.
- Return type:
- post(url, data=None, json=None, **kwargs)¶
Sends a POST request. Returns
Responseobject.- Parameters:
url – URL for the new
Requestobject.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
requesttakes.
- Return type:
- prepare_request(request)¶
Constructs a
PreparedRequestfor transmission and returns it. ThePreparedRequesthas settings merged from theRequestinstance and those of theSession.- Parameters:
request –
Requestinstance 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
Responseobject.- Parameters:
url – URL for the new
Requestobject.data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the
Request.**kwargs – Optional arguments that
requesttakes.
- 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, params=None, data=None, headers=None, cookies=None, files=None, auth=None, timeout=None, allow_redirects=True, proxies=None, hooks=None, stream=None, verify=None, cert=None, json=None)¶
Constructs a
Request, prepares it and sends it. ReturnsResponseobject.- Parameters:
method – method for the new
Requestobject.url – URL for the new
Requestobject.params – (optional) Dictionary or bytes to be sent in the query string for the
Request.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.headers – (optional) Dictionary of HTTP Headers to send with the
Request.cookies – (optional) Dict or CookieJar object to send with the
Request.files – (optional) Dictionary of
'filename': file-like-objectsfor multipart encoding upload.auth – (optional) Auth tuple or callable to enable Basic/Digest/Custom HTTP Auth.
timeout (float or tuple) – (optional) How many seconds to wait for the server to send data before giving up, as a float, or a (connect timeout, read timeout) tuple.
allow_redirects (bool) – (optional) Set to True by default.
proxies – (optional) Dictionary mapping protocol or protocol and hostname to the URL of the proxy.
hooks – (optional) Dictionary mapping hook name to one event or list of events, event must be callable.
stream – (optional) whether to immediately download the response content. Defaults to
False.verify – (optional) Either a boolean, in which case it controls whether we verify the server’s TLS certificate, or a string, in which case it must be a path to a CA bundle to use. Defaults to
True. When set toFalse, 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. Setting verify toFalsemay be useful during local development or testing.cert – (optional) if String, path to ssl client cert file (.pem). If Tuple, (‘cert’, ‘key’) pair.
- Return type:
- 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.
- 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_ratelimiter.LimiterAdapter(per_second=0, per_minute=0, per_hour=0, per_day=0, per_month=0, burst=1, bucket_class=<class 'pyrate_limiter.buckets.in_memory_bucket.InMemoryBucket'>, bucket_kwargs=None, time_function=None, limiter=None, per_host=True, limit_statuses=(429, ), bucket_name=None, **kwargs)¶
Bases:
LimiterMixin,HTTPAdapterTransport adapter that adds rate-limiting behavior to requests.
See
LimiterSessionfor parameter details.- add_headers(request, **kwargs)¶
Add any headers needed by the connection. As of v2.0 this does nothing by default, but is left for overriding by users that subclass the
HTTPAdapter.This should not be called from user code, and is only exposed for use when subclassing the
HTTPAdapter.- Parameters:
request – The
PreparedRequestto add headers to.kwargs – The keyword arguments from the call to send().
- build_connection_pool_key_attributes(request, verify, cert=None)¶
Build the PoolKey attributes used by urllib3 to return a connection.
This looks at the PreparedRequest, the user-specified verify value, and the value of the cert parameter to determine what PoolKey values to use to select a connection from a given urllib3 Connection Pool.
The SSL related pool key arguments are not consistently set. As of this writing, use the following to determine what keys may be in that dictionary:
If
verifyisTrue,"ssl_context"will be set and will be the default Requests SSL ContextIf
verifyisFalse,"ssl_context"will not be set but"cert_reqs"will be setIf
verifyis a string, (i.e., it is a user-specified trust bundle)"ca_certs"will be set if the string is not a directory recognized byos.path.isdir(), otherwise"ca_cert_dir"will be set.If
"cert"is specified,"cert_file"will always be set. If"cert"is a tuple with a second item,"key_file"will also be present
To override these settings, one may subclass this class, call this method and use the above logic to change parameters as desired. For example, if one wishes to use a custom
ssl.SSLContextone must both set"ssl_context"and based on what else they require, alter the other keys to ensure the desired behaviour.- Parameters:
request (
PreparedRequest) – The PreparedReqest being sent over the connection.verify – Either a boolean, in which case it controls whether we verify the server’s TLS certificate, or a string, in which case it must be a path to a CA bundle to use.
cert – (optional) Any user-provided SSL certificate for client authentication (a.k.a., mTLS). This may be a string (i.e., just the path to a file which holds both certificate and key) or a tuple of length 2 with the certificate file path and key file path.
- Returns:
A tuple of two dictionaries. The first is the “host parameters” portion of the Pool Key including scheme, hostname, and port. The second is a dictionary of SSLContext related parameters.
- build_response(req, resp)¶
Builds a
Responseobject from a urllib3 response. This should not be called from user code, and is only exposed for use when subclassing theHTTPAdapter- Parameters:
req – The
PreparedRequestused to generate the response.resp – The urllib3 response object.
- Return type:
- cert_verify(conn, url, verify, cert)¶
Verify a SSL certificate. This method should not be called from user code, and is only exposed for use when subclassing the
HTTPAdapter.- Parameters:
conn – The urllib3 connection object associated with the cert.
url – The requested URL.
verify – Either a boolean, in which case it controls whether we verify the server’s TLS certificate, or a string, in which case it must be a path to a CA bundle to use
cert – The SSL certificate to verify.
- close()¶
Close the session or adapter and release all rate-limiter resources.
Calls the parent
close()to tear down connection pools, then stops the backgroundLeakerthread owned by the internalBucketFactory. The thread is only stopped when the limiter was created internally (i.e. via theper_second/per_minute/ … parameters); if a customLimiterwas supplied by the caller its lifecycle is the caller’s responsibility.- Return type:
- get_connection(url, proxies=None)¶
DEPRECATED: Users should move to get_connection_with_tls_context for all subclasses of HTTPAdapter using Requests>=2.32.2.
Returns a urllib3 connection for the given URL. This should not be called from user code, and is only exposed for use when subclassing the
HTTPAdapter.- Parameters:
url – The URL to connect to.
proxies – (optional) A Requests-style dictionary of proxies used on this request.
- Return type:
urllib3.ConnectionPool
- get_connection_with_tls_context(request, verify, proxies=None, cert=None)¶
Returns a urllib3 connection for the given request and TLS settings. This should not be called from user code, and is only exposed for use when subclassing the
HTTPAdapter.- Parameters:
request – The
PreparedRequestobject to be sent over the connection.verify – Either a boolean, in which case it controls whether we verify the server’s TLS certificate, or a string, in which case it must be a path to a CA bundle to use.
proxies – (optional) The proxies dictionary to apply to the request.
cert – (optional) Any user-provided SSL certificate to be used for client authentication (a.k.a., mTLS).
- Return type:
urllib3.ConnectionPool
- init_poolmanager(connections, maxsize, block=False, **pool_kwargs)¶
Initializes a urllib3 PoolManager.
This method should not be called from user code, and is only exposed for use when subclassing the
HTTPAdapter.- Parameters:
connections – The number of urllib3 connection pools to cache.
maxsize – The maximum number of connections to save in the pool.
block – Block when no free connections are available.
pool_kwargs – Extra keyword arguments used to initialize the Pool Manager.
- proxy_headers(proxy)¶
Returns a dictionary of the headers to add to any request sent through a proxy. This works with urllib3 magic to ensure that they are correctly sent to the proxy, rather than in a tunnelled request if CONNECT is being used.
This should not be called from user code, and is only exposed for use when subclassing the
HTTPAdapter.- Parameters:
proxy – The url of the proxy being used for this request.
- Return type:
- proxy_manager_for(proxy, **proxy_kwargs)¶
Return urllib3 ProxyManager for the given proxy.
This method should not be called from user code, and is only exposed for use when subclassing the
HTTPAdapter.- Parameters:
proxy – The proxy to return a urllib3 ProxyManager for.
proxy_kwargs – Extra keyword arguments used to configure the Proxy Manager.
- Returns:
ProxyManager
- Return type:
- request_url(request, proxies)¶
Obtain the url to use when making the final request.
If the message is being sent through a HTTP proxy, the full URL has to be used. Otherwise, we should only use the path portion of the URL.
This should not be called from user code, and is only exposed for use when subclassing the
HTTPAdapter.- Parameters:
request – The
PreparedRequestbeing sent.proxies – A dictionary of schemes or schemes and hosts to proxy URLs.
- Return type:
- class requests_ratelimiter.LimiterMixin(per_second=0, per_minute=0, per_hour=0, per_day=0, per_month=0, burst=1, bucket_class=<class 'pyrate_limiter.buckets.in_memory_bucket.InMemoryBucket'>, bucket_kwargs=None, time_function=None, limiter=None, per_host=True, limit_statuses=(429, ), bucket_name=None, **kwargs)¶
Mixin class that adds rate-limiting behavior to requests.
See
LimiterSessionfor parameter details.