-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhbclass.py
161 lines (143 loc) · 4.97 KB
/
hbclass.py
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import requests
import json
class HomeboxApi:
def __init__(self,user,password,base_url):
self.user = user
self.password = password
self.base_url = base_url
self.token = self.login()
self.payload = {}
def login(self):
api = "users/login"
url = self.base_url + api
payload = f'username={self.user}&password={self.password}'
headers = {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
}
response = requests.request("POST", url, headers=headers, data=payload)
hbresponse = json.loads(response.text)
token = hbresponse['token']
#print(token)
return token
def get_location(self):
api = "locations"
method = "GET"
json_data = ""
locations = self.hb_post(method,api,json_data)
return locations
def get_location_by_id(self,location_id):
api = f"locations/{location_id}"
method = "GET"
json_data = ""
locations = self.hb_post(method,api,json_data)
return locations
def get_items(self):
api = "items"
method = "GET"
json_data = ""
items = self.hb_post(method,api,json_data)
return items
def get_item_by_id(self,item_id):
api = f"items/{item_id}"
method = "GET"
json_data = ""
items = self.hb_post(method,api,json_data)
return items
def create_location(self,location):
api = "locations"
method = "POST"
json_data = {
"description": None,
"name": f"{location}",
"parentId": None
}
location_id = self.hb_post(method,api,json_data)
return location_id
def create_item(self,location_id,name):
api = "items"
method = "POST"
json_data = {
'description': None,
'labelIds': [],
'locationId': f'{location_id}',
'name': f'{name}',
'parentId': None,
}
item_id = self.hb_post(method,api,json_data)
return item_id
def update_item(self,parent_item_id,item_id,location_id,name):
api = f"items/{item_id}"
method = "PUT"
json_data = {
"id":f"{item_id}",
"name":f"{name}",
"locationId":f"{location_id}",
"parentId":f"{parent_item_id}"
}
item_id = self.hb_post(method,api,json_data)
return item_id
def get_labels(self):
api = "labels"
method = "GET"
json_data = ""
labels = self.hb_post(method,api,json_data)
return labels
def create_label(self,name):
api = "labels"
method = "POST"
json_data = {
"name": f"{name}"
}
label_id = self.hb_post(method,api,json_data)
return label_id
def update_item_label(self,parent_item_id,item_id,location_id,name,labels,labelids):
api = f"items/{item_id}"
method = "PUT"
json_data = {
"id":f"{item_id}",
"name":f"{name}",
#"labels":[{"id":"e79a3812-54a4-4aff-abc6-b36e75f0f07c","name":"VSlabel"},{"id":"7d20699f-1fae-4cc4-acb4-f50df48e248b","name":"Feest"}],
"labels":labels,
"locationId":f"{location_id}",
"parentId":f"{parent_item_id}",
#"labelIds": ["e79a3812-54a4-4aff-abc6-b36e75f0f07c","7d20699f-1fae-4cc4-acb4-f50df48e248b"]
"labelIds": labelids
}
#"labelIds": ["string"],
#print(f"self.hb_post({method},{api},{json_data}")
item_id = self.hb_post(method,api,json_data)
return item_id
def upload_photo(self,item_id,item,item_location):
api = f"items/{item_id}/attachments"
fp = f"{item_location}/{item}"
method = "POST"
files_data = {
'file': (f'{item}', open(fp, 'rb'), 'image/jpeg'),
'type': (None, 'photo'),
'name': (None, f'{item}'),
}
upload_post = self.hb_post(method,api,files_data)
def hb_post(self,method,api,json_data):
url = self.base_url + api
headers = {
'Authorization': f'{self.token}',
'accept': 'application/json',
'Content-Type': 'application/json',
}
if "attachments" in api:
headers = {
'accept': 'application/json',
'Authorization': f'{self.token}',
}
response = requests.request(method, url, headers=headers, files=json_data)
else:
if method == "GET":
response = requests.request(method,url, headers=headers)
response = json.loads(response.text)
return response
else:
response = requests.request(method,url, headers=headers, json=json_data)
item = json.loads(response.text)
item_id = item['id']
return item_id