zenbroker 1.1.0__py3-none-any.whl → 1.3.0__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.
- zenbroker/__init__.py +3 -2
- zenbroker/client.py +78 -1
- {zenbroker-1.1.0.dist-info → zenbroker-1.3.0.dist-info}/METADATA +2 -1
- zenbroker-1.3.0.dist-info/RECORD +6 -0
- zenbroker-1.1.0.dist-info/RECORD +0 -6
- {zenbroker-1.1.0.dist-info → zenbroker-1.3.0.dist-info}/WHEEL +0 -0
- {zenbroker-1.1.0.dist-info → zenbroker-1.3.0.dist-info}/top_level.txt +0 -0
zenbroker/__init__.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
from .client import ZenbrokerClient, PostPublishEventResponse
|
1
|
+
from .client import ZenbrokerClient, PostPublishEventResponse, ZenBrokerIncommingMessage
|
2
2
|
|
3
3
|
__all__ = [
|
4
4
|
"ZenbrokerClient",
|
5
|
-
"PostPublishEventResponse"
|
5
|
+
"PostPublishEventResponse",
|
6
|
+
"ZenBrokerIncommingMessage"
|
6
7
|
]
|
zenbroker/client.py
CHANGED
@@ -1,6 +1,10 @@
|
|
1
|
+
import json
|
2
|
+
import threading
|
1
3
|
import httpx
|
2
|
-
from typing import Dict, Any
|
4
|
+
from typing import Dict, Any, Set
|
3
5
|
from pydantic import BaseModel, Field, HttpUrl
|
6
|
+
import socketio
|
7
|
+
import time
|
4
8
|
|
5
9
|
class PostPublishEventPayload(BaseModel):
|
6
10
|
applicationId: str = Field(..., description="Application ID")
|
@@ -11,13 +15,71 @@ class PostPublishEventResponse(BaseModel):
|
|
11
15
|
message: str = Field(..., description="Message from the server")
|
12
16
|
id: str = Field(..., description="ID of the message")
|
13
17
|
|
18
|
+
class ZenBrokerIncommingMessage(BaseModel):
|
19
|
+
id: str = Field(..., description="ID of the message")
|
20
|
+
channel: str = Field(..., description="Channel of the message")
|
21
|
+
createdAt: str = Field(..., description="Created at timestamp")
|
22
|
+
data: str = Field(..., description="Data of the message")
|
23
|
+
|
14
24
|
class ZenbrokerClient:
|
15
25
|
def __init__(self, base_url: HttpUrl, application_id: str) -> None:
|
16
26
|
self._url: str = str(base_url)
|
17
27
|
self._application_id: str = str(application_id)
|
18
28
|
|
19
29
|
self._api = httpx.Client(base_url=self._url)
|
30
|
+
|
31
|
+
# Socket.IO client
|
32
|
+
self._sio = socketio.Client(logger=True)
|
33
|
+
self._sio.connect(f"{self._url}?applicationId={self._application_id}", socketio_path="/ws")
|
34
|
+
self._sio.on("message", self._on_socket_message)
|
35
|
+
|
36
|
+
# Channels
|
37
|
+
self._channels: Set[str] = set()
|
38
|
+
|
39
|
+
# Callbacks
|
40
|
+
self._callbacks: list[callable] = []
|
41
|
+
|
42
|
+
def _on_socket_message(self, data):
|
43
|
+
_message = ZenBrokerIncommingMessage.model_validate(json.loads(data))
|
44
|
+
for callback in self._callbacks:
|
45
|
+
callback(_message)
|
46
|
+
|
47
|
+
|
48
|
+
def on_message(self, callback: callable) -> callable:
|
49
|
+
self._callbacks.append(callback)
|
50
|
+
|
51
|
+
def unsubscribe():
|
52
|
+
if callback in self._callbacks:
|
53
|
+
self._callbacks.remove(callback)
|
54
|
+
|
55
|
+
return unsubscribe
|
56
|
+
|
57
|
+
def subscribe(self, channel: str) -> bool:
|
58
|
+
if channel in self._channels:
|
59
|
+
return False
|
60
|
+
|
61
|
+
self._sio.emit("subscribe", channel)
|
62
|
+
self._channels.add(channel)
|
63
|
+
|
64
|
+
return True
|
20
65
|
|
66
|
+
def unsubscribe(self, channel: str) -> bool:
|
67
|
+
if channel not in self._channels:
|
68
|
+
return False
|
69
|
+
|
70
|
+
self._sio.emit("unsubscribe", channel)
|
71
|
+
self._channels.remove(channel)
|
72
|
+
return True
|
73
|
+
|
74
|
+
def publish_ws(self, channel: str, data: Dict[str, Any]) -> bool:
|
75
|
+
post_data: PostPublishEventPayload = PostPublishEventPayload(
|
76
|
+
applicationId=self._application_id,
|
77
|
+
channel=channel,
|
78
|
+
data=data
|
79
|
+
)
|
80
|
+
self._sio.emit("message", data=post_data.model_dump())
|
81
|
+
return True
|
82
|
+
|
21
83
|
def publish(self, channel: str, data: Dict[str, Any]) -> PostPublishEventResponse:
|
22
84
|
post_data: PostPublishEventPayload = PostPublishEventPayload(
|
23
85
|
applicationId=self._application_id,
|
@@ -38,3 +100,18 @@ class ZenbrokerClient:
|
|
38
100
|
message=result['message']
|
39
101
|
)
|
40
102
|
|
103
|
+
def listen(self):
|
104
|
+
"""
|
105
|
+
Keeps the Socket.IO client running and listening for messages.
|
106
|
+
This method will not block the main thread.
|
107
|
+
"""
|
108
|
+
def _listen():
|
109
|
+
try:
|
110
|
+
while True:
|
111
|
+
time.sleep(1)
|
112
|
+
except KeyboardInterrupt:
|
113
|
+
self._sio.disconnect()
|
114
|
+
|
115
|
+
thread = threading.Thread(target=_listen)
|
116
|
+
thread.start()
|
117
|
+
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: zenbroker
|
3
|
-
Version: 1.
|
3
|
+
Version: 1.3.0
|
4
4
|
Summary: Zenbroker client
|
5
5
|
Home-page: UNKNOWN
|
6
6
|
License: UNKNOWN
|
@@ -8,6 +8,7 @@ Platform: UNKNOWN
|
|
8
8
|
Description-Content-Type: text/markdown
|
9
9
|
Requires-Dist: pydantic<3.0.0,>=2.0.0
|
10
10
|
Requires-Dist: httpx<1.0.0,>=0.28.0
|
11
|
+
Requires-Dist: python-socketio<6.0.0,>=5.0.0
|
11
12
|
|
12
13
|
# Zenbroker Client Example
|
13
14
|
|
@@ -0,0 +1,6 @@
|
|
1
|
+
zenbroker/__init__.py,sha256=1FVuCI5MNs_uk5hukwIlkHiPhXVhLtc4DUYXNYh5WIY,190
|
2
|
+
zenbroker/client.py,sha256=hwWF1St1vAzDEF8BalsHgZV3JYHqCcGZr9g-JSHz6Y8,3674
|
3
|
+
zenbroker-1.3.0.dist-info/METADATA,sha256=mGtepoX1emIlq2kp9h-MDErAbFLUwXwhz79dumowDJI,1057
|
4
|
+
zenbroker-1.3.0.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
|
5
|
+
zenbroker-1.3.0.dist-info/top_level.txt,sha256=u-V5a1mTLsnD3DQb01LQo8WtbYc75BIZ6jSWrZ7vDkY,10
|
6
|
+
zenbroker-1.3.0.dist-info/RECORD,,
|
zenbroker-1.1.0.dist-info/RECORD
DELETED
@@ -1,6 +0,0 @@
|
|
1
|
-
zenbroker/__init__.py,sha256=J-UtdbKCUJ2xJVcng2slPY81nl015BHBTNNSE6ZAwvA,130
|
2
|
-
zenbroker/client.py,sha256=FDjIbV9yRhdbAsWp_kc-0GHqUYuAENlOI3b5l2ZB85o,1279
|
3
|
-
zenbroker-1.1.0.dist-info/METADATA,sha256=1tE4Lq1SA_zvz-jdPrHBUn8vMqw9piwNNxTtW_lUnYU,1012
|
4
|
-
zenbroker-1.1.0.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
|
5
|
-
zenbroker-1.1.0.dist-info/top_level.txt,sha256=u-V5a1mTLsnD3DQb01LQo8WtbYc75BIZ6jSWrZ7vDkY,10
|
6
|
-
zenbroker-1.1.0.dist-info/RECORD,,
|
File without changes
|
File without changes
|