fixed most of the api stuff
This commit is contained in:
parent
fa0eba88e1
commit
c027f96309
4 changed files with 106 additions and 72 deletions
54
libs/api.js
54
libs/api.js
|
@ -5,57 +5,59 @@ const modep = require('./modep.js');
|
|||
|
||||
const endpoints = new Map();
|
||||
|
||||
function setEndpoints(url, data) {
|
||||
var startTime = new Date();
|
||||
var index = 0;
|
||||
var elements = [];
|
||||
for (var index = 0; index < data.length; index++) {
|
||||
var element = data[index];
|
||||
element.id = index;
|
||||
setEndpoint(url + '/' + element.id, [constants.HTTP_GET, constants.HTTP_POST], element);
|
||||
elements.push(element);
|
||||
function setEndpoint(url, get, post) {
|
||||
if (get == undefined && post == undefined) {
|
||||
return;
|
||||
}
|
||||
elements = elements.sort(function (a, b) {
|
||||
return a.id - b.id;
|
||||
});
|
||||
setEndpoint(url, constants.HTTP_GET, elements);
|
||||
logger.debug('setting up ' + elements.length + ' endpoint(s) for path \'' + url + '\' took ' + util.timeDiff(startTime) + 'ms');
|
||||
}
|
||||
|
||||
function setEndpoint(url, types, method, args) {
|
||||
logger.debug('setting up \'' + types + '\' endpoint \'' + url + '\'...');
|
||||
endpoints.set(url, { types: types, method: method, args: args });
|
||||
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;
|
||||
}
|
||||
|
||||
// TODO: IMPLEMENT POST ENDPOINTS
|
||||
function setupEndpoints() {
|
||||
return new Promise(function (resolve, reject) {
|
||||
modep.getBanks()
|
||||
.then(function (banks) {
|
||||
setEndpoint(constants.API_BANKS, constants.HTTP_GET, modep.getBanks);
|
||||
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, constants.HTTP_GET, modep.getBankById, id)
|
||||
setEndpoint(constants.API_BANKS + '/' + id, { method: modep.getBankById, args: id });
|
||||
}
|
||||
})
|
||||
.then(modep.getPedalboards)
|
||||
.then(function (pedalboards) {
|
||||
setEndpoint(constants.API_PEDALBOARDS, constants.HTTP_GET, modep.getPedalboards);
|
||||
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, constants.HTTP_GET, modep.getPedalboardById, 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, constants.HTTP_GET, modep.getCurrentPedals);
|
||||
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, constants.HTTP_GET, modep.getCurrentPedalById, id)
|
||||
setEndpoint(constants.API_PEDALS + '/' + id, { method: modep.getCurrentPedalById, args: id });
|
||||
}
|
||||
})
|
||||
.then(resolve)
|
||||
|
|
|
@ -7,6 +7,7 @@ const fs = require('fs');
|
|||
const { spawn } = require('child_process')
|
||||
const ttl2jsonld = require('@frogcat/ttl2jsonld').parse;
|
||||
|
||||
// TODO: MOVE TO CACHE.JS
|
||||
var banks = undefined;
|
||||
var pedalboards = undefined;
|
||||
var defaultPedalboard = undefined;
|
||||
|
@ -328,7 +329,7 @@ function sendValueToControl(value, control) {
|
|||
|
||||
function setPedalboardById(pedalboardId) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
if (!pedalboardId) {
|
||||
if (pedalboardId == undefined) {
|
||||
return reject('error: no pedalboard id given');
|
||||
}
|
||||
getPedalboardById(pedalboardId)
|
||||
|
@ -346,11 +347,15 @@ function setPedalboard(pedalboard) {
|
|||
getCurrentPedalboard()
|
||||
.then(function (currentPedalboard) {
|
||||
if (pedalboard.id == currentPedalboard.id) {
|
||||
return resolve('pedalboard \'' + pedalboard.id + '\' is already active');
|
||||
return Promise.reject('pedalboard \'' + pedalboard.id + '\' is already active');
|
||||
}
|
||||
})
|
||||
.then(reset)
|
||||
.then(util.httpPOST(config.modep.host, config.modep.port, '/pedalboard/load_bundle/?bundlepath=' + pedalboard.bundle))
|
||||
.then(function () {
|
||||
return reset()
|
||||
})
|
||||
.then(function () {
|
||||
return util.httpPOST(config.modep.host, config.modep.port, '/pedalboard/load_bundle/?bundlepath=' + pedalboard.bundle)
|
||||
})
|
||||
.then(function () {
|
||||
currentPedalboard = pedalboard
|
||||
parseCurrentPedalboard(currentPedalboard)
|
||||
|
|
|
@ -33,54 +33,77 @@ function handleRequests() {
|
|||
logger.debug(msg);
|
||||
return;
|
||||
}
|
||||
if (!endpoint.types.includes(request.method)) {
|
||||
var msg = 'endpoint \'' + request.url + '\' does not support ' + request.method + ' requests';
|
||||
|
||||
// if (!endpoint.types.includes(request.method)) {
|
||||
// var msg = 'endpoint \'' + request.url + '\' does not support ' + request.method + ' requests';
|
||||
// response.writeHead(405);
|
||||
// response.end(msg);
|
||||
// logger.debug(msg);
|
||||
// return;
|
||||
// }
|
||||
// if (request.method == constants.HTTP_GET) {
|
||||
// handleGET(endpoint[constants.HTTP_GET])
|
||||
// .then(function (result) {
|
||||
// response.writeHead(200);
|
||||
// response.end(result);
|
||||
// })
|
||||
// .catch(function (err, code) {
|
||||
// if (!code) {
|
||||
// code = 500;
|
||||
// }
|
||||
// response.writeHead(code);
|
||||
// response.end(err);
|
||||
// });
|
||||
// return;
|
||||
// }
|
||||
// if (request.method == constants.HTTP_POST) {
|
||||
// handlePOST(request, response)
|
||||
// .then(function (result) {
|
||||
// response.writeHead(200);
|
||||
// response.end(result);
|
||||
// })
|
||||
// .catch(function (err, code) {
|
||||
// if (!code) {
|
||||
// code = 500;
|
||||
// }
|
||||
// response.writeHead(code);
|
||||
// response.end(err);
|
||||
// });
|
||||
// return;
|
||||
// }
|
||||
endpoint = endpoint[request.method];
|
||||
if (endpoint == undefined || endpoint.method == undefined) {
|
||||
var msg = 'error: endpoint \'' + request.url + '\' does not have any handlers for ' + request.method + ' requests';
|
||||
response.writeHead(405);
|
||||
response.end(msg);
|
||||
logger.debug(msg);
|
||||
return;
|
||||
}
|
||||
if (request.method == constants.HTTP_GET) {
|
||||
handleGET(endpoint)
|
||||
.then(function (result) {
|
||||
response.writeHead(200);
|
||||
endpoint.method(endpoint.args)
|
||||
.then(function (result) {
|
||||
response.writeHead(200);
|
||||
try {
|
||||
response.end(JSON.stringify(result));
|
||||
} catch (err) {
|
||||
response.end(result);
|
||||
})
|
||||
.catch(function (err, code) {
|
||||
if (!code) {
|
||||
code = 500;
|
||||
}
|
||||
response.writeHead(code);
|
||||
response.end(err);
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (request.method == constants.HTTP_POST) {
|
||||
handlePOST(request, response)
|
||||
.then(function (result) {
|
||||
response.writeHead(200);
|
||||
response.end(result);
|
||||
})
|
||||
.catch(function (err, code) {
|
||||
if (!code) {
|
||||
code = 500;
|
||||
}
|
||||
response.writeHead(code);
|
||||
response.end(err);
|
||||
});
|
||||
return;
|
||||
}
|
||||
var msg = 'endpoint \'' + request.url + '\' does not have any handlers for ' + request.method + ' requests';
|
||||
response.writeHead(405);
|
||||
response.end(msg);
|
||||
logger.debug(msg);
|
||||
}
|
||||
return;
|
||||
})
|
||||
.catch(function (err, code) {
|
||||
if (code == undefined) {
|
||||
code = 500;
|
||||
}
|
||||
response.writeHead(500);
|
||||
response.end(err);
|
||||
return;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function handleGET(endpoint) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
if (!endpoint.method) {
|
||||
return reject('error: no method defined for endpoint \'' + endpoint.url + '\'');
|
||||
return reject('error: no handler defined for endpoint \'' + endpoint.url + '\'');
|
||||
}
|
||||
endpoint.method(endpoint.args)
|
||||
.then(function (data) {
|
||||
|
@ -90,7 +113,7 @@ function handleGET(endpoint) {
|
|||
});
|
||||
}
|
||||
|
||||
function handlePOST(request, response) {
|
||||
function handlePOST(endpoint) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
if (request.url.startsWith(constants.API_PEDALS)) {
|
||||
handlePOSTPedals(request)
|
||||
|
|
14
libs/util.js
14
libs/util.js
|
@ -40,21 +40,25 @@ function httpRequest(host, port, path, method, args) {
|
|||
if (!response) {
|
||||
return reject('error: no response from host for http \'' + method + '\' request \'' + host + ':' + port + path + '\'');
|
||||
}
|
||||
logger.debug('http \'' + method + '\' request \'' + host + ':' + port + path + '\' returned status code ' + response.statusCode);
|
||||
var responseData = "";
|
||||
response.on('data', function (data) {
|
||||
responseData += data;
|
||||
});
|
||||
response.on('end', function () {
|
||||
logger.debug('http \'' + method + '\' request \'' + host + ':' + port + path + '\' returned data \'' + responseData + '\'');
|
||||
logger.debug('http \'' + method + '\' request \'' + host + ':' + port + path + '\' returned status code \'' + response.statusCode + '\' and data \'' + responseData + '\'');
|
||||
var fn = resolve;
|
||||
if (response.statusCode != 200) {
|
||||
fn = reject;
|
||||
}
|
||||
if (!responseData) {
|
||||
return resolve();
|
||||
return fn();
|
||||
}
|
||||
try {
|
||||
return resolve(JSON.parse(responseData));
|
||||
return fn(JSON.parse(responseData));
|
||||
} catch (err) {
|
||||
return resolve(responseData);
|
||||
return fn(responseData);
|
||||
}
|
||||
|
||||
});
|
||||
});
|
||||
request.on('error', function (err) {
|
||||
|
|
Loading…
Reference in a new issue