Errors handling

This commit is contained in:
Benoit 2022-12-06 14:28:05 +01:00
parent ab6d51a88c
commit ea8e297e84

View File

@ -38,6 +38,7 @@ async function login(loginUrl, username, password) {
return [status, token ?? data]; return [status, token ?? data];
} }
export default async function omadaProxyHandler(req, res) { export default async function omadaProxyHandler(req, res) {
const { group, service } = req.query; const { group, service } = req.query;
@ -50,8 +51,6 @@ export default async function omadaProxyHandler(req, res) {
if (widget) { if (widget) {
// const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
const loginUrl = `${widget.url}/api/user/login?ajax`; const loginUrl = `${widget.url}/api/user/login?ajax`;
let status; let status;
@ -59,53 +58,86 @@ export default async function omadaProxyHandler(req, res) {
let data; let data;
let result; let result;
let token; let token;
if (widget.legacy) {
[status, token] = await login(loginUrl, widget.username, widget.password); [status, token] = await login(loginUrl, widget.username, widget.password);
if (status !== 200) { if (status !== 200) {
logger.debug(`HTTTP ${status} logging into Omada api: ${token}`); logger.debug(`HTTTP ${status} logging into Omada api: ${token}`);
return res.status(status).send(token); return res.status(status).send(token);
} }
// Switching to the site we want to gather stats from
const url = `${widget.url}/web/v1/controller?globalStat=&token=${token}`; // First, we get the list of sites
const sitesUrl = `${widget.url}/web/v1/controller?ajax=&token=${token}`;
// eslint-disable-next-line prefer-const [status, contentType, data] = await httpProxy(sitesUrl, {
[status, contentType, result] = await httpProxy(url, { method: "POST",
method: "POST", params: { "token": token },
params: {"token": token}, body: JSON.stringify({
body: JSON.stringify({ "method": "getUserSites",
"method": "getGlobalStat", "params": {
}), "userName": widget.username
headers: { }
"Content-Type": "application/json", }),
}, headers: {
"Content-Type": "application/json",
},
}); });
const listresult = JSON.parse(data);
data = JSON.parse(result); if (listresult.errorCode !== 0) {
if (status === 403) { logger.debug(`HTTTP ${listresult.errorCode} getting list of sites with message ${listresult.msg}`);
logger.debug(`HTTTP ${status} retrieving data from Omada api, logging in and trying again.`);
cache.del(tokenCacheKey);
[status, token] = await login(loginUrl, widget.username, widget.password);
if (status !== 200) {
logger.debug(`HTTTP ${status} logging into Omada api: ${data}`);
return res.status(status).send(data); return res.status(status).send(data);
} }
// eslint-disable-next-line no-unused-vars const sites = JSON.parse(data);
[status, contentType, data] = await httpProxy(url, {
method: "GET", const sitetoswitch = sites.result.siteList.filter(site => site.name === widget.site);
const { siteName } = sitetoswitch[0];
const switchUrl = `${widget.url}/web/v1/controller?ajax=&token=${token}`;
[status, contentType, result] = await httpProxy(switchUrl, {
method: "POST",
params: { "token": token },
body: JSON.stringify({
"method": "switchSite",
"params": { "siteName": siteName, "userName": widget.username }
}),
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: `Bearer ${token}`,
}, },
}); });
} const switchresult = JSON.parse(result);
if (status !== 200) { if (switchresult.errorCode !== 0) {
return res.status(status).send(data); logger.debug(`HTTTP ${switchresult.errorCode} switching site with message ${switchresult.msg}`);
} return res.status(status).send(data);
}
return res.send(data.result); const url = `${widget.url}/web/v1/controller?globalStat=&token=${token}`;
// eslint-disable-next-line prefer-const
[status, contentType, result] = await httpProxy(url, {
method: "POST",
params: { "token": token },
body: JSON.stringify({
"method": "getGlobalStat",
}),
headers: {
"Content-Type": "application/json",
},
});
data = JSON.parse(result);
if (data.errorCode !== 0) {
return res.status(status).send(data);
}
return res.send(data.result);
} else {
// Working on it but I can't test it
logger.debug(`unsupported for now but I'm working on it`);
}
} }
} }