Billing and Quota Backend for Plan-Based, Restricted API Access
Project description
Frappy API Billing
API Quota and Billing System for Flask - compatible with the Frappy framework.
- This is the backend module for Flask
- Frontend package for React can be found here: @frappy/react-api-billing
- Usage store modules for Python can be found here:
Usage
from frappyapibilling import ApiBilling, QuotaDuration, QuotaDefinition
from frappymongoapibilling import UsageStore
from datetime import datetime
from flask import Flask, jsonify, make_response
# init the usage store and the api billing handler instance
store = UsageStore(mongo_url="mongodb://localhost:27015", mongo_db="myDbName", collection_name="apiUsage")
api_billing = ApiBilling(usage_store=store)
# define the quotas for any existing client and feed them into the ApiBilling instance
client_id = "client1"
quotas = [
QuotaDefinition(duration_type=QuotaDuration.DAY, credit_limit=500, start_date=datetime(2022, 1, 1)),
QuotaDefinition(duration_type=QuotaDuration.MONTH, credit_limit=10000, start_date=datetime(2022, 1, 1)),
]
api_billing.update_client_quotas(client_id=client_id, quota_definitions=quotas)
app = Flask(__name__)
@app.route("/api/fetch-data", methods=["GET"])
def fetch_data_api_handler():
# run authentication of the user
authenticated_client = "client1" # replace with your auth mechanism
api_billing.track_client_usage(client_id=authenticated_client) # will abort if insufficient credits and deduct 1 credit
# run your method
result = ...
return jsonify(result)
@app.route("/api/fetch-expensive-data", methods=["GET"])
def fetch_expensive_data_api_handler():
# run authentication of the user
authenticated_client = "client1" # replace with your auth mechanism
credits_used = 5.0 # this can also be determined dynamically by the request
api_billing.track_client_usage(client_id=authenticated_client, credits_used=credits_used)
# run your method
result = ...
# return a response with X-RateLimit-[Remaining|Reset] headers
return api_billing.create_response_with_header(client_id=authenticated_client, response_body=jsonify(result))
Quota Definitions
It is up to you, where and how you store quota definitions. The API Billing module does not store the quota definitions anywhere.
When your API starts up you need to fetch any pre-existing clients and their respective quota limits and create the quota definitions. The module will fetch existing usage and update the current renew interval with already used up credits.
Likewise, while the API is running, if a new client registers, you can feed the quota definitions into the ApiBilling
instance using the update_client_quotas(client_id, quota_definitions: List[QuotdaDefinition])
method.
Error Handling
The track_client_usage
method will first check if sufficient credits are available. If not there are 2 possible
behaviours:
- by default a 429 will be returned to the caller with an error message and
RateLimit
headers - if the parameter
use_abort=False
is passed into thetrack_client_usage
aQuotaException
will be thrown and you have to handle the error.
A QuotaException
has 2 fields storing quota_remaining
(a number) and quota_renew
(datetime
) for the "shortest
quota" - i.e. the quota definition with the fewest remaining credits.
You can also pass the QuotaException
instance into the create_response_with_header
method of ApiBilling
as the
optional exception
parameter, which can reduce required computation to determining the next renew and remaining
credits.
Please note, all datetime
objects are relative to the server's timezone.
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
Built Distribution
File details
Details for the file frappyapibilling-0.1.0.tar.gz
.
File metadata
- Download URL: frappyapibilling-0.1.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f749b4c2ee2cfec91282a9ec5ad3a89db6bd96c76ca9c9ce0e0733caf1e194fc |
|
MD5 | df85cf8b467bde4d1f7279370ab812d0 |
|
BLAKE2b-256 | 610820ecfef4e2f6863a2fdbac0d69431e1eff8afe3074efd471a0f2003df434 |
File details
Details for the file frappyapibilling-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: frappyapibilling-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 45e485513ea83f4f189a2c37b4c7648701219c7df7408d14055f2640b098f0df |
|
MD5 | b1c33dfbd5d1a3b247ff6be5a7dbc602 |
|
BLAKE2b-256 | c4ee022bee7ff1cf55bffe00f579bf25fb8d95ec54542bfa248cfc5492101ae2 |