courseexe-realtime 1.0.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.
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import hmac
|
|
2
|
+
import hashlib
|
|
3
|
+
import json
|
|
4
|
+
import time
|
|
5
|
+
import requests
|
|
6
|
+
from typing import Union, List, Dict, Any, Optional
|
|
7
|
+
|
|
8
|
+
class RealtimeServer:
|
|
9
|
+
def __init__(
|
|
10
|
+
self,
|
|
11
|
+
app_id: str,
|
|
12
|
+
key: str,
|
|
13
|
+
secret: str,
|
|
14
|
+
host: str = "courseexe.com",
|
|
15
|
+
port: int = 443,
|
|
16
|
+
secure: bool = True,
|
|
17
|
+
timeout: int = 10,
|
|
18
|
+
):
|
|
19
|
+
self.app_id = str(app_id)
|
|
20
|
+
self.key = str(key)
|
|
21
|
+
self.secret = str(secret)
|
|
22
|
+
self.host = host
|
|
23
|
+
self.port = port
|
|
24
|
+
self.secure = secure
|
|
25
|
+
self.timeout = timeout
|
|
26
|
+
|
|
27
|
+
def trigger(
|
|
28
|
+
self,
|
|
29
|
+
channels: Union[str, List[str]],
|
|
30
|
+
event: str,
|
|
31
|
+
data: Any,
|
|
32
|
+
socket_id: Optional[str] = None,
|
|
33
|
+
) -> Dict[str, Any]:
|
|
34
|
+
"""
|
|
35
|
+
Trigger an event on one or multiple channels.
|
|
36
|
+
"""
|
|
37
|
+
if isinstance(channels, str):
|
|
38
|
+
channel_list = [channels]
|
|
39
|
+
else:
|
|
40
|
+
channel_list = list(channels)
|
|
41
|
+
|
|
42
|
+
body_dict: Dict[str, Any] = {
|
|
43
|
+
"name": event,
|
|
44
|
+
"channels": channel_list,
|
|
45
|
+
"data": json.dumps(data) if not isinstance(data, str) else data,
|
|
46
|
+
}
|
|
47
|
+
if socket_id:
|
|
48
|
+
body_dict["socket_id"] = socket_id
|
|
49
|
+
|
|
50
|
+
body_json = json.dumps(body_dict)
|
|
51
|
+
timestamp = str(int(time.time()))
|
|
52
|
+
body_md5 = hashlib.md5(body_json.encode("utf-8")).hexdigest()
|
|
53
|
+
|
|
54
|
+
canonical = (
|
|
55
|
+
f"POST\n/apps/{self.app_id}/events\n"
|
|
56
|
+
f"auth_key={self.key}&auth_timestamp={timestamp}&auth_version=1.0&body_md5={body_md5}"
|
|
57
|
+
)
|
|
58
|
+
sig = hmac.new(
|
|
59
|
+
self.secret.encode("utf-8"),
|
|
60
|
+
canonical.encode("utf-8"),
|
|
61
|
+
hashlib.sha256
|
|
62
|
+
).hexdigest()
|
|
63
|
+
|
|
64
|
+
proto = "https" if self.secure else "http"
|
|
65
|
+
port_suffix = (
|
|
66
|
+
""
|
|
67
|
+
if (self.secure and self.port == 443) or (not self.secure and self.port == 80)
|
|
68
|
+
else f":{self.port}"
|
|
69
|
+
)
|
|
70
|
+
url = f"{proto}://{self.host}{port_suffix}/apps/{self.app_id}/events"
|
|
71
|
+
|
|
72
|
+
params = {
|
|
73
|
+
"auth_key": self.key,
|
|
74
|
+
"auth_timestamp": timestamp,
|
|
75
|
+
"auth_version": "1.0",
|
|
76
|
+
"body_md5": body_md5,
|
|
77
|
+
"auth_signature": sig,
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
resp = requests.post(
|
|
81
|
+
url,
|
|
82
|
+
params=params,
|
|
83
|
+
data=body_json,
|
|
84
|
+
headers={"Content-Type": "application/json"},
|
|
85
|
+
timeout=self.timeout,
|
|
86
|
+
)
|
|
87
|
+
resp.raise_for_status()
|
|
88
|
+
return resp.json() if resp.text else {}
|
|
89
|
+
|
|
90
|
+
def authenticate_channel(
|
|
91
|
+
self,
|
|
92
|
+
socket_id: str,
|
|
93
|
+
channel_name: str,
|
|
94
|
+
channel_data: Optional[Dict[str, Any]] = None,
|
|
95
|
+
) -> Dict[str, str]:
|
|
96
|
+
"""
|
|
97
|
+
Generates HMAC-SHA256 signature for private or presence channels.
|
|
98
|
+
"""
|
|
99
|
+
to_sign = f"{socket_id}:{channel_name}"
|
|
100
|
+
data_str = None
|
|
101
|
+
if channel_data:
|
|
102
|
+
data_str = json.dumps(channel_data)
|
|
103
|
+
to_sign += f":{data_str}"
|
|
104
|
+
|
|
105
|
+
signature = hmac.new(
|
|
106
|
+
self.secret.encode("utf-8"),
|
|
107
|
+
to_sign.encode("utf-8"),
|
|
108
|
+
hashlib.sha256
|
|
109
|
+
).hexdigest()
|
|
110
|
+
|
|
111
|
+
res = {"auth": f"{self.key}:{signature}"}
|
|
112
|
+
if data_str:
|
|
113
|
+
res["channel_data"] = data_str
|
|
114
|
+
return res
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: courseexe-realtime
|
|
3
|
+
Version: 1.0.1
|
|
4
|
+
Summary: Official Python Client and Server Publisher SDK for Courseexe Realtime Channels
|
|
5
|
+
Home-page: https://courseexe.com/realtime/docs
|
|
6
|
+
Author: Courseexe
|
|
7
|
+
Author-email: info@courseexe.com
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.7
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
Requires-Dist: requests>=2.25.0
|
|
14
|
+
Provides-Extra: client
|
|
15
|
+
Requires-Dist: websocket-client>=1.3.0; extra == "client"
|
|
16
|
+
Dynamic: author
|
|
17
|
+
Dynamic: author-email
|
|
18
|
+
Dynamic: classifier
|
|
19
|
+
Dynamic: description
|
|
20
|
+
Dynamic: description-content-type
|
|
21
|
+
Dynamic: home-page
|
|
22
|
+
Dynamic: provides-extra
|
|
23
|
+
Dynamic: requires-dist
|
|
24
|
+
Dynamic: requires-python
|
|
25
|
+
Dynamic: summary
|
|
26
|
+
|
|
27
|
+
# Courseexe Realtime Python SDK
|
|
28
|
+
|
|
29
|
+
Official Python server publisher and event dispatcher for **Courseexe Realtime Channels** (`https://courseexe.com`).
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install courseexe-realtime
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from courseexe_realtime import RealtimeServer
|
|
45
|
+
|
|
46
|
+
realtime = RealtimeServer(
|
|
47
|
+
app_id="YOUR_APP_ID",
|
|
48
|
+
key="YOUR_APP_KEY",
|
|
49
|
+
secret="YOUR_APP_SECRET",
|
|
50
|
+
host="courseexe.com",
|
|
51
|
+
port=443,
|
|
52
|
+
secure=True,
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
# Broadcast event to public or private channels
|
|
56
|
+
realtime.trigger(
|
|
57
|
+
channels="orders",
|
|
58
|
+
event="order-created",
|
|
59
|
+
data={"order_id": 1042, "total": 99.00, "status": "paid"},
|
|
60
|
+
)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Authenticating Private & Presence Channels
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
# In your Flask / FastAPI / Django auth endpoint
|
|
67
|
+
auth_response = realtime.authenticate_channel(
|
|
68
|
+
socket_id="12345.67890",
|
|
69
|
+
channel_name="private-user-10",
|
|
70
|
+
)
|
|
71
|
+
return auth_response
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## License
|
|
77
|
+
MIT © [Courseexe](https://courseexe.com)
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
courseexe_realtime/__init__.py,sha256=J1uibzc3uMInUbMwq-RjTt9J-fKq5Aqmad_VuKBdnMQ,202
|
|
2
|
+
courseexe_realtime/server.py,sha256=ooBhLQcQfWGwXTwlCNmdzTbcx23aKE5PshBy8MfiSqQ,3249
|
|
3
|
+
courseexe_realtime-1.0.1.dist-info/METADATA,sha256=BNVQ-9W6PMgwtYdq9OaDq7zEgTdQky_sOZcMhsUygzY,1817
|
|
4
|
+
courseexe_realtime-1.0.1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
5
|
+
courseexe_realtime-1.0.1.dist-info/top_level.txt,sha256=0UgnRgnZEpA6kHXJlr3wcKOfSnmuMFLaYuRmOQlzdPM,19
|
|
6
|
+
courseexe_realtime-1.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
courseexe_realtime
|