-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQonicApi.py
More file actions
315 lines (255 loc) · 13.7 KB
/
Copy pathQonicApi.py
File metadata and controls
315 lines (255 loc) · 13.7 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import os
import uuid
from typing import Dict, List, Optional, Any, Iterable
import requests
from QonicApiLib import QonicApiError, ModificationInputError, ProductFilter
from oauth import login
class QonicApi:
def __init__(self):
self.base_url = os.getenv("QONIC_API_URL", "https://api.qonic.com/v1/").rstrip("/") + "/"
self.session = requests.Session()
self.session_id = self.new_session_id()
self.access_token = None
def _url(self, path: str) -> str:
path = path.lstrip("/")
return self.base_url + path
def _headers(self) -> Dict[str, str]:
return {
"Authorization": f"Bearer {self.access_token}",
"Accept": "application/json",
"X-Client-Session-Id": self.session_id
}
def _request(
self,
method: str,
path: str,
*,
params: Dict[str, Any] | None = None,
json: Any = None,
data: Any = None,
allow_redirects: bool = True,
) -> requests.Response:
url = self._url(path)
resp = self.session.request(
method,
url,
params=params,
json=json,
data=data,
headers=self._headers(),
allow_redirects=allow_redirects,
)
if not resp.ok:
raise QonicApiError(resp)
return resp
def get(self, path: str, **kwargs) -> Any:
resp = self._request("GET", path, **kwargs)
return resp.json()
def _post(self, path: str, **kwargs) -> Any:
resp = self._request("POST", path, **kwargs)
if resp.content:
try:
return resp.json()
except ValueError:
return resp.text
return None
def _delete(self, path: str, **kwargs) -> Any:
resp = self._request("DELETE", path, **kwargs)
if resp.content:
try:
return resp.json()
except ValueError:
return resp.text
return None
def _put(self, path: str, **kwargs) -> Any:
resp = self._request("PUT", path, **kwargs)
if resp.content:
try:
return resp.json()
except ValueError:
return resp.text
return None
def authorize(self):
self.access_token = login()["access_token"]
def list_projects(self) -> List[Any]:
return self.get("projects").get("projects", [])
def list_models(self, project_id: str) -> List[Any]:
return self.get(f"projects/{project_id}/models").get("models", [])
def get_available_product_fields(self, project_id: str, model_id: str) -> List[str]:
return self.get(f"projects/{project_id}/models/{model_id}/products/properties/available-data").get("fields", [])
def query_products(
self,
project_id: str,
model_id: str,
fields: Iterable[str],
filters: Iterable[ProductFilter] | None = None
) -> Dict[str, Any]:
body = {
"fields": list(fields),
"filters": filters or {},
}
return self._post(f"projects/{project_id}/models/{model_id}/products/properties/query", json=body).get("result", {})
def calculate_quantities(self, project_id: str, model_id: str, calculators: Iterable[str],
filters: Iterable[ProductFilter] | None = None) -> Dict[str, Any]:
body = {
"calculators": list(calculators),
"filters": filters or {},
}
return self._post(f"projects/{project_id}/models/{model_id}/products/quantities/query", json=body)
def get_quantities_result_url(self, project_id: str, model_id: str, operation_id: str) -> str:
resp = self._request(
"GET",
f"projects/{project_id}/models/{model_id}/products/quantities/{operation_id}/result",
allow_redirects=False,
)
location = resp.headers.get("Location")
if not location:
raise QonicApiError(resp)
return location
def get_operation(self, operation_id: str) -> Dict[str, Any]:
return self.get(f"operations/{operation_id}")
@staticmethod
def new_session_id() -> str:
return str(uuid.uuid4())
def start_session(self, project_id: str, model_id: str) -> None:
self.session_id = self.new_session_id()
self._post(f"projects/{project_id}/models/{model_id}/start-session")
def end_session(self, project_id: str, model_id: str) -> None:
self._post(f"projects/{project_id}/models/{model_id}/end-session")
def modify_products(self, project_id: str, model_id: str, changes: Dict[str, Any]) -> List[ModificationInputError]:
result = self._post(
f"projects/{project_id}/models/{model_id}/products",
json=changes,
)
errors_json = result.get("errors", []) if isinstance(result, dict) else []
return [ModificationInputError(**e) for e in errors_json]
def delete_product(self, project_id: str, model_id: str, guid: str) -> None:
self._delete(f"projects/{project_id}/models/{model_id}/products/{guid}")
def publish_changes(self, project_id: str, model_id: str, title: Optional[str] = None, description: Optional[str] = None) -> None:
body = {
"title": title,
"description": description,
}
self._post(f"projects/{project_id}/models/{model_id}/publish", json=body)
def discard_changes(self, project_id: str, model_id: str) -> None:
self._post(f"projects/{project_id}/models/{model_id}/discard")
def get_upload_url(self) -> str:
data = self.get("upload-url")
return data["uploadUrl"]
def create_model(
self,
project_id: str,
*,
model_name: str,
upload_url: str,
upload_file_name: str,
tags: Optional[List[str]] = None,
default_role: Optional[str] = None,
) -> Dict[str, Any]:
body = {
"modelName": model_name,
"uploadUrl": upload_url,
"uploadFileName": upload_file_name,
}
if tags is not None:
body["tags"] = tags
if default_role is not None:
body["defaultRole"] = default_role
return self._post(f"projects/{project_id}/models", json=body)
def start_export_ifc(self, project_id: str, model_id: str) -> Dict[str, Any]:
return self._post(f"projects/{project_id}/models/{model_id}/export-ifc")
def get_export_ifc_result_url(self, project_id: str, model_id: str, operation_id: str) -> str:
resp = self._request(
"GET",
f"projects/{project_id}/models/{model_id}/export-ifc/{operation_id}/result",
allow_redirects=False,
)
location = resp.headers.get("Location")
if not location:
raise QonicApiError(resp)
return location
def list_codification_libraries(self, project_id: str) -> List[Dict[str, Any]]:
data = self.get(f"projects/{project_id}/codifications")
return data.get("codificationLibraries", []) if isinstance(data, dict) else []
def create_codification_library(self, project_id: str, library_properties: Dict[str, Any]) -> Dict[str, Any]:
result = self._post(f"projects/{project_id}/codifications", json=library_properties)
return result if isinstance(result, dict) else {}
def get_codification_library(self, project_id: str, library_guid: str) -> Dict[str, Any]:
data = self.get(f"projects/{project_id}/codifications/{library_guid}")
return data if isinstance(data, dict) else {}
def delete_codification_library(self, project_id: str, library_guid: str) -> None:
self._delete(f"projects/{project_id}/codifications/{library_guid}")
def create_classification_code(self, project_id: str, library_guid: str, code: Dict[str, Any]) -> Dict[str, Any]:
result = self._post(f"projects/{project_id}/codifications/{library_guid}/codification", json=code)
return result if isinstance(result, dict) else {}
def update_classification_code(self, project_id: str, library_guid: str, codification_guid: str,
changes: Dict[str, Any]) -> None:
self._put(f"projects/{project_id}/codifications/{library_guid}/codification/{codification_guid}",
json=changes, )
def delete_classification_code(self, project_id: str, library_guid: str, codification_guid: str) -> None:
self._delete(f"projects/{project_id}/codifications/{library_guid}/codification/{codification_guid}")
def get_custom_properties(self, project_id: str) -> Dict[str, Any]:
data = self.get(f"projects/{project_id}/customProperties")
return data if isinstance(data, dict) else {}
def create_property_set(self, project_id: str, property_set: Dict[str, Any]) -> Dict[str, Any]:
result = self._post(f"projects/{project_id}/customProperties/property-sets", json=property_set)
return result if isinstance(result, dict) else {}
def update_property_set(self, project_id: str, property_set_id: int | str, changes: Dict[str, Any]) -> None:
self._put(f"projects/{project_id}/customProperties/property-sets/{property_set_id}", json=changes)
def delete_property_set(self, project_id: str, property_set_id: int | str) -> None:
self._delete(f"projects/{project_id}/customProperties/property-sets/{property_set_id}")
def add_property_definition(self, project_id: str, property_set_id: int | str, definition: Dict[str, Any]) -> Dict[str, Any]:
result = self._post(f"projects/{project_id}/customProperties/property-sets/{property_set_id}/property",
json=definition)
return result if isinstance(result, dict) else {}
def update_property_definition(self, project_id: str, property_set_id: int | str, property_definition_id: int | str,
changes: Dict[str, Any]) -> Dict[str, Any]:
result = self._put(
f"projects/{project_id}/customProperties/property-sets/{property_set_id}/property/{property_definition_id}",
json=changes)
return result if isinstance(result, dict) else {}
def delete_property_definition(self, project_id: str, property_set_id: int | str,
property_definition_id: int | str) -> None:
self._delete(
f"projects/{project_id}/customProperties/property-sets/{property_set_id}/property/{property_definition_id}")
def get_material_overview(self, project_id: str) -> Dict[str, Any]:
data = self.get(f"projects/{project_id}/material-libraries")
return data if isinstance(data, dict) else {}
def get_material_library(self, project_id: str, library_guid: str) -> Dict[str, Any]:
data = self.get(f"projects/{project_id}/material-libraries/{library_guid}")
return data if isinstance(data, dict) else {}
def create_material(self, project_id: str, library_guid: str, material: Dict[str, Any], ) -> Dict[str, Any]:
result = self._post(f"projects/{project_id}/material-libraries/{library_guid}/materials", json=material)
return result if isinstance(result, dict) else {}
def update_material(self, project_id: str, library_guid: str, material_guid: str, material: Dict[str, Any], ) -> None:
self._put(f"projects/{project_id}/material-libraries/{library_guid}/materials/{material_guid}", json=material)
def delete_material(self, project_id: str, library_guid: str, material_guid: str) -> None:
self._delete(f"projects/{project_id}/material-libraries/{library_guid}/materials/{material_guid}")
def create_material_library(self, project_id: str, library_properties: Dict[str, Any]) -> Dict[str, Any]:
result = self._post(f"projects/{project_id}/material-libraries", json=library_properties)
return result if isinstance(result, dict) else {}
def delete_material_library(self, project_id: str, library_guid: str) -> None:
self._delete(f"projects/{project_id}/material-libraries/{library_guid}")
def get_locations(self, project_id: str) -> List[Dict[str, Any]]:
data = self.get(f"projects/{project_id}/locations")
if isinstance(data, dict):
return data.get("locationViews", [])
return []
def create_location(self, project_id: str, properties: Dict[str, Any]) -> Dict[str, Any]:
result = self._post(f"projects/{project_id}/locations", json=properties)
return result if isinstance(result, dict) else {}
def update_location(self, project_id: str, location_guid: str, properties: Dict[str, Any]) -> Dict[str, Any]:
result = self._put(f"projects/{project_id}/locations/{location_guid}", json=properties)
return result if isinstance(result, dict) else {}
def delete_location(self, project_id: str, location_guid: str) -> None:
self._delete(f"projects/{project_id}/locations/{location_guid}")
def get_types(self, project_id: str) -> Dict[str, Any]:
data = self.get(f"projects/{project_id}/types")
return data if isinstance(data, dict) else {}
def create_type(self, project_id: str, library_guid: str, type_item: Dict[str, Any]) -> Dict[str, Any]:
result = self._post(f"projects/{project_id}/types/{library_guid}", json=type_item)
return result if isinstance(result, dict) else {}
def update_type(self, project_id: str, library_guid: str, type_guid: str, changes: Dict[str, Any]) -> None:
self._put(f"projects/{project_id}/types/{library_guid}/types/{type_guid}", json=changes)
def delete_type(self, project_id: str, library_guid: str, type_guid: str) -> None:
self._delete(f"projects/{project_id}/types/{library_guid}/types/{type_guid}")