A python toolkit to enforce API rate limit on the backend
Project description
A Python toolkit to enforce API rate limit on the backend.
Usage
To use this API throttler toolkit, first install it using pip:
pip install api-throttler
Then, import the package in your python script and use appropriate throttler classes:
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)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
api_throttler-0.1.0.tar.gz
(3.9 kB
view hashes)
Built Distribution
Close
Hashes for api_throttler-0.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3721777f20044b44067ef779609b5544f594645def108357a557eb2e3592d6c |
|
MD5 | c25b83452449df4da7b6aaa91d7d347e |
|
BLAKE2b-256 | 610ecd84ae1c9127a46c6a9f48c2d0deca8788130997423fbb6efef2deda297b |