-
Notifications
You must be signed in to change notification settings - Fork 5
/
createActivity.js
97 lines (90 loc) · 3.79 KB
/
createActivity.js
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
const async = require('async');
const Authenticator = require('./lib/authenticator');
const configFile = require('./config');
const AutodeskForgeDesignAutomation = require('autodesk.forge.designautomation');
const fs = require('fs');
const handlebar = require('handlebars');
const logger = require('./lib/logger');
const activityTemplateFileContent = fs.readFileSync('./templates/Activity.hbs', 'utf8');
const activityTemplate = handlebar.compile(activityTemplateFileContent);
const aliasTemplateFileContent = fs.readFileSync('./templates/Alias.hbs', 'utf8');
const aliasTemplate = handlebar.compile(aliasTemplateFileContent);
let config = {
"retry" : {
"maxNumberOfRetries" : 7,
"backoffDelay" : 4000,
"backoffPolicy" : "exponentialBackoffWithJitter"
}
};
let DesignAutomationClient = new AutodeskForgeDesignAutomation.AutodeskForgeDesignAutomationClient(config);
let DesignAutomationApi = new AutodeskForgeDesignAutomation.AutodeskForgeDesignAutomationApi(DesignAutomationClient);
function createActivity() {
let nickname;
async.waterfall([
function getAccessToken(next)
{
logger.log('Getting Access Token...');
const authenticator = new Authenticator(configFile.forge.clientId, configFile.forge.clientSecret);
authenticator.getForgeOAuth2TwoLeggedObject((error, forgeOAuth2TwoLegged) => {
if (error)
{
next(error);
return;
}
let oauth = DesignAutomationClient.authManager.authentications['2-legged'];
oauth.accessToken = forgeOAuth2TwoLegged.getCredentials().access_token;
next();
});
},
function getNickname(next) {
logger.log('Getting Nickname...');
DesignAutomationApi.getNickname("me").then((data) => {
nickname = data;
next();
}, next);
},
function deleteActivityIfExist(next) {
logger.log('Deleting old activity if it already exist...');
DesignAutomationApi.deleteActivity(configFile.designAutomation.activityId).then(next, (error) => {
next(error && error.status === 404 ? null : error);
});
},
function createActivityVersion(next) {
logger.log('Creating activity version 1...');
let activityObjJson = activityTemplate({
activityId: configFile.designAutomation.activityId,
nickname: nickname,
appId: configFile.designAutomation.appId,
appAlias: configFile.designAutomation.appAlias,
engineId: configFile.designAutomation.engineId
});
let activityObj = JSON.parse(activityObjJson);
DesignAutomationApi.createActivity(activityObj).then((data) => next(), next);
},
function createActivityAlias(next) {
logger.log('Creating activity alias pointing to version 1...');
let aliasObjJson = aliasTemplate({
id: configFile.designAutomation.activityAlias,
version: 1
});
let aliasObj = JSON.parse(aliasObjJson);
DesignAutomationApi.createActivityAlias(configFile.designAutomation.activityId, aliasObj).then((data) => next(), next);
}
], function (err) {
if (err) {
logger.log(err);
if (err && err.status)
{
logger.log('Status: ' + err.status);
}
if (err && err.response && err.response.text)
{
logger.log('Text: ' + err.response.text);
}
process.exit(-1);
}
logger.log('Finished creating activity');
process.exit(0);
});
}
createActivity();