-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_create.js
86 lines (72 loc) · 2.29 KB
/
_create.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
// node 7.x
// uses async/await - promises
var logger = require('./logger');
var rp = require('request-promise');
var fse = require('fs-extra');
var path = require('path');
var exists=false;
// main function to call
// Call Apps_Create
var createApp = async (config) => {
try {
// JSON for the request body
// { "name": MyAppName, "culture": "en-us"}
var jsonBody = {
"name": config.appName,
"culture": config.culture
};
// Create a LUIS app
var createAppPromise = callCreateApp({
uri: config.uri,
method: 'POST',
headers: {
'Ocp-Apim-Subscription-Key': config.LUIS_subscriptionKey
},
json: true,
body: jsonBody
});
let results = await createAppPromise;
// Create app returns an app ID
let appId="abc";
if(exists)
{
//logger.writeLog ('logging', new Date()+ ': LUIS Model: '+config.appName+' already exists\n')
appId = results;
//console.log(`Called createApp, App Already Exists with ID ${appId}`);
}
else
{
logger.writeLog ('logging', new Date()+ ': LUIS Model: '+config.appName+' created \n')
appId = results.response;
console.log(`Called createApp, created app with ID ${appId}`);
}
return appId;
} catch (err) {
console.log(`Error creating app: ${err.message} `);
throw err;
}
}
// Send JSON as the body of the POST request to the API
var callCreateApp = async (options) => {
try {
var response;
if (options.method === 'POST') {
response = await rp.post(options);
} else if (options.method === 'GET') { // TODO: There's no GET for create app
response = await rp.get(options);
}
// response from successful create should be the new app ID
exists=false;
return { response };
} catch (err) {
if(err.message.indexOf("already exist")){
exists=true;
//var response="cfeab95c-890f-44a8-8afe-bc6414ae62a5" ;
//console.log("Application ID: "+ applicationID);
return "-AlreadyExists";
}
else
throw err;
}
}
module.exports = createApp;