SDK
Python SDK
Flare has a Python SDK available on PyPI. It is a light wrapper around requests that automatically manages API authentication.
Links
Installing
pip install flareio
Basic Usage
The flareio
package provides the FlareApiClient
class, which automatically manages
authentication and exposes get
, post
, put
, and delete
methods.
Basic Usage Example
import os
from flareio import FlareApiClient
client = FlareApiClient(
api_key=os.environ["FLARE_API_KEY"],
tenant_id=None, # Use my default tenant.
)
resp = client.get("/tokens/test")
print(resp.json())
Paging Util
The FlareApiClient
has a scroll
method that can be used with endpoints that support the
Flare standard paging pattern .
Paging Util Example
import os
import time
from flareio import FlareApiClient
api_key = os.environ.get("FLARE_API_KEY")
if not api_key:
raise Exception("Please provide an API key")
api_client = FlareApiClient(api_key=api_key)
last_from: str | None = None
fetched_pages: int = 0
for resp in api_client.scroll(
method="GET",
url="/leaksdb/v2/sources",
params={
"from": None,
},
):
# Rate limiting.
time.sleep(1)
# Get results from the response
resp_data = resp.json()
items = resp_data.get("items")
fetched_pages += 1
print(f"Fetched page {fetched_pages} ({last_from=}) with {len(items)} items...")
# Save the last "next" value.
last_from = resp_data.get("next") or last_from
print("The last value for 'next' was", last_from)
Custom Session
The FlareApiClient
can be initialized with a custom requests.Session
. This allows, for example,
to inject custom retry configuration.
Custom Session
import requests
from flareio import FlareApiClient
from requests.adapters import HTTPAdapter
from urllib3.util import Retry
session = requests.Session()
retries = Retry(
total=5,
backoff_factor=2,
status_forcelist=[429, 502, 503, 504],
allowed_methods={"GET", "POST"},
backoff_max=15,
)
session.mount(
"https://",
HTTPAdapter(
max_retries=retries,
),
)
api_client = FlareApiClient(
api_key="fw_...",
session=session,
)