Skip to content

Commit

Permalink
Pull and push scenarios using a CLI.
Browse files Browse the repository at this point in the history
  • Loading branch information
patniko committed Apr 22, 2020
1 parent 2e5c754 commit 0187d82
Show file tree
Hide file tree
Showing 6 changed files with 620 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .vscode/launch.json
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/getConfig.js"
}
]
}
100 changes: 100 additions & 0 deletions healthbotCli.js
@@ -0,0 +1,100 @@
const jwt = require("jsonwebtoken");
const rp = require("request-promise");
const fs = require('fs');

if (process.argv.length < 5 && process.argv.length > 7) {
console.log("Usage: node healthbotCli.js get_scenarios <tenantName> <API_JWT_secret>");
console.log("Usage: node healthbotCli.js get_scenario <tenantName> <API_JWT_secret> <scenarioName> <outputFilePath>");
console.log("Usage: node healthbotCli.js post_scenario <tenantName> <API_JWT_secret> <inputFilePath>");
process.exit();
}
const action = process.argv[2];
const tenantName = process.argv[3];
const jwtSecret = process.argv[4];
const scenarioName = process.argv[5];
const targetFile = process.argv[6];

const BASE_URL = "https://us.healthbot.microsoft.com/";

const jwtToken = jwt.sign({ "tenantName": tenantName }, jwtSecret);

if (action === "post_scenario") {

fs.readFile(targetFile, 'utf8', (err, scenarioJson) => {
if (err) {
console.log("File read failed:", err)
return
} else {
const options = {
method: 'POST',
uri: `${BASE_URL}api/account/${tenantName}/scenarios`,
headers: {
'Authorization': 'Bearer ' + jwtToken
},
body: [scenarioJson],
json: true
};

rp(options)
.then(function (parsedBody) {
console.log(parsedBody);
})
.catch(function (err) {
console.log(err.message);
});
}
});


}

if (action === "get_scenarios") {
const options = {
method: 'GET',
uri: `${BASE_URL}api/account/${tenantName}/scenarios`,
headers: {
'Authorization': 'Bearer ' + jwtToken
}
};

rp(options)
.then(function (parsedBody) {
const scenarios = JSON.parse(parsedBody);
scenarios.map(i => {
console.log(`${i.name} - ${i.id} - ${i.description ? i.description : "No description"}`)
});
})
.catch(function (err) {
console.log(err.message);
});
}

if (action === "get_scenario") {
const options = {
method: 'GET',
uri: `${BASE_URL}api/account/${tenantName}/scenarios`,
headers: {
'Authorization': 'Bearer ' + jwtToken
}
};

rp(options)
.then(function (parsedBody) {
const scenarios = JSON.parse(parsedBody);
scenarios.map(i => {
if (i.name === scenarioName) {
var data = JSON.stringify(i);
fs.writeFile(targetFile, data, (err) => {
if (err) {
console.error(err);
return;
};
});
}
});
})
.catch(function (err) {
console.log(err.message);
});
}

0 comments on commit 0187d82

Please sign in to comment.