zenbroker 1.0.0__tar.gz → 1.2.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.0.0
3
+ Version: 1.2.0
4
4
  Summary: Zenbroker client
5
5
  Home-page: UNKNOWN
6
6
  License: UNKNOWN
@@ -44,20 +44,6 @@ resp = client.publish(
44
44
  )
45
45
  ```
46
46
 
47
- ### Publishing multiple messages
48
-
49
- ```python
50
- for i in range(10000):
51
- resp = client.publish(
52
- channel="testing-channel",
53
- data={
54
- "index": i,
55
- "message": "Hey There"
56
- }
57
- )
58
- print("DONE", i)
59
- ```
60
-
61
47
  ## Notes
62
48
 
63
49
  - Replace `YOUR_APPLICATION_ID` with your actual application ID
@@ -35,20 +35,6 @@ resp = client.publish(
35
35
  )
36
36
  ```
37
37
 
38
- ### Publishing multiple messages
39
-
40
- ```python
41
- for i in range(10000):
42
- resp = client.publish(
43
- channel="testing-channel",
44
- data={
45
- "index": i,
46
- "message": "Hey There"
47
- }
48
- )
49
- print("DONE", i)
50
- ```
51
-
52
38
  ## Notes
53
39
 
54
40
  - Replace `YOUR_APPLICATION_ID` with your actual application ID
@@ -8,13 +8,14 @@ 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.0.0",
11
+ version="1.2.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
- "httpx>=0.28.0,<1.0.0"
17
+ "httpx>=0.28.0,<1.0.0",
18
+ "python-socketio>=5.0.0,<6.0.0"
18
19
  ],
19
20
  long_description=long_description,
20
21
  long_description_content_type='text/markdown'
@@ -0,0 +1,7 @@
1
+ from .client import ZenbrokerClient, PostPublishEventResponse, ZenBrokerIncommingMessage
2
+
3
+ __all__ = [
4
+ "ZenbrokerClient",
5
+ "PostPublishEventResponse",
6
+ "ZenBrokerIncommingMessage"
7
+ ]
@@ -0,0 +1,108 @@
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
+
@@ -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
@@ -44,20 +44,6 @@ resp = client.publish(
44
44
  )
45
45
  ```
46
46
 
47
- ### Publishing multiple messages
48
-
49
- ```python
50
- for i in range(10000):
51
- resp = client.publish(
52
- channel="testing-channel",
53
- data={
54
- "index": i,
55
- "message": "Hey There"
56
- }
57
- )
58
- print("DONE", i)
59
- ```
60
-
61
47
  ## Notes
62
48
 
63
49
  - Replace `YOUR_APPLICATION_ID` with your actual application ID
@@ -1,2 +1,3 @@
1
1
  pydantic<3.0.0,>=2.0.0
2
2
  httpx<1.0.0,>=0.28.0
3
+ python-socketio<6.0.0,>=5.0.0
@@ -1,6 +0,0 @@
1
- from .client import ZenbrokerClient, PostPublishEventResponse
2
-
3
- __all__ = [
4
- "ZenbrokerClient",
5
- "PostPublishEventResponse"
6
- ]
@@ -1,40 +0,0 @@
1
- import httpx
2
- from typing import Dict, Any
3
- from pydantic import BaseModel, Field, HttpUrl
4
-
5
- class PostPublishEventPayload(BaseModel):
6
- applicationId: str = Field(..., description="Application ID")
7
- channel: str = Field(..., description="ChannelID")
8
- data: Dict[str, Any]
9
-
10
- class PostPublishEventResponse(BaseModel):
11
- message: str = Field(..., description="Message from the server")
12
- id: str = Field(..., description="ID of the message")
13
-
14
- class ZenbrokerClient:
15
- def __init__(self, base_url: HttpUrl, application_id: str) -> None:
16
- self._url: str = str(base_url)
17
- self._application_id: str = str(application_id)
18
-
19
- self._api = httpx.Client(base_url=self._url)
20
-
21
- def publish(self, channel: str, data: Dict[str, Any]) -> PostPublishEventResponse:
22
- post_data: PostPublishEventPayload = PostPublishEventPayload(
23
- applicationId=self._application_id,
24
- channel=channel,
25
- data=data
26
- )
27
-
28
- response = self._api.post(
29
- url="/producer/emit",
30
- json=post_data.model_dump()
31
- )
32
-
33
- response.raise_for_status()
34
- result = response.json()
35
-
36
- return PostPublishEventResponse(
37
- id=result['id'],
38
- message=result['message']
39
- )
40
-
File without changes