tempest_fastapi_sdk.integrations.messaging.zap.router¶
O router opcional do FastAPI que recebe o webhook do zap-api.
ZapClientDependency
module-attribute
¶
A FastAPI dependency returning the :class:ZapClient (sync or async).
ZapInboundHandler
module-attribute
¶
ZapInboundHandler = Callable[[ZapInboundMessage], Awaitable[None]]
An async callable receiving one verified message.received body.
Receives the :class:ZapInboundMessage the dependency already validated,
and nothing else — close over the service, queue or session factory it
needs. Raise to have the gateway re-deliver; return to accept.
ZapInboxReplyHandler
module-attribute
¶
ZapInboxReplyHandler = Callable[
[ZapInboundMessage, ZapInbox[InboundT, DeliveryT]], Awaitable[None]
]
An async callback executed in BackgroundTasks with an independent inbox.
Receives the claimed :class:ZapInboundMessage and a fresh :class:ZapInbox
bound to its own database session opened after the HTTP request finishes.
ZapShouldReplyPredicate
module-attribute
¶
ZapShouldReplyPredicate = Callable[[ZapInboundOutcomeEnum, ZapInboundMessage], bool]
A predicate deciding whether a claimed inbound message triggers on_reply.
ZapStatusHandler
module-attribute
¶
ZapStatusHandler = Callable[[ZapStatusCallback], Awaitable[None]]
An async callable receiving one verified message.${status} body.
Receives the :class:ZapStatusCallback the dependency already validated,
which covers message.sent, message.delivered, message.read and
message.failed — check
:attr:ZapStatusCallback.status to tell them apart, and
:func:~tempest_fastapi_sdk.integrations.messaging.zap.is_forward_transition
before storing one, since notifications arrive out of order.
ZapWebhookAckSchema
¶
Bases: BaseSchema
The acknowledgement the webhook route answers with.
The gateway only reads the status code, so the body exists for the
consumer reading a log or a TestClient transcript: event is the
event field exactly as delivered, kept even when this SDK does not
model it.
Attributes:
| Name | Type | Description |
|---|---|---|
ok |
bool
|
Always |
event |
str
|
The |
make_zap_inbox_router
¶
make_zap_inbox_router(
*,
session_factory: Callable[..., AsyncIterator[AsyncSession]],
message_model: type[InboundT],
delivery_model: type[DeliveryT],
client_dependency: ZapClientDependency,
secret: str | bytes | None = None,
verify: Callable[..., Coroutine[Any, Any, ZapWebhookDelivery]] | None = None,
on_reply: ZapInboxReplyHandler[InboundT, DeliveryT] | None = None,
should_reply: ZapShouldReplyPredicate = is_replyable,
accepted_servers: frozenset[ZapJidServerEnum] = DEFAULT_ACCEPTED_SERVERS,
path: str = "/webhooks/zap",
tags: list[str] | None = None,
include_in_schema: bool = False,
docs_locale: str | None = None,
) -> APIRouter
Build a zap-api webhook router wired directly to :class:ZapInbox.
Mounts POST {path}, verifies the HMAC signature, and handles every
delivery through :class:ZapInbox:
message.receivedruns :meth:ZapInbox.claimon the request's database session (declared withscope="function"so the session closes as soon as the endpoint returns, releasing its pool connection before the response is sent and before background work starts). Whenon_replyis provided andshould_reply(outcome, message)returnsTrue, scheduleson_reply(message, bg_inbox)on FastAPI's :class:~fastapi.BackgroundTaskswith a fresh, independent database session and :class:ZapInbox.message.{sent,delivered,read,failed}runs :meth:ZapInbox.trackon the request's session, advancing delivery state monotonically.- Unmodeled or unrecognized events answer
200withstatus="ignored"and log the payload keys atdebuglevel.
Every verified delivery answers 200 with
:class:ZapWebhookResponseSchema carrying status and event;
exceptions raised inside on_reply are logged with message_id and
never bubble out of the background task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_factory
|
Callable[..., AsyncIterator[AsyncSession]]
|
Yields a
database session per invocation (for example
|
required |
message_model
|
type[InboundT]
|
Consumer ORM model mixing in
:class: |
required |
delivery_model
|
type[DeliveryT]
|
Consumer ORM model mixing in
:class: |
required |
client_dependency
|
ZapClientDependency
|
FastAPI dependency returning
the :class: |
required |
secret
|
str | bytes | None
|
Webhook secret registered on the gateway.
Builds the signature dependency with
:func: |
None
|
verify
|
Callable[..., Coroutine[Any, Any, ZapWebhookDelivery]] | None
|
Custom verification dependency used instead of |
None
|
on_reply
|
ZapInboxReplyHandler[InboundT, DeliveryT] | None
|
Async
callback invoked in |
None
|
should_reply
|
ZapShouldReplyPredicate
|
Predicate receiving
|
is_replyable
|
accepted_servers
|
frozenset[ZapJidServerEnum]
|
JID servers accepted by
:meth: |
DEFAULT_ACCEPTED_SERVERS
|
path
|
str
|
Route path to mount. Defaults to |
'/webhooks/zap'
|
tags
|
list[str] | None
|
OpenAPI tags. Defaults to |
None
|
include_in_schema
|
bool
|
Whether the route appears in OpenAPI.
Defaults to |
False
|
docs_locale
|
str | None
|
Language of OpenAPI |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
APIRouter |
APIRouter
|
Ready to mount with |
Raises:
| Type | Description |
|---|---|
ValueError
|
If both |
TypeError
|
If |
make_zap_webhook_router
¶
make_zap_webhook_router(
*,
secret: str | bytes | None = None,
verify: Callable[..., Coroutine[Any, Any, ZapWebhookDelivery]] | None = None,
on_inbound: ZapInboundHandler | None = None,
on_status: ZapStatusHandler | None = None,
path: str = "/webhooks/zap",
tags: list[str] | None = None,
include_in_schema: bool = True,
docs_locale: str | None = None,
) -> APIRouter
Build a router with the one POST the zap-api webhook needs.
One route, POST {path}, dispatching by event: on_inbound for a
message.received delivery and on_status for the four status
events. It answers 200 with :class:ZapWebhookAckSchema once the
handler returns, and 401 when the signature does not verify.
Both events reach the same URL, so pass --events status,inbound when
registering the webhook on the gateway, and one or both handlers
depending on which half the service subscribes to.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
secret
|
str | bytes | None
|
The webhook secret, as registered on
the gateway. Builds the signature dependency with
:func: |
None
|
verify
|
Callable[..., Coroutine[Any, Any, ZapWebhookDelivery]] | None
|
A dependency verifying the delivery instead of |
None
|
on_inbound
|
ZapInboundHandler | None
|
Called with the validated
:class: |
None
|
on_status
|
ZapStatusHandler | None
|
Called with the validated
:class: |
None
|
path
|
str
|
The path to mount. Register this same path on the
gateway ( |
'/webhooks/zap'
|
tags
|
list[str] | None
|
OpenAPI tags. Defaults to |
None
|
include_in_schema
|
bool
|
Whether the route appears in the OpenAPI document. Turn it off to keep the webhook out of a public schema. |
True
|
docs_locale
|
str | None
|
Language of the OpenAPI |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
APIRouter |
APIRouter
|
Ready to mount with |
Raises:
| Type | Description |
|---|---|
ValueError
|
If both |
TypeError
|
If |
A delivery the signature accepted but no handler claimed answers
200; a handler that raises propagates, so the response is 500
and the gateway re-delivers. See the module docstring for the gateway
code both come from.