zenbroker 1.3.0__py3-none-any.whl → 1.4.1__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 CHANGED
@@ -1,7 +1,9 @@
1
1
  from .client import ZenbrokerClient, PostPublishEventResponse, ZenBrokerIncommingMessage
2
+ from .client import AsyncZenbrokerClient
2
3
 
3
4
  __all__ = [
4
5
  "ZenbrokerClient",
6
+ "AsyncZenbrokerClient",
5
7
  "PostPublishEventResponse",
6
8
  "ZenBrokerIncommingMessage"
7
9
  ]
zenbroker/client.py CHANGED
@@ -1,7 +1,8 @@
1
1
  import json
2
2
  import threading
3
+ import asyncio
3
4
  import httpx
4
- from typing import Dict, Any, Set
5
+ from typing import Dict, Any, Set, Callable, List
5
6
  from pydantic import BaseModel, Field, HttpUrl
6
7
  import socketio
7
8
  import time
@@ -21,6 +22,7 @@ class ZenBrokerIncommingMessage(BaseModel):
21
22
  createdAt: str = Field(..., description="Created at timestamp")
22
23
  data: str = Field(..., description="Data of the message")
23
24
 
25
+
24
26
  class ZenbrokerClient:
25
27
  def __init__(self, base_url: HttpUrl, application_id: str) -> None:
26
28
  self._url: str = str(base_url)
@@ -28,51 +30,43 @@ class ZenbrokerClient:
28
30
 
29
31
  self._api = httpx.Client(base_url=self._url)
30
32
 
31
- # Socket.IO client
32
33
  self._sio = socketio.Client(logger=True)
33
34
  self._sio.connect(f"{self._url}?applicationId={self._application_id}", socketio_path="/ws")
34
35
  self._sio.on("message", self._on_socket_message)
35
36
 
36
- # Channels
37
37
  self._channels: Set[str] = set()
38
-
39
- # Callbacks
40
- self._callbacks: list[callable] = []
38
+ self._callbacks: List[Callable] = []
41
39
 
42
40
  def _on_socket_message(self, data):
43
41
  _message = ZenBrokerIncommingMessage.model_validate(json.loads(data))
44
42
  for callback in self._callbacks:
45
43
  callback(_message)
46
44
 
47
-
48
- def on_message(self, callback: callable) -> callable:
45
+ def on_message(self, callback: Callable) -> Callable:
49
46
  self._callbacks.append(callback)
50
-
47
+
51
48
  def unsubscribe():
52
49
  if callback in self._callbacks:
53
50
  self._callbacks.remove(callback)
54
-
51
+
55
52
  return unsubscribe
56
53
 
57
54
  def subscribe(self, channel: str) -> bool:
58
55
  if channel in self._channels:
59
56
  return False
60
-
61
57
  self._sio.emit("subscribe", channel)
62
58
  self._channels.add(channel)
63
-
64
59
  return True
65
-
60
+
66
61
  def unsubscribe(self, channel: str) -> bool:
67
62
  if channel not in self._channels:
68
63
  return False
69
-
70
64
  self._sio.emit("unsubscribe", channel)
71
65
  self._channels.remove(channel)
72
66
  return True
73
-
67
+
74
68
  def publish_ws(self, channel: str, data: Dict[str, Any]) -> bool:
75
- post_data: PostPublishEventPayload = PostPublishEventPayload(
69
+ post_data = PostPublishEventPayload(
76
70
  applicationId=self._application_id,
77
71
  channel=channel,
78
72
  data=data
@@ -81,7 +75,7 @@ class ZenbrokerClient:
81
75
  return True
82
76
 
83
77
  def publish(self, channel: str, data: Dict[str, Any]) -> PostPublishEventResponse:
84
- post_data: PostPublishEventPayload = PostPublishEventPayload(
78
+ post_data = PostPublishEventPayload(
85
79
  applicationId=self._application_id,
86
80
  channel=channel,
87
81
  data=data
@@ -99,12 +93,8 @@ class ZenbrokerClient:
99
93
  id=result['id'],
100
94
  message=result['message']
101
95
  )
102
-
96
+
103
97
  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
98
  def _listen():
109
99
  try:
110
100
  while True:
@@ -114,4 +104,93 @@ class ZenbrokerClient:
114
104
 
115
105
  thread = threading.Thread(target=_listen)
116
106
  thread.start()
117
-
107
+
108
+
109
+ # ------------------------------
110
+ # Async Version of the Client
111
+ # ------------------------------
112
+
113
+ class AsyncZenbrokerClient:
114
+ def __init__(self, base_url: HttpUrl, application_id: str) -> None:
115
+ self._url: str = str(base_url)
116
+ self._application_id: str = str(application_id)
117
+
118
+ self._api = httpx.AsyncClient(base_url=self._url)
119
+
120
+ self._sio = socketio.AsyncClient(logger=True)
121
+ self._callbacks: List[Callable] = []
122
+ self._channels: Set[str] = set()
123
+
124
+ async def connect(self):
125
+ await self._sio.connect(f"{self._url}?applicationId={self._application_id}", socketio_path="/ws")
126
+ self._sio.on("message", self._on_socket_message)
127
+
128
+ async def disconnect(self):
129
+ await self._sio.disconnect()
130
+
131
+ def _on_socket_message(self, data):
132
+ _message = ZenBrokerIncommingMessage.model_validate(json.loads(data))
133
+ for callback in self._callbacks:
134
+ callback(_message)
135
+
136
+ def on_message(self, callback: Callable) -> Callable:
137
+ self._callbacks.append(callback)
138
+
139
+ def unsubscribe():
140
+ if callback in self._callbacks:
141
+ self._callbacks.remove(callback)
142
+
143
+ return unsubscribe
144
+
145
+ async def subscribe(self, channel: str) -> bool:
146
+ if channel in self._channels:
147
+ return False
148
+ await self._sio.emit("subscribe", channel)
149
+ self._channels.add(channel)
150
+ return True
151
+
152
+ async def unsubscribe(self, channel: str) -> bool:
153
+ if channel not in self._channels:
154
+ return False
155
+ await self._sio.emit("unsubscribe", channel)
156
+ self._channels.remove(channel)
157
+ return True
158
+
159
+ async def publish_ws(self, channel: str, data: Dict[str, Any]) -> bool:
160
+ post_data = PostPublishEventPayload(
161
+ applicationId=self._application_id,
162
+ channel=channel,
163
+ data=data
164
+ )
165
+ await self._sio.emit("message", data=post_data.model_dump())
166
+ return True
167
+
168
+ async def publish(self, channel: str, data: Dict[str, Any]) -> PostPublishEventResponse:
169
+ post_data = PostPublishEventPayload(
170
+ applicationId=self._application_id,
171
+ channel=channel,
172
+ data=data
173
+ )
174
+
175
+ response = await self._api.post(
176
+ url="/producer/emit",
177
+ json=post_data.model_dump()
178
+ )
179
+
180
+ response.raise_for_status()
181
+ result = response.json()
182
+
183
+ return PostPublishEventResponse(
184
+ id=result['id'],
185
+ message=result['message']
186
+ )
187
+
188
+ async def listen(self):
189
+ """
190
+ Keeps the Socket.IO client running and listening for messages.
191
+ """
192
+ try:
193
+ while True:
194
+ await asyncio.sleep(1)
195
+ except asyncio.CancelledError:
196
+ await self._sio.disconnect()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: zenbroker
3
- Version: 1.3.0
3
+ Version: 1.4.1
4
4
  Summary: Zenbroker client
5
5
  Home-page: UNKNOWN
6
6
  License: UNKNOWN
@@ -9,6 +9,7 @@ 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
11
  Requires-Dist: python-socketio<6.0.0,>=5.0.0
12
+ Requires-Dist: websocket-client<2.0.0,>=1.8.0
12
13
 
13
14
  # Zenbroker Client Example
14
15
 
@@ -0,0 +1,6 @@
1
+ zenbroker/__init__.py,sha256=Fl_qm_smnDS6xGp9uVZKpSZ6DfEPIPLkhBj1xEmQ3p8,259
2
+ zenbroker/client.py,sha256=GN65TsFKYQEeOpDmOVtogT74klRaKY7XAPR7DVildh4,6224
3
+ zenbroker-1.4.1.dist-info/METADATA,sha256=tcSgIirf8Gz0zYNcynlOuiYsDOf-XdUkXW3TU6PzlCM,1103
4
+ zenbroker-1.4.1.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
5
+ zenbroker-1.4.1.dist-info/top_level.txt,sha256=u-V5a1mTLsnD3DQb01LQo8WtbYc75BIZ6jSWrZ7vDkY,10
6
+ zenbroker-1.4.1.dist-info/RECORD,,
@@ -1,6 +0,0 @@
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,,