-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexist_client.py
More file actions
executable file
·122 lines (106 loc) · 3.98 KB
/
exist_client.py
File metadata and controls
executable file
·122 lines (106 loc) · 3.98 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/python
"""
Very basic client for Exist.io. Code mostly stolen from https://developer.exist.io/guide/write_client/
"""
from enum import IntEnum
import json
from pprint import PrettyPrinter
import requests
SECRETS = json.load(open('secrets.json'))
# Constants to represent Exist's value types
class ValueType(IntEnum):
QUANTITY = 0
DECIMAL = 1
STRING = 2
DURATION = 3
TIMEOFDAY = 4
PERCENTAGE = 5
BOOLEAN = 7
SCALE = 8
def acquire_attribute(token : str, attribute : str):
# make the json string to send to Exist
body = json.dumps([{'template': attribute, 'manual': False}])
# make the POST request, sending the json as the POST body
# we need a content-type header so Exist knows it's json
response = requests.post("https://exist.io/api/2/attributes/acquire/",
data=body,
headers={'Authorization':f'Bearer {token}',
'Content-type':'application/json'})
if response.status_code == 200:
# a 200 status code indicates a successful outcome
print("Acquired successfully.")
else:
# print the error if something went wrong
data = response.json()
print("Error:", data)
def make_update(attribute : str, date : str, value):
return {'name': attribute, 'date': date, 'value': value}
def update_attributes(updates):
# make the json string to send to Exist
while len(updates) > 36:
# Exist only allows 36 updates per request
# so we need to split the updates into chunks
chunk = updates[:36]
updates = updates[36:]
body = json.dumps(chunk)
# make the POST request, sending the json as the POST body
# we need a content-type header so Exist knows it's json
response = requests.post("https://exist.io/api/2/attributes/update/",
data = body,
headers = {
"Authorization" : f"Bearer {SECRETS['developerAccessToken']}",
"Content-type" : "application/json"
})
if response.status_code == 200:
# a 200 status code indicates a successful outcome
print("Updated successfully.")
else:
# print the error if something went wrong
data = response.json()
print("Error:", data)
def create_attribute(label, value_type, group, subgroup, manual):
# make the json string to send to Exist
body = json.dumps([
{'label': label,
'value_type': value_type,
'group': group,
'subgroup' : subgroup,
'manual': manual
}])
# make the POST request, sending the json as the POST body
# we need a content-type header so Exist knows it's json
response = requests.post("https://exist.io/api/2/attributes/create/",
data = body,
headers = {
"Authorization" : f"Bearer {SECRETS['developerAccessToken']}",
"Content-type" : "application/json"
})
if response.status_code == 200:
# a 200 status code indicates a successful outcome
# let's get out the full version of our attribute
data = response.json()
obj = data['success'][0]
print("Created successfully:")
# and let's print it (nicely) so we can see its fields
pp = PrettyPrinter()
pp.pprint(obj)
else:
# print the error if something went wrong
data = response.json()
print("Error:", data)
def list_attributes(limit=None):
"""Get list of all user attributes from Exist.io"""
url = "https://exist.io/api/2/attributes/?owned=true&groups=custom"
if limit:
url += f"&limit={limit}"
response = requests.get(url,
headers={
"Authorization": f"Bearer {SECRETS['developerAccessToken']}",
"Content-type": "application/json"
})
if response.status_code == 200:
data = response.json()
return data.get('results', [])
else:
print("Error fetching attributes:", response.json())
return []