fixed most of the api stuff

This commit is contained in:
Daniel Sommer 2022-02-08 11:11:49 +01:00
parent fa0eba88e1
commit c027f96309
4 changed files with 106 additions and 72 deletions

View file

@ -5,57 +5,59 @@ const modep = require('./modep.js');
const endpoints = new Map(); const endpoints = new Map();
function setEndpoints(url, data) { function setEndpoint(url, get, post) {
var startTime = new Date(); if (get == undefined && post == undefined) {
var index = 0; return;
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);
} }
elements = elements.sort(function (a, b) { var endpoint = {};
return a.id - b.id; if (get != undefined) {
}); endpoint.GET = get;
setEndpoint(url, constants.HTTP_GET, elements);
logger.debug('setting up ' + elements.length + ' endpoint(s) for path \'' + url + '\' took ' + util.timeDiff(startTime) + 'ms');
} }
if (post != undefined) {
function setEndpoint(url, types, method, args) { endpoint.POST = post;
logger.debug('setting up \'' + types + '\' endpoint \'' + url + '\'...'); }
endpoints.set(url, { types: types, method: method, args: args }); endpoints.set(url, endpoint);
logger.debug('set up endpoint \'' + url + '\' > ' + JSON.stringify(endpoint));
} }
function getEndpoints() { function getEndpoints() {
return endpoints; return endpoints;
} }
// TODO: IMPLEMENT POST ENDPOINTS
function setupEndpoints() { function setupEndpoints() {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
modep.getBanks() modep.getBanks()
.then(function (banks) { .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++) { for (var index = 0; index < banks.length; index++) {
var id = banks[index].id; 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(modep.getPedalboards)
.then(function (pedalboards) { .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++) { for (var index = 0; index < pedalboards.length; index++) {
var id = pedalboards[index].id; 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(modep.getCurrentPedals)
.then(function (pedals) { .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++) { for (var index = 0; index < pedals.length; index++) {
var id = pedals[index].id; 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) .then(resolve)

View file

@ -7,6 +7,7 @@ const fs = require('fs');
const { spawn } = require('child_process') const { spawn } = require('child_process')
const ttl2jsonld = require('@frogcat/ttl2jsonld').parse; const ttl2jsonld = require('@frogcat/ttl2jsonld').parse;
// TODO: MOVE TO CACHE.JS
var banks = undefined; var banks = undefined;
var pedalboards = undefined; var pedalboards = undefined;
var defaultPedalboard = undefined; var defaultPedalboard = undefined;
@ -328,7 +329,7 @@ function sendValueToControl(value, control) {
function setPedalboardById(pedalboardId) { function setPedalboardById(pedalboardId) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
if (!pedalboardId) { if (pedalboardId == undefined) {
return reject('error: no pedalboard id given'); return reject('error: no pedalboard id given');
} }
getPedalboardById(pedalboardId) getPedalboardById(pedalboardId)
@ -346,11 +347,15 @@ function setPedalboard(pedalboard) {
getCurrentPedalboard() getCurrentPedalboard()
.then(function (currentPedalboard) { .then(function (currentPedalboard) {
if (pedalboard.id == currentPedalboard.id) { 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(function () {
.then(util.httpPOST(config.modep.host, config.modep.port, '/pedalboard/load_bundle/?bundlepath=' + pedalboard.bundle)) return reset()
})
.then(function () {
return util.httpPOST(config.modep.host, config.modep.port, '/pedalboard/load_bundle/?bundlepath=' + pedalboard.bundle)
})
.then(function () { .then(function () {
currentPedalboard = pedalboard currentPedalboard = pedalboard
parseCurrentPedalboard(currentPedalboard) parseCurrentPedalboard(currentPedalboard)

View file

@ -33,54 +33,77 @@ function handleRequests() {
logger.debug(msg); logger.debug(msg);
return; 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.writeHead(405);
response.end(msg); response.end(msg);
logger.debug(msg); logger.debug(msg);
return; return;
} }
if (request.method == constants.HTTP_GET) { endpoint.method(endpoint.args)
handleGET(endpoint)
.then(function (result) { .then(function (result) {
response.writeHead(200); response.writeHead(200);
try {
response.end(JSON.stringify(result));
} catch (err) {
response.end(result); response.end(result);
}
return;
}) })
.catch(function (err, code) { .catch(function (err, code) {
if (!code) { if (code == undefined) {
code = 500; code = 500;
} }
response.writeHead(code); response.writeHead(500);
response.end(err); response.end(err);
});
return; 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);
}); });
} }
function handleGET(endpoint) { function handleGET(endpoint) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
if (!endpoint.method) { 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) endpoint.method(endpoint.args)
.then(function (data) { .then(function (data) {
@ -90,7 +113,7 @@ function handleGET(endpoint) {
}); });
} }
function handlePOST(request, response) { function handlePOST(endpoint) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
if (request.url.startsWith(constants.API_PEDALS)) { if (request.url.startsWith(constants.API_PEDALS)) {
handlePOSTPedals(request) handlePOSTPedals(request)

View file

@ -40,21 +40,25 @@ function httpRequest(host, port, path, method, args) {
if (!response) { if (!response) {
return reject('error: no response from host for http \'' + method + '\' request \'' + host + ':' + port + path + '\''); 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 = ""; var responseData = "";
response.on('data', function (data) { response.on('data', function (data) {
responseData += data; responseData += data;
}); });
response.on('end', function () { 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) { if (!responseData) {
return resolve(); return fn();
} }
try { try {
return resolve(JSON.parse(responseData)); return fn(JSON.parse(responseData));
} catch (err) { } catch (err) {
return resolve(responseData); return fn(responseData);
} }
}); });
}); });
request.on('error', function (err) { request.on('error', function (err) {