Package: ratelimitcli
Adding rate limits to your APIThis is a python CLI that communicates with a cloud based HTTPRatelimit Serviceto configure rate limits based on the token bucket algorithm.Redis is used as the backing storage for the rate limit configurations, and using Lua scripts, theRatelimit Servicecan make a determination to allow or deny a request within milliseconds.This python package also comes with:An HTTP client that can be used to manually test your limits.A python decorator for your HTTP endpoint functions.Where to startThe quickstart below will guide you through configuring the CLI on your system, authenticating to theRatelimit Service, creating a rate limit, and testing it.For more detailed documentation check outhttps://docs.ratelimit.xyz.Configure the CLIInstall the RateLimit CLI.$pipinstallratelimitcliConfigure the RateLimit CLI and follow the interactive prompts. You won't have an API key yet so you can just press[ENTER]when asked for a value.$ratelimitcliconfigure
ClientID[None]:
APIkey[None]:
Firstname[None]:Jasmin
Lastname[None]:Smith
Emailname[None]:jasmin.smith@gmail.com
Updatedconfigfile(~/.ratelimit/config).Request an API key and a Client ID. You'll be asked to enter credit card information.$ ratelimitcli billing configure
Enter a credit card number: 1234567890123456
Enter the expiration date for that credit card: 10/12
Enter the CCV for that credit card: 333
New values have been written to the config file.DisclaimerI have not yet implemented payments and as such, you won't be charged for anything. That said, this service is currently in a closed alpha. There is only a single credit card that will allow you to get an API key and you'd have to get that from me.If this service or the concept is interesting enough for you and you don't mind sending an email, feel free to contact me and we can discuss your usecase.Oh and the fake card I show above won't work - just FYI.Check to see that the config file has been written to$HOME/.ratelimit/config$cat~/.ratelimit/configThe CLI is now configured and ready to use.Creating your first limitCreate your first limit by supplying integers for theburst limitand therate limit.$ratelimitclilimitsupsert\--throttling-burst-limit2\--throttling-rate-limit0Burst LimitThe maximum available instantaneous capacity.https://en.wikipedia.org/wiki/Token_bucketRate LimitThe rate at which used capacity is restored.https://en.wikipedia.org/wiki/Token_bucketYou'll know everything is set up when you get an API response with a limit ID that identifies the rate limit you just created.ReturnsOk: API response ({"limit_id": "a9f9f31b-2c0b-321b-b398-f9d36kd30820"}).We can store that limit ID into an environment variable for ease of access.exportLIMIT_ID=a9f9f31b-2c0b-321b-b398-f9d36kd30820Test your first rate limit.# ok - capacity 2, using the env var$ratelimitclilimitsrecord$LIMIT_ID# ok - capacity 1, using ID directly$ratelimitclilimitsrecord\a9f9f31b-2c0b-321b-b398-f9d36kd30820# ERROR - capacity 0, we've exhausted the capacity$ratelimitclilimitsrecord$LIMIT_IDTest rate limits in a python interpreter shell. Note that in production, you most likely will not be using theRatelimitClientdirectly, nor will you be using synchronous calls to theRatelimit Service.$python# Get the limit ID from the env var>>>importos>>>limit_id=os.environ['LIMIT_ID']>>>limit_id'a9f9f31b-2c0b-321b-b398-f9d36kd30820'# Import the client>>>fromratelimitcli.client.clientimportRatelimitClient# All args are optional.# Defaults are pulled from env and config file.>>>client=RatelimitClient()# Synchronous calls to the service.>>>client.sync_record_request(limit_id)>>>client.sync_record_request(..."a9f9f31b-2c0b-321b-b398-f9d36kd30820"...)Use your rate limit in your code.fromfastapiimportFastAPIfromratelimitcli.client.exceptionsimportAPIRateLimitExceptionfromratelimitcli.client.decoratorimportRatelimitDecoratorapp=FastAPI()defon_error_callback(msg:str,exception:APIRateLimitException):"""Callback function.Args:1. All the `*args` and `**kwargs` that were passedto the original function. In this case `msg: str`.2. The exception that was raised by RatelimitDecoratorReturns:Some value that takes the place of the wrapped function'sreturn value."""return"Goodbye, World!"+msg@app.get("/")@RatelimitDecorator(# The limit ID to check capacity against.id="a9f9f31b-2c0b-321b-b398-f9d36kd30820",# The function to call if capacity has been exhausted.callback=on_error_callback,)asyncdefhello(msg:str):return"Hello, World!"+msg