lexrpc package
Reference documentation.
client
XRPC client implementation.
TODO:
asyncio support for subscription websockets
- class Client(address=None, access_token=None, refresh_token=None, session_callback=None, lexicons=None, validate=True, truncate=False, **requests_kwargs)[source]
Bases:
BaseXRPC client.
Calling
com.atproto.server.createSessionwill store the returned session and include its acccess token in subsequent requests. If a request fails withExpiredTokenand we have a session stored, the access token will be refreshed withcom.atproto.server.refreshSessionand then the original request will be retried.- requests_kwargs
passed to
requests.get()/requests.post()- Type:
- call(nsid, input=None, headers={}, decode=True, **params)[source]
Makes a remote XRPC method call.
- Parameters:
nsid (str) – method NSID
input (dict or bytes) – input body, optional for subscriptions
headers (dict) – HTTP headers to include in this request. Overrides any headers passed to the constructor.
decode (bool) – if this is a subscription, decode header and payload before returning, otherwise return raw bytes
params – optional method parameters
- Returns:
for queries and procedures with JSON output, decoded JSON object or None if the method has no output. For non-JSON output, the full requests.Response object. For subscriptions, generator of messages from server, as (dict header, dict payload) tuple if
decodeis True, bytes otherwise.- Return type:
- Raises:
NotImplementedError – if the given NSID is not found in any of the loaded lexicons
ValidationError – if the parameters, input, or returned output don’t validate against the method’s schemas
RequestException – if the connection or HTTP request to the remote server failed
server
XRPC server implementation.
- exception Redirect(to, status=302, headers=None)[source]
Bases:
ExceptionRaised by XRPC handlers to direct the server to serve an HTTP redirect.
Whether this is official supported by the XRPC spec is still TBD: https://github.com/bluesky-social/atproto/discussions/1228
- class Server(**kwargs)[source]
Bases:
BaseXRPC server base class. Subclass to implement specific methods.
- method(nsid)[source]
XRPC method decorator. Use on each function that implements a method.
- Parameters:
nsid (str)
- register(nsid, fn)[source]
Registers an XRPC method decorator. Alternative to
method().- Parameters:
nsid (str)
fn (callable)
- call(nsid, input=None, **params)[source]
Calls an XRPC query or procedure method.
For subscriptions, returns a generator that yields
(header dict, payload dict)tuples to be DAG-CBOR encoded and sent to the websocket client.- Parameters:
- Returns:
output
- Return type:
- Raises:
NotImplementedError – if the given NSID is not implemented or found in any of the loaded lexicons
ValidationError – if the parameters, input, or returned output don’t validate against the method’s schemas
flask_server
Flask handler for /xrpc/... endpoints.
- init_flask(xrpc_server, app, limit_ips=False)[source]
Connects a
lexrpc.Serverto serve/xrpc/...on a Flask app.
- class XrpcEndpoint(server)[source]
Bases:
ViewHandles inbound XRPC query and procedure (but not subscription) methods.
- server
- Type:
lexrpc.Server
- subscription(xrpc_server, nsid, limit_ips=False)[source]
Generates websocket handlers for inbound XRPC subscription methods.
Note that this calls the XRPC method on a different thread, so that it can block on it there while still periodically checking in the request thread that the websocket client is still connected.
base
Base code shared by both server and client.
- exception XrpcError(message, name=None, **kwargs)[source]
Bases:
ValueErrorA named error in an XRPC call.
nameis the error, egRepoNotFoundincom.atproto.sync.getRepo.messageis the human-readable string error message.
- exception ValidationError[source]
Bases:
ValueErrorRaised when an object or XRPC input or output doesn’t match its schema.
- class Base(lexicons=None, validate=True, truncate=False)[source]
Bases:
objectBase class for both XRPC client and server.
- validate(nsid, type, obj)[source]
If configured to do so, validates a ATProto value against its lexicon.
Returns
Noneif the object validates, otherwise raises an exception.Does nothing if this object was initialized with
validate=False.- Parameters:
- Returns:
- obj, either unchanged, or possible a modified copy if
truncateis enabled and a string value was too long
- Return type:
- Raises:
NotImplementedError – if no lexicon exists for the given NSID, or the lexicon does not define a schema for the given type
ValidationError – if the object is invalid
- static validate_mime_type(mime_type, accept, name='')[source]
Validates that a MIME type matches an accept range.
For validating blob types. Returns
Noneif theacceptis empty ormime_typematches, otherwise raises an exception.https://atproto.com/specs/lexicon#field-type-definitions
- Parameters:
- Raises:
ValidationError – if
mime_typedoesn’t match any pattern inaccept
- encode_params(params)[source]
Encodes decoded parameter values.
Based on https://atproto.com/specs/xrpc#lexicon-http-endpoints
- decode_params(method_nsid, params)[source]
Decodes encoded parameter values.
Based on https://atproto.com/specs/xrpc#lexicon-http-endpoints
- Parameters:
- Returns:
maps str names to decoded boolean, number, str, and array values
- Return type:
- Raises:
ValueError – if a parameter value can’t be decoded
NotImplementedError – if no method lexicon is registered for the given NSID