-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathAPSDataManagement.ts
More file actions
348 lines (307 loc) · 10 KB
/
Copy pathAPSDataManagement.ts
File metadata and controls
348 lines (307 loc) · 10 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import { Mutex } from "async-mutex"
import EventSystem from "@/systems/EventSystem.ts"
import { globalAddToast } from "@/ui/components/GlobalUIControls"
import APS from "./APS"
export const FOLDER_DATA_TYPE = "folders"
export const ITEM_DATA_TYPE = "items"
let mirabufFiles: Data[] | undefined
const mirabufFilesMutex: Mutex = new Mutex()
export class APSDataError extends Error {
errorCode: string
title: string
detail: string
constructor(errorCode: string, title: string, detail: string) {
super(title)
this.name = "APSDataError"
this.errorCode = errorCode
this.title = title
this.detail = detail
}
}
export type APSHubError = {
Id: string | null
HttpStatusCode: string
ErrorCode: string
Title: string
Detail: string
AboutLink: string | null
Source: string | null
meta: object | null
}
export type Filter = {
fieldName: string
matchValue: string
}
export interface Hub {
id: string
name: string
}
export interface Project {
id: string
name: string
folder: Folder
}
export type DataAttributes = {
name: string
displayName?: string
versionNumber?: number
fileType?: string
}
export type Relationships = {
storage: { meta: { link: { href?: string } } }
parent: { data: { id?: string } }
rootFolder: { data: RawData }
}
export type RawData = Omit<{ [key in keyof Data]: Data[key] }, "raw" | "href"> & { relationships: Relationships }
export class Data {
id: string
type: string
attributes: DataAttributes
href: string | undefined
raw: { [k: string]: unknown }
constructor(x: RawData) {
this.id = x.id
this.type = x.type
this.attributes = x.attributes
this.raw = x
this.href = x.relationships?.storage?.meta?.link?.href
}
}
export class Folder extends Data {
displayName: string | undefined
parentId: string | undefined
constructor(x: RawData) {
super(x)
if (x.attributes) {
if (x.attributes.displayName) {
this.displayName = x.attributes.displayName
}
}
if (x.relationships) {
if (x.relationships.parent) {
this.parentId = x.relationships.parent.data.id
}
}
}
}
export class Item extends Data {
displayName: string | undefined
constructor(x: RawData) {
super(x)
if (x.attributes) {
if (x.attributes.displayName) {
this.displayName = x.attributes.displayName
}
}
}
}
export async function getHubs(): Promise<Hub[] | undefined> {
const auth = await APS.getAuth()
if (!auth) {
return undefined
}
try {
APS.incApsCalls("project-v1-hubs")
return await fetch("https://developer.api.autodesk.com/project/v1/hubs", {
method: "GET",
headers: {
Authorization: `Bearer ${auth.access_token}`,
},
})
.then(x => x.json())
.then(x => {
if ((x.data as RawData[] | undefined)?.length ?? 0 > 0) {
return (x.data as RawData[]).map<Hub>(y => {
return { id: y.id, name: y.attributes.name }
})
} else {
return undefined
}
})
} catch (e) {
console.error("Failed to get hubs")
console.error(e)
console.log(auth)
console.log(APS.userInfo)
if (e instanceof APSDataError) {
globalAddToast("error", e.title, e.detail)
} else if (e instanceof Error) {
globalAddToast("error", "Failed to get hubs.", e.message)
}
return undefined
}
}
export async function getProjects(hub: Hub): Promise<Project[] | undefined> {
const auth = await APS.getAuth()
if (!auth) {
return undefined
}
try {
APS.incApsCalls("project-v1-hubs-x-projects")
return await fetch(`https://developer.api.autodesk.com/project/v1/hubs/${hub.id}/projects/`, {
method: "GET",
headers: {
Authorization: `Bearer ${auth.access_token}`,
},
})
.then(x => x.json())
.then(x => {
if ((x.data as RawData[]).length > 0) {
return (x.data as RawData[]).map<Project>(y => {
return {
id: y.id,
name: y.attributes.name,
folder: new Folder(y.relationships.rootFolder.data),
}
})
} else {
return undefined
}
})
} catch (e) {
console.error("Failed to get hubs")
if (e instanceof Error) {
globalAddToast("error", "Failed to get hubs.", e.message)
}
return undefined
}
}
export async function getFolderData(project: Project, folder: Folder): Promise<Data[] | undefined> {
const auth = await APS.getAuth()
if (!auth) {
return undefined
}
try {
APS.incApsCalls("data-v1-projects-x-folders-x-contents")
return await fetch(
`https://developer.api.autodesk.com/data/v1/projects/${project.id}/folders/${folder.id}/contents`,
{
method: "GET",
headers: {
Authorization: `Bearer ${auth.access_token}`,
},
}
)
.then(x => x.json())
.then(x => {
console.log("Raw Folder Data")
console.log(x)
if ((x.data as RawData[]).length > 0) {
return (x.data as RawData[]).map<Data>(y => {
if (y.type == ITEM_DATA_TYPE) {
return new Item(y)
} else if (y.type == FOLDER_DATA_TYPE) {
return new Folder(y)
} else {
return new Data(y)
}
})
} else {
console.log("No data in folder")
return undefined
}
})
} catch (e) {
console.error("Failed to get folder data")
if (e instanceof Error) {
globalAddToast("error", "Failed to get folder data.", e.message)
}
return undefined
}
}
function filterToQuery(filters: Filter[]): string {
return filters.map(filter => encodeURIComponent(`filter[${filter.fieldName}]`) + `=${filter.matchValue}`).join("&")
}
export async function searchFolder(project: Project, folder: Folder, filters?: Filter[]): Promise<Data[] | undefined> {
const auth = await APS.getAuth()
if (!auth) return undefined
let endpoint = `https://developer.api.autodesk.com/data/v1/projects/${project.id}/folders/${folder.id}/search`
if (filters && filters.length > 0) {
endpoint += `?${filterToQuery(filters)}`
}
APS.incApsCalls("data-v1-projects-x-folders-x-search")
const res = await fetch(endpoint, {
method: "GET",
headers: {
Authorization: `Bearer ${auth.access_token}`,
},
})
if (!res.ok) {
globalAddToast("error", "Error getting cloud files.", "Please sign in again.")
return []
}
const json = await res.json()
return json.data.map((data: RawData) => new Data(data))
}
export async function searchRootForMira(project: Project): Promise<Data[] | undefined> {
return searchFolder(project, project.folder, [{ fieldName: "fileType", matchValue: "mira" }])
}
export async function downloadData(data: Data): Promise<ArrayBuffer | undefined> {
if (!data.href) {
return undefined
}
const auth = await APS.getAuth()
if (!auth) {
return undefined
}
APS.incApsCalls("object_bucket")
return await fetch(data.href, {
method: "GET",
headers: {
Authorization: `Bearer ${auth.access_token}`,
},
}).then(x => x.arrayBuffer())
}
export function hasMirabufFiles(): boolean {
return mirabufFiles != undefined
}
export async function requestMirabufFiles() {
if (mirabufFilesMutex.isLocked()) {
return
}
await mirabufFilesMutex.runExclusive(async () => {
const auth = await APS.getAuth()
if (auth) {
getHubs().then(async hubs => {
if (!hubs) {
EventSystem.dispatch("MirabufFilesStatusUpdateEvent", {
isDone: true,
message: "Failed to get Hubs",
progress: 1,
})
return
}
const fileData: Data[] = []
let i = 0
const projects = (
await Promise.all(
hubs.map(async hub => {
const projects = await getProjects(hub)
return projects ?? []
})
)
).flat()
if (!projects.length) return
for (const project of projects) {
EventSystem.dispatch("MirabufFilesStatusUpdateEvent", {
isDone: false,
message: `Searching Project '${project.name}'`,
progress: i++ / projects.length,
})
const data = await searchRootForMira(project)
if (data) fileData.push(...data)
}
EventSystem.dispatch("MirabufFilesStatusUpdateEvent", {
isDone: true,
message: `Found ${fileData.length} file${fileData.length == 1 ? "" : "s"}`,
progress: 1,
})
mirabufFiles = fileData
EventSystem.dispatch("MirabufFilesUpdateEvent", mirabufFiles)
})
}
})
}
export function getMirabufFiles(): Data[] | undefined {
return mirabufFiles
}