SDK to interact with the Aperture Agent
Project description
🚀 Just launched v1 of managed rate limiting service
Support us with your feedback and questions on Product Hunt or Discord
Rate Limiting for Python Applications
The aperture-py
SDK provides an easy way to integrate your Python applications
with FluxNinja Aperture. It allows flow
control functionality on fine-grained features inside service code.
Refer to documentation for more details.
Usage
Install SDK
Run the command below to install the SDK:
pip install aperture-py
Create Aperture Client
The next step is to create an Aperture Client instance, for which, the address of the organization created in Aperture Cloud and API key are needed. You can locate both these details by clicking on the Aperture tab in the sidebar menu of Aperture Cloud.
from aperture_sdk.client import ApertureClient, FlowParams
agent_address = os.getenv("APERTURE_AGENT_ADDRESS", default_agent_address)
api_key = os.getenv("APERTURE_API_KEY", "")
insecure = os.getenv("APERTURE_AGENT_INSECURE", "true").lower() == "true"
aperture_client = ApertureClient.new_client(
address=agent_address, insecure=insecure, api_key=api_key
)
Flow Functionality
The created instance can then be used to start a flow:
# business logic produces labels
labels = {
"user_id": "some_user_id",
"user_tier": "gold",
"priority": "100",
}
flow_params = FlowParams(
check_timeout=timedelta(seconds=200),
explicit_labels=labels,
)
# start_flow performs a flowcontrol.v1.Check call to Aperture Agent.
# It returns a Flow or raises an error if any.
flow = await aperture_client.start_flow(
control_point="AwesomeFeature",
params=flow_params,
)
# Check if flow check was successful.
if not flow.success:
logger.info("Flow check failed - will fail-open")
# See whether flow was accepted by Aperture Agent.
if flow.should_run():
# do actual work
pass
else:
# handle flow rejection by Aperture Agent
flow.set_status(FlowStatus.Error)
res = await flow.end()
if res.get_error():
logger.error("Error: {}".format(res.get_error()))
elif res.get_flow_end_response():
logger.info("Flow End Response: {}".format(res.get_flow_end_response()))
# Simulate work being done
await asyncio.sleep(2)
return "", 202
The above code snippet is making start_flow
calls to Aperture. For this call,
it is important to specify the control point (AwesomeFeature
in the example)
and FlowParams
that will be aligned with the policy created in Aperture Cloud.
For request prioritization use cases, it's important to set a higher gRPC
deadline. This parameter specifies the maximum duration a request can remain in
the queue. For each flow that is started, a should_run
decision is made,
determining whether to allow the request into the system or to rate limit it. It
is important to make the end
call made after processing each request, to send
telemetry data that would provide granular visibility for each flow.
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 aperture_py-2.26.0.tar.gz
.
File metadata
- Download URL: aperture_py-2.26.0.tar.gz
- Upload date:
- Size: 25.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.11.5 Linux/5.15.0-1050-aws
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 93a41b3c5cc444b3b08bfa130495c44213eef53f0ec3360e63aac4e475573eee |
|
MD5 | d03fe5fa4664e3a146d070d972ada0da |
|
BLAKE2b-256 | 4891bd75f4ab1c491884d55d0ed61730d31fa2bd53473f1c2597892278052df0 |
File details
Details for the file aperture_py-2.26.0-py3-none-any.whl
.
File metadata
- Download URL: aperture_py-2.26.0-py3-none-any.whl
- Upload date:
- Size: 29.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.11.5 Linux/5.15.0-1050-aws
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 912e66a3440fc7cf7967087417a8e3028213311b4ec944b4b5c5ec6687ad60c6 |
|
MD5 | 4994c303701cf60ae5b7801e544bb3ce |
|
BLAKE2b-256 | bc358717288ee3b7931c997c56247c2dc3ad833414850e17a603f645bdc90322 |