From ea8e297e849c2ef5659bfec94d76d2fff8677c4c Mon Sep 17 00:00:00 2001 From: Benoit Date: Tue, 6 Dec 2022 14:28:05 +0100 Subject: [PATCH] Errors handling --- src/widgets/omada/proxy.js | 104 ++++++++++++++++++++++++------------- 1 file changed, 68 insertions(+), 36 deletions(-) diff --git a/src/widgets/omada/proxy.js b/src/widgets/omada/proxy.js index 544502c5..7366dca8 100644 --- a/src/widgets/omada/proxy.js +++ b/src/widgets/omada/proxy.js @@ -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`); + } } }