Routers#
A router groups subscribers and publishers in a module and is included into the broker.
service.py
from faststream import FastStream
from faststream_sqs_sns import (
CapoSqsBroker,
SqsPublisher,
SqsRoute,
SqsRouter,
)
broker = CapoSqsBroker(region="eu-central-1")
app = FastStream(broker)
router = SqsRouter(prefix="billing-")
@router.subscriber("orders")
@router.publisher("receipts")
async def bill(order: dict[str, int]) -> dict[str, int]:
return {"order_id": order["id"]}
async def notify(receipt: dict[str, int]) -> None: ...
# routes declare handlers without a decorator
routes = SqsRouter(
prefix="shop-",
handlers=(
SqsRoute(
notify,
"receipts",
publishers=(SqsPublisher("notifications"),),
),
),
)
broker.include_router(router)
broker.include_router(routes)
SqsRouter(prefix="billing-") prepends the prefix to the queue and topic names it declares: the
handler above reads billing-orders and publishes to billing-receipts. A URL or an ARN keeps
its value.
SqsRoute and SqsPublisher#
SqsRoute registers a handler without a decorator: it takes the function, the queue and the
same options as @broker.subscriber. SqsPublisher is a publisher registration that a route
passes in publishers=. Routes go to the router's handlers=; routers nest with routers=.
Routers work with either broker class.
Options of the router#
SqsRouter also accepts dependencies, middlewares, parser, decoder, ack_policy and
include_in_schema. They apply to everything the router declares.