Request/Reply¶
Request/reply is where a requester sends a message and waits for a response, with the response sent back to the
requester. In AsyncFast the reply is the message returned by the channel handler, so the pattern is declared by
annotating the return type of the handler with Reply.
Basic Usage¶
Annotating the return type with Reply sends the returned message, and adds the AsyncAPI Operation Reply
object to the receive operation, where the reply channel is the channel of the returned message:
from dataclasses import dataclass
from typing import Annotated
from asyncfast import AsyncFast
from asyncfast import Message
from asyncfast import Reply
app = AsyncFast()
@dataclass
class Ping:
value: str
@dataclass
class Pong(Message, address="pong"):
value: str
@app.channel("ping")
async def ping(payload: Ping) -> Annotated[Pong, Reply()]:
return Pong(value=payload.value)
{
"asyncapi": "3.0.0",
"info": {
"title": "AsyncFast",
"version": "0.1.0"
},
"channels": {
"Ping": {
"address": "ping",
"messages": {
"PingMessage": {
"$ref": "#/components/messages/PingMessage"
}
}
},
"Pong": {
"address": "pong",
"messages": {
"Pong": {
"$ref": "#/components/messages/Pong"
}
}
}
},
"operations": {
"receivePing": {
"action": "receive",
"channel": {
"$ref": "#/channels/Ping"
},
"reply": {
"channel": {
"$ref": "#/channels/Pong"
},
"messages": [
{
"$ref": "#/channels/Pong/messages/Pong"
}
]
}
},
"sendPong": {
"action": "send",
"channel": {
"$ref": "#/channels/Pong"
}
}
},
"components": {
"messages": {
"PingMessage": {
"payload": {
"$ref": "#/components/schemas/Ping"
}
},
"Pong": {
"payload": {
"type": "string"
}
}
},
"schemas": {
"Ping": {
"properties": {
"value": {
"title": "Value",
"type": "string"
}
},
"required": [
"value"
],
"title": "Ping",
"type": "object"
}
}
}
}
The return type must be a single Message type, otherwise an InvalidChannelDefinitionError is
raised when the channel is registered, that is, when the @app.channel(...) decorator is applied. Returning
None sends no reply. Reply can’t be used on a generator handler, since a generator yields messages
rather than returning one.
Dynamic Reply Address¶
When the address of the reply isn’t known at design time, it is determined at runtime from the request itself. This
is typically done by the requester including a replyTo header. Passing that Header as the address of
the Reply sends the returned message to the address in the header, so the handler doesn’t need the header
in its signature:
from dataclasses import dataclass
from typing import Annotated
from asyncfast import AsyncFast
from asyncfast import Header
from asyncfast import Message
from asyncfast import Reply
app = AsyncFast()
@dataclass
class Ping:
value: str
@dataclass
class Pong(Message, address=None):
value: str
@app.channel("ping")
async def ping(
payload: Ping,
) -> Annotated[Pong, Reply(address=Header(alias="replyTo"))]:
return Pong(value=payload.value)
{
"asyncapi": "3.0.0",
"info": {
"title": "AsyncFast",
"version": "0.1.0"
},
"channels": {
"Ping": {
"address": "ping",
"messages": {
"PingMessage": {
"$ref": "#/components/messages/PingMessage"
}
}
},
"Pong": {
"address": null,
"messages": {
"Pong": {
"$ref": "#/components/messages/Pong"
}
}
}
},
"operations": {
"receivePing": {
"action": "receive",
"channel": {
"$ref": "#/channels/Ping"
},
"reply": {
"address": {
"location": "$message.header#/replyTo"
},
"channel": {
"$ref": "#/channels/Pong"
},
"messages": [
{
"$ref": "#/channels/Pong/messages/Pong"
}
]
}
},
"sendPong": {
"action": "send",
"channel": {
"$ref": "#/channels/Pong"
}
}
},
"components": {
"messages": {
"PingMessage": {
"headers": {
"$ref": "#/components/schemas/PingMessageHeaders"
},
"payload": {
"$ref": "#/components/schemas/Ping"
}
},
"Pong": {
"payload": {
"type": "string"
}
}
},
"schemas": {
"Ping": {
"properties": {
"value": {
"title": "Value",
"type": "string"
}
},
"required": [
"value"
],
"title": "Ping",
"type": "object"
},
"PingMessageHeaders": {
"properties": {
"replyTo": {
"title": "Replyto",
"type": "string"
}
},
"required": [
"replyTo"
],
"title": "PingMessageHeaders",
"type": "object"
}
}
}
}
The header must be given an alias, which is the name of the header in the request. The header is added to the
request message, and the AsyncAPI reply address is the runtime expression $message.header#/replyTo. A
description given to the Header becomes the description of the reply address.
The reply message must be declared with address=None, since its address only exists at runtime. AsyncAPI 3.0
requires that, when a reply address is specified, the address of the channel referenced by the reply is null,
which is how the reply channel is rendered.
Same Reply Address¶
The request and the reply can also share an address, which is a reply channel using the same address as the request channel:
@dataclass
class Pong(Message, address="ping"):
value: str
@app.channel("ping")
async def ping(payload: Ping) -> Annotated[Pong, Reply()]:
return Pong(value=payload.value)
Note
AsyncFast generates a channel per message sent, so the reply is a new channel which happens to reuse the request address, rather than the request channel itself.