apeye.rate_limiter
¶
Rate limiters for making calls to external APIs in a polite manner.
Attention
This module has the following additional requirements:
cachecontrol[filecache]>=0.12.6 lockfile>=0.12.2
These can be installed as follows:
python -m pip install apeye[limiter]
Classes:
|
Cache HTTP requests for up to 28 days and limit the rate of requests to no more than 5/second. |
|
Custom |
Functions:
|
Decorator to force a function to run no less than |
- class HTTPCache(app_name, expires_after=datetime.timedelta(days=28))[source]¶
Cache HTTP requests for up to 28 days and limit the rate of requests to no more than 5/second.
- Parameters
Attributes:
The name of the app.
The location of the cache directory on disk.
Mapping of function names to their caches.
Methods:
clear
()Clear the cache.
- rate_limit(min_time=0.2, logger=None)[source]¶
Decorator to force a function to run no less than
min_time
seconds after it last ran. Used for rate limiting.- Parameters
- Return type
- class RateLimitAdapter(cache=None, cache_etags=True, controller_class=None, serializer=None, heuristic=None, cacheable_methods=None, *args, **kw)[source]¶
Bases:
CacheControlAdapter
Custom
cachecontrol.adapter.CacheControlAdapter
to limit the rate of requests to 5 per second.- Parameters
Methods:
rate_limited_send
(*args, **kwargs)Wrapper around
CacheControlAdapter.send
to limit the rate of requests.send
(request[, cacheable_methods])Send a request.
- rate_limited_send(*args, **kwargs)[source]¶
Wrapper around
CacheControlAdapter.send
to limit the rate of requests.- Return type
- send(request, cacheable_methods=None, **kwargs)[source]¶
Send a request.
Use the request information to see if it exists in the cache and cache the response if we need to and can.
- Parameters
request (
PreparedRequest
) – Therequests.PreparedRequest
being sent.cacheable_methods (
Optional
[Collection
[str
]]) – DefaultNone
.**kwargs – Additional arguments taken by
requests.adapters.HTTPAdapter.send()
(e.g.timeout
).
- Return type