API rate limit decorator
Project description
NOTE:
THIS PACKAGE IS A CLONE OF https://github.com/tomasbasham/ratelimit PACKAGE. I HAVE JUST CHANGE PACKAGE DICTIONARY NAME TO AVOID CONFLICT WITH django-ratelimit. I AM NOT CLAIMING OWNERSHIP OF THIS PACKAGE.
APIs are a very common way to interact with web services. As the need to consume data grows, so does the number of API calls necessary to remain up to date with data sources. However many API providers constrain developers from making too many API calls. This is know as rate limiting and in a worst case scenario your application can be banned from making further API calls if it abuses these limits.
This packages introduces a function decorator preventing a function from being called more often than that allowed by the API provider. This should prevent API providers from banning your applications by conforming to their rate limits.
Installation
PyPi
Add this line to your application’s requirements.txt:
apiratelimit
And then execute:
$ pip install -r requirements.txt
Or install it yourself:
$ pip install apiratelimit
GitHub
Installing the latest version from Github:
$ git clone https://github.com/tomasbasham/ratelimit
$ cd ratelimit
$ python setup.py install
Usage
To use this package simply decorate any function that makes an API call:
from apiratelimit import limits
import requests
FIFTEEN_MINUTES = 900
@limits(calls=15, period=FIFTEEN_MINUTES)
def call_api(url):
response = requests.get(url)
if response.status_code != 200:
raise Exception('API response: {}'.format(response.status_code))
return response
This function will not be able to make more then 15 API call within a 15 minute time period.
The arguments passed into the decorator describe the number of function invocation allowed over a specified time period (in seconds). If no time period is specified then it defaults to 15 minutes (the time window imposed by Twitter).
If a decorated function is called more times than that allowed within the specified time period then a ratelimit.RateLimitException is raised. This may be used to implement a retry strategy such as an expoential backoff
from apiratelimit import limits, RateLimitException
from backoff import on_exception, expo
import requests
FIFTEEN_MINUTES = 900
@on_exception(expo, RateLimitException, max_tries=8)
@limits(calls=15, period=FIFTEEN_MINUTES)
def call_api(url):
response = requests.get(url)
if response.status_code != 200:
raise Exception('API response: {}'.format(response.status_code))
return response
Alternatively to cause the current thread to sleep until the specified time period has ellapsed and then retry the function use the sleep_and_retry decorator. This ensures that every function invocation is successful at the cost of halting the thread.
from apiratelimit import limits, sleep_and_retry
import requests
FIFTEEN_MINUTES = 900
@sleep_and_retry
@limits(calls=15, period=FIFTEEN_MINUTES)
def call_api(url):
response = requests.get(url)
if response.status_code != 200:
raise Exception('API response: {}'.format(response.status_code))
return response
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file apiratelimit-2.2.1.tar.gz
.
File metadata
- Download URL: apiratelimit-2.2.1.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.4.0 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9bdca26cd6858d03a93aa4dae09a9fe364cfb7adb970da59164262ac7264fe9b |
|
MD5 | 0597c93f08f7a9d604788d6dc8870135 |
|
BLAKE2b-256 | 8842d7f4a7125139e063d6cab3360a0fa09b6fcbaa1f6e1fec5c135f9ae0c7ce |
File details
Details for the file apiratelimit-2.2.1-py3-none-any.whl
.
File metadata
- Download URL: apiratelimit-2.2.1-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.4.0 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 31f12a43b3aa2d2217ab4e97aa7d3e02ad8dd2d1d6862496993818d0d3c2aac2 |
|
MD5 | 28e39bc3aa7e72479f20f3239147a95b |
|
BLAKE2b-256 | 76a320b941ee5f01b29da13da19121132fd06f33c6702bdc1c9c3abb3f17fc57 |