Selaa lähdekoodia

Errors handling

Benoit 2 vuotta sitten
vanhempi
commit
ea8e297e84
1 muutettua tiedostoa jossa 66 lisäystä ja 34 poistoa
  1. 66 34
      src/widgets/omada/proxy.js

+ 66 - 34
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",
+          },
         });
+        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);
+        }
 
-      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);
+        const sites = JSON.parse(data);
 
-        if (status !== 200) {
-          logger.debug(`HTTTP ${status} logging into Omada api: ${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",
+          },
+        });
+        const switchresult = JSON.parse(result);
+
+        if (switchresult.errorCode !== 0) {
+          logger.debug(`HTTTP ${switchresult.errorCode} switching site with message ${switchresult.msg}`);
           return res.status(status).send(data);
         }
 
-        // eslint-disable-next-line no-unused-vars
-        [status, contentType, data] = await httpProxy(url, {
-          method: "GET",
+        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",
-            Authorization: `Bearer ${token}`,
           },
         });
-      }
 
-      if (status !== 200) {
-        return res.status(status).send(data);
-      }
+        data = JSON.parse(result);
 
-      return res.send(data.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`);
+      }
     }
   }