Metadata-Version: 2.1
Name: api-throttler
Version: 0.1.0
Summary: A python toolkit to enforce API rate limit on the backend
Home-page: https://github.com/berknology/api-throttler
Author: He Hao
Author-email: berknology@gmail.com
License: BSD license
Keywords: ratelimit,rate-limit,rate-limiter,throttling,throttle,throttler,api,decorator
Platform: UNKNOWN
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: BSD License
Classifier: Natural Language :: English
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Requires-Dist: unittest-xml-reporting

==================================================
API Throttler
==================================================

A Python toolkit to enforce API rate limit on the backend.

Usage
-----
To use this API throttler toolkit, first install it using pip:

.. code-block:: bash

    pip install api-throttler


Then, import the package in your python script and use appropriate throttler classes:

.. code-block:: python

    import time

    from api_throttler import Throttler, FixedWindowThrottler, SlidingWindowThrottler


    # Limit 3 calls per 10 seconds
    fixed_window_throttler = FixedWindowThrottler(calls=3, period=10)
    sliding_window_throttler = SlidingWindowThrottler(calls=3, period=10)


    def call_api(throttler: Throttler, key: str = 'some_string_key'):
        if not throttler.is_throttled(key):
            print('API call is NOT throttled')
        else:
            print('API call is throttled')


    if __name__ == '__main__':
        print('Using fixed window API throttler')
        for i in range(20):
            print(f'This is the {i}-th second')
            # Call API in the following i-th seconds
            if i in {0, 8, 9, 10, 11, 12}:
                call_api(fixed_window_throttler)
            time.sleep(1)

        print('-'*40)

        print('Using sliding window API throttler')
        for i in range(20):
            print(f'This is the {i}-th second')
            if i in {0, 8, 9, 10, 11, 12}:
                call_api(sliding_window_throttler)
            time.sleep(1)


