-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add nats support #58
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
jackburridge
wants to merge
11
commits into
main
Choose a base branch
from
feat/amgi-nats-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
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8f09ff4
feat: add nats support
jackburridge 3a175b8
feat(amgi-nats-py): add nats entrypoint and example
jackburridge 04f2342
feat(amgi-common): run servers using a new event loop, this adds mult…
jackburridge 0d1b1e6
test(test-utils): add helper to test for termination
jackburridge 36e4ba0
test: add coverage settings to allow for multiprocessing
jackburridge 645fb76
test(amgi-nats-py): add tests for run functions
jackburridge b98ac8a
test(amgi-aiobotocore): add tests for sqs run functions
jackburridge f66c2fb
test(amgi-aiokafka): add tests for run functions
jackburridge ad37025
style(amgi-common): use ellipsis as an indication that the protocol r…
jackburridge 90c6fca
test(amgi-redis): add tests for run functions
jackburridge 1f722b3
fix(amgi-paho-mqtt): fix server so it does not attempt to get event l…
jackburridge 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,4 @@ | ||
| [run] | ||
| concurrency = multiprocessing | ||
| parallel = true | ||
| sigterm = true |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| services: | ||
| nats: | ||
| image: nats:2.12.2 | ||
| ports: | ||
| - "4222:4222" | ||
| - "8222:8222" | ||
| nui: | ||
| image: ghcr.io/nats-nui/nui:0.8.1 | ||
| ports: | ||
| - 31311:31311 | ||
| depends_on: | ||
| - nats |
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
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,21 @@ | ||
| MIT License | ||
|
|
||
| Copyright (c) 2025 Jack Burridge | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
Empty file.
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,40 @@ | ||
| [build-system] | ||
| build-backend = "uv_build" | ||
| requires = [ "uv-build>=0.8.14,<0.9.0" ] | ||
|
|
||
| [project] | ||
| name = "amgi-nats-py" | ||
| version = "0.25.1" | ||
| description = "Add your description here" | ||
| readme = "README.md" | ||
| license = "MIT" | ||
| license-files = [ "LICENSE" ] | ||
| authors = [ | ||
| { name = "jack.burridge", email = "jack.burridge@mail.com" }, | ||
| ] | ||
| requires-python = ">=3.10" | ||
| classifiers = [ | ||
| "Programming Language :: Python :: 3 :: Only", | ||
| "Programming Language :: Python :: 3.10", | ||
| "Programming Language :: Python :: 3.11", | ||
| "Programming Language :: Python :: 3.12", | ||
| "Programming Language :: Python :: 3.13", | ||
| "Programming Language :: Python :: 3.14", | ||
| ] | ||
| dependencies = [ | ||
| "amgi-common==0.25.1", | ||
| "amgi-types==0.25.1", | ||
| "nats-py>=2.12.0", | ||
| ] | ||
| entry-points.amgi_server.amgi-nats-py-core = "amgi_nats_py.core:_run_cli" | ||
|
|
||
| [dependency-groups] | ||
| dev = [ | ||
| "test-utils", | ||
| "testcontainers[nats]>=4.13.0", | ||
| ] | ||
|
|
||
| [tool.uv.sources] | ||
| amgi-common = { workspace = true } | ||
| amgi-types = { workspace = true } | ||
| test-utils = { workspace = true } |
Empty file.
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,87 @@ | ||
| import asyncio | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this a problem? |
||
| from abc import ABC | ||
| from abc import abstractmethod | ||
| from asyncio import Event | ||
| from functools import partial | ||
| from typing import Any | ||
| from typing import Union | ||
|
|
||
| from amgi_common import Lifespan | ||
| from amgi_types import AMGIApplication | ||
| from amgi_types import AMGISendCallable | ||
| from amgi_types import MessageReceiveEvent | ||
| from amgi_types import MessageScope | ||
| from amgi_types import MessageSendEvent | ||
| from nats.aio.client import Client | ||
| from nats.aio.msg import Msg | ||
| from nats.js import JetStreamContext | ||
|
|
||
|
|
||
| NatsClient = Union[Client, JetStreamContext] | ||
|
|
||
|
|
||
| class _Receive: | ||
| def __init__(self, msg: Msg) -> None: | ||
| self._msg = msg | ||
|
|
||
| async def __call__(self) -> MessageReceiveEvent: | ||
| return { | ||
| "type": "message.receive", | ||
| "id": self._msg.reply or "", | ||
| "headers": [ | ||
| (key.encode(), value.encode()) | ||
| for key, value in (self._msg.headers or {}).items() | ||
| ], | ||
| "payload": self._msg.data, | ||
| } | ||
|
|
||
|
|
||
| async def _message_send(client: NatsClient, event: MessageSendEvent) -> None: | ||
| await client.publish( | ||
| event["address"], | ||
| event.get("payload") or b"", | ||
| headers={key.decode(): value.decode() for key, value in event["headers"]}, | ||
| ) | ||
|
|
||
|
|
||
| class _BaseServer(ABC): | ||
| def __init__( | ||
| self, app: AMGIApplication, *subjects: str, servers: str | list[str] | ||
| ) -> None: | ||
| self._app = app | ||
| self._subjects = subjects | ||
| self._servers = servers | ||
|
|
||
| self._stop_event = Event() | ||
| self._state: dict[str, Any] = {} | ||
|
|
||
| async def _client_serve(self, client: NatsClient) -> None: | ||
| client_subscribe_cb = partial(self._subscribe_cb, client) | ||
| subscriptions = await asyncio.gather( | ||
| *( | ||
| client.subscribe(subject, cb=client_subscribe_cb) | ||
| for subject in self._subjects | ||
| ) | ||
| ) | ||
|
|
||
| async with Lifespan(self._app, self._state): | ||
| await self._stop_event.wait() | ||
|
|
||
| for subscription in subscriptions: | ||
| await subscription.unsubscribe() | ||
|
|
||
| async def _subscribe_cb(self, client: NatsClient, msg: Msg) -> None: | ||
| scope: MessageScope = { | ||
| "type": "message", | ||
| "amgi": {"version": "1.0", "spec_version": "1.0"}, | ||
| "address": msg.subject, | ||
| "state": self._state.copy(), | ||
| } | ||
| await self._app(scope, _Receive(msg), self._send(client, msg)) | ||
|
|
||
| def stop(self) -> None: | ||
| self._stop_event.set() | ||
|
|
||
| @abstractmethod | ||
| def _send(self, client: NatsClient, msg: Msg) -> AMGISendCallable: | ||
| raise NotImplementedError # pragma: no cover | ||
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,50 @@ | ||
| from amgi_common import server_serve | ||
| from amgi_nats_py._base import _BaseServer | ||
| from amgi_nats_py._base import _message_send | ||
| from amgi_nats_py._base import NatsClient | ||
| from amgi_types import AMGIApplication | ||
| from amgi_types import AMGISendCallable | ||
| from amgi_types import AMGISendEvent | ||
| from nats.aio.client import Client | ||
| from nats.aio.msg import Msg | ||
|
|
||
|
|
||
| def run(app: AMGIApplication, *subjects: str, servers: str | list[str]) -> None: | ||
| server = Server(app, *subjects, servers=servers) | ||
| server_serve(server) | ||
|
|
||
|
|
||
| def _run_cli( | ||
| app: AMGIApplication, | ||
| subjects: list[str], | ||
| servers: list[str] | None = None, | ||
| ) -> None: | ||
| if servers is None: | ||
| servers = ["nats://localhost:4222"] | ||
| run(app, *subjects, servers=servers) | ||
|
|
||
|
|
||
| class _Send: | ||
| def __init__(self, client: Client, msg: Msg) -> None: | ||
| self._client = client | ||
| self._msg = msg | ||
|
|
||
| async def __call__(self, event: AMGISendEvent) -> None: | ||
| if event["type"] == "message.send": | ||
| await _message_send(self._client, event) | ||
|
|
||
|
|
||
| class Server(_BaseServer): | ||
| def __init__( | ||
| self, app: AMGIApplication, *subjects: str, servers: str | list[str] | ||
| ) -> None: | ||
| super().__init__(app, *subjects, servers=servers) | ||
|
|
||
| async def serve(self) -> None: | ||
| client = Client() | ||
| await client.connect(self._servers) | ||
|
|
||
| await self._client_serve(client) | ||
|
|
||
| def _send(self, client: NatsClient, msg: Msg) -> AMGISendCallable: | ||
| return _Send(client, msg) |
Empty file.
Empty file.
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 Generator | ||
|
|
||
| import pytest | ||
| from nats.aio.client import Client | ||
| from testcontainers.nats import NatsContainer | ||
|
|
||
|
|
||
| @pytest.fixture(scope="package") | ||
| def nats_container() -> Generator[NatsContainer, None, None]: | ||
| with NatsContainer() as nats_container: | ||
| yield nats_container | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| async def client(nats_container: NatsContainer) -> Client: | ||
| client = Client() | ||
| await client.connect(nats_container.nats_uri()) | ||
| return client |
14 changes: 14 additions & 0 deletions
14
packages/amgi-nats-py/tests_amgi_nats/test_nats_core_message.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,14 @@ | ||
| from unittest.mock import Mock | ||
| from unittest.mock import patch | ||
|
|
||
| from amgi_nats_py import core | ||
| from amgi_nats_py.core import _run_cli | ||
|
|
||
|
|
||
| def test_run_cli_defaults() -> None: | ||
| with patch.object(core, "run") as mock_run: | ||
| mock_app = Mock() | ||
| _run_cli(mock_app, ["subject"]) | ||
| mock_run.assert_called_once_with( | ||
| mock_app, "subject", servers=["nats://localhost:4222"] | ||
| ) |
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.