swarmauri_transport_websocket 0.1.0.dev4__py3-none-any.whl
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.
- swarmauri_transport_websocket/AsyncWebSocketConnection.py +5 -0
- swarmauri_transport_websocket/WebSocketClient.py +49 -0
- swarmauri_transport_websocket/WebSocketConnection.py +94 -0
- swarmauri_transport_websocket/__init__.py +9 -0
- swarmauri_transport_websocket-0.1.0.dev4.dist-info/METADATA +55 -0
- swarmauri_transport_websocket-0.1.0.dev4.dist-info/RECORD +9 -0
- swarmauri_transport_websocket-0.1.0.dev4.dist-info/WHEEL +4 -0
- swarmauri_transport_websocket-0.1.0.dev4.dist-info/entry_points.txt +3 -0
- swarmauri_transport_websocket-0.1.0.dev4.dist-info/licenses/LICENSE +13 -0
|
@@ -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,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
|
+

|
|
34
|
+
|
|
35
|
+
[](https://pypi.org/project/swarmauri_transport_websocket/) [](https://pypi.org/project/swarmauri_transport_websocket/) [](LICENSE) [](https://pepy.tech/project/swarmauri_transport_websocket) [](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,9 @@
|
|
|
1
|
+
swarmauri_transport_websocket/AsyncWebSocketConnection.py,sha256=PpHHlYq4t3BJZV6Rzz1XMIa1FAJ7rNX_k00_qzaAMZg,178
|
|
2
|
+
swarmauri_transport_websocket/WebSocketClient.py,sha256=iwKAhQALLMFL4eEoMVktljvt2fVaEfqLDOutMzaPFhY,1403
|
|
3
|
+
swarmauri_transport_websocket/WebSocketConnection.py,sha256=9dy-RQJ1ectQuG2l6FqGLTUg9bdLkc1sj-Kvhh2pR7c,3509
|
|
4
|
+
swarmauri_transport_websocket/__init__.py,sha256=IkIvxz8TW3rkaZtEKkDLV93RM8C3WpPKJ-F8ORcukUQ,258
|
|
5
|
+
swarmauri_transport_websocket-0.1.0.dev4.dist-info/METADATA,sha256=ty3Rq33VfMa8DHQirUVihYYEU-Wy3KOMoA8ZSugEULA,2642
|
|
6
|
+
swarmauri_transport_websocket-0.1.0.dev4.dist-info/WHEEL,sha256=eY7nduwzv-ldUxpzbRlxwvC693Hg6PX8bWDjEHjZ_dk,88
|
|
7
|
+
swarmauri_transport_websocket-0.1.0.dev4.dist-info/entry_points.txt,sha256=e1dwXOA_Nntoy1Sxh7HM3il0twJoQ4ToTPo6IT0cMlg,86
|
|
8
|
+
swarmauri_transport_websocket-0.1.0.dev4.dist-info/licenses/LICENSE,sha256=kkwAgrDtfQ7c-Ozql2xlztDDjHt0YwGmRG7b_oUU9Jg,554
|
|
9
|
+
swarmauri_transport_websocket-0.1.0.dev4.dist-info/RECORD,,
|
|
@@ -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.
|