1+ //
2+ // Copyright (c) 2019 Autodesk, Inc.
3+ //
4+ // Permission is hereby granted, free of charge, to any person obtaining a copy
5+ // of this software and associated documentation files (the "Software"), to deal
6+ // in the Software without restriction, including without limitation the rights
7+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+ // copies of the Software, and to permit persons to whom the Software is
9+ // furnished to do so, subject to the following conditions:
10+ //
11+ // The above copyright notice and this permission notice shall be included in all
12+ // copies or substantial portions of the Software.
13+ //
14+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+ // SOFTWARE.
21+ //
22+
23+ /*jshint esversion: 9 */
24+
25+ var fs = require ( 'fs' ) ;
26+ var ForgeSDK = require ( '../src/index' ) ;
27+
28+ // ForgeSDK.ApiClient.instance.switchServerPath('https://developer-stg.api.autodesk.com');
29+ // var StgApiClient = new ForgeSDK.ApiClient('https://developer-stg.api.autodesk.com');
30+ // var bucketsApiStg = new ForgeSDK.BucketsApi(StgApiClient);
31+ // var oAuth2TwoLeggedStg = new ForgeSDK.AuthClientTwoLegged(FORGE_CLIENT_ID, FORGE_CLIENT_SECRET, ['data:read'], true, StgApiClient);
32+ // var oAuth2TwoLeggedtest = new ForgeSDK.AuthClientTwoLegged(FORGE_CLIENT_ID, FORGE_CLIENT_SECRET, ['data:read'], true); // back to prod
33+
34+ // TODO - insert your CLIENT_ID and CLIENT_SECRET
35+ var FORGE_CLIENT_ID = process . env . FORGE_CLIENT_ID || 'your forge client id' ;
36+ var FORGE_CLIENT_SECRET = process . env . FORGE_CLIENT_SECRET || 'your forge client secret' ;
37+
38+ // TODO - Choose a bucket key - a unique name to assign to a bucket. It must be globally unique across all applications and
39+ // regions, otherwise the call will fail. Possible values: -_.a-z0-9 (between 3-128 characters in
40+ // length). Note that you cannot change a bucket key.
41+ var BUCKET_KEY = 'forge_sample_' + FORGE_CLIENT_ID . toLowerCase ( ) ;
42+
43+ // TODO - Choose a filename - a key for the uploaded object
44+ var FILE_NAME = 'test.nwd' ;
45+
46+ // TODO - specify the full filename and path
47+ var FILE_PATH = 'test.nwd' ;
48+
49+ var bucketsApi = new ForgeSDK . BucketsApi ( ) ;
50+ var objectsApi = new ForgeSDK . ObjectsApi ( ) ;
51+ var derivativesApi = new ForgeSDK . DerivativesApi ( /* undefined, ForgeSDK.JobPayloadDestination.RegionEnum.EMEA */ ) ;
52+
53+ // Initialize the 2-legged oauth2 client
54+ var oAuth2TwoLegged = new ForgeSDK . AuthClientTwoLegged ( FORGE_CLIENT_ID , FORGE_CLIENT_SECRET ,
55+ [ 'data:write' , 'data:read' , 'bucket:read' , 'bucket:update' , 'bucket:create' ] , true ) ;
56+
57+ /**
58+ * General error handling method
59+ * @param err
60+ */
61+ function defaultHandleError ( err ) {
62+ console . error ( '\x1b[31m Error:' , err , '\x1b[0m' ) ;
63+ }
64+
65+ /**
66+ * Gets the details of an object specified by a bucketKey / objectKey.
67+ * Uses the oAuth2TwoLegged object that you retrieved previously.
68+ * @param {String } bucketKey
69+ * @param {String } objectKey
70+ * @returns {Promise }
71+ */
72+ var getObjectDetails = function ( bucketKey , objectKey ) {
73+ console . log ( "**** Get object details :" , bucketKey , objectKey ) ;
74+ return objectsApi . getObjectDetails ( bucketKey , objectKey , { } , oAuth2TwoLegged , oAuth2TwoLegged . getCredentials ( ) ) ;
75+ } ;
76+
77+ /**
78+ * Gets the resource manifest.
79+ * Uses the oAuth2TwoLegged object that you retrieved previously.
80+ * @param {String } urn
81+ * @returns {Promise }
82+ */
83+ var getManifest = function ( urn ) {
84+ console . log ( "**** Get resource manifest:" , urn ) ;
85+ return derivativesApi . getManifest ( urn , { } , oAuth2TwoLegged , oAuth2TwoLegged . getCredentials ( ) ) ;
86+ } ;
87+
88+ /**
89+ * Gets the resource metadata.
90+ * Uses the oAuth2TwoLegged object that you retrieved previously.
91+ * @param {String } urn
92+ * @returns {Promise }
93+ */
94+ var getMetadata = function ( urn ) {
95+ console . log ( "**** Get resource metadata:" , urn ) ;
96+ return derivativesApi . getMetadata ( urn , { } , oAuth2TwoLegged , oAuth2TwoLegged . getCredentials ( ) ) ;
97+ } ;
98+
99+ /**
100+ * Delete the file uploaded by the application.
101+ * Uses the oAuth2TwoLegged object that you retrieved previously.
102+ * @param {String } bucketKey
103+ * @param {String } fileName
104+ * @returns {Promise }
105+ */
106+ var deleteFile = function ( bucketKey , fileName ) {
107+ console . log ( "**** Deleting file from bucket:" + bucketKey + ", filename:" + fileName ) ;
108+ return objectsApi . deleteObject ( bucketKey , fileName , oAuth2TwoLegged , oAuth2TwoLegged . getCredentials ( ) ) ;
109+ } ;
110+
111+ /**
112+ * Create OBJ.
113+ * Uses the oAuth2TwoLegged object that you retrieved previously.
114+ * @param {String } urn
115+ * @param {String } guid
116+ * @returns {Promise }
117+ */
118+ var createObj = function ( urn , guid ) {
119+ console . log ( "**** Creating OBJ : " + urn ) ;
120+
121+ var job = new ForgeSDK . JobPayload ( {
122+ input : new ForgeSDK . JobPayloadInput ( urn ) ,
123+ output : new ForgeSDK . JobPayloadOutput (
124+ [
125+ new ForgeSDK . JobObjOutputPayload ( 'obj' ,
126+ {
127+ advanced : new ForgeSDK . JobObjOutputPayloadAdvanced (
128+ {
129+ unit : ForgeSDK . JobObjOutputPayloadAdvanced . UnitEnum . meter ,
130+ modelGuid : guid ,
131+ objectIds : [ - 1 ]
132+ } )
133+ } ) ,
134+ ] ,
135+ {
136+ destination : new ForgeSDK . JobPayloadDestination ( ForgeSDK . JobPayloadDestination . RegionEnum . US )
137+ }
138+ ) ,
139+ //misc: new ForgeSDK.JobPayloadMisc ()
140+ } ) ;
141+
142+ return derivativesApi . translate ( job , { } , oAuth2TwoLegged , oAuth2TwoLegged . getCredentials ( ) ) ;
143+ } ;
144+
145+ /**
146+ * Create an access token and run the API calls.
147+ */
148+ oAuth2TwoLegged . authenticate ( )
149+ . then ( function ( credentials ) {
150+
151+ console . log ( "**** Got Credentials" , credentials ) ;
152+
153+ getObjectDetails ( BUCKET_KEY , FILE_NAME )
154+ . then ( function ( details ) {
155+ console . log ( "**** Object details:" , details . body ) ;
156+
157+ var _details = new ForgeSDK . ObjectDetails ( details . body ) ; // ObjectFullDetails
158+ var urn = Buffer . from ( _details . objectId ) . toString ( 'base64' )
159+ . replace ( / \+ / g, '-' ) // Convert '+' to '-'
160+ . replace ( / \/ / g, '_' ) // Convert '/' to '_'
161+ . replace ( / = + $ / , '' ) ;
162+
163+ getMetadata ( urn )
164+ . then ( function ( metadata ) {
165+ console . log ( "**** Metadata requested:" , metadata . body ) ;
166+
167+ var _metadata = ForgeSDK . Metadata . constructFromObject ( metadata . body ) ;
168+ var guid = _metadata . data . metadata [ 0 ] . guid ;
169+
170+ createObj ( urn , guid )
171+ . then ( function ( reponse ) {
172+ console . log ( "**** OBJ requested:" , reponse . body ) ;
173+
174+ getManifest ( urn )
175+ . then ( function ( manifest ) {
176+ console . log ( "**** Manifest:" , manifest . body ) ;
177+
178+ var _manifest = new ForgeSDK . Manifest . constructFromObject ( manifest . body ) ;
179+ console . log ( "**** Manifest:" , _manifest ) ;
180+
181+ } , defaultHandleError ) ;
182+
183+ } , defaultHandleError ) ;
184+
185+ } , defaultHandleError ) ;
186+
187+ } , defaultHandleError ) ;
188+
189+ } , defaultHandleError )
190+
191+ . catch ( defaultHandleError ) ;
0 commit comments