FastAPI#
CapoSqsRouter and AiobotocoreSqsRouter from faststream_sqs_sns.fastapi are FastAPI routers
that own a broker. Each takes the options of its broker class and of fastapi.APIRouter.
app.py
from fastapi import FastAPI
from faststream_sqs_sns import SqsQueue
from faststream_sqs_sns.fastapi import CapoSqsRouter
router = CapoSqsRouter(region="eu-central-1")
@router.subscriber(SqsQueue("orders-billing", declare=True))
async def bill(order: dict[str, int]) -> None: ...
@router.post("/orders")
async def place_order(order: dict[str, int]) -> str:
return await router.broker.publish(order, "orders-billing")
app = FastAPI()
app.include_router(router)
Subscribers start with the FastAPI application and stop with it. router.broker is the broker, so
router.broker.publish(...) publishes from a route.
AiobotocoreSqsRouter is the same router over AiobotocoreSqsBroker:
app.py
from fastapi import FastAPI
from faststream_sqs_sns.fastapi import AiobotocoreSqsRouter
router = AiobotocoreSqsRouter(region="eu-central-1")
@router.subscriber("orders-billing")
async def bill(order: dict[str, int]) -> None: ...
app = FastAPI()
app.include_router(router)
The broker's dependencies are FastAPI dependencies here, and its tags are
specification_tags.
Testing#
Test the router with the broker it owns:
test_app.py
async with TestSqsBroker(router.broker) as br:
await br.publish({"id": 1}, "orders-billing")
bill.mock.assert_called_once_with({"id": 1})
The module also exports SqsMessage, SqsBroker, Context, ContextRepo and Logger as
FastAPI-aware annotations for handler arguments.