Skip to content

Commit 9b8716c

Browse files
committed
Merge branch 'release/v0.2.0'
2 parents d20b8cf + 3f4ef31 commit 9b8716c

File tree

9 files changed

+124
-5
lines changed

9 files changed

+124
-5
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,20 @@ for await (const buckets of await data.buckets()) {
2626
console.log('Buckets', buckets.map(bucket => bucket.bucketKey).join(','));
2727
}
2828
// List objects in bucket
29-
for await (const objects of data.objects("foo-bucket")) {
29+
for await (const objects of data.objects('foo-bucket')) {
3030
console.log('Objects', objects.map(object => object.objectId).join(','));
3131
}
3232
```
3333

34+
### Model Derivatives
35+
36+
```js
37+
const { ModelDerivativeClient, AuthenticationClient } = require('autodesk-forge-tools');
38+
const derivatives = new ModelDerivativeClient(new AuthenticationClient());
39+
const job = await derivatives.submitJob('<your-document-urn>', [{ type: 'svf', views: ['2d', '3d'] }]);
40+
console.log('Job', job);
41+
```
42+
3443
## Testing
3544

3645
```bash

docs/tutorials/deriv-basic.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Basic usage of model derivative APIs
2+
3+
```js
4+
const { ModelDerivativeClient, AuthenticationClient } = require('autodesk-forge-tools');
5+
const derivatives = new ModelDerivativeClient(new AuthenticationClient());
6+
const job = await derivatives.submitJob('<your-document-urn>', [{ type: 'svf', views: ['2d', '3d'] }]);
7+
console.log('Job', job);
8+
```

docs/tutorials/index.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
},
55
"data-basic": {
66
"title": "Basic Data Management"
7+
},
8+
"deriv-basic": {
9+
"title": "Basic Model Derivatives"
710
}
811
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "autodesk-forge-tools",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "Tools for accessing Autodesk Forge APIs from Node.js apps.",
55
"main": "src/index.js",
66
"scripts": {

src/data.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const WriteTokenScopes = ['bucket:create', 'data:write'];
99

1010
/**
1111
* Client providing access to Autodesk Forge {@link https://forge.autodesk.com/en/docs/data/v2|data management APIs}.
12+
* @tutorial data-basic
1213
*/
1314
class DataManagementClient {
1415
/**

src/deriv.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
};

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const { AuthenticationClient } = require('./auth');
22
const { DataManagementClient } = require('./data');
3+
const { ModelDerivativeClient } = require('./deriv');
34

45
module.exports = {
56
AuthenticationClient,
6-
DataManagementClient
7+
DataManagementClient,
8+
ModelDerivativeClient
79
};

test/data.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ describe('DataManagementClient', function() {
1111
const auth = new AuthenticationClient(FORGE_CLIENT_ID, FORGE_CLIENT_SECRET);
1212
this.client = new DataManagementClient(auth);
1313
this.bucket = FORGE_BUCKET;
14+
this.timeout(5000); // Increase timeout to 5 seconds
1415
});
1516

1617
describe('buckets()', function() {
1718
it('should return a list of buckets', async function() {
18-
for await (const buckets of this.client.buckets()) {
19+
for await (const buckets of this.client.buckets(8)) {
1920
assert(buckets.length > 0);
21+
break; // Skip additional pages
2022
}
2123
});
2224
});
@@ -52,7 +54,7 @@ describe('DataManagementClient', function() {
5254

5355
describe('objects()', function() {
5456
it('should return a list of objects', async function() {
55-
for await (const objects of this.client.objects(this.bucket)) {
57+
for await (const objects of this.client.objects(this.bucket, 8)) {
5658
assert(objects.length > 0);
5759
break; // Skip additional pages
5860
}

test/deriv.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const assert = require('assert');
2+
3+
const { AuthenticationClient, ModelDerivativeClient } = require('..');
4+
5+
describe('ModelDerivativeClient', function() {
6+
beforeEach(function() {
7+
const { FORGE_CLIENT_ID, FORGE_CLIENT_SECRET } = process.env;
8+
assert(FORGE_CLIENT_ID);
9+
assert(FORGE_CLIENT_SECRET);
10+
const auth = new AuthenticationClient(FORGE_CLIENT_ID, FORGE_CLIENT_SECRET);
11+
this.client = new ModelDerivativeClient(auth);
12+
this.urn = 'dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6cGV0cmJyb3otZGF0YXNhbXBsZXMvYmFzaWMucnZ0';
13+
});
14+
15+
describe('formats()', function() {
16+
it('should return a list of formats', async function() {
17+
const formats = await this.client.formats();
18+
assert(formats);
19+
});
20+
});
21+
22+
describe('submitJob()', function() {
23+
it('should return a job info', async function() {
24+
const job = await this.client.submitJob(this.urn, [{ type: 'svf', views: ['2d', '3d'] }]);
25+
assert(job);
26+
assert(job.result);
27+
});
28+
});
29+
});

0 commit comments

Comments
 (0)