polyapi-python 0.2.0.dev9__py3-none-any.whl → 0.2.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.
polyapi/webhook.py CHANGED
@@ -4,7 +4,10 @@ from typing import Any, Dict, List, Tuple
4
4
  from polyapi.typedefs import PropertySpecification
5
5
 
6
6
  WEBHOOK_TEMPLATE = """
7
- async def {function_name}(callback, options=None):
7
+ import asyncio
8
+
9
+
10
+ def {function_name}(callback, options=None):
8
11
  \"""{description}
9
12
 
10
13
  Function ID: {function_id}
@@ -14,56 +17,60 @@ async def {function_name}(callback, options=None):
14
17
  function_id = "{function_id}"
15
18
 
16
19
  api_key, base_url = get_api_key_and_url()
17
- socket = socketio.AsyncClient()
18
- await socket.connect(base_url, transports=['websocket'], namespaces=['/events'])
19
-
20
- def registerCallback(registered: bool):
21
- nonlocal socket
22
- if registered:
23
- socket.on('handleWebhookEvent:{function_id}', handleEvent, namespace="/events")
24
- else:
25
- print("Could not set register webhook event handler for {function_id}")
26
-
27
- async def handleEvent(data):
28
- nonlocal api_key
29
- nonlocal options
30
- polyCustom = {{}}
31
- resp = await callback(data.get("body"), data.get("headers"), data.get("params"), polyCustom)
32
- if resp and options.get("waitForResponse"):
33
- await socket.emit('setWebhookListenerResponse', {{
34
- "webhookHandleID": function_id,
35
- "apiKey": api_key,
36
- "clientID": eventsClientId,
37
- "executionId": data.get("executionId"),
38
- "response": {{
39
- "data": resp,
40
- "statusCode": polyCustom.get("responseStatusCode", 200),
41
- "contentType": polyCustom.get("responseContentType", None),
42
- }},
43
- }}, namespace="/events")
44
20
 
45
- data = {{
46
- "clientID": eventsClientId,
47
- "webhookHandleID": function_id,
48
- "apiKey": api_key,
49
- "waitForResponse": options.get("waitForResponse"),
50
- }}
51
- await socket.emit('registerWebhookEventHandler', data, namespace="/events", callback=registerCallback)
21
+ async def _inner():
22
+ socket = socketio.AsyncClient()
23
+ await socket.connect(base_url, transports=['websocket'], namespaces=['/events'])
24
+
25
+ def registerCallback(registered: bool):
26
+ nonlocal socket
27
+ if registered:
28
+ socket.on('handleWebhookEvent:{function_id}', handleEvent, namespace="/events")
29
+ else:
30
+ print("Could not set register webhook event handler for {function_id}")
52
31
 
53
- async def closeEventHandler():
54
- nonlocal socket
55
- if not socket:
56
- return
32
+ async def handleEvent(data):
33
+ nonlocal api_key
34
+ nonlocal options
35
+ polyCustom = {{}}
36
+ resp = await callback(data.get("body"), data.get("headers"), data.get("params"), polyCustom)
37
+ if options.get("waitForResponse"):
38
+ await socket.emit('setWebhookListenerResponse', {{
39
+ "webhookHandleID": function_id,
40
+ "apiKey": api_key,
41
+ "clientID": eventsClientId,
42
+ "executionId": data.get("executionId"),
43
+ "response": {{
44
+ "data": resp,
45
+ "statusCode": polyCustom.get("responseStatusCode", 200),
46
+ "contentType": polyCustom.get("responseContentType", None),
47
+ }},
48
+ }}, namespace="/events")
57
49
 
58
- await socket.emit('unregisterWebhookEventHandler', {{
50
+ data = {{
59
51
  "clientID": eventsClientId,
60
52
  "webhookHandleID": function_id,
61
- "apiKey": api_key
62
- }}, namespace="/events")
53
+ "apiKey": api_key,
54
+ "waitForResponse": options.get("waitForResponse"),
55
+ }}
56
+ await socket.emit('registerWebhookEventHandler', data, namespace="/events", callback=registerCallback)
57
+
58
+ async def closeEventHandler():
59
+ nonlocal socket
60
+ if not socket:
61
+ return
62
+
63
+ await socket.emit('unregisterWebhookEventHandler', {{
64
+ "clientID": eventsClientId,
65
+ "webhookHandleID": function_id,
66
+ "apiKey": api_key
67
+ }}, namespace="/events")
68
+
69
+ await socket.wait()
63
70
 
64
- await socket.wait()
71
+ return closeEventHandler
65
72
 
66
- return closeEventHandler
73
+ return asyncio.run(_inner())
67
74
  """
68
75
 
69
76
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: polyapi-python
3
- Version: 0.2.0.dev9
3
+ Version: 0.2.1
4
4
  Summary: The PolyAPI Python Client
5
5
  Author-email: Dan Fellin <dan@polyapi.io>
6
6
  License: MIT License
@@ -15,9 +15,9 @@ polyapi/server.py,sha256=mcbH8pG16fFMiFL52wdii0217ZLsznQnElAFEvXZ7IA,2211
15
15
  polyapi/typedefs.py,sha256=RZ3I6sgJm_5MuuORG1QjUE-UJy_z2WRXNdiWjEdLvQg,1371
16
16
  polyapi/utils.py,sha256=wZQrwSOst5wyoYYLoMk1DnAEbVsJ0-2YkmmDQ9gI3FY,5399
17
17
  polyapi/variables.py,sha256=d36-trnfTL_8m2NkorMiImb4O3UrJbiFV38CHxV5i0A,4200
18
- polyapi/webhook.py,sha256=d-qCnZs5d8m4x4YX-QSsvwAwWWD4ZIyhVcMFlXFolCk,2690
19
- polyapi_python-0.2.0.dev9.dist-info/LICENSE,sha256=Hi0kDr56Dsy0uYIwNt4r9G7tI8x8miXRTlyvbeplCP8,1068
20
- polyapi_python-0.2.0.dev9.dist-info/METADATA,sha256=vDqk474YSS3jOw04armGAxCgeUM8qmBuOnk7F9KCMWA,4823
21
- polyapi_python-0.2.0.dev9.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
22
- polyapi_python-0.2.0.dev9.dist-info/top_level.txt,sha256=CEFllOnzowci_50RYJac-M54KD2IdAptFsayVVF_f04,8
23
- polyapi_python-0.2.0.dev9.dist-info/RECORD,,
18
+ polyapi/webhook.py,sha256=A89eNnYcVpVe9doJPDLfscIhF-C7Q2AI3vu-SzGxMBg,2923
19
+ polyapi_python-0.2.1.dist-info/LICENSE,sha256=Hi0kDr56Dsy0uYIwNt4r9G7tI8x8miXRTlyvbeplCP8,1068
20
+ polyapi_python-0.2.1.dist-info/METADATA,sha256=jn5GF02l2EVgP9NW5D1axfkqnCKnJWi5VnHjngfx8ts,4818
21
+ polyapi_python-0.2.1.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
22
+ polyapi_python-0.2.1.dist-info/top_level.txt,sha256=CEFllOnzowci_50RYJac-M54KD2IdAptFsayVVF_f04,8
23
+ polyapi_python-0.2.1.dist-info/RECORD,,