Errors handling
This commit is contained in:
parent
ab6d51a88c
commit
ea8e297e84
1 changed files with 68 additions and 36 deletions
|
@ -38,6 +38,7 @@ async function login(loginUrl, username, password) {
|
|||
return [status, token ?? data];
|
||||
}
|
||||
|
||||
|
||||
export default async function omadaProxyHandler(req, res) {
|
||||
const { group, service } = req.query;
|
||||
|
||||
|
@ -50,8 +51,6 @@ export default async function omadaProxyHandler(req, res) {
|
|||
|
||||
if (widget) {
|
||||
|
||||
// const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
|
||||
|
||||
const loginUrl = `${widget.url}/api/user/login?ajax`;
|
||||
|
||||
let status;
|
||||
|
@ -59,53 +58,86 @@ export default async function omadaProxyHandler(req, res) {
|
|||
let data;
|
||||
let result;
|
||||
let token;
|
||||
|
||||
[status, token] = await login(loginUrl, widget.username, widget.password);
|
||||
if (status !== 200) {
|
||||
if (widget.legacy) {
|
||||
[status, token] = await login(loginUrl, widget.username, widget.password);
|
||||
if (status !== 200) {
|
||||
logger.debug(`HTTTP ${status} logging into Omada api: ${token}`);
|
||||
return res.status(status).send(token);
|
||||
}
|
||||
|
||||
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",
|
||||
},
|
||||
// Switching to the site we want to gather stats from
|
||||
// First, we get the list of sites
|
||||
const sitesUrl = `${widget.url}/web/v1/controller?ajax=&token=${token}`;
|
||||
[status, contentType, data] = await httpProxy(sitesUrl, {
|
||||
method: "POST",
|
||||
params: { "token": token },
|
||||
body: JSON.stringify({
|
||||
"method": "getUserSites",
|
||||
"params": {
|
||||
"userName": widget.username
|
||||
}
|
||||
}),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
data = JSON.parse(result);
|
||||
if (status === 403) {
|
||||
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}`);
|
||||
const listresult = JSON.parse(data);
|
||||
if (listresult.errorCode !== 0) {
|
||||
logger.debug(`HTTTP ${listresult.errorCode} getting list of sites with message ${listresult.msg}`);
|
||||
return res.status(status).send(data);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
[status, contentType, data] = await httpProxy(url, {
|
||||
method: "GET",
|
||||
const sites = JSON.parse(data);
|
||||
|
||||
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: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
}
|
||||
const switchresult = JSON.parse(result);
|
||||
|
||||
if (status !== 200) {
|
||||
return res.status(status).send(data);
|
||||
}
|
||||
if (switchresult.errorCode !== 0) {
|
||||
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`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue