pbc/libs/api.js

71 lines
2.5 KiB
JavaScript

const logger = require('./logger.js');
const util = require('./util.js');
const constants = require('./constants.js');
const modep = require('./modep.js');
const endpoints = new Map();
function setEndpoint(url, get, post) {
if (get == undefined && post == undefined) {
return;
}
var endpoint = {};
if (get != undefined) {
endpoint.GET = get;
}
if (post != undefined) {
endpoint.POST = post;
}
endpoints.set(url, endpoint);
logger.debug('set up endpoint \'' + url + '\' > ' + JSON.stringify(endpoint));
}
function getEndpoints() {
return endpoints;
}
function setupEndpoints() {
return new Promise(function (resolve, reject) {
modep.getBanks()
.then(function (banks) {
setEndpoint(constants.API_BANKS, { method: modep.getBanks });
for (var index = 0; index < banks.length; index++) {
var id = banks[index].id;
setEndpoint(constants.API_BANKS + '/' + id, { method: modep.getBankById, args: id });
}
})
.then(modep.getPedalboards)
.then(function (pedalboards) {
setEndpoint(constants.API_PEDALBOARDS, { method: modep.getPedalboards });
for (var index = 0; index < pedalboards.length; index++) {
var id = pedalboards[index].id;
setEndpoint(
constants.API_PEDALBOARDS + '/' + id,
{ method: modep.getPedalboardById, args: id },
{ method: modep.setPedalboardById, args: id }
);
}
})
.then(function () {
setEndpoint(constants.API_PEDALBOARDS_DEFAULT, { method: modep.getDefaultPedalboard });
})
.then(function () {
setEndpoint(constants.API_PEDALBOARDS_CURRENT, { method: modep.getCurrentPedalboard });
})
.then(modep.getCurrentPedals)
.then(function (pedals) {
setEndpoint(constants.API_PEDALS, { method: modep.getCurrentPedals });
for (var index = 0; index < pedals.length; index++) {
var id = pedals[index].id;
setEndpoint(constants.API_PEDALS + '/' + id, { method: modep.getCurrentPedalById, args: id });
}
})
.then(resolve)
.catch(reject);
});
}
module.exports = {
setupEndpoints,
getEndpoints
}