Update to use secured routes
This commit is contained in:
parent
ab52c0eff7
commit
1ec2a7bd65
33
src/asset.js
33
src/asset.js
@ -18,9 +18,10 @@ const log = require('./utils').log
|
|||||||
|
|
||||||
class Asset {
|
class Asset {
|
||||||
|
|
||||||
constructor(id, configs) {
|
constructor(id, configs, token) {
|
||||||
|
|
||||||
this.id = id;
|
this.id = id;
|
||||||
|
this.token = token;
|
||||||
this.configs = configs
|
this.configs = configs
|
||||||
this._processConfigs();
|
this._processConfigs();
|
||||||
this.startTime = +new Date();
|
this.startTime = +new Date();
|
||||||
@ -71,13 +72,16 @@ class Asset {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_getDataFromApi(callback) {
|
_getDataFromApi(callback) {
|
||||||
|
utils.writeLog(`Get informations about asset ${this.id}`)
|
||||||
Request.get(
|
Request.get(
|
||||||
this.apiEndpoint + '/asset/' + this.id,
|
this.apiEndpoint + '/asset/' + this.id,
|
||||||
{},
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${this.token}`
|
||||||
|
}
|
||||||
|
},
|
||||||
(error, response, body) => {
|
(error, response, body) => {
|
||||||
utils.writeLog(`Get informations about asset ${this.id}`)
|
|
||||||
if (!error && (response.statusCode === 200 || response.statusCode === 201)) {
|
if (!error && (response.statusCode === 200 || response.statusCode === 201)) {
|
||||||
|
|
||||||
let bodyObj = JSON.parse(body);
|
let bodyObj = JSON.parse(body);
|
||||||
|
|
||||||
// Here are the asset fields.
|
// Here are the asset fields.
|
||||||
@ -97,7 +101,11 @@ class Asset {
|
|||||||
|
|
||||||
Request.get(
|
Request.get(
|
||||||
this.apiEndpoint + '/asset/ ' + this.id + '/groups',
|
this.apiEndpoint + '/asset/ ' + this.id + '/groups',
|
||||||
{},
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${this.token}`
|
||||||
|
}
|
||||||
|
},
|
||||||
(error, response, body) => {
|
(error, response, body) => {
|
||||||
if (!error && (response.statusCode === 200 || response.statusCode === 201)) {
|
if (!error && (response.statusCode === 200 || response.statusCode === 201)) {
|
||||||
|
|
||||||
@ -203,10 +211,13 @@ class Asset {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_register(callback) {
|
_register(callback) {
|
||||||
|
|
||||||
Request.post(
|
Request.post(
|
||||||
this.apiEndpoint + '/audio/register/' + this.id,
|
this.apiEndpoint + '/audio/register/' + this.id,
|
||||||
{},
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${this.token}`
|
||||||
|
}
|
||||||
|
},
|
||||||
(error, response, body) => {
|
(error, response, body) => {
|
||||||
if (!error && (response.statusCode === 200 || response.statusCode === 201)) {
|
if (!error && (response.statusCode === 200 || response.statusCode === 201)) {
|
||||||
utils.writeLog(`Asset ${this.id} audio registered`);
|
utils.writeLog(`Asset ${this.id} audio registered`);
|
||||||
@ -221,13 +232,15 @@ class Asset {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_moveToChannel(callback) {
|
_moveToChannel(callback) {
|
||||||
|
|
||||||
Request.post(
|
Request.post(
|
||||||
this.apiEndpoint + '/audio/enter-group/' + this.id + '/' + this.groupId,
|
this.apiEndpoint + '/audio/enter-group/' + this.id + '/' + this.groupId,
|
||||||
{},
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${this.token}`
|
||||||
|
}
|
||||||
|
},
|
||||||
(error, response, body) => {
|
(error, response, body) => {
|
||||||
if (!error && (response.statusCode === 200 || response.statusCode === 201)) {
|
if (!error && (response.statusCode === 200 || response.statusCode === 201)) {
|
||||||
|
|
||||||
let hub = this.hub;
|
let hub = this.hub;
|
||||||
if (hub && hub.connected) {
|
if (hub && hub.connected) {
|
||||||
|
|
||||||
|
@ -10,7 +10,8 @@ class Simulator {
|
|||||||
constructor(configs) {
|
constructor(configs) {
|
||||||
|
|
||||||
this.configs = configs;
|
this.configs = configs;
|
||||||
|
this.token;
|
||||||
|
|
||||||
// Read assets ids from configs
|
// Read assets ids from configs
|
||||||
this.assetIds = configs.assets.ids;
|
this.assetIds = configs.assets.ids;
|
||||||
|
|
||||||
@ -19,20 +20,22 @@ class Simulator {
|
|||||||
|
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
this._manageLogs.bind(this),
|
this._manageLogs.bind(this),
|
||||||
this._unregisterAsstes.bind(this)
|
this._unregisterAsstes.bind(this),
|
||||||
|
this._getToken.bind(this),
|
||||||
],
|
],
|
||||||
(err, result) => {
|
(err, result) => {
|
||||||
if(err) {
|
if(err) {
|
||||||
console.log(err)
|
console.log(err)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
utils.writeLog(`Using token: ${this.token}`);
|
||||||
this._start();
|
this._start();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
_start() {
|
_start() {
|
||||||
this.assetIds.forEach(id => {
|
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;
|
module.exports = Simulator;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user