zenbroker 1.2.0__tar.gz → 1.4.0__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: zenbroker
3
- Version: 1.2.0
3
+ Version: 1.4.0
4
4
  Summary: Zenbroker client
5
5
  Home-page: UNKNOWN
6
6
  License: UNKNOWN
@@ -8,14 +8,15 @@ with open(os.path.join(this_directory, 'README.md'), encoding='utf-8') as f:
8
8
 
9
9
  setup(
10
10
  name="zenbroker",
11
- version="1.2.0",
11
+ version="1.4.0",
12
12
  description="Zenbroker client",
13
13
  package_dir={"": "zenbroker"},
14
14
  packages=find_packages("zenbroker"),
15
15
  install_requires=[
16
16
  "pydantic>=2.0.0,<3.0.0",
17
17
  "httpx>=0.28.0,<1.0.0",
18
- "python-socketio>=5.0.0,<6.0.0"
18
+ "python-socketio>=5.0.0,<6.0.0",
19
+ "websocket-client>=1.8.0,<2.0.0"
19
20
  ],
20
21
  long_description=long_description,
21
22
  long_description_content_type='text/markdown'
@@ -0,0 +1,196 @@
1
+ import json
2
+ import threading
3
+ import asyncio
4
+ import httpx
5
+ from typing import Dict, Any, Set, Callable, List
6
+ from pydantic import BaseModel, Field, HttpUrl
7
+ import socketio
8
+ import time
9
+
10
+ class PostPublishEventPayload(BaseModel):
11
+ applicationId: str = Field(..., description="Application ID")
12
+ channel: str = Field(..., description="ChannelID")
13
+ data: Dict[str, Any]
14
+
15
+ class PostPublishEventResponse(BaseModel):
16
+ message: str = Field(..., description="Message from the server")
17
+ id: str = Field(..., description="ID of the message")
18
+
19
+ class ZenBrokerIncommingMessage(BaseModel):
20
+ id: str = Field(..., description="ID of the message")
21
+ channel: str = Field(..., description="Channel of the message")
22
+ createdAt: str = Field(..., description="Created at timestamp")
23
+ data: str = Field(..., description="Data of the message")
24
+
25
+
26
+ class ZenbrokerClient:
27
+ def __init__(self, base_url: HttpUrl, application_id: str) -> None:
28
+ self._url: str = str(base_url)
29
+ self._application_id: str = str(application_id)
30
+
31
+ self._api = httpx.Client(base_url=self._url)
32
+
33
+ self._sio = socketio.Client(logger=True)
34
+ self._sio.connect(f"{self._url}?applicationId={self._application_id}", socketio_path="/ws")
35
+ self._sio.on("message", self._on_socket_message)
36
+
37
+ self._channels: Set[str] = set()
38
+ self._callbacks: List[Callable] = []
39
+
40
+ def _on_socket_message(self, data):
41
+ _message = ZenBrokerIncommingMessage.model_validate(json.loads(data))
42
+ for callback in self._callbacks:
43
+ callback(_message)
44
+
45
+ def on_message(self, callback: Callable) -> Callable:
46
+ self._callbacks.append(callback)
47
+
48
+ def unsubscribe():
49
+ if callback in self._callbacks:
50
+ self._callbacks.remove(callback)
51
+
52
+ return unsubscribe
53
+
54
+ def subscribe(self, channel: str) -> bool:
55
+ if channel in self._channels:
56
+ return False
57
+ self._sio.emit("subscribe", channel)
58
+ self._channels.add(channel)
59
+ return True
60
+
61
+ def unsubscribe(self, channel: str) -> bool:
62
+ if channel not in self._channels:
63
+ return False
64
+ self._sio.emit("unsubscribe", channel)
65
+ self._channels.remove(channel)
66
+ return True
67
+
68
+ def publish_ws(self, channel: str, data: Dict[str, Any]) -> bool:
69
+ post_data = PostPublishEventPayload(
70
+ applicationId=self._application_id,
71
+ channel=channel,
72
+ data=data
73
+ )
74
+ self._sio.emit("message", data=post_data.model_dump())
75
+ return True
76
+
77
+ def publish(self, channel: str, data: Dict[str, Any]) -> PostPublishEventResponse:
78
+ post_data = PostPublishEventPayload(
79
+ applicationId=self._application_id,
80
+ channel=channel,
81
+ data=data
82
+ )
83
+
84
+ response = self._api.post(
85
+ url="/producer/emit",
86
+ json=post_data.model_dump()
87
+ )
88
+
89
+ response.raise_for_status()
90
+ result = response.json()
91
+
92
+ return PostPublishEventResponse(
93
+ id=result['id'],
94
+ message=result['message']
95
+ )
96
+
97
+ def listen(self):
98
+ def _listen():
99
+ try:
100
+ while True:
101
+ time.sleep(1)
102
+ except KeyboardInterrupt:
103
+ self._sio.disconnect()
104
+
105
+ thread = threading.Thread(target=_listen)
106
+ thread.start()
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.2.0
3
+ Version: 1.4.0
4
4
  Summary: Zenbroker client
5
5
  Home-page: UNKNOWN
6
6
  License: UNKNOWN
@@ -1,3 +1,4 @@
1
1
  pydantic<3.0.0,>=2.0.0
2
2
  httpx<1.0.0,>=0.28.0
3
3
  python-socketio<6.0.0,>=5.0.0
4
+ websocket-client<2.0.0,>=1.8.0
@@ -1,108 +0,0 @@
1
- import json
2
- import threading
3
- import httpx
4
- from typing import Dict, Any, Set
5
- from pydantic import BaseModel, Field, HttpUrl
6
- import socketio
7
- import time
8
-
9
- class PostPublishEventPayload(BaseModel):
10
- applicationId: str = Field(..., description="Application ID")
11
- channel: str = Field(..., description="ChannelID")
12
- data: Dict[str, Any]
13
-
14
- class PostPublishEventResponse(BaseModel):
15
- message: str = Field(..., description="Message from the server")
16
- id: str = Field(..., description="ID of the message")
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
-
24
- class ZenbrokerClient:
25
- def __init__(self, base_url: HttpUrl, application_id: str) -> None:
26
- self._url: str = str(base_url)
27
- self._application_id: str = str(application_id)
28
-
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
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(self, channel: str, data: Dict[str, Any]) -> PostPublishEventResponse:
75
- post_data: PostPublishEventPayload = PostPublishEventPayload(
76
- applicationId=self._application_id,
77
- channel=channel,
78
- data=data
79
- )
80
-
81
- response = self._api.post(
82
- url="/producer/emit",
83
- json=post_data.model_dump()
84
- )
85
-
86
- response.raise_for_status()
87
- result = response.json()
88
-
89
- return PostPublishEventResponse(
90
- id=result['id'],
91
- message=result['message']
92
- )
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
-
File without changes
File without changes