✨ Updated to FastAPI to version 0.100.0 with **Pydantic v2** ✨
Changes
- Added support for reply messages without a payload. Messages without a payload only contain their message type.
- All messages are now uniquely identified by their operation name, this corresponds to the `Message ID` in the AsyncAPI-specification.
- `send` operations with a return annotation **must** now specify a `reply` in the decorator. This creates a new message in the AsyncAPI-specification. The `reply`-argument must therefore also be unique across operations.
- `recv` operations now implicitly sets the reply-operation.
- `send` operations with a `reply` defined now create a `x-response` in the AsyncAPI-documentation mapping to the reply message.
Example 1
Python
service = FastWS()
service.send("ping", reply="pong")
async def ping(client: Client, app: FastWS):
logging.info(f"{client=} | {app.connections=}")
The snippet above allows us to send and recieve WebSocket-messages with the following format:
json
// Send message
{
"type": "ping"
}
Recieve message
{
"type": "pong"
}
Example 2
Python
class Payload(BaseModel):
job_id: str
class Result(BaseModel):
name: str
service.send("work", reply="result")
async def send_work(payload: Payload) -> Result:
logging.info(f"{payload.job_id=}")
return Result(name="ok")
The snippet above allows us to send and recieve WebSocket-messages with the following format:
json
// Send message
{
"type": "work",
"payload": {
"job_id": "string"
}
}
Recieve message
{
"type": "result",
"payload": {
"name": "string"
}
}