polyapi-python 0.2.3.dev9__py3-none-any.whl → 0.2.4.dev1__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.
- polyapi/error_handler.py +60 -34
- {polyapi_python-0.2.3.dev9.dist-info → polyapi_python-0.2.4.dev1.dist-info}/METADATA +2 -1
- {polyapi_python-0.2.3.dev9.dist-info → polyapi_python-0.2.4.dev1.dist-info}/RECORD +6 -6
- {polyapi_python-0.2.3.dev9.dist-info → polyapi_python-0.2.4.dev1.dist-info}/LICENSE +0 -0
- {polyapi_python-0.2.3.dev9.dist-info → polyapi_python-0.2.4.dev1.dist-info}/WHEEL +0 -0
- {polyapi_python-0.2.3.dev9.dist-info → polyapi_python-0.2.4.dev1.dist-info}/top_level.txt +0 -0
polyapi/error_handler.py
CHANGED
|
@@ -1,52 +1,78 @@
|
|
|
1
1
|
import asyncio
|
|
2
2
|
import copy
|
|
3
3
|
import socketio # type: ignore
|
|
4
|
-
from typing import Any, Callable, Dict, Optional
|
|
4
|
+
from typing import Any, Callable, Dict, List, Optional
|
|
5
5
|
|
|
6
6
|
from polyapi.config import get_api_key_and_url
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
active_handlers: List[Dict[str, Any]] = []
|
|
10
|
+
client = None
|
|
10
11
|
|
|
11
12
|
|
|
12
|
-
def
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
def prepare():
|
|
14
|
+
loop = asyncio.get_event_loop()
|
|
15
|
+
loop.run_until_complete(get_client_and_connect())
|
|
16
|
+
print("Client initialized!")
|
|
16
17
|
|
|
17
|
-
async def _inner():
|
|
18
|
-
await socket.connect(base_url, transports=["websocket"], namespaces=["/events"])
|
|
19
18
|
|
|
20
|
-
handler_id = None
|
|
21
|
-
data = copy.deepcopy(options or {})
|
|
22
|
-
data["path"] = path
|
|
23
|
-
data["apiKey"] = api_key
|
|
24
19
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
20
|
+
async def get_client_and_connect():
|
|
21
|
+
_, base_url = get_api_key_and_url()
|
|
22
|
+
global client
|
|
23
|
+
client = socketio.AsyncClient()
|
|
24
|
+
await client.connect(base_url, transports=["websocket"], namespaces=["/events"])
|
|
29
25
|
|
|
30
|
-
await socket.emit("registerErrorHandler", data, "/events", registerCallback)
|
|
31
|
-
if local_error_handlers.get(path):
|
|
32
|
-
local_error_handlers[path].append(callback)
|
|
33
|
-
else:
|
|
34
|
-
local_error_handlers[path] = [callback]
|
|
35
26
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
27
|
+
async def unregister(data: Dict[str, Any]):
|
|
28
|
+
print(f"stopping error handler for '{data['path']}'...")
|
|
29
|
+
assert client
|
|
30
|
+
await client.emit(
|
|
31
|
+
"unregisterErrorHandler",
|
|
32
|
+
data,
|
|
33
|
+
"/events",
|
|
34
|
+
)
|
|
44
35
|
|
|
45
|
-
if local_error_handlers.get(path):
|
|
46
|
-
local_error_handlers[path].remove(callback)
|
|
47
36
|
|
|
48
|
-
|
|
37
|
+
async def unregister_all():
|
|
38
|
+
_, base_url = get_api_key_and_url()
|
|
39
|
+
# need to reconnect because maybe socketio client disconnected after Ctrl+C?
|
|
40
|
+
await client.connect(base_url, transports=["websocket"], namespaces=["/events"])
|
|
41
|
+
await asyncio.gather(*[unregister(handler) for handler in active_handlers])
|
|
49
42
|
|
|
50
|
-
return unregister
|
|
51
43
|
|
|
52
|
-
|
|
44
|
+
async def on(
|
|
45
|
+
path: str, callback: Callable, options: Optional[Dict[str, Any]] = None
|
|
46
|
+
) -> None:
|
|
47
|
+
print(f"starting error handler for {path}...")
|
|
48
|
+
|
|
49
|
+
if not client:
|
|
50
|
+
raise Exception("Client not initialized. Please call error_handler.prepare() first.")
|
|
51
|
+
|
|
52
|
+
api_key, _ = get_api_key_and_url()
|
|
53
|
+
handler_id = None
|
|
54
|
+
data = copy.deepcopy(options or {})
|
|
55
|
+
data["path"] = path
|
|
56
|
+
data["apiKey"] = api_key
|
|
57
|
+
|
|
58
|
+
def registerCallback(id: int):
|
|
59
|
+
nonlocal handler_id
|
|
60
|
+
handler_id = id
|
|
61
|
+
client.on(f"handleError:{handler_id}", callback, namespace="/events")
|
|
62
|
+
active_handlers.append({"path": path, "id": handler_id, "apiKey": api_key})
|
|
63
|
+
|
|
64
|
+
await client.emit("registerErrorHandler", data, "/events", registerCallback)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def start(*args):
|
|
68
|
+
loop = asyncio.get_event_loop()
|
|
69
|
+
loop.run_until_complete(get_client_and_connect())
|
|
70
|
+
asyncio.gather(*args)
|
|
71
|
+
|
|
72
|
+
try:
|
|
73
|
+
loop.run_forever()
|
|
74
|
+
except KeyboardInterrupt:
|
|
75
|
+
pass
|
|
76
|
+
finally:
|
|
77
|
+
loop.run_until_complete(unregister_all())
|
|
78
|
+
loop.stop()
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: polyapi-python
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.4.dev1
|
|
4
4
|
Summary: The PolyAPI Python Client
|
|
5
5
|
Author-email: Dan Fellin <dan@polyapi.io>
|
|
6
6
|
License: MIT License
|
|
@@ -36,6 +36,7 @@ Requires-Dist: stdlib-list ==0.10.0
|
|
|
36
36
|
Requires-Dist: colorama ==0.4.4
|
|
37
37
|
Requires-Dist: python-socketio[asyncio_client] ==5.11.1
|
|
38
38
|
Requires-Dist: truststore ==0.8.0
|
|
39
|
+
Requires-Dist: pyjwt ==2.3.0
|
|
39
40
|
|
|
40
41
|
# PolyAPI Python Library
|
|
41
42
|
|
|
@@ -6,7 +6,7 @@ polyapi/cli.py,sha256=xlKH4cSmSo7eXbyXCLWyL4rXM1QFsltC_MxoxMPgt6I,2187
|
|
|
6
6
|
polyapi/client.py,sha256=8k50Vwg9HnmHHTyfKH1vfMJqF0jnnVMsWuWI9AfASkM,761
|
|
7
7
|
polyapi/config.py,sha256=S8TU10upy5OW1_vX-CqQTJD-ZOB6329aMjiUCmukfUI,2292
|
|
8
8
|
polyapi/constants.py,sha256=NGjso6K5rGnE8TGdrXmdEfvvr-HI3DTVGwOYiWO68LM,511
|
|
9
|
-
polyapi/error_handler.py,sha256=
|
|
9
|
+
polyapi/error_handler.py,sha256=frP8-76DQx5n2hp3sBMt6586ibE4zryh2M-PvooScb0,2169
|
|
10
10
|
polyapi/exceptions.py,sha256=Zh7i7eCUhDuXEdUYjatkLFTeZkrx1BJ1P5ePgbJ9eIY,89
|
|
11
11
|
polyapi/execute.py,sha256=06XWTxGJqtsDiLY10RjRebIMFRfhtAIMmBRbuWu3e8A,1873
|
|
12
12
|
polyapi/function_cli.py,sha256=KkFvnGSSCwYPMV4cX4yMQmXKyLtgYR64Fz5BfjGv3qY,8230
|
|
@@ -18,8 +18,8 @@ polyapi/typedefs.py,sha256=a5WfHaAvjeql3y1iA3_SkpUztTbKvS5bPqkVxkCvr9E,1459
|
|
|
18
18
|
polyapi/utils.py,sha256=UD3uV3kzt2w23zxAdHCUROhdvtdO44KCJIpqnFwEhqE,5937
|
|
19
19
|
polyapi/variables.py,sha256=d36-trnfTL_8m2NkorMiImb4O3UrJbiFV38CHxV5i0A,4200
|
|
20
20
|
polyapi/webhook.py,sha256=A89eNnYcVpVe9doJPDLfscIhF-C7Q2AI3vu-SzGxMBg,2923
|
|
21
|
-
polyapi_python-0.2.
|
|
22
|
-
polyapi_python-0.2.
|
|
23
|
-
polyapi_python-0.2.
|
|
24
|
-
polyapi_python-0.2.
|
|
25
|
-
polyapi_python-0.2.
|
|
21
|
+
polyapi_python-0.2.4.dev1.dist-info/LICENSE,sha256=Hi0kDr56Dsy0uYIwNt4r9G7tI8x8miXRTlyvbeplCP8,1068
|
|
22
|
+
polyapi_python-0.2.4.dev1.dist-info/METADATA,sha256=nxu9RxaO-TN60gINVtxyGPqVVLGGSiV_MHGeNPGt190,4852
|
|
23
|
+
polyapi_python-0.2.4.dev1.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
|
24
|
+
polyapi_python-0.2.4.dev1.dist-info/top_level.txt,sha256=CEFllOnzowci_50RYJac-M54KD2IdAptFsayVVF_f04,8
|
|
25
|
+
polyapi_python-0.2.4.dev1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|