HTTP client
BrainHttpClient — construction, the builder configuration methods, borrowed inputs, and what the HTTP tier cannot do.
Construction
fn new(api_key: impl Into<String>, base_url: impl Into<String>) -> Self
fn try_new(api_key: impl Into<String>, base_url: impl Into<String>)
-> Result<Self, BrainHttpError>
fn localhost(api_key: impl Into<String>) -> Self
fn with_timeout(self, timeout: Duration) -> Self
fn with_retry_policy(self, policy: HttpRetryPolicy) -> Selfnew(api_key, base_url)BrainHttpClientrequiredSent as Authorization: Bearer <api_key>. Validates nothing — it returns
Self and has nowhere to report a problem.
try_new(api_key, base_url)Result<BrainHttpClient, BrainHttpError>optionalThe checked form. Refuses a credential or base URL that cannot work, with
status: 0 and code: "config", instead of failing later as an opaque
401 or a request to nowhere.
localhost(api_key)BrainHttpClientoptionalShorthand for the self-host address, http://127.0.0.1:8080.
with_timeout(timeout)BrainHttpClientoptionalPer-request timeout, as a Duration. A timeout surfaces as a
BrainHttpError with status == 0.
with_retry_policy(policy)BrainHttpClientoptionalReplaces the default policy. See Retries.
use std::time::Duration;
use brain_db_sdk::http::BrainHttpClient;
let brain = BrainHttpClient::new(
std::env::var("BRAIN_API_KEY")?,
std::env::var("BRAIN_BASE_URL").unwrap_or_else(|_| "http://127.0.0.1:8080".into()),
)
.with_timeout(Duration::from_secs(15));The configuration methods consume and return Self, so they chain at
construction. There is no close — the client holds no connection.
What try_new checks
Every failure is a BrainHttpError with status: 0 and code: "config",
returned before any request is attempted.
| 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 reqwest sends it, so
rejecting it would make this constructor stricter than the request path it
guards. 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; the message names no part of the key, because it is a
credential.
The scheme check is not the SSRF guard it is in Python — reqwest cannot
speak anything but HTTP — so here it earns its place by catching the argument
swap below. base_url is trimmed before it is checked and before it is
stored, so a stray space or newline from an env var neither fails validation
nor resurfaces at request time.
All three checks are identical in the Python and TypeScript clients, which validate from their single constructor, so the same key is accepted or refused the same way in all three SDKs.
Both parameters are impl Into<String>, so new("https://edge", key)
type-checks and silently sends your base URL as the bearer token. try_new
is what catches it: an API key in the URL position has no scheme, so the
scheme check fails loudly.
let brain = BrainHttpClient::try_new(
std::env::var("BRAIN_API_KEY")?,
std::env::var("BRAIN_BASE_URL")?,
)?;Conventions
Borrowed inputs
Every verb takes &Input and returns Result<Output, BrainHttpError>:
let res = brain.encode(&EncodeInput { text: "...".into(), ..Default::default() }).await?;Because the input is borrowed rather than consumed, retrying is the same call with the same value — nothing to clone and nothing to rebuild.
Most input structs implement Default, so ..Default::default() is the
idiomatic way to set only the fields you care about.
Async runtime
The verbs are async fn. The crate does not start a runtime for you; drive
them from whatever executor your application already runs.
Error type
One error type, BrainHttpError, carrying status, a stable string code
and field_errors. status == 0 means the request never reached the edge.
See Errors.
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. See act_as.
Frame-level streamingwire onlyNo *_frames variants. 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?