ratelimit-extended
==================
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``:
.. code:: python
ratelimit-extended
And then execute:
.. code:: bash
$ pip install -r requirements.txt
Or install it yourself:
.. code:: bash
$ pip install ratelimit-extended
GitHub
~~~~~~
Installing the latest version from GitHub:
.. code:: bash
$ 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:
.. code:: python
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 <https://pypi.org/project/backoff/>`__.
.. code:: python
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.
.. code:: python
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 <LICENSE.txt>`__.
Raw data
{
"_id": null,
"home_page": "https://github.com/omert11/ratelimit",
"name": "ratelimit-extended",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "ratelimit, api, decorator",
"author": "\u00d6mer Faruk Y\u0131\u011f\u0131n",
"author_email": "omert1122@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/05/2b/84b559363dabed6d4c16812c8356f174d13578f7f41ece097b7e3ea8eec2/ratelimit-extended-1.0.5.tar.gz",
"platform": null,
"description": "ratelimit-extended\n==================\n\nAPIs are a very common way to interact with web services. As the need to\nconsume data grows, so does the number of API calls necessary to remain\nup to date with data sources. However, many API providers constrain\ndevelopers from making too many API calls. This is known as rate\nlimiting and, in a worst-case scenario, your application can be banned\nfrom making further API calls if it abuses these limits.\n\nThis package introduces a function decorator preventing a function from\nbeing called more often than that allowed by the API provider. This\nshould prevent API providers from banning your applications by\nconforming to their rate limits.\n\nInstallation\n------------\n\nPyPi\n~~~~\n\nAdd this line to your application\u2019s ``requirements.txt``:\n\n.. code:: python\n\n ratelimit-extended\n\nAnd then execute:\n\n.. code:: bash\n\n $ pip install -r requirements.txt\n\nOr install it yourself:\n\n.. code:: bash\n\n $ pip install ratelimit-extended\n\nGitHub\n~~~~~~\n\nInstalling the latest version from GitHub:\n\n.. code:: bash\n\n $ git clone https://github.com/omert11/ratelimit.git\n $ cd ratelimit\n $ python setup.py install\n\nUsage\n-----\n\nTo use this package, simply decorate any function that makes an API\ncall:\n\n.. code:: python\n\n from ratelimit import limits\n\n import requests\n\n FIFTEEN_MINUTES = 900\n\n @limits(calls=15, period=FIFTEEN_MINUTES)\n def call_api(url):\n response = requests.get(url)\n\n if response.status_code != 200:\n raise Exception('API response: {}'.format(response.status_code))\n return response\n\nThis function will not be able to make more than 15 API calls within a\n15-minute time period.\n\nThe arguments passed into the decorator describe the number of function\ninvocations allowed over a specified time period (in seconds). If no\ntime period is specified, then it defaults to 15 minutes (the time\nwindow imposed by Twitter).\n\nIf a decorated function is called more times than allowed within the\nspecified time period, then a ``ratelimit.RateLimitException`` is\nraised. This may be used to implement a retry strategy such as an\n`exponential backoff <https://pypi.org/project/backoff/>`__.\n\n.. code:: python\n\n from ratelimit import limits, RateLimitException\n from backoff import on_exception, expo\n\n import requests\n\n FIFTEEN_MINUTES = 900\n\n @on_exception(expo, RateLimitException, max_tries=8)\n @limits(calls=15, period=FIFTEEN_MINUTES)\n def call_api(url):\n response = requests.get(url)\n\n if response.status_code != 200:\n raise Exception('API response: {}'.format(response.status_code))\n return response\n\nAlternatively, to cause the current thread to sleep until the specified\ntime period has elapsed and then retry the function, use the\n``sleep_and_retry`` decorator. This ensures that every function\ninvocation is successful at the cost of halting the thread.\n\n.. code:: python\n\n from ratelimit import limits, sleep_and_retry\n\n import requests\n\n FIFTEEN_MINUTES = 900\n\n @sleep_and_retry\n @limits(calls=15, period=FIFTEEN_MINUTES)\n def call_api(url):\n response = requests.get(url)\n\n if response.status_code != 200:\n raise Exception('API response: {}'.format(response.status_code))\n return response\n\nLicense\n-------\n\nThis project is licensed under the `MIT License <LICENSE.txt>`__.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "API rate limit decorator",
"version": "1.0.5",
"project_urls": {
"Homepage": "https://github.com/omert11/ratelimit"
},
"split_keywords": [
"ratelimit",
" api",
" decorator"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "fdcb25f6089d73bf265453b3a08c6131d6df66e58bae9478caddcc731fe387c1",
"md5": "6336d5f4b31dccc6715b8d30230afe9e",
"sha256": "4884e24b74bd4de619820be6e706744310d501a60b0ae76e6e43119ba375a53b"
},
"downloads": -1,
"filename": "ratelimit_extended-1.0.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "6336d5f4b31dccc6715b8d30230afe9e",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 7835,
"upload_time": "2024-07-23T12:37:43",
"upload_time_iso_8601": "2024-07-23T12:37:43.789274Z",
"url": "https://files.pythonhosted.org/packages/fd/cb/25f6089d73bf265453b3a08c6131d6df66e58bae9478caddcc731fe387c1/ratelimit_extended-1.0.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "052b84b559363dabed6d4c16812c8356f174d13578f7f41ece097b7e3ea8eec2",
"md5": "787b200341d1b54e58dfe8ecd8a08b5a",
"sha256": "143dbdeeba59ea06ebe8c1faf4778d0cfb228d7674d0e9e6f8c235cab4c38e5a"
},
"downloads": -1,
"filename": "ratelimit-extended-1.0.5.tar.gz",
"has_sig": false,
"md5_digest": "787b200341d1b54e58dfe8ecd8a08b5a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 8029,
"upload_time": "2024-07-23T12:37:45",
"upload_time_iso_8601": "2024-07-23T12:37:45.325765Z",
"url": "https://files.pythonhosted.org/packages/05/2b/84b559363dabed6d4c16812c8356f174d13578f7f41ece097b7e3ea8eec2/ratelimit-extended-1.0.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-23 12:37:45",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "omert11",
"github_project": "ratelimit",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "pytest",
"specs": [
[
"==",
"2.6.4"
]
]
},
{
"name": "pytest-cov",
"specs": [
[
"==",
"2.5.1"
]
]
},
{
"name": "isort",
"specs": [
[
"==",
"5.13.2"
]
]
},
{
"name": "mypy",
"specs": [
[
"==",
"1.10.0"
]
]
},
{
"name": "pre-commit",
"specs": [
[
"==",
"3.7.1"
]
]
},
{
"name": "ruff",
"specs": [
[
"==",
"0.4.4"
]
]
}
],
"lcname": "ratelimit-extended"
}