bijection.client is the Python client: the same verbs, frames and results as
the TypeScript client, with Python’s standard types.
It needs Python 3.12 or newer.
Install
Generate a client
bijection client python writes a distribution of the app’s exposed tree,
built on bijection.client. It needs a committed bijection.lock whose
contract line matches the source (run bijection check --write-lock). The
package holds:
app, the exposed tree:app.orders.placeis the ref ofplacein theordersmodule ofapp({ orders }). A segment is written in snake case (listRecentislist_recent);- a
TypedDictper argument and value object inacme_orders.types, named by tree path (OrdersPlaceArgs,OrdersPlaceValue); connect, which pins the app contract;- each function’s types as validator JSON, which the runtime encodes and decodes by.
Connect
token is a fixed bearer token. For a token that changes, pass
token_source, any object with token(refresh=False) such as a
bijection.credentials.ServiceCredential. With neither, the client calls as
anonymous.
The generated connect pins the contract the package was written against.
When the installation runs another contract, every call answers
Refusal(code="Conflict"), its message starting ContractStale:. Generate
the package again from the deployed app’s lock. The verbs take only the
generated package’s refs: there is no call by tree path string.
connect_async answers an AsyncClient with the same verbs as coroutines.
Close a client with client.close(), or use it as a context manager.
For a dynamic tool, the raw connector uses contract="discover". A pinned client refuses a path absent from its contract map before sending it.
Calls
client.query(fn, args) runs a query once. client.mutate(fn, args) runs a
mutation as one transaction. Leave out args for a function that declares
none.
A mutation has one identity, once. The same once with the same arguments
answers the original result and runs nothing again; the same once with other
arguments refuses Invalid. Without once the client mints one for the call.
Give your own when the identity belongs to something outside the process,
such as a job you may run again after a restart.
Unknown means the mutation may have committed, for example when the reply
was lost. Call again with the same once:
An uncertain mutation carries its original request key in Refusal.once.
Frames
A frame is who acts, under which label, in which world and at which valid instant. Each method answers a new client with that field set:as_ and in_ carry a trailing underscore because as and in are Python
keywords. a.and_(b) is the conjunction of two labels and
Label.any_of(a, b) the disjunction. World.version(v) reads at a version.
A committed mutation’s result carries its commit version. Pass it as seen
to read your own write:
seen answers from state at least that new. query, watch, mutate and
trial all take it.
Results
Every verb answers a result and never raises for how the call ended. A result isOk(value, version) or Refusal(code, message, guard); both have .ok.
code is one of the twelve host codes in bijection.client.HOST_CODES
(Unknown, Disallowed, Forbidden, Invalid, Absent, Exists,
Conflict, Erased, Expired, Withdrawn, Limit, Failed) or one of the
codes the function declares in refuses. A generated ref knows its function’s
codes: a declared code arrives as written, and any other kernel code is folded
into its host code with the kernel code as reason. A host code’s reason is
its precise cause (BudgetExhausted under Limit), or None, and message
never starts with it. On
Forbidden, guard is the tree path of the guard that refused. message is
the sentence the caller may read.
Watch
client.watch(fn, args) runs a query and keeps its result current. Iterate it;
it yields a new result each time the answer changes:
with block, or calling live.stop(), ends the watch. One live
connection serves every watch of a frame, and reconnects on its own. An
AsyncClient watch is iterated with async for:
in_, at) answers once and ends:
only the head moves.
Trial
client.trial(fn, args) runs a call in a discarded world and commits nothing:
result is how the call would have ended, with the refusing guard in
result.guard. writes are the { verb, table, key, before, after } it would
have written and tasks the { target, args, due } it would have submitted.
valid_until is the instant the answer is valid until (a call at the same version
committed before it is decided the same way), or None when nothing
time-dependent bounds it.
Values
A naive
datetime is refused: give it a time zone. A duration in years or
months has no fixed length and does not decode as a timedelta. An int
outside the int64 range is refused.
Change entries
A query that answersselection.changes answers change entries.
bijection.client.Change holds one: key, before (None on an insert),
after (None on a delete) and the change’s version.