swarmauri_transport_websocket 0.1.0.dev4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,13 @@
1
+ Copyright 2024 Jacob Stewart
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
@@ -0,0 +1,55 @@
1
+ Metadata-Version: 2.4
2
+ Name: swarmauri_transport_websocket
3
+ Version: 0.1.0.dev4
4
+ Summary: Synchronous and asynchronous WebSocket client transport for Swarmauri.
5
+ License: Copyright 2024 Jacob Stewart
6
+
7
+ Licensed under the Apache License, Version 2.0 (the "License");
8
+ you may not use this file except in compliance with the License.
9
+ You may obtain a copy of the License at
10
+
11
+ http://www.apache.org/licenses/LICENSE-2.0
12
+
13
+ Unless required by applicable law or agreed to in writing, software
14
+ distributed under the License is distributed on an "AS IS" BASIS,
15
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ See the License for the specific language governing permissions and
17
+ limitations under the License.
18
+ License-File: LICENSE
19
+ Keywords: swarmauri,websocket,realtime,bidirectional,async
20
+ Author: Jacob Stewart
21
+ Author-email: jacob@swarmauri.com
22
+ Requires-Python: >=3.10,<3.13
23
+ Classifier: Development Status :: 1 - Planning
24
+ Classifier: License :: OSI Approved :: Apache Software License
25
+ Classifier: Programming Language :: Python :: 3.10
26
+ Classifier: Programming Language :: Python :: 3.11
27
+ Classifier: Programming Language :: Python :: 3.12
28
+ Requires-Dist: swarmauri_base
29
+ Requires-Dist: swarmauri_core
30
+ Requires-Dist: websockets (>=14)
31
+ Description-Content-Type: text/markdown
32
+
33
+ ![Swarmauri](https://github.com/swarmauri/swarmauri-sdk/blob/3d4d1cfa949399d7019ae9d8f296afba773dfb7f/assets/swarmauri.brand.theme.svg)
34
+
35
+ [![PyPI version](https://img.shields.io/pypi/v/swarmauri_transport_websocket.svg)](https://pypi.org/project/swarmauri_transport_websocket/) [![Python](https://img.shields.io/pypi/pyversions/swarmauri_transport_websocket.svg)](https://pypi.org/project/swarmauri_transport_websocket/) [![License](https://img.shields.io/badge/license-Apache--2.0-blue)](LICENSE) [![Downloads](https://static.pepy.tech/badge/swarmauri_transport_websocket)](https://pepy.tech/project/swarmauri_transport_websocket) [![Hits](https://hits.sh/github.com/swarmauri/swarmauri-sdk.svg)](https://hits.sh/github.com/swarmauri/swarmauri-sdk/)
36
+
37
+ # swarmauri_transport_websocket
38
+
39
+ ## Features
40
+
41
+ - Separate synchronous and asynchronous connection paths.
42
+ - Bidirectional text and binary messages.
43
+ - Context-independent ping, receive iteration, and deterministic close.
44
+
45
+ ## Installation
46
+
47
+ ```bash
48
+ uv add swarmauri_transport_websocket
49
+ pip install swarmauri_transport_websocket
50
+ ```
51
+
52
+ ## Usage
53
+
54
+ Create a `WebSocketClient`, call `connect` or `aconnect`, and use the matching send/receive methods. This package does not claim logical-channel multiplexing.
55
+
@@ -0,0 +1,22 @@
1
+ ![Swarmauri](https://github.com/swarmauri/swarmauri-sdk/blob/3d4d1cfa949399d7019ae9d8f296afba773dfb7f/assets/swarmauri.brand.theme.svg)
2
+
3
+ [![PyPI version](https://img.shields.io/pypi/v/swarmauri_transport_websocket.svg)](https://pypi.org/project/swarmauri_transport_websocket/) [![Python](https://img.shields.io/pypi/pyversions/swarmauri_transport_websocket.svg)](https://pypi.org/project/swarmauri_transport_websocket/) [![License](https://img.shields.io/badge/license-Apache--2.0-blue)](LICENSE) [![Downloads](https://static.pepy.tech/badge/swarmauri_transport_websocket)](https://pepy.tech/project/swarmauri_transport_websocket) [![Hits](https://hits.sh/github.com/swarmauri/swarmauri-sdk.svg)](https://hits.sh/github.com/swarmauri/swarmauri-sdk/)
4
+
5
+ # swarmauri_transport_websocket
6
+
7
+ ## Features
8
+
9
+ - Separate synchronous and asynchronous connection paths.
10
+ - Bidirectional text and binary messages.
11
+ - Context-independent ping, receive iteration, and deterministic close.
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ uv add swarmauri_transport_websocket
17
+ pip install swarmauri_transport_websocket
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ Create a `WebSocketClient`, call `connect` or `aconnect`, and use the matching send/receive methods. This package does not claim logical-channel multiplexing.
@@ -0,0 +1,37 @@
1
+ [project]
2
+ name = "swarmauri_transport_websocket"
3
+ version = "0.1.0.dev4"
4
+ description = "Synchronous and asynchronous WebSocket client transport for Swarmauri."
5
+ readme = "README.md"
6
+ requires-python = ">=3.10,<3.13"
7
+ authors = [{ name = "Jacob Stewart", email = "jacob@swarmauri.com" }]
8
+ license = { file = "LICENSE" }
9
+ dependencies = ["websockets>=14", "swarmauri_core", "swarmauri_base"]
10
+ classifiers = [
11
+ "Development Status :: 1 - Planning",
12
+ "License :: OSI Approved :: Apache Software License",
13
+ "Programming Language :: Python :: 3.10",
14
+ "Programming Language :: Python :: 3.11",
15
+ "Programming Language :: Python :: 3.12",
16
+ ]
17
+ keywords = ["swarmauri", "websocket", "realtime", "bidirectional", "async"]
18
+
19
+ [tool.uv.sources]
20
+ swarmauri_core = { workspace = true }
21
+ swarmauri_base = { workspace = true }
22
+
23
+ [project.entry-points."swarmauri.transports"]
24
+ WebSocketClient = "swarmauri_transport_websocket:WebSocketClient"
25
+
26
+ [build-system]
27
+ requires = ["poetry-core>=1.8.0"]
28
+ build-backend = "poetry.core.masonry.api"
29
+
30
+ [dependency-groups]
31
+ dev = ["pytest>=8.0", "pytest-asyncio>=0.24", "ruff>=0.9.9"]
32
+
33
+ [tool.ruff]
34
+ extend = "../../pyproject.toml"
35
+
36
+ [tool.ruff.lint]
37
+ ignore = ["N999"]
@@ -0,0 +1,5 @@
1
+ from .WebSocketConnection import WebSocketConnection
2
+
3
+
4
+ class AsyncWebSocketConnection(WebSocketConnection):
5
+ """Compatibility name for connections created by ``aconnect``."""
@@ -0,0 +1,49 @@
1
+ from collections.abc import Mapping, Sequence
2
+ from typing import Any
3
+
4
+ from swarmauri_base.transports.websocket.WebSocketClientBase import (
5
+ WebSocketClientBase,
6
+ )
7
+
8
+ from .WebSocketConnection import WebSocketConnection
9
+
10
+
11
+ class WebSocketClient(WebSocketClientBase):
12
+ def connect(
13
+ self,
14
+ url: str,
15
+ *,
16
+ headers: Mapping[str, str] | None = None,
17
+ subprotocols: Sequence[str] | None = None,
18
+ **kwargs: Any,
19
+ ) -> WebSocketConnection:
20
+ from websockets.sync.client import connect
21
+
22
+ socket = connect(
23
+ url,
24
+ additional_headers=dict(headers or {}),
25
+ subprotocols=list(subprotocols or []),
26
+ open_timeout=self.timeout,
27
+ **kwargs,
28
+ )
29
+ return WebSocketConnection.from_sync(socket)
30
+
31
+ async def aconnect(
32
+ self,
33
+ url: str,
34
+ *,
35
+ headers: Mapping[str, str] | None = None,
36
+ subprotocols: Sequence[str] | None = None,
37
+ **kwargs: Any,
38
+ ) -> WebSocketConnection:
39
+ from websockets.asyncio.client import connect
40
+
41
+ socket = await connect(
42
+ url,
43
+ additional_headers=dict(headers or {}),
44
+ subprotocols=list(subprotocols or []),
45
+ open_timeout=self.timeout,
46
+ ping_interval=self.ping_interval,
47
+ **kwargs,
48
+ )
49
+ return WebSocketConnection.from_async(socket)
@@ -0,0 +1,94 @@
1
+ import asyncio
2
+ from typing import Any
3
+
4
+ from pydantic import PrivateAttr
5
+ from swarmauri_base.transports.websocket.WebSocketConnectionBase import (
6
+ WebSocketConnectionBase,
7
+ )
8
+ from swarmauri_base.transports.websocket.WebSocketState import WebSocketState
9
+ from swarmauri_core.transports.i_websocket_connection import WebSocketMessage
10
+
11
+
12
+ class WebSocketConnection(WebSocketConnectionBase):
13
+ _sync_socket: Any = PrivateAttr(default=None)
14
+ _async_socket: Any = PrivateAttr(default=None)
15
+
16
+ @classmethod
17
+ def from_sync(cls, socket: Any) -> "WebSocketConnection":
18
+ result = cls(
19
+ state=WebSocketState.OPEN.value,
20
+ subprotocol=getattr(socket, "subprotocol", None),
21
+ )
22
+ result._sync_socket = socket
23
+ return result
24
+
25
+ @classmethod
26
+ def from_async(cls, socket: Any) -> "WebSocketConnection":
27
+ result = cls(
28
+ state=WebSocketState.OPEN.value,
29
+ subprotocol=getattr(socket, "subprotocol", None),
30
+ )
31
+ result._async_socket = socket
32
+ return result
33
+
34
+ def send(self, message: WebSocketMessage) -> None:
35
+ if self._sync_socket is not None:
36
+ self._sync_socket.send(message)
37
+ return
38
+ self._run_sync(self.asend(message))
39
+
40
+ async def asend(self, message: WebSocketMessage) -> None:
41
+ if self._async_socket is not None:
42
+ await self._async_socket.send(message)
43
+ return
44
+ if self._sync_socket is None:
45
+ raise RuntimeError("WebSocket is not connected")
46
+ await asyncio.to_thread(self._sync_socket.send, message)
47
+
48
+ def receive(self) -> WebSocketMessage:
49
+ if self._sync_socket is not None:
50
+ return self._sync_socket.recv()
51
+ return self._run_sync(self.areceive())
52
+
53
+ async def areceive(self) -> WebSocketMessage:
54
+ if self._async_socket is not None:
55
+ return await self._async_socket.recv()
56
+ if self._sync_socket is None:
57
+ raise RuntimeError("WebSocket is not connected")
58
+ return await asyncio.to_thread(self._sync_socket.recv)
59
+
60
+ def ping(self, data: bytes = b"") -> None:
61
+ if self._sync_socket is not None:
62
+ self._sync_socket.ping(data)
63
+ return
64
+ self._run_sync(self.aping(data))
65
+
66
+ async def aping(self, data: bytes = b"") -> None:
67
+ if self._async_socket is not None:
68
+ waiter = await self._async_socket.ping(data)
69
+ await waiter
70
+ return
71
+ if self._sync_socket is None:
72
+ raise RuntimeError("WebSocket is not connected")
73
+ await asyncio.to_thread(self._sync_socket.ping, data)
74
+
75
+ def close(self, code: int = 1000, reason: str = "") -> None:
76
+ if self.state == WebSocketState.CLOSED.value:
77
+ return
78
+ if self._sync_socket is not None:
79
+ self._sync_socket.close(code=code, reason=reason)
80
+ self.state = WebSocketState.CLOSED.value
81
+ return
82
+ self._run_sync(self.aclose(code, reason))
83
+
84
+ async def aclose(self, code: int = 1000, reason: str = "") -> None:
85
+ if self.state == WebSocketState.CLOSED.value:
86
+ return
87
+ self.state = WebSocketState.CLOSING.value
88
+ if self._async_socket is not None:
89
+ await self._async_socket.close(code=code, reason=reason)
90
+ elif self._sync_socket is not None:
91
+ await asyncio.to_thread(
92
+ self._sync_socket.close, code=code, reason=reason
93
+ )
94
+ self.state = WebSocketState.CLOSED.value
@@ -0,0 +1,9 @@
1
+ from .AsyncWebSocketConnection import AsyncWebSocketConnection
2
+ from .WebSocketClient import WebSocketClient
3
+ from .WebSocketConnection import WebSocketConnection
4
+
5
+ __all__ = [
6
+ "AsyncWebSocketConnection",
7
+ "WebSocketClient",
8
+ "WebSocketConnection",
9
+ ]