ratelimit


Nameratelimit JSON
Version 2.2.1 PyPI version JSON
download
home_pagehttps://github.com/tomasbasham/ratelimit
SummaryAPI rate limit decorator
upload_time2018-12-17 18:55:49
maintainer
docs_urlNone
authorTomas Basham
requires_python
licenseMIT
keywords ratelimit api decorator
VCS
bugtrack_url
requirements pytest pytest-cov pylint
Travis-CI
coveralls test coverage No coveralls.
            ratelimit |build| |maintainability|
===================================

.. |build| image:: https://travis-ci.org/tomasbasham/ratelimit.svg?branch=master
    :target: https://travis-ci.org/tomasbasham/ratelimit

.. |maintainability| image:: https://api.codeclimate.com/v1/badges/21dc7c529c35cd7ef732/maintainability
    :target: https://codeclimate.com/github/tomasbasham/ratelimit/maintainability
    :alt: Maintainability

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

To install ratelimit, simply:

.. code:: bash

    $ pip install ratelimit

GitHub
~~~~~~

Installing the latest version from Github:

.. code:: bash

    $ 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:

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

Contributing
------------

1. Fork it (https://github.com/tomasbasham/ratelimit/fork)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/tomasbasham/ratelimit",
    "name": "ratelimit",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "ratelimit,api,decorator",
    "author": "Tomas Basham",
    "author_email": "me@tomasbasham.co.uk",
    "download_url": "https://files.pythonhosted.org/packages/ab/38/ff60c8fc9e002d50d48822cc5095deb8ebbc5f91a6b8fdd9731c87a147c9/ratelimit-2.2.1.tar.gz",
    "platform": "",
    "description": "ratelimit |build| |maintainability|\n===================================\n\n.. |build| image:: https://travis-ci.org/tomasbasham/ratelimit.svg?branch=master\n    :target: https://travis-ci.org/tomasbasham/ratelimit\n\n.. |maintainability| image:: https://api.codeclimate.com/v1/badges/21dc7c529c35cd7ef732/maintainability\n    :target: https://codeclimate.com/github/tomasbasham/ratelimit/maintainability\n    :alt: Maintainability\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\nTo install ratelimit, simply:\n\n.. code:: bash\n\n    $ pip install ratelimit\n\nGitHub\n~~~~~~\n\nInstalling the latest version from Github:\n\n.. code:: bash\n\n    $ git clone https://github.com/tomasbasham/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\nContributing\n------------\n\n1. Fork it (https://github.com/tomasbasham/ratelimit/fork)\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "API rate limit decorator",
    "version": "2.2.1",
    "split_keywords": [
        "ratelimit",
        "api",
        "decorator"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "1e183851a8703a755ceddd05d446e608",
                "sha256": "af8a9b64b821529aca09ebaf6d8d279100d766f19e90b5059ac6a718ca6dee42"
            },
            "downloads": -1,
            "filename": "ratelimit-2.2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "1e183851a8703a755ceddd05d446e608",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 5251,
            "upload_time": "2018-12-17T18:55:49",
            "upload_time_iso_8601": "2018-12-17T18:55:49.675747Z",
            "url": "https://files.pythonhosted.org/packages/ab/38/ff60c8fc9e002d50d48822cc5095deb8ebbc5f91a6b8fdd9731c87a147c9/ratelimit-2.2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2018-12-17 18:55:49",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "tomasbasham",
    "github_project": "ratelimit",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "pytest",
            "specs": [
                [
                    "==",
                    "2.6.4"
                ]
            ]
        },
        {
            "name": "pytest-cov",
            "specs": [
                [
                    "==",
                    "2.5.1"
                ]
            ]
        },
        {
            "name": "pylint",
            "specs": [
                [
                    "==",
                    "1.7.2"
                ]
            ]
        }
    ],
    "lcname": "ratelimit"
}
        
Elapsed time: 0.01457s