Repository navigation
feat(asyncfast): add request/reply asyncapi support #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TomKimber
wants to merge
1
commit into
main
Choose a base branch
from
feat/add-request-reply-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| from collections.abc import AsyncGenerator | ||
| from dataclasses import dataclass | ||
|
|
||
| from asyncfast import AsyncFast | ||
| from asyncfast import Message | ||
| from asyncfast import Reply | ||
|
|
||
| app = AsyncFast() | ||
|
|
||
|
|
||
| @dataclass | ||
| class Pong(Message, address="pong"): | ||
| payload: str | ||
|
|
||
|
|
||
| @app.channel("ping", reply=Reply()) | ||
| async def ping(payload: str) -> AsyncGenerator[Pong, None]: | ||
| yield Pong(payload="pong") |
24 changes: 24 additions & 0 deletions
24
packages/asyncfast/docs/examples/request_reply_dynamic_address.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| from collections.abc import AsyncGenerator | ||
| from dataclasses import dataclass | ||
| from typing import Annotated | ||
|
|
||
| from asyncfast import AsyncFast | ||
| from asyncfast import Message | ||
| from asyncfast import Reply | ||
| from asyncfast import ReplyTo | ||
|
|
||
| app = AsyncFast() | ||
|
|
||
|
|
||
| @dataclass | ||
| class Pong(Message, address="{reply_to}"): | ||
| reply_to: str | ||
| payload: str | ||
|
|
||
|
|
||
| @app.channel("ping", reply=Reply()) | ||
| async def ping( | ||
| payload: str, | ||
| reply_to: Annotated[str, ReplyTo(alias="replyTo")], | ||
| ) -> AsyncGenerator[Pong, None]: | ||
| yield Pong(reply_to=reply_to, payload="pong") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,6 +83,7 @@ Taking ideas from: | |
|
|
||
| receiving | ||
| sending | ||
| request_reply | ||
| message_send_router | ||
| dependencies | ||
| lifespan | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| ############### | ||
| 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 of the interaction is the message the channel handler sends, so the pattern is | ||
| declared by passing :py:class:`Reply` to the channel. | ||
|
|
||
| ************* | ||
| Basic Usage | ||
| ************* | ||
|
|
||
| Marking a channel with :py:class:`Reply` adds the AsyncAPI Operation Reply object to the receive operation, where the | ||
| reply channel is the channel of the message the handler sends: | ||
|
|
||
| .. async-fast-example:: examples/request_reply.py | ||
|
|
||
| The handler must send exactly one message type, otherwise an | ||
| :py:class:`InvalidChannelDefinitionError` is raised when the channel is registered, that is, when the ``@app.channel(...)`` decorator is applied. | ||
|
|
||
| :py:class:`Reply` works with either sending style, so the :py:class:`MessageSender` can be used instead of yielding: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| @app.channel("ping", reply=Reply()) | ||
| async def ping(message_sender: MessageSender[Pong]) -> None: | ||
| await message_sender.send(Pong(payload="pong")) | ||
|
|
||
| *********************** | ||
| 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. Marking that header with :py:class:`ReplyTo` | ||
| tells AsyncFast it carries the reply address, so the AsyncAPI reply address is derived from it and the header only | ||
| needs to be written once: | ||
|
|
||
| .. async-fast-example:: examples/request_reply_dynamic_address.py | ||
|
|
||
| :py:class:`ReplyTo` behaves like :py:class:`Header`, so the value is injected into the handler, and it can equally be | ||
| used in a dependency of the channel. A channel can have at most one :py:class:`ReplyTo` header. | ||
|
|
||
| The derived location is an AsyncAPI runtime expression, ``$message.header#/replyTo`` in the example above. To describe | ||
| the address, or to point at a location that isn't a header marked with :py:class:`ReplyTo`, pass a | ||
| :py:class:`ReplyAddress` to :py:class:`Reply`. The location must be either ``$message.header`` or | ||
| ``$message.payload`` followed by a JSON Pointer. As a shortcut, a location can also be passed as a string: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| @app.channel("ping", reply=Reply(address="$message.payload#/replyTo")) | ||
| async def ping(payload: PingPayload) -> AsyncGenerator[Pong, None]: | ||
| yield Pong(payload="pong") | ||
|
|
||
| The reply address is documentation only, so the handler is still responsible for sending to the address the requester | ||
| supplied. Binding the :py:class:`ReplyTo` header to a parameter of the reply channel address, as in the example above, | ||
| is the most direct way of doing this. | ||
|
|
||
| .. note:: | ||
|
|
||
| AsyncAPI 3.0 requires that, when a reply address is specified, the address of the channel referenced by the reply | ||
| is ``null``. When :py:class:`Reply` is given an address, the reply channel is therefore rendered with a ``null`` | ||
| address, with the reply address runtime expression carrying the routing information. The reply message should not | ||
| be reused as the send message of a standalone channel, since its channel address is rendered as ``null``. | ||
|
|
||
| ******************** | ||
| 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: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| @dataclass | ||
| class Pong(Message, address="ping"): | ||
| payload: str | ||
|
|
||
|
|
||
| @app.channel("ping", reply=Reply()) | ||
| async def ping(payload: str) -> AsyncGenerator[Pong, None]: | ||
| yield Pong(payload="pong") | ||
|
|
||
| .. 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.