deckar01-ratelimit


Namedeckar01-ratelimit JSON
Version 3.0.2 PyPI version JSON
download
home_pagehttps://github.com/deckar01/ratelimit
SummaryAPI rate limit decorator
upload_time2021-11-19 21:26:22
maintainer
docs_urlNone
authorJared Deckard
requires_python>3.6.0
licenseMIT
keywords ratelimit api decorator
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ratelimit |build| |maintainability| |coverage|
==============================================

This project is a fork of `tomasbasham/ratelimit <https://github.com/tomasbasham/ratelimit>`_
that implements a `sliding log <https://konghq.com/blog/how-to-design-a-scalable-rate-limiting-algorithm/>`_
for correctness and provides persistance via sqlite. See the usage section on
`Persistence <#persistence>`_ for more details. Turning on persistence is highly
recommended, especially during development, to ensure rate limits are respected
between application restarts.

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:

.. code:: python

    deckar01-ratelimit

And then execute:

.. code:: bash

    $ pip install -r requirements.txt

Or install it yourself:

.. code:: bash

    $ pip install deckar01-ratelimit

GitHub
~~~~~~

Installing the latest version from Github:

.. code:: bash

    $ git clone https://github.com/deckar01/ratelimit
    $ 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 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
<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 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.

.. 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

Persistence
~~~~~~~~~~~

If a limit needs to be respected between application restarts or shared by
multiple processes, the ``storage`` argument can be used to save the limit
state to disk and load it automatically.

.. code:: python

    from ratelimit import limits, sleep_and_retry

    import requests

    FIFTEEN_MINUTES = 900

    @sleep_and_retry
    @limits(calls=15, period=FIFTEEN_MINUTES, storage='ratelimit.db')
    def call_api(url):
        response = requests.get(url)

        if response.status_code != 200:
            raise Exception('API response: {}'.format(response.status_code))
        return response

If multiple limits need to be persisted, the ``name`` argument can be used to
store them in the same database using different tables.

.. code:: python

    from ratelimit import limits, sleep_and_retry

    import requests

    HOUR = 3600
    DAY = 24*HOUR

    @sleep_and_retry
    @limits(calls=15, period=HOUR, storage='ratelimit.db', name='hourly_limit')
    @sleep_and_retry
    @limits(calls=100, period=DAY, storage='ratelimit.db', name='daily_limit')
    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>`_.

.. |build| image:: https://github.com/deckar01/ratelimit/actions/workflows/test.yml/badge.svg
    :target: https://github.com/deckar01/ratelimit/actions/workflows/test.yml

.. |maintainability| image:: https://api.codeclimate.com/v1/badges/8bf92a976a1763a93339/maintainability
    :target: https://codeclimate.com/github/deckar01/ratelimit/maintainability
    :alt: Maintainability

.. |coverage| image:: https://api.codeclimate.com/v1/badges/8bf92a976a1763a93339/test_coverage
    :target: https://codeclimate.com/github/deckar01/ratelimit/test_coverage
    :alt: Test Coverage


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/deckar01/ratelimit",
    "name": "deckar01-ratelimit",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">3.6.0",
    "maintainer_email": "",
    "keywords": "ratelimit,api,decorator",
    "author": "Jared Deckard",
    "author_email": "jared@shademaps.com",
    "download_url": "https://files.pythonhosted.org/packages/4d/78/f005bb3b66152a60d590c1c939692431cea54bfc6dd91d10f5c5dd218583/deckar01-ratelimit-3.0.2.tar.gz",
    "platform": "",
    "description": "ratelimit |build| |maintainability| |coverage|\n==============================================\n\nThis project is a fork of `tomasbasham/ratelimit <https://github.com/tomasbasham/ratelimit>`_\nthat implements a `sliding log <https://konghq.com/blog/how-to-design-a-scalable-rate-limiting-algorithm/>`_\nfor correctness and provides persistance via sqlite. See the usage section on\n`Persistence <#persistence>`_ for more details. Turning on persistence is highly\nrecommended, especially during development, to ensure rate limits are respected\nbetween application restarts.\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 up to\ndate with data sources. However many API providers constrain developers from\nmaking too many API calls. This is know as rate limiting and in a worst case\nscenario your application can be banned from making further API calls if it\nabuses these limits.\n\nThis packages introduces a function decorator preventing a function from being\ncalled more often than that allowed by the API provider. This should prevent\nAPI providers from banning your applications by conforming to their rate\nlimits.\n\nInstallation\n------------\n\nPyPi\n~~~~\n\nAdd this line to your application's requirements.txt:\n\n.. code:: python\n\n    deckar01-ratelimit\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 deckar01-ratelimit\n\nGitHub\n~~~~~~\n\nInstalling the latest version from Github:\n\n.. code:: bash\n\n    $ git clone https://github.com/deckar01/ratelimit\n    $ cd ratelimit\n    $ python setup.py install\n\nUsage\n-----\n\nTo use this package simply decorate any function that makes an API call:\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 then 15 API call within a 15 minute\ntime period.\n\nThe arguments passed into the decorator describe the number of function\ninvocation allowed over a specified time period (in seconds). If no time period\nis specified then it defaults to 15 minutes (the time window imposed by\nTwitter).\n\nIf a decorated function is called more times than that allowed within the\nspecified time period then a ``ratelimit.RateLimitException`` is raised. This\nmay be used to implement a retry strategy such as an `expoential backoff\n<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 time\nperiod has ellapsed and then retry the function use the ``sleep_and_retry``\ndecorator. This ensures that every function invocation is successful at the\ncost 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\nPersistence\n~~~~~~~~~~~\n\nIf a limit needs to be respected between application restarts or shared by\nmultiple processes, the ``storage`` argument can be used to save the limit\nstate to disk and load it automatically.\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, storage='ratelimit.db')\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\nIf multiple limits need to be persisted, the ``name`` argument can be used to\nstore them in the same database using different tables.\n\n.. code:: python\n\n    from ratelimit import limits, sleep_and_retry\n\n    import requests\n\n    HOUR = 3600\n    DAY = 24*HOUR\n\n    @sleep_and_retry\n    @limits(calls=15, period=HOUR, storage='ratelimit.db', name='hourly_limit')\n    @sleep_and_retry\n    @limits(calls=100, period=DAY, storage='ratelimit.db', name='daily_limit')\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\n.. |build| image:: https://github.com/deckar01/ratelimit/actions/workflows/test.yml/badge.svg\n    :target: https://github.com/deckar01/ratelimit/actions/workflows/test.yml\n\n.. |maintainability| image:: https://api.codeclimate.com/v1/badges/8bf92a976a1763a93339/maintainability\n    :target: https://codeclimate.com/github/deckar01/ratelimit/maintainability\n    :alt: Maintainability\n\n.. |coverage| image:: https://api.codeclimate.com/v1/badges/8bf92a976a1763a93339/test_coverage\n    :target: https://codeclimate.com/github/deckar01/ratelimit/test_coverage\n    :alt: Test Coverage\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "API rate limit decorator",
    "version": "3.0.2",
    "project_urls": {
        "Homepage": "https://github.com/deckar01/ratelimit"
    },
    "split_keywords": [
        "ratelimit",
        "api",
        "decorator"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8245a0d5bfcd8ce26bf6cea60df85c813dc596d4e42d99ec2739c7a502abeeb0",
                "md5": "4aab42c3eeed61c7f1edb2c94a032890",
                "sha256": "5de6341ccc2198cf1179c37d372313611dcd671765b3305b7c0340eb52c7549b"
            },
            "downloads": -1,
            "filename": "deckar01_ratelimit-3.0.2-py2-none-any.whl",
            "has_sig": false,
            "md5_digest": "4aab42c3eeed61c7f1edb2c94a032890",
            "packagetype": "bdist_wheel",
            "python_version": "py2",
            "requires_python": ">3.6.0",
            "size": 6875,
            "upload_time": "2021-11-19T21:26:18",
            "upload_time_iso_8601": "2021-11-19T21:26:18.752010Z",
            "url": "https://files.pythonhosted.org/packages/82/45/a0d5bfcd8ce26bf6cea60df85c813dc596d4e42d99ec2739c7a502abeeb0/deckar01_ratelimit-3.0.2-py2-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fe1aa2a270f038ea23336cffb0d274904a822585c0a7de5c5e60cdaada2a05f4",
                "md5": "e12cb8a20e5cc145f33976a5d5ca99f7",
                "sha256": "c29bd1e03d768754a528622c5b6538ac0ae3ef8c0e76415e8a7125ae715852cd"
            },
            "downloads": -1,
            "filename": "deckar01_ratelimit-3.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e12cb8a20e5cc145f33976a5d5ca99f7",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">3.6.0",
            "size": 6886,
            "upload_time": "2021-11-19T21:26:20",
            "upload_time_iso_8601": "2021-11-19T21:26:20.611178Z",
            "url": "https://files.pythonhosted.org/packages/fe/1a/a2a270f038ea23336cffb0d274904a822585c0a7de5c5e60cdaada2a05f4/deckar01_ratelimit-3.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4d78f005bb3b66152a60d590c1c939692431cea54bfc6dd91d10f5c5dd218583",
                "md5": "031ad96cf8f917e01329574ad074527f",
                "sha256": "6f3f16f908c8e56bde66bb57e4491971e61b509e9ee8ac721354bc1a3c518dd5"
            },
            "downloads": -1,
            "filename": "deckar01-ratelimit-3.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "031ad96cf8f917e01329574ad074527f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">3.6.0",
            "size": 7921,
            "upload_time": "2021-11-19T21:26:22",
            "upload_time_iso_8601": "2021-11-19T21:26:22.365469Z",
            "url": "https://files.pythonhosted.org/packages/4d/78/f005bb3b66152a60d590c1c939692431cea54bfc6dd91d10f5c5dd218583/deckar01-ratelimit-3.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2021-11-19 21:26:22",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "deckar01",
    "github_project": "ratelimit",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "deckar01-ratelimit"
}
        
Elapsed time: 0.40815s