-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecute.py
More file actions
48 lines (40 loc) · 1.9 KB
/
execute.py
File metadata and controls
48 lines (40 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import requests
from requests import Response
from polyapi.config import get_api_key_and_url
from polyapi.exceptions import PolyApiException
def execute(function_type, function_id, data) -> Response:
""" execute a specific function id/type
"""
api_key, api_url = get_api_key_and_url()
headers = {"Authorization": f"Bearer {api_key}"}
url = f"{api_url}/functions/{function_type}/{function_id}/execute"
resp = requests.post(url, json=data, headers=headers)
# print(resp.status_code)
# print(resp.headers["content-type"])
if resp.status_code < 200 or resp.status_code >= 300:
error_content = resp.content.decode("utf-8", errors="ignore")
raise PolyApiException(f"{resp.status_code}: {error_content}")
return resp
def execute_post(path, data):
api_key, api_url = get_api_key_and_url()
headers = {"Authorization": f"Bearer {api_key}"}
resp = requests.post(api_url + path, json=data, headers=headers)
return resp
def variable_get(variable_id: str) -> Response:
api_key, base_url = get_api_key_and_url()
headers = {"Authorization": f"Bearer {api_key}"}
url = f"{base_url}/variables/{variable_id}/value"
resp = requests.get(url, headers=headers)
if resp.status_code != 200 and resp.status_code != 201:
error_content = resp.content.decode("utf-8", errors="ignore")
raise PolyApiException(f"{resp.status_code}: {error_content}")
return resp
def variable_update(variable_id: str, value) -> Response:
api_key, base_url = get_api_key_and_url()
headers = {"Authorization": f"Bearer {api_key}"}
url = f"{base_url}/variables/{variable_id}"
resp = requests.patch(url, data={"value": value}, headers=headers)
if resp.status_code != 200 and resp.status_code != 201:
error_content = resp.content.decode("utf-8", errors="ignore")
raise PolyApiException(f"{resp.status_code}: {error_content}")
return resp