Python SDK: the client, errors, stream and helpers

What doorman_inbox has besides the generated methods: the client and its arguments, the exceptions, the event stream, webhook verification and the OAuth helpers.

On this page
  1. The client
  2. Errors
  3. The stream
  4. Verifying webhooks
  5. OAuth

The client

DoormanInbox is synchronous and is a context manager: leaving the with block closes its connections. Every argument is given by keyword.

Python

DoormanInbox(*, base_url: str, api_key: str | None = None, timeout: float = 30, transport: httpx.BaseTransport | None = None)
base_url
The API origin. It is always given: the client never assumes a deployment. Use HTTPS outside isolated local development.
api_key
An API key or an app's access token, sent as a bearer header. Leave it out for the operations that take no credential.
timeout
Seconds for one request.
transport
An httpx transport in place of the network, for a test.

Python

import os
from doorman_inbox import DoormanInbox

with DoormanInbox(
    base_url="https://api.doorman.fyi",
    api_key=os.environ["DOORMAN_API_KEY"],
) as client:
    me = client.me()
  • client.base_url is the origin as the client keeps it, and client.close() closes it outside a with block.
  • One attempt is made for each call. Nothing is retried, no redirect is followed, and no ambient cookie or proxy credential is sent.
  • A path parameter is a positional argument in snake_case. A request body is one dictionary named body, with the API's own JSON keys. Query parameters are keyword arguments in snake_case; one set to None is left out.
  • An answer is a dictionary with the API's own JSON keys. doorman_inbox.models holds a TypedDict for each schema of the contract, and the package ships py.typed.

Errors

Every error is importable from doorman_inbox. A failure of the network itself stays the httpx exception it was.

ExceptionRaised whenAttributes
ApiErrorThe API answered 400 or more.operation, status, code, message, details, retry_after_seconds
WebhookVerificationErrorA delivery failed verification.reason
StreamFrameErrorThe server refused a subscription, or sent a frame that is not valid.the message
StreamClosedErrorThe stream closed and will not reconnect.code, reason
OAuthProtocolErrorAn OAuth protocol request was refused.status, code, retry_after_seconds

ApiError.code is a code of the error catalogue, and details holds the fields some codes add. No request header, credential or raw body is kept on an error. OAuthProtocolError carries the protocol's error code only, never a description the server sent back.

The stream

Python

client.stream(*, inbox_ids: Sequence[str] = (), event_types: Sequence[str] = (), last_event_id: str | None = None, reconnect: bool = True, max_reconnects: int = 5, ping_interval: float = 45, open_timeout: float = 10) -> InboxStream

An InboxStream is a context manager and an iterator of events. Entering the context opens the socket and waits for the server to acknowledge the subscription, without consuming replayed events.

inbox_ids, event_types
What to subscribe to. Empty means every inbox and every event type the credential may see.
last_event_id
Replay what came after this event before the live feed.
reconnect, max_reconnects
Whether an unexpected close is followed by a new connection, and how many in a row are tried.
ping_interval, open_timeout
Seconds between keepalive pings while idle, and seconds to wait for the connection to open.
stream.last_event_id
The last event the iterator yielded. It says the event was handed to the loop, not that the loop finished its work.
stream.closed, stream.close()
Whether the stream has ended, and the call that ends it.

The credential goes in a bearer header and never in the address. A close for the credential or for the account's connection limit is final and raises StreamClosedError. See The WebSocket stream.

Verifying webhooks

Python

verify_webhook(headers: Mapping[str, str | Sequence[str]], body: bytes | str, secret: str, *, tolerance_seconds: float = 300, now: float | None = None) -> WebhookEvent

Checks the signature over the body exactly as it was received, refuses a timestamp further than tolerance_seconds from now, then reads the JSON and checks it against the schema of its event type. now is seconds since the epoch, for a test.

Python

verify_webhook_signature(headers: Mapping[str, str | Sequence[str]], body: bytes | str, secret: str, *, tolerance_seconds: float = 300, now: float | None = None) -> str

The same check without reading the payload: it returns the body as text, for a receiver that parses it itself. Both raise WebhookVerificationError. See Verifying a delivery.

OAuth

Python

create_pkce() -> Pkce

A fresh verifier, its S256 challenge and a state, as pkce.verifier, pkce.challenge, pkce.state and pkce.method. Keep the verifier and the state in the session of the person who started the flow until the callback, and never log them.

Python

parse_oauth_callback(value: str, *, redirect_uri: str, state: str, issuer: str) -> str

Checks the callback address against the redirect address, the state and the issuer, and returns the code. It raises OAuthProtocolError for a denial and for a callback that does not match.

The protocol's own requests are methods of client.oauth, listed under OAuth. They leave out the client's credential and cookies. See Apps and OAuth.

Up Down to move, Enter to open, Esc to close