-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
168 lines (149 loc) · 4.5 KB
/
app.js
File metadata and controls
168 lines (149 loc) · 4.5 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
// Axios is a promise-based HTTP client for the browser and node.js.
import axios from "axios";
// We need the following in order to save files to the machine
import fs from "fs";
import path from "path";
// Application constructor
export default class App {
constructor(accessToken) {
this.graphAPI = 'https://developer.api.autodesk.com/mfg/graphql';
this.accessToken = accessToken;
}
getRequestHeaders() {
return {
"Content-type": "application/json",
"Authorization": "Bearer " + this.accessToken,
};
}
async sendQuery(query, variables) {
try {
let response = await axios({
method: "POST",
url: `${this.graphAPI}`,
headers: this.getRequestHeaders(),
data: {
query,
variables,
},
});
return response;
} catch (err) {
if (err.response.data.errors) {
let formatted = JSON.stringify(err.response.data.errors, null, 2);
console.log(`API error:\n${formatted}`);
}
throw err;
}
}
async getProjectId(hubName, projectName) {
try {
// Get first batch of occurrences
let response = await this.sendQuery(
`query GetProjectId($hubName: String!, $projectName: String!) {
hubs(filter:{name:$hubName}) {
results {
name
projects(filter:{name:$projectName}) {
results {
name
id
}
}
}
}
}`,
{
hubName,
projectName
}
);
let projectId = response.data.data.hubs.results[0].projects.results[0].id;
return projectId;
} catch (err) {
console.log("There was an issue: " + err.message);
}
}
async getComponentVersionId(projectId, componentName) {
try {
// Get first batch of occurrences
let response = await this.sendQuery(
`query GetComponentVersionId($projectId: ID!, $componentName: String!) {
project(projectId: $projectId) {
name
items(filter:{name:$componentName}) {
results {
... on DesignItem {
name
tipRootComponentVersion {
id
}
}
}
}
}
}`,
{
projectId,
componentName
}
);
let componentVersionId = response.data.data.project.items.results[0].tipRootComponentVersion.id;
return componentVersionId;
} catch (err) {
console.log("There was an issue: " + err.message);
}
}
// <downloadGeometry>
async downloadGeometry(hubName, projectName, componentName) {
try {
let projectId = await this.getProjectId(hubName, projectName);
let componentVersionId = await this.getComponentVersionId(projectId, componentName);
while (true) {
let response = await this.sendQuery(
`query GetGeometry($componentVersionId: ID!) {
componentVersion(componentVersionId: $componentVersionId) {
derivatives (derivativeInput: {outputFormat: STEP, generate: true}) {
expires
signedUrl
status
outputFormat
}
}
}`,
{
componentVersionId
}
)
let geometry = response.data.data.componentVersion.derivatives[0];
if (geometry.status === "SUCCESS") {
// If the geometry generation finished then we can download it
// from the url
let geometryPath = path.resolve('geometry.stp');
await this.downloadFile(geometry.signedUrl, geometryPath);
return "file://" + encodeURI(geometryPath);
} else {
console.log("Extracting geometry … (it may take a few seconds)")
// Let's wait a second before checking the status of the geometry again
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
} catch (err) {
console.log("There was an issue: " + err.message)
}
}
// </downloadGeometry>
async downloadFile(url, path) {
const writer = fs.createWriteStream(path);
const response = await axios({
url,
method: 'GET',
headers: this.getRequestHeaders(),
responseType: 'stream'
});
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
}
}