Update to use secured routes

This commit is contained in:
Sergiu Toma 2019-12-05 10:59:13 +02:00
parent ab52c0eff7
commit 1ec2a7bd65
2 changed files with 50 additions and 13 deletions

View File

@ -18,9 +18,10 @@ const log = require('./utils').log
class Asset {
constructor(id, configs) {
constructor(id, configs, token) {
this.id = id;
this.token = token;
this.configs = configs
this._processConfigs();
this.startTime = +new Date();
@ -71,13 +72,16 @@ class Asset {
}
_getDataFromApi(callback) {
utils.writeLog(`Get informations about asset ${this.id}`)
Request.get(
this.apiEndpoint + '/asset/' + this.id,
{},
{
headers: {
Authorization: `Bearer ${this.token}`
}
},
(error, response, body) => {
utils.writeLog(`Get informations about asset ${this.id}`)
if (!error && (response.statusCode === 200 || response.statusCode === 201)) {
let bodyObj = JSON.parse(body);
// Here are the asset fields.
@ -97,7 +101,11 @@ class Asset {
Request.get(
this.apiEndpoint + '/asset/ ' + this.id + '/groups',
{},
{
headers: {
Authorization: `Bearer ${this.token}`
}
},
(error, response, body) => {
if (!error && (response.statusCode === 200 || response.statusCode === 201)) {
@ -203,10 +211,13 @@ class Asset {
}
_register(callback) {
Request.post(
this.apiEndpoint + '/audio/register/' + this.id,
{},
{
headers: {
Authorization: `Bearer ${this.token}`
}
},
(error, response, body) => {
if (!error && (response.statusCode === 200 || response.statusCode === 201)) {
utils.writeLog(`Asset ${this.id} audio registered`);
@ -221,13 +232,15 @@ class Asset {
}
_moveToChannel(callback) {
Request.post(
this.apiEndpoint + '/audio/enter-group/' + this.id + '/' + this.groupId,
{},
{
headers: {
Authorization: `Bearer ${this.token}`
}
},
(error, response, body) => {
if (!error && (response.statusCode === 200 || response.statusCode === 201)) {
let hub = this.hub;
if (hub && hub.connected) {

View File

@ -10,7 +10,8 @@ class Simulator {
constructor(configs) {
this.configs = configs;
this.token;
// Read assets ids from configs
this.assetIds = configs.assets.ids;
@ -19,20 +20,22 @@ class Simulator {
async.waterfall([
this._manageLogs.bind(this),
this._unregisterAsstes.bind(this)
this._unregisterAsstes.bind(this),
this._getToken.bind(this),
],
(err, result) => {
if(err) {
console.log(err)
return;
}
utils.writeLog(`Using token: ${this.token}`);
this._start();
});
}
_start() {
this.assetIds.forEach(id => {
new Asset(id, this.configs);
new Asset(id, this.configs, this.token);
});
}
@ -73,6 +76,27 @@ class Simulator {
);
}
_getToken(callback) {
let url = this.apiEndpoint + '/generate-token';
Request.get(
url,
{timeout: 15000},
(error, response, body) => {
if(error) {
utils.writeLog(`ERROR can't get token`, error);
utils.writeErrorLog(`ERROR_API`);
return callback(error);
}
else {
let res = JSON.parse(response.body)
this.token = res.data.token;
utils.writeLog(`API TOKEN: ${this.token}`)
return callback();
}
}
);
}
}
module.exports = Simulator;