polyapi-python 0.2.0.dev8__tar.gz → 0.2.1.dev0__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.
Files changed (35) hide show
  1. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/PKG-INFO +1 -1
  2. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/polyapi/auth.py +26 -5
  3. polyapi-python-0.2.1.dev0/polyapi/webhook.py +89 -0
  4. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/polyapi_python.egg-info/PKG-INFO +1 -1
  5. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/pyproject.toml +1 -1
  6. polyapi-python-0.2.0.dev8/polyapi/webhook.py +0 -85
  7. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/LICENSE +0 -0
  8. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/README.md +0 -0
  9. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/polyapi/__init__.py +0 -0
  10. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/polyapi/__main__.py +0 -0
  11. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/polyapi/api.py +0 -0
  12. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/polyapi/cli.py +0 -0
  13. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/polyapi/config.py +0 -0
  14. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/polyapi/constants.py +0 -0
  15. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/polyapi/exceptions.py +0 -0
  16. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/polyapi/execute.py +0 -0
  17. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/polyapi/function_cli.py +0 -0
  18. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/polyapi/generate.py +0 -0
  19. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/polyapi/py.typed +0 -0
  20. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/polyapi/schema.py +0 -0
  21. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/polyapi/server.py +0 -0
  22. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/polyapi/typedefs.py +0 -0
  23. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/polyapi/utils.py +0 -0
  24. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/polyapi/variables.py +0 -0
  25. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/polyapi_python.egg-info/SOURCES.txt +0 -0
  26. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/polyapi_python.egg-info/dependency_links.txt +0 -0
  27. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/polyapi_python.egg-info/requires.txt +0 -0
  28. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/polyapi_python.egg-info/top_level.txt +0 -0
  29. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/setup.cfg +0 -0
  30. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/tests/test_api.py +0 -0
  31. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/tests/test_auth.py +0 -0
  32. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/tests/test_function_cli.py +0 -0
  33. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/tests/test_server.py +0 -0
  34. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/tests/test_utils.py +0 -0
  35. {polyapi-python-0.2.0.dev8 → polyapi-python-0.2.1.dev0}/tests/test_variables.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: polyapi-python
3
- Version: 0.2.0.dev8
3
+ Version: 0.2.1.dev0
4
4
  Summary: The PolyAPI Python Client
5
5
  Author-email: Dan Fellin <dan@polyapi.io>
6
6
  License: MIT License
@@ -15,6 +15,12 @@ GET_TOKEN_TEMPLATE = """
15
15
  import asyncio
16
16
 
17
17
 
18
+ class AuthFunctionResponse(TypedDict):
19
+ status: int
20
+ data: Any
21
+ headers: Dict[str, str]
22
+
23
+
18
24
  async def getToken(clientId: str, clientSecret: str, scopes: List[str], callback, options: Optional[Dict[str, Any]] = None):
19
25
  \"""{description}
20
26
 
@@ -102,27 +108,40 @@ async def getToken(clientId: str, clientSecret: str, scopes: List[str], callback
102
108
  return {{"close": closeEventHandler}}
103
109
  """
104
110
 
111
+ INTROSPECT_TOKEN_TEMPLATE = """
112
+ def introspectToken(token: str) -> AuthFunctionResponse:
113
+ \"""{description}
114
+
115
+ Function ID: {function_id}
116
+ \"""
117
+ url = "/auth-providers/{function_id}/introspect"
118
+ resp = execute_post(url, {{"token": token}})
119
+ return resp.json()
120
+ """
121
+
105
122
  REFRESH_TOKEN_TEMPLATE = """
106
- def refreshToken(token: str) -> str:
123
+ def refreshToken(token: str) -> AuthFunctionResponse:
107
124
  \"""{description}
108
125
 
109
126
  Function ID: {function_id}
110
127
  \"""
111
128
  url = "/auth-providers/{function_id}/refresh"
112
129
  resp = execute_post(url, {{"token": token}})
113
- assert resp.status_code == 201, (resp.status_code, resp.content)
114
- return resp.text
130
+ return resp.json()
115
131
  """
116
132
 
117
133
  REVOKE_TOKEN_TEMPLATE = """
118
- def revokeToken(token: str) -> None:
134
+ def revokeToken(token: str) -> Optional[AuthFunctionResponse]:
119
135
  \"""{description}
120
136
 
121
137
  Function ID: {function_id}
122
138
  \"""
123
139
  url = "/auth-providers/{function_id}/revoke"
124
140
  resp = execute_post(url, {{"token": token}})
125
- assert resp.status_code == 201, (resp.status_code, resp.content)
141
+ try:
142
+ return resp.json()
143
+ except:
144
+ return None
126
145
  """
127
146
 
128
147
 
@@ -147,6 +166,8 @@ def render_auth_function(
147
166
 
148
167
  if function_name == "getToken":
149
168
  func_str = GET_TOKEN_TEMPLATE.format(function_id=function_id, description=function_description, client_id=uuid.uuid4().hex)
169
+ elif function_name == "introspectToken":
170
+ func_str = INTROSPECT_TOKEN_TEMPLATE.format(function_id=function_id, description=function_description)
150
171
  elif function_name == "refreshToken":
151
172
  func_str = REFRESH_TOKEN_TEMPLATE.format(function_id=function_id, description=function_description)
152
173
  elif function_name == "revokeToken":
@@ -0,0 +1,89 @@
1
+ import uuid
2
+ from typing import Any, Dict, List, Tuple
3
+
4
+ from polyapi.typedefs import PropertySpecification
5
+
6
+ WEBHOOK_TEMPLATE = """
7
+ def {function_name}(callback, options=None):
8
+ \"""{description}
9
+
10
+ Function ID: {function_id}
11
+ \"""
12
+ options = options or {{}}
13
+ eventsClientId = "{client_id}"
14
+ function_id = "{function_id}"
15
+
16
+ api_key, base_url = get_api_key_and_url()
17
+
18
+ async def _inner():
19
+ socket = socketio.AsyncClient()
20
+ await socket.connect(base_url, transports=['websocket'], namespaces=['/events'])
21
+
22
+ def registerCallback(registered: bool):
23
+ nonlocal socket
24
+ if registered:
25
+ socket.on('handleWebhookEvent:{function_id}', handleEvent, namespace="/events")
26
+ else:
27
+ print("Could not set register webhook event handler for {function_id}")
28
+
29
+ async def handleEvent(data):
30
+ nonlocal api_key
31
+ nonlocal options
32
+ polyCustom = {{}}
33
+ resp = await callback(data.get("body"), data.get("headers"), data.get("params"), polyCustom)
34
+ if resp and options.get("waitForResponse"):
35
+ await socket.emit('setWebhookListenerResponse', {{
36
+ "webhookHandleID": function_id,
37
+ "apiKey": api_key,
38
+ "clientID": eventsClientId,
39
+ "executionId": data.get("executionId"),
40
+ "response": {{
41
+ "data": resp,
42
+ "statusCode": polyCustom.get("responseStatusCode", 200),
43
+ "contentType": polyCustom.get("responseContentType", None),
44
+ }},
45
+ }}, namespace="/events")
46
+
47
+ data = {{
48
+ "clientID": eventsClientId,
49
+ "webhookHandleID": function_id,
50
+ "apiKey": api_key,
51
+ "waitForResponse": options.get("waitForResponse"),
52
+ }}
53
+ await socket.emit('registerWebhookEventHandler', data, namespace="/events", callback=registerCallback)
54
+
55
+ async def closeEventHandler():
56
+ nonlocal socket
57
+ if not socket:
58
+ return
59
+
60
+ await socket.emit('unregisterWebhookEventHandler', {{
61
+ "clientID": eventsClientId,
62
+ "webhookHandleID": function_id,
63
+ "apiKey": api_key
64
+ }}, namespace="/events")
65
+
66
+ await socket.wait()
67
+
68
+ return closeEventHandler
69
+
70
+ return asyncio.run(inner())
71
+ """
72
+
73
+
74
+ def render_webhook_handle(
75
+ function_type: str,
76
+ function_name: str,
77
+ function_id: str,
78
+ function_description: str,
79
+ arguments: List[PropertySpecification],
80
+ return_type: Dict[str, Any],
81
+ ) -> Tuple[str, str]:
82
+ func_str = WEBHOOK_TEMPLATE.format(
83
+ description=function_description,
84
+ client_id=uuid.uuid4().hex,
85
+ function_id=function_id,
86
+ function_name=function_name,
87
+ )
88
+
89
+ return func_str, ""
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: polyapi-python
3
- Version: 0.2.0.dev8
3
+ Version: 0.2.1.dev0
4
4
  Summary: The PolyAPI Python Client
5
5
  Author-email: Dan Fellin <dan@polyapi.io>
6
6
  License: MIT License
@@ -3,7 +3,7 @@ requires = ["setuptools>=61.2", "wheel"]
3
3
 
4
4
  [project]
5
5
  name = "polyapi-python"
6
- version = "0.2.0.dev8"
6
+ version = "0.2.1.dev0"
7
7
  description = "The PolyAPI Python Client"
8
8
  authors = [{ name = "Dan Fellin", email = "dan@polyapi.io" }]
9
9
  dependencies = [
@@ -1,85 +0,0 @@
1
- import uuid
2
- from typing import Any, Dict, List, Tuple
3
-
4
- from polyapi.typedefs import PropertySpecification
5
-
6
- WEBHOOK_TEMPLATE = """
7
- async def {function_name}(callback, options=None):
8
- \"""{description}
9
-
10
- Function ID: {function_id}
11
- \"""
12
- options = options or {{}}
13
- eventsClientId = "{client_id}"
14
- function_id = "{function_id}"
15
-
16
- 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
-
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)
52
-
53
- async def closeEventHandler():
54
- nonlocal socket
55
- if not socket:
56
- return
57
-
58
- await socket.emit('unregisterWebhookEventHandler', {{
59
- "clientID": eventsClientId,
60
- "webhookHandleID": function_id,
61
- "apiKey": api_key
62
- }}, namespace="/events")
63
-
64
- await socket.wait()
65
-
66
- return closeEventHandler
67
- """
68
-
69
-
70
- def render_webhook_handle(
71
- function_type: str,
72
- function_name: str,
73
- function_id: str,
74
- function_description: str,
75
- arguments: List[PropertySpecification],
76
- return_type: Dict[str, Any],
77
- ) -> Tuple[str, str]:
78
- func_str = WEBHOOK_TEMPLATE.format(
79
- description=function_description,
80
- client_id=uuid.uuid4().hex,
81
- function_id=function_id,
82
- function_name=function_name,
83
- )
84
-
85
- return func_str, ""