HTTP client
BrainHttpClient — construction, Bearer auth, flat keyword arguments, and what the HTTP tier cannot do.
Construction
BrainHttpClient(
api_key: str,
base_url: str = "http://127.0.0.1:8080",
timeout: float = 30.0,
retry: HttpRetryPolicy | None = None,
)api_keystrrequiredSent as Authorization: Bearer <api_key>. Empty raises BrainHttpError
with status == 0 and code == "config" before any request is made,
and so does a key carrying an ASCII control character — see below.
base_urlstroptionalThe edge. Defaults to the self-host address. A scheme that is not http
or https is rejected at construction, not at call time.
timeoutfloatoptionalPer-request timeout in seconds — not milliseconds. Defaults to 30.0.
A timeout surfaces as a BrainHttpError with status == 0.
retryHttpRetryPolicy | NoneoptionalDefaults to HttpRetryPolicy() — 3 attempts, 0.1 s base, 2.0 s cap. See
Retries.
import os
from brain_db_sdk import BrainHttpClient
brain = BrainHttpClient(
api_key=os.environ["BRAIN_API_KEY"],
base_url=os.environ.get("BRAIN_BASE_URL", "http://127.0.0.1:8080"),
timeout=15.0,
)There is no close()
The HTTP client holds no connection. Construct one per process and keep it; there is nothing to release.
What the constructor rejects
Three checks, all raising BrainHttpError(0, "config", ...) before any
request is attempted. The same three rules, with the same messages, apply in
the TypeScript client's constructor (which spells the field apiKey) and in
Rust's BrainHttpClient::try_new — so a key is accepted or refused the same
way in every SDK. Rust's plain new is the one exception: it returns Self
and so cannot report a problem at all.
| Check | Message |
|---|---|
api_key is non-empty | api_key is required |
api_key carries no ASCII control character | api_key must not contain control characters |
base_url has an http or https scheme | base_url must be http:// or https://, got … |
The control-character rule covers every C0 character and DEL, except
HTAB — RFC 7230 permits a tab in a field value and all three transports send
it. The range stops below 0x80, so a key carrying a non-ASCII letter still
works. The ordinary way one gets in is a key read from an env file with a
trailing newline; left unchecked, urllib sent a NUL straight into the
Authorization header, where a proxy may truncate the value. The message
names no part of the key, because it is a credential.
The scheme check is a real SSRF guard here, not just a typo check: urlopen
honours every scheme urllib supports, so a file:// base URL would turn an
SDK call into a local file read.
Conventions
Flat keyword arguments
Unlike the TypeScript and Rust clients, which take input objects, the Python HTTP client takes keyword arguments and builds the body itself:
brain.encode(text="...", session=42, occurred_at=1_741_000_000_000_000_000)
brain.recall(query="...", max_results=5, scope="space")Arguments left at None are dropped from the body rather than sent as JSON
null, so an omitted option means "let the edge decide" instead of
"explicitly nothing".
Naming
You write Python's snake_case, which is what the edge speaks, so there is no
conversion layer to misread. Where a keyword differs from the wire field name
— occurred_at for occurred_at_unix_nanos — the operation's page says so.
64-bit numbers
Python's int is arbitrary precision, so session ids, LSNs and nanosecond
timestamps are plain int with nothing to lose. The TypeScript SDK needs
bigint for the same values; Python does not.
What the HTTP tier cannot do
Transactionswire onlyNo txn_begin. Each HTTP verb is its own unit. See
txnBegin.
act_aswire onlyThe space comes from the credential. Addressing another space per request needs the wire client — see act_as.
Frame-level streamingwire onlyNo *_frames variants. The HTTP tier concatenates; only
subscribe streams, over SSE.
ping and cancel_streamwire onlyThere is no connection to keep alive and no stream id to cancel.
Was this page helpful?