@shapediver/sdk.geometry-api-sdk-v2 1.12.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +133 -58
- package/dist/UtilsApi.d.ts +20 -0
- package/dist/UtilsApi.d.ts.map +1 -0
- package/dist/UtilsApi.js +185 -0
- package/dist/UtilsApi.js.map +1 -0
- package/dist/client/api.d.ts +2117 -0
- package/dist/client/api.d.ts.map +1 -0
- package/dist/client/api.js +3473 -0
- package/dist/client/api.js.map +1 -0
- package/dist/client/base.d.ts +32 -0
- package/dist/client/base.d.ts.map +1 -0
- package/dist/client/base.js +37 -0
- package/dist/client/base.js.map +1 -0
- package/dist/client/common.d.ts +14 -0
- package/dist/client/common.d.ts.map +1 -0
- package/dist/client/common.js +108 -0
- package/dist/client/common.js.map +1 -0
- package/dist/client/configuration.d.ts +23 -0
- package/dist/client/configuration.d.ts.map +1 -0
- package/dist/client/configuration.js +21 -0
- package/dist/client/configuration.js.map +1 -0
- package/dist/client/index.d.ts +3 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +19 -0
- package/dist/client/index.js.map +1 -0
- package/dist/configuration.d.ts +8 -0
- package/dist/configuration.d.ts.map +1 -0
- package/dist/configuration.js +22 -0
- package/dist/configuration.js.map +1 -0
- package/dist/error.d.ts +22 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.js +48 -0
- package/dist/error.js.map +1 -0
- package/dist/index.d.ts +5 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -17
- package/dist/index.js.map +1 -1
- package/dist/utils.d.ts +12 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +93 -0
- package/dist/utils.js.map +1 -0
- package/package.json +55 -57
- package/src/UtilsApi.ts +393 -0
- package/src/client/api.ts +14084 -0
- package/src/client/base.ts +86 -0
- package/src/client/common.ts +150 -0
- package/src/client/configuration.ts +110 -0
- package/src/client/index.ts +18 -0
- package/src/configuration.ts +27 -0
- package/src/error.ts +92 -0
- package/src/index.ts +5 -17
- package/src/utils.ts +126 -0
- package/dist/ShapeDiverErrors.d.ts +0 -10
- package/dist/ShapeDiverErrors.d.ts.map +0 -1
- package/dist/ShapeDiverErrors.js +0 -18
- package/dist/ShapeDiverErrors.js.map +0 -1
- package/dist/ShapeDiverSdk.d.ts +0 -48
- package/dist/ShapeDiverSdk.d.ts.map +0 -1
- package/dist/ShapeDiverSdk.js +0 -87
- package/dist/ShapeDiverSdk.js.map +0 -1
- package/dist/resources/ShapeDiverAnalyticsApi.d.ts +0 -12
- package/dist/resources/ShapeDiverAnalyticsApi.d.ts.map +0 -1
- package/dist/resources/ShapeDiverAnalyticsApi.js +0 -63
- package/dist/resources/ShapeDiverAnalyticsApi.js.map +0 -1
- package/dist/resources/ShapeDiverArSceneApi.d.ts +0 -8
- package/dist/resources/ShapeDiverArSceneApi.d.ts.map +0 -1
- package/dist/resources/ShapeDiverArSceneApi.js +0 -63
- package/dist/resources/ShapeDiverArSceneApi.js.map +0 -1
- package/dist/resources/ShapeDiverAssetApi.d.ts +0 -13
- package/dist/resources/ShapeDiverAssetApi.d.ts.map +0 -1
- package/dist/resources/ShapeDiverAssetApi.js +0 -131
- package/dist/resources/ShapeDiverAssetApi.js.map +0 -1
- package/dist/resources/ShapeDiverExportApi.d.ts +0 -10
- package/dist/resources/ShapeDiverExportApi.d.ts.map +0 -1
- package/dist/resources/ShapeDiverExportApi.js +0 -49
- package/dist/resources/ShapeDiverExportApi.js.map +0 -1
- package/dist/resources/ShapeDiverFileApi.d.ts +0 -15
- package/dist/resources/ShapeDiverFileApi.d.ts.map +0 -1
- package/dist/resources/ShapeDiverFileApi.js +0 -67
- package/dist/resources/ShapeDiverFileApi.js.map +0 -1
- package/dist/resources/ShapeDiverGltfApi.d.ts +0 -7
- package/dist/resources/ShapeDiverGltfApi.d.ts.map +0 -1
- package/dist/resources/ShapeDiverGltfApi.js +0 -32
- package/dist/resources/ShapeDiverGltfApi.js.map +0 -1
- package/dist/resources/ShapeDiverModelApi.d.ts +0 -22
- package/dist/resources/ShapeDiverModelApi.d.ts.map +0 -1
- package/dist/resources/ShapeDiverModelApi.js +0 -156
- package/dist/resources/ShapeDiverModelApi.js.map +0 -1
- package/dist/resources/ShapeDiverModelStateApi.d.ts +0 -14
- package/dist/resources/ShapeDiverModelStateApi.d.ts.map +0 -1
- package/dist/resources/ShapeDiverModelStateApi.js +0 -104
- package/dist/resources/ShapeDiverModelStateApi.js.map +0 -1
- package/dist/resources/ShapeDiverOutputApi.d.ts +0 -10
- package/dist/resources/ShapeDiverOutputApi.d.ts.map +0 -1
- package/dist/resources/ShapeDiverOutputApi.js +0 -52
- package/dist/resources/ShapeDiverOutputApi.js.map +0 -1
- package/dist/resources/ShapeDiverSdtfApi.d.ts +0 -10
- package/dist/resources/ShapeDiverSdtfApi.d.ts.map +0 -1
- package/dist/resources/ShapeDiverSdtfApi.js +0 -52
- package/dist/resources/ShapeDiverSdtfApi.js.map +0 -1
- package/dist/resources/ShapeDiverSessionApi.d.ts +0 -11
- package/dist/resources/ShapeDiverSessionApi.d.ts.map +0 -1
- package/dist/resources/ShapeDiverSessionApi.js +0 -66
- package/dist/resources/ShapeDiverSessionApi.js.map +0 -1
- package/dist/resources/ShapeDiverSystemApi.d.ts +0 -12
- package/dist/resources/ShapeDiverSystemApi.d.ts.map +0 -1
- package/dist/resources/ShapeDiverSystemApi.js +0 -63
- package/dist/resources/ShapeDiverSystemApi.js.map +0 -1
- package/dist/resources/ShapeDiverTextureApi.d.ts +0 -7
- package/dist/resources/ShapeDiverTextureApi.d.ts.map +0 -1
- package/dist/resources/ShapeDiverTextureApi.js +0 -31
- package/dist/resources/ShapeDiverTextureApi.js.map +0 -1
- package/dist/resources/ShapeDiverUtilsApi.d.ts +0 -18
- package/dist/resources/ShapeDiverUtilsApi.d.ts.map +0 -1
- package/dist/resources/ShapeDiverUtilsApi.js +0 -146
- package/dist/resources/ShapeDiverUtilsApi.js.map +0 -1
- package/dist/utils/utils.d.ts +0 -12
- package/dist/utils/utils.d.ts.map +0 -1
- package/dist/utils/utils.js +0 -109
- package/dist/utils/utils.js.map +0 -1
- package/src/ShapeDiverErrors.ts +0 -34
- package/src/ShapeDiverSdk.ts +0 -121
- package/src/resources/ShapeDiverAnalyticsApi.ts +0 -108
- package/src/resources/ShapeDiverArSceneApi.ts +0 -77
- package/src/resources/ShapeDiverAssetApi.ts +0 -241
- package/src/resources/ShapeDiverExportApi.ts +0 -103
- package/src/resources/ShapeDiverFileApi.ts +0 -150
- package/src/resources/ShapeDiverGltfApi.ts +0 -46
- package/src/resources/ShapeDiverModelApi.ts +0 -374
- package/src/resources/ShapeDiverModelStateApi.ts +0 -163
- package/src/resources/ShapeDiverOutputApi.ts +0 -109
- package/src/resources/ShapeDiverSdtfApi.ts +0 -103
- package/src/resources/ShapeDiverSessionApi.ts +0 -142
- package/src/resources/ShapeDiverSystemApi.ts +0 -118
- package/src/resources/ShapeDiverTextureApi.ts +0 -36
- package/src/resources/ShapeDiverUtilsApi.ts +0 -318
- package/src/utils/utils.ts +0 -135
package/src/UtilsApi.ts
ADDED
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
import { AxiosInstance, AxiosPromise, RawAxiosRequestConfig, RawAxiosRequestHeaders } from 'axios';
|
|
2
|
+
import { createRequestFunction, serializeDataIfNeeded } from './client/common';
|
|
3
|
+
import {
|
|
4
|
+
AssetsApi,
|
|
5
|
+
Configuration,
|
|
6
|
+
ExportApi,
|
|
7
|
+
OutputApi,
|
|
8
|
+
ReqCache,
|
|
9
|
+
ReqCustomization,
|
|
10
|
+
ReqExport,
|
|
11
|
+
ResAssetUploadHeaders,
|
|
12
|
+
ResComputeExports,
|
|
13
|
+
ResComputeOutputs,
|
|
14
|
+
ResExport,
|
|
15
|
+
ResGetCachedExports,
|
|
16
|
+
ResGetCachedOutputs,
|
|
17
|
+
ResOutput,
|
|
18
|
+
} from './client';
|
|
19
|
+
import { BaseAPI, RequestArgs } from './client/base';
|
|
20
|
+
import { contentDispositionFromFilename, sleep } from './utils';
|
|
21
|
+
import { IllegalArgumentError, TimeoutError } from './error';
|
|
22
|
+
|
|
23
|
+
/* Regex patterns for different asset types targeting the ShapeDiver API. */
|
|
24
|
+
const apiAssetExportUri = /.+\/session\/.+\/export\/.+/;
|
|
25
|
+
const apiAssetOutputUri = /.+\/session\/.+\/output\/.+/;
|
|
26
|
+
const apiAssetTextureUri = /.+\/session\/.+\/texture\/.+/;
|
|
27
|
+
|
|
28
|
+
/* Regex patterns for different asset types targeting the ShapeDiver CDN. */
|
|
29
|
+
const cdnAssetUri = /.+\/cdn-asset-(exports|outputs|textures)\/.+/;
|
|
30
|
+
const cdnAssetExportUri = /.+\/cdn-asset-exports\/.+/;
|
|
31
|
+
const cdnAssetOutputUri = /.+\/cdn-asset-outputs\/.+/;
|
|
32
|
+
const cdnAssetTextureUri = /.+\/cdn-asset-textures\/.+/;
|
|
33
|
+
|
|
34
|
+
/* Regex patterns for direct download URIs. */
|
|
35
|
+
const directDownloadUri = /^(http[s]?:\/\/)?(viewer|textures|downloads)\.shapediver\.com(\/.*)?$/;
|
|
36
|
+
|
|
37
|
+
export class UtilsApi extends BaseAPI {
|
|
38
|
+
constructor(configuration?: Configuration, basePath?: string, axios?: AxiosInstance) {
|
|
39
|
+
super(configuration, basePath, axios);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Upload the given file to the specified URL.
|
|
44
|
+
* @param {string} url The target URL of the upload request.
|
|
45
|
+
* @param {string | Record<string, any> | ArrayBuffer | File} data The data that should be uploaded. Warning: Type `File` in Node.js might lead to problems!
|
|
46
|
+
* @param {string} contentType Indicate the original media type of the resource.
|
|
47
|
+
* @param {string} [filename] The name of the file to be uploaded. When a filename has been specified in the request-upload call, then the same filename has to be specified for the upload as well.
|
|
48
|
+
* @param {*} [options] Override http request option.
|
|
49
|
+
*/
|
|
50
|
+
public upload(
|
|
51
|
+
url: string,
|
|
52
|
+
data: string | Record<string, any> | ArrayBuffer | File,
|
|
53
|
+
contentType: string,
|
|
54
|
+
filename?: string,
|
|
55
|
+
options?: RawAxiosRequestConfig
|
|
56
|
+
): AxiosPromise<unknown> {
|
|
57
|
+
// Prepare headers for the upload.
|
|
58
|
+
const reqHeaders: RawAxiosRequestHeaders = {
|
|
59
|
+
Authorization: undefined, // Disable by default to avoid accidental token exposure.
|
|
60
|
+
'Content-Type': contentType,
|
|
61
|
+
};
|
|
62
|
+
if (filename) reqHeaders['Content-Disposition'] = contentDispositionFromFilename(filename);
|
|
63
|
+
|
|
64
|
+
const reqOptions: RawAxiosRequestConfig = { ...options };
|
|
65
|
+
reqOptions.headers = { ...reqHeaders, ...options?.headers };
|
|
66
|
+
|
|
67
|
+
const request = this.buildRequest('PUT', url, data, reqOptions)();
|
|
68
|
+
return request();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Upload the given asset to the specified ShapeDiver URL.
|
|
73
|
+
* @param {string} url The target URL of the upload request.
|
|
74
|
+
* @param {string | Record<string, any> | ArrayBuffer | File} data The data that should be uploaded. Warning: Type `File` in Node.js might lead to problems!
|
|
75
|
+
* @param {ResAssetUploadHeaders} headers The headers object that was returned from the request-upload call.
|
|
76
|
+
* @param {*} [options] Override http request option.
|
|
77
|
+
*/
|
|
78
|
+
public uploadAsset(
|
|
79
|
+
url: string,
|
|
80
|
+
data: string | Record<string, any> | ArrayBuffer | File,
|
|
81
|
+
headers: ResAssetUploadHeaders,
|
|
82
|
+
options?: RawAxiosRequestConfig
|
|
83
|
+
): AxiosPromise<unknown> {
|
|
84
|
+
// Prepare headers for the upload.
|
|
85
|
+
const reqHeaders: RawAxiosRequestHeaders = {
|
|
86
|
+
Authorization: undefined, // Disable by default to avoid accidental token exposure.
|
|
87
|
+
'Content-Type': headers.contentType,
|
|
88
|
+
};
|
|
89
|
+
if (headers.contentDisposition)
|
|
90
|
+
reqHeaders['Content-Disposition'] = headers.contentDisposition;
|
|
91
|
+
|
|
92
|
+
const reqOptions: RawAxiosRequestConfig = { ...options };
|
|
93
|
+
reqOptions.headers = { ...reqHeaders, ...options?.headers };
|
|
94
|
+
|
|
95
|
+
const request = this.buildRequest('PUT', url, data, reqOptions)();
|
|
96
|
+
return request();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Download from the specified URL.
|
|
101
|
+
* @param {string} url The target URL of the download request.
|
|
102
|
+
* @param {*} [options] Override http request option.
|
|
103
|
+
*/
|
|
104
|
+
public download(url: string, options?: RawAxiosRequestConfig): AxiosPromise<File> {
|
|
105
|
+
const request = this.buildRequest('GET', url, options)();
|
|
106
|
+
return request();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Downloads a ShapeDiver export, output, or texture asset from the specified URL. The type of
|
|
111
|
+
* the asset is determined by the URL and returned with the promise.
|
|
112
|
+
* @param {string} url The URL of the asset to download.
|
|
113
|
+
* @param {*} [options] Override http request option.
|
|
114
|
+
* @throws {IllegalArgumentError} in case the URL is not a valid ShapeDiver asset URL.
|
|
115
|
+
*/
|
|
116
|
+
public downloadAsset(
|
|
117
|
+
url: string,
|
|
118
|
+
options?: RawAxiosRequestConfig
|
|
119
|
+
): [AxiosPromise<File>, 'export' | 'output' | 'texture'] {
|
|
120
|
+
let type: 'output' | 'export' | 'texture';
|
|
121
|
+
this.disableAuthHeaderForShapeDiverUris(url, options);
|
|
122
|
+
|
|
123
|
+
// Check if the given URL is a valid API or CDN asset URL
|
|
124
|
+
if (apiAssetExportUri.test(url) || cdnAssetExportUri.test(url)) type = 'export';
|
|
125
|
+
else if (apiAssetOutputUri.test(url) || cdnAssetOutputUri.test(url)) type = 'output';
|
|
126
|
+
else if (apiAssetTextureUri.test(url) || cdnAssetTextureUri.test(url)) type = 'texture';
|
|
127
|
+
else {
|
|
128
|
+
throw new IllegalArgumentError(
|
|
129
|
+
`Cannot fetch asset: Invalid URL '${url}' - Only ShapeDiver asset URLs are allowed.`
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return [this.download(url, options), type];
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Helper function that downloads all ShapeDiver texture URLs directly, and redirects all other
|
|
138
|
+
* URLs to the `AssetsApi.downloadImage` endpoint to avoid CORS issues.
|
|
139
|
+
* @param {string} sessionId The session ID.
|
|
140
|
+
* @param {string} url The URL of the image to download.
|
|
141
|
+
* @param {*} [options] Override http request option.
|
|
142
|
+
*/
|
|
143
|
+
public downloadImage(
|
|
144
|
+
sessionId: string,
|
|
145
|
+
url: string,
|
|
146
|
+
options?: RawAxiosRequestConfig
|
|
147
|
+
): AxiosPromise<File> {
|
|
148
|
+
this.disableAuthHeaderForShapeDiverUris(url, options);
|
|
149
|
+
|
|
150
|
+
if (
|
|
151
|
+
apiAssetTextureUri.test(url) ||
|
|
152
|
+
cdnAssetTextureUri.test(url) ||
|
|
153
|
+
directDownloadUri.test(url)
|
|
154
|
+
) {
|
|
155
|
+
// Call ShapeDiver texture-asset URLs directly
|
|
156
|
+
return this.download(url, options);
|
|
157
|
+
} else {
|
|
158
|
+
// All other source URLs are called via the download-image endpoint
|
|
159
|
+
return new AssetsApi(this.configuration).downloadImage(sessionId, url, options);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Submit a customization request and wait for the result to be finished.
|
|
165
|
+
* @param {string} sessionId The session ID.
|
|
166
|
+
* @param {ReqCustomization} body The body of the customization request.
|
|
167
|
+
* @param {number} [maxWaitMsec=-1] Maximum duration to wait for result (in milliseconds), pass value < 0 to disable limit.
|
|
168
|
+
* @param {*} [options] Override http request option.
|
|
169
|
+
* @throws {TimeoutError} in case a maximum duration has been specified and is exceeded.
|
|
170
|
+
*/
|
|
171
|
+
public async submitAndWaitForOutput(
|
|
172
|
+
sessionId: string,
|
|
173
|
+
body: ReqCustomization,
|
|
174
|
+
maxWaitMsec = -1,
|
|
175
|
+
options?: RawAxiosRequestConfig
|
|
176
|
+
): Promise<ResComputeOutputs> {
|
|
177
|
+
const startMsec = Date.now();
|
|
178
|
+
const dto = (
|
|
179
|
+
await new OutputApi(this.configuration).computeOutputs(sessionId, body, options)
|
|
180
|
+
).data;
|
|
181
|
+
const waitMsec = Date.now() - startMsec;
|
|
182
|
+
|
|
183
|
+
// Reduce the total max waiting time by the amount the customization-request took
|
|
184
|
+
maxWaitMsec = maxWaitMsec < 0 ? maxWaitMsec : Math.max(0, maxWaitMsec - waitMsec);
|
|
185
|
+
|
|
186
|
+
return this.waitForOutputResult(sessionId, dto, maxWaitMsec, options);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Submit an export request and wait for the result to be finished.
|
|
191
|
+
* @param {string} sessionId The session ID.
|
|
192
|
+
* @param {ReqExport} body The body of the export request.
|
|
193
|
+
* @param {number} [maxWaitMsec=-1] Maximum duration to wait for result (in milliseconds), pass value < 0 to disable limit.
|
|
194
|
+
* @param {*} [options] Override http request option.
|
|
195
|
+
* @throws {TimeoutError} in case a maximum duration has been specified and is exceeded.
|
|
196
|
+
*/
|
|
197
|
+
public async submitAndWaitForExport(
|
|
198
|
+
sessionId: string,
|
|
199
|
+
body: ReqExport,
|
|
200
|
+
maxWaitMsec = -1,
|
|
201
|
+
options?: RawAxiosRequestConfig
|
|
202
|
+
): Promise<ResComputeExports> {
|
|
203
|
+
const startMsec = Date.now();
|
|
204
|
+
const dto = (
|
|
205
|
+
await new ExportApi(this.configuration).computeExports(sessionId, body, options)
|
|
206
|
+
).data;
|
|
207
|
+
const waitMsec = Date.now() - startMsec;
|
|
208
|
+
|
|
209
|
+
// Reduce the total max waiting time by the amount the compute-request took
|
|
210
|
+
maxWaitMsec = maxWaitMsec < 0 ? maxWaitMsec : Math.max(0, maxWaitMsec - waitMsec);
|
|
211
|
+
|
|
212
|
+
return this.waitForExportResult(sessionId, body, dto, maxWaitMsec, options);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Given a DTO resulting from a customization request, wait for the results to be finished.
|
|
217
|
+
* @param {string} sessionId The session ID.
|
|
218
|
+
* @param {ResComputeOutputs} dto The DTO resulting from a customization request.
|
|
219
|
+
* @param {number} [maxWaitMsec=-1] Maximum duration to wait for result (in milliseconds), pass value < 0 to disable limit.
|
|
220
|
+
* @param {*} [options] Override http request option.
|
|
221
|
+
* @throws {TimeoutError} in case a maximum duration has been specified and is exceeded.
|
|
222
|
+
*/
|
|
223
|
+
private async waitForOutputResult(
|
|
224
|
+
sessionId: string,
|
|
225
|
+
dto: ResComputeOutputs,
|
|
226
|
+
maxWaitMsec: number,
|
|
227
|
+
options?: RawAxiosRequestConfig
|
|
228
|
+
): Promise<ResComputeOutputs> {
|
|
229
|
+
if (!dto.outputs) return dto;
|
|
230
|
+
|
|
231
|
+
// Build new cache request
|
|
232
|
+
const outputVersions: ReqCache = {};
|
|
233
|
+
Object.keys(dto.outputs).forEach(
|
|
234
|
+
(id) => (outputVersions[id] = (dto.outputs![id] as ResOutput).version)
|
|
235
|
+
);
|
|
236
|
+
|
|
237
|
+
let delay = this.getMaxOutputDelay(dto);
|
|
238
|
+
const startMsec = Date.now();
|
|
239
|
+
|
|
240
|
+
while (delay > 0) {
|
|
241
|
+
// Check whether maxWaitMsec has been reached
|
|
242
|
+
if (maxWaitMsec >= 0) {
|
|
243
|
+
const waitMsec = Date.now() - startMsec;
|
|
244
|
+
if (waitMsec >= maxWaitMsec) {
|
|
245
|
+
throw new TimeoutError(`Maximum wait time of ${maxWaitMsec} ms reached`);
|
|
246
|
+
}
|
|
247
|
+
if (waitMsec + delay > maxWaitMsec) {
|
|
248
|
+
delay = maxWaitMsec - waitMsec;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
await sleep(delay);
|
|
253
|
+
|
|
254
|
+
// Send cache request
|
|
255
|
+
dto = (
|
|
256
|
+
await new OutputApi(this.configuration).getCachedOutputs(
|
|
257
|
+
sessionId,
|
|
258
|
+
outputVersions,
|
|
259
|
+
options
|
|
260
|
+
)
|
|
261
|
+
).data;
|
|
262
|
+
delay = this.getMaxOutputDelay(dto);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return dto;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Given a DTO resulting from an export request, wait for the result to be finished.
|
|
270
|
+
* @param {string} sessionId The session ID.
|
|
271
|
+
* @param {ReqExport} body The body of the export request.
|
|
272
|
+
* @param {ResComputeExports} dto The DTO resulting from an export request.
|
|
273
|
+
* @param {number} [maxWaitMsec=-1] Maximum duration to wait for result (in milliseconds), pass value < 0 to disable limit.
|
|
274
|
+
* @param {*} [options] Override http request option.
|
|
275
|
+
* @throws {TimeoutError} in case a maximum duration has been specified and is exceeded.
|
|
276
|
+
*/
|
|
277
|
+
private async waitForExportResult(
|
|
278
|
+
sessionId: string,
|
|
279
|
+
body: ReqExport,
|
|
280
|
+
dto: ResComputeExports,
|
|
281
|
+
maxWaitMsec: number,
|
|
282
|
+
options?: RawAxiosRequestConfig
|
|
283
|
+
): Promise<ResComputeExports> {
|
|
284
|
+
let delay = this.getMaxExportDelay(body, dto);
|
|
285
|
+
const startMsec = Date.now();
|
|
286
|
+
|
|
287
|
+
while (delay > 0) {
|
|
288
|
+
// Check whether maxWaitMsec has been reached
|
|
289
|
+
if (maxWaitMsec >= 0) {
|
|
290
|
+
const waitMsec = Date.now() - startMsec;
|
|
291
|
+
if (waitMsec >= maxWaitMsec) {
|
|
292
|
+
throw new TimeoutError(`Maximum wait time of ${maxWaitMsec} ms reached`);
|
|
293
|
+
}
|
|
294
|
+
if (waitMsec + delay > maxWaitMsec) {
|
|
295
|
+
delay = maxWaitMsec - waitMsec;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
await sleep(delay);
|
|
300
|
+
|
|
301
|
+
// Send cache request
|
|
302
|
+
dto = (
|
|
303
|
+
await new ExportApi(this.configuration).getCachedExports(sessionId, body, options)
|
|
304
|
+
).data;
|
|
305
|
+
delay = this.getMaxExportDelay(body, dto);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
return dto;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Get the maximum delay that was reported for output versions.
|
|
313
|
+
* @param {ResComputeOutputs | ResGetCachedOutputs} dto The DTO resulting from a customization request.
|
|
314
|
+
* @returns maximum delay, -1 in case no delay was reported
|
|
315
|
+
*/
|
|
316
|
+
private getMaxOutputDelay(dto: ResComputeOutputs | ResGetCachedOutputs): number {
|
|
317
|
+
return Math.max(
|
|
318
|
+
...Object.values(dto.outputs ?? {}).map((output) => (output as ResOutput).delay ?? -1),
|
|
319
|
+
-1
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Get the maximum delay that was reported for the exports. If outputs have been reported as
|
|
325
|
+
* well, their delay time is included too.
|
|
326
|
+
* @param {ResComputeExports | ResGetCachedExports} dto The DTO resulting from an export request.
|
|
327
|
+
* @returns delay, -1 in case no delay was reported
|
|
328
|
+
*/
|
|
329
|
+
private getMaxExportDelay(
|
|
330
|
+
body: ReqExport,
|
|
331
|
+
dto: ResComputeExports | ResGetCachedExports
|
|
332
|
+
): number {
|
|
333
|
+
const exports = body.exports ?? [];
|
|
334
|
+
const outputs = body.outputs ?? [];
|
|
335
|
+
|
|
336
|
+
return Math.max(
|
|
337
|
+
...Object.values(dto.exports ?? {})
|
|
338
|
+
.filter((e) => exports.includes(e.id))
|
|
339
|
+
.map((e) => (e as ResExport).delay ?? -1),
|
|
340
|
+
...Object.values(dto.outputs ?? {})
|
|
341
|
+
.filter((o) => outputs.includes(o.id))
|
|
342
|
+
.map((o) => (o as ResOutput).delay ?? -1),
|
|
343
|
+
-1
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Builds an Axios request from the given configuration, the API's base configuration, and the
|
|
349
|
+
* optional http request option.
|
|
350
|
+
* @param {string} method The HTTP method to use.
|
|
351
|
+
* @param {string} url The URL to send the request to.
|
|
352
|
+
* @param {*} data The data to send with the request.
|
|
353
|
+
* @param {*} [options={}] Optional http request options.
|
|
354
|
+
*/
|
|
355
|
+
private buildRequest(
|
|
356
|
+
method: string,
|
|
357
|
+
url: string,
|
|
358
|
+
data: any,
|
|
359
|
+
options: RawAxiosRequestConfig = {}
|
|
360
|
+
) {
|
|
361
|
+
return (basePath: string = '') => {
|
|
362
|
+
const baseOptions = this.configuration?.baseOptions ?? {};
|
|
363
|
+
const baseHeaders = baseOptions && baseOptions.headers ? baseOptions.headers : {};
|
|
364
|
+
|
|
365
|
+
const reqOptions = { method, ...baseOptions, ...options };
|
|
366
|
+
reqOptions.headers = { ...baseHeaders, ...options.headers };
|
|
367
|
+
if (!reqOptions.data && data) {
|
|
368
|
+
// Create a new configuration object if necessary to enable mime type detection
|
|
369
|
+
// in the serialization process.
|
|
370
|
+
const configuration = this.configuration ?? new Configuration();
|
|
371
|
+
reqOptions.data = serializeDataIfNeeded(data, reqOptions, configuration);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const axiosArgs: RequestArgs = { url, options: reqOptions };
|
|
375
|
+
return createRequestFunction(axiosArgs, this.axios, basePath, this.configuration).bind(
|
|
376
|
+
this.axios
|
|
377
|
+
);
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** Disable the Authorization header for ShapeDiver URIs if not explicitly set. */
|
|
382
|
+
private disableAuthHeaderForShapeDiverUris(
|
|
383
|
+
url: string,
|
|
384
|
+
options?: RawAxiosRequestConfig
|
|
385
|
+
): void {
|
|
386
|
+
options = { ...options };
|
|
387
|
+
if (
|
|
388
|
+
!options.headers?.Authorization &&
|
|
389
|
+
(cdnAssetUri.test(url) || directDownloadUri.test(url))
|
|
390
|
+
)
|
|
391
|
+
options.headers = { Authorization: undefined, ...options.headers };
|
|
392
|
+
}
|
|
393
|
+
}
|