zenbroker 1.0.0__py3-none-any.whl → 1.2.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 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,62 @@ 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
+
21
74
  def publish(self, channel: str, data: Dict[str, Any]) -> PostPublishEventResponse:
22
75
  post_data: PostPublishEventPayload = PostPublishEventPayload(
23
76
  applicationId=self._application_id,
@@ -38,3 +91,18 @@ class ZenbrokerClient:
38
91
  message=result['message']
39
92
  )
40
93
 
94
+ def listen(self):
95
+ """
96
+ Keeps the Socket.IO client running and listening for messages.
97
+ This method will not block the main thread.
98
+ """
99
+ def _listen():
100
+ try:
101
+ while True:
102
+ time.sleep(1)
103
+ except KeyboardInterrupt:
104
+ self._sio.disconnect()
105
+
106
+ thread = threading.Thread(target=_listen)
107
+ thread.start()
108
+
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: zenbroker
3
- Version: 1.0.0
3
+ Version: 1.2.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
 
@@ -46,20 +47,6 @@ resp = client.publish(
46
47
  )
47
48
  ```
48
49
 
49
- ### Publishing multiple messages
50
-
51
- ```python
52
- for i in range(10000):
53
- resp = client.publish(
54
- channel="testing-channel",
55
- data={
56
- "index": i,
57
- "message": "Hey There"
58
- }
59
- )
60
- print("DONE", i)
61
- ```
62
-
63
50
  ## Notes
64
51
 
65
52
  - Replace `YOUR_APPLICATION_ID` with your actual application ID
@@ -0,0 +1,6 @@
1
+ zenbroker/__init__.py,sha256=1FVuCI5MNs_uk5hukwIlkHiPhXVhLtc4DUYXNYh5WIY,190
2
+ zenbroker/client.py,sha256=aDBlva_EZrASO0h2LuEDU4-rxUEGiOGYiCsraeoHWDI,3337
3
+ zenbroker-1.2.0.dist-info/METADATA,sha256=UvuRqxD7lp_WX8s7C-yv9YQRPps6gkgtsdtpTaHrqBg,1057
4
+ zenbroker-1.2.0.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
5
+ zenbroker-1.2.0.dist-info/top_level.txt,sha256=u-V5a1mTLsnD3DQb01LQo8WtbYc75BIZ6jSWrZ7vDkY,10
6
+ zenbroker-1.2.0.dist-info/RECORD,,
@@ -1,6 +0,0 @@
1
- zenbroker/__init__.py,sha256=J-UtdbKCUJ2xJVcng2slPY81nl015BHBTNNSE6ZAwvA,130
2
- zenbroker/client.py,sha256=FDjIbV9yRhdbAsWp_kc-0GHqUYuAENlOI3b5l2ZB85o,1279
3
- zenbroker-1.0.0.dist-info/METADATA,sha256=JC-XaDfw-tLx5mz6CiYJhuZynT_M50bNUwMGeVsAdaY,1257
4
- zenbroker-1.0.0.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
5
- zenbroker-1.0.0.dist-info/top_level.txt,sha256=u-V5a1mTLsnD3DQb01LQo8WtbYc75BIZ6jSWrZ7vDkY,10
6
- zenbroker-1.0.0.dist-info/RECORD,,