-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathapi.py
90 lines (69 loc) · 2.26 KB
/
api.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
import requests
def create_room(endpoint, token=None, scene_name=None, scene_id=None):
payload = {}
if scene_id:
payload = {
"hub": {
"name": scene_name,
"scene_id": scene_id
}
}
headers = {}
if token:
headers = {
"content-type": "application/json",
"authorization": f'Bearer {token}'
}
import json
body = json.dumps(payload)
url = f'{endpoint}/api/v1/hubs'
resp = requests.post(url, body, headers=headers)
return resp.json()
def upload_media(endpoint, file):
resp = requests.post(f'{endpoint}/api/v1/media', files={'media': (
'glb', file, 'application/octet-stream')}, verify=False)
json = resp.json()
if "error" in json:
raise Exception(f'Unknown error')
return {
"file_id": json.get("file_id"),
"access_token": json.get("meta").get("access_token")
}
def publish_scene(endpoint, token, scene_data, scene_id=None):
headers = {
"content-type": "application/json",
"authorization": f'Bearer {token}'
}
import json
body = json.dumps({"scene": scene_data})
url = f'{endpoint}/api/v1/scenes{"/" + scene_id if scene_id else ""}'
if scene_id:
resp = requests.put(url, body, headers=headers)
else:
resp = requests.post(url, body, headers=headers)
jsonFile = resp.json()
if "error" in jsonFile:
error = jsonFile.get("error")
if error == "invalid_token":
raise Exception("Authentication error")
else:
raise Exception(f'Unknown error: {error}')
return jsonFile
def get_projects(endpoint, token):
headers = {
"content-type": "application/json",
"authorization": f'Bearer {token}'
}
resp = requests.get(
f'{endpoint}/api/v1/scenes/projectless', headers=headers)
jsonFile = resp.json()
if "error" in jsonFile:
error = jsonFile.get("error")
if error == "invalid_token":
raise Exception("Authentication error")
else:
raise Exception(f'Unknown error: {error}')
if "scenes" not in jsonFile:
raise Exception(f'Projects request error')
scenes = jsonFile.get("scenes")
return scenes