Skip to main content

API rate limit decorator

Project description

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 known 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 package 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:

ratelimit-extended

And then execute:

$ pip install -r requirements.txt

Or install it yourself:

$ pip install ratelimit-extended

GitHub

Installing the latest version from GitHub:

$ git clone https://github.com/omert11/ratelimit.git
$ cd ratelimit
$ python setup.py install

Usage

To use this package, simply decorate any function that makes an API call:

from ratelimit 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 than 15 API calls within a 15-minute time period.

The arguments passed into the decorator describe the number of function invocations 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 allowed within the specified time period, then a ratelimit.RateLimitException is raised. This may be used to implement a retry strategy such as an exponential backoff.

from ratelimit 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 elapsed 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 ratelimit 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ratelimit-extended-1.0.5.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

ratelimit_extended-1.0.5-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file ratelimit-extended-1.0.5.tar.gz.

File metadata

  • Download URL: ratelimit-extended-1.0.5.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.7

File hashes

Hashes for ratelimit-extended-1.0.5.tar.gz
Algorithm Hash digest
SHA256 143dbdeeba59ea06ebe8c1faf4778d0cfb228d7674d0e9e6f8c235cab4c38e5a
MD5 787b200341d1b54e58dfe8ecd8a08b5a
BLAKE2b-256 052b84b559363dabed6d4c16812c8356f174d13578f7f41ece097b7e3ea8eec2

See more details on using hashes here.

File details

Details for the file ratelimit_extended-1.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for ratelimit_extended-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 4884e24b74bd4de619820be6e706744310d501a60b0ae76e6e43119ba375a53b
MD5 6336d5f4b31dccc6715b8d30230afe9e
BLAKE2b-256 fdcb25f6089d73bf265453b3a08c6131d6df66e58bae9478caddcc731fe387c1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page