requests-counter
A tool to monitor the number of HTTP requests.
It uses a key as extra parameter in the HTTP header, and optionally can filter the HTTP request per source (like origin).
Use cases
Scenario: A Company that sell a Service that is limited by a max amount of requests and/or filtered by source HTTP request parameter.
- As a Company, I want to set a request limit for an _apiKey_and/or by source.
- As a Company, I want to update/destroy/inspect the status of subscription via api.
Installation
Requirement
Install redis or run a docker container as below
$> docker run --name test-redis -p6379:6379 -ti redis redis-server --appendonly yes
Package Installation
$> pip install requests-counter
Usage
As request counter for fastapi
from fastapi import Depends, FastAPI, HTTPException, Header
# 1. Import the library
from requests_counter.reqcounter import ReqCounter
import asyncio
app = FastAPI()
# 2. Create an ReqCounter object with the url to redis instance as parameter
cl = ReqCounter("redis://localhost")
# 3. populate the Object with a list of tuple (key, max_value)
asyncio.create_task(cl.setup_api_key([("my-api-key-test", 100)]))
asyncio.create_task(cl.setup_source(["source1", "source2"]))
# 4. Declare a function to be injected into Depends module.
# It will decrease the max_value for each request. It will raise a 429 HTTPException when max_value is 0.
# It will raise a 403 HTTPException when source is not in the list.
async def check_key(apiKey: str = Header(None), source: str = Header(None)):
res = await cl.decrease(apiKey)
if res is False:
raise HTTPException(400, "User Requests Limit Exceeded")
if await cl.check_source(source) is False:
raise HTTPException(403, "Forbidden")
return apiKey
# 5. Inject the check_key function to endpoint
@app.get("/consume")
async def consume_key(apiKey=Depends(check_key)):
return {"job": "done", "apiKey": apiKey}
To run this example
$> uvicorn requests_counter.example:app --reload --port 8080
As endpoint
Command below run the server to interact with your redis instance for
- Destroy a key
- Update the value (i.e. renewal)
- Get the status of all keys
$> uvicorn requests_counter.api:app --reload --port 8080
Run http://locahost:8080/docs for documentation.
Metadata
Release files for requests-counter 2.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| requests-counter-2.0.1.tar.gz | 4.5 kB | Without provenance | Details for requests-counter-2.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| requests_counter-2.0.1-py3-none-any.whl | Python 3 | none | any | Details for requests_counter-2.0.1-py3-none-any.whl |
Total release size: 10.9 kB
Release files / requests-counter-2.0.1.tar.gz
| Download URL | requests-counter-2.0.1.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e6dddb88660c774bb42dc115a52bc8adea38e4aaede32b6abc6630d19eab1a26
|
|
BLAKE2b-256 checksum How to use checksums |
6fc76d26a850bb273162da334438936fba65394dc813a78ff3cc4b2cb1060837
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.4 CPython/3.8.3 Linux/4.15.0-130-generic
|
Release files / requests_counter-2.0.1-py3-none-any.whl
| Download URL | requests_counter-2.0.1-py3-none-any.whl |
|---|---|
| Size | 6.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a6161fe25f8b5977de6e2b49a0ccdd8ffddb5e0619be9da6de5fce4aa5e5400c
|
|
BLAKE2b-256 checksum How to use checksums |
3c13b352c19f4a22e94ff788b14f75e265a441007afb7bd2a3ae9663cac3c356
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.4 CPython/3.8.3 Linux/4.15.0-130-generic
|
Security for requests-counter 2.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.