1+ const { get, post, put } = require ( './request' ) ;
2+ const { AuthenticationClient } = require ( './auth' ) ;
3+
4+ const RootPath = '/modelderivative/v2' ;
5+ const ReadTokenScopes = [ 'data:read' ] ;
6+ const WriteTokenScopes = [ 'data:read' , 'data:write' , 'data:create' ] ;
7+
8+ /**
9+ * Client providing access to Autodesk Forge
10+ * {@link https://forge.autodesk.com/en/docs/model-derivative/v2|model derivative APIs}.
11+ * @tutorial deriv-basic
12+ */
13+ class ModelDerivativeClient {
14+ /**
15+ * Initializes new client with specific Forge app credentials.
16+ * @param {AuthenticationClient } auth Authentication client used to obtain tokens
17+ * for all requests.
18+ */
19+ constructor ( auth ) {
20+ this . auth = auth ;
21+ }
22+
23+ /**
24+ * Gets a list of supported translation formats.
25+ * ({@link https://forge.autodesk.com/en/docs/model-derivative/v2/reference/http/formats-GET|docs}).
26+ * @async
27+ * @yields {Promise<object>} Dictionary of all supported output formats
28+ * mapped to arrays of formats these outputs can be obtained from.
29+ * @throws Error when the request fails, for example, due to insufficient rights.
30+ */
31+ async formats ( ) {
32+ const authentication = await this . auth . authenticate ( ReadTokenScopes ) ;
33+ const response = await get ( `${ RootPath } /designdata/formats` , { 'Authorization' : 'Bearer ' + authentication . access_token } ) ;
34+ return response . formats ;
35+ }
36+
37+ /**
38+ * Submits a translation job
39+ * ({@link https://forge.autodesk.com/en/docs/model-derivative/v2/reference/http/job-POST|docs}).
40+ * @async
41+ * @param {string } urn Document to be translated.
42+ * @param {object[] } outputs List of requested output formats. Currently the one
43+ * supported format is `{ type: 'svf', views: ['2d', '3d'] }`.
44+ * @returns {Promise<object> } Translation job details, with properties 'result',
45+ * 'urn', and 'acceptedJobs'.
46+ * @throws Error when the request fails, for example, due to insufficient rights.
47+ */
48+ async submitJob ( urn , outputs ) {
49+ const authentication = await this . auth . authenticate ( WriteTokenScopes ) ;
50+ const params = {
51+ input : {
52+ urn : urn
53+ } ,
54+ output : {
55+ formats : outputs
56+ }
57+ } ;
58+ const response = await post ( `${ RootPath } /designdata/job` , { json : params } , { 'Authorization' : 'Bearer ' + authentication . access_token } ) ;
59+ return response ;
60+ }
61+ }
62+
63+ module . exports = {
64+ ModelDerivativeClient
65+ } ;
0 commit comments