summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornicolargo <nicolas@nicolargo.com>2023-12-09 10:09:17 +0100
committernicolargo <nicolas@nicolargo.com>2023-12-09 10:09:17 +0100
commit5d054e12e1d38aeb6f5c026e1760665291bce110 (patch)
tree5d022fb980c29acc6d78c1d6929abf37121b61f4
parent5fa7ae42641ca8e53d993d17d4b2b31eedf9563c (diff)
 Works in progress
-rw-r--r--glances/outputs/glances_restful_api.py9
-rwxr-xr-xunitest-restful.py28
2 files changed, 19 insertions, 18 deletions
diff --git a/glances/outputs/glances_restful_api.py b/glances/outputs/glances_restful_api.py
index 3c5ba284..54c4f908 100644
--- a/glances/outputs/glances_restful_api.py
+++ b/glances/outputs/glances_restful_api.py
@@ -111,6 +111,8 @@ class GlancesRestfulApi(object):
# FastAPI Enable GZIP compression
# https://fastapi.tiangolo.com/advanced/middleware/
+ # TODO: do not work for the moment
+ # curl return a binary stream, not the JSON
self._app.add_middleware(GZipMiddleware,
minimum_size=1000)
@@ -509,9 +511,8 @@ class GlancesRestfulApi(object):
self.__update__()
try:
- # TODO: to be refactor to not return a JSON object
- # Get the JSON value of the stat ID
- statval = self.stats.get_plugin(plugin).get_stats_history(nb=int(nb))
+ # Get the RAW value of the stat ID
+ statval = self.stats.get_plugin(plugin).get_raw_history(nb=int(nb))
except Exception as e:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
@@ -610,7 +611,7 @@ class GlancesRestfulApi(object):
try:
# Get the RAW value of the stat history
- ret = self.stats.get_plugin(plugin).get_stats_history(item, nb=nb)
+ ret = self.stats.get_plugin(plugin).get_raw_history(item, nb=nb)
except Exception as e:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
diff --git a/unitest-restful.py b/unitest-restful.py
index dd725f2c..e4695d69 100755
--- a/unitest-restful.py
+++ b/unitest-restful.py
@@ -39,12 +39,12 @@ class TestGlances(unittest.TestCase):
"""The function is called *every time* before test_*."""
print('\n' + '=' * 78)
- def http_get(self, url, deflate=False):
+ def http_get(self, url, gzip=False):
"""Make the request"""
- if deflate:
+ if gzip:
ret = requests.get(url,
stream=True,
- headers={'Accept-encoding': 'deflate'})
+ headers={'Accept-encoding': 'gzip'})
else:
ret = requests.get(url,
headers={'Accept-encoding': 'identity'})
@@ -76,16 +76,18 @@ class TestGlances(unittest.TestCase):
req = self.http_get("%s/%s" % (URL, method))
self.assertTrue(req.ok)
+ self.assertTrue(req.json(), dict)
- def test_001a_all_deflate(self):
+ def test_001a_all_gzip(self):
"""All."""
method = "all"
- print('INFO: [TEST_001a] Get all stats (with Deflate compression)')
+ print('INFO: [TEST_001a] Get all stats (with GZip compression)')
print("HTTP RESTful request: %s/%s" % (URL, method))
- req = self.http_get("%s/%s" % (URL, method), deflate=True)
+ req = self.http_get("%s/%s" % (URL, method), gzip=True)
self.assertTrue(req.ok)
- self.assertTrue(req.headers['Content-Encoding'] == 'deflate')
+ self.assertTrue(req.headers['Content-Encoding'] == 'gzip')
+ self.assertTrue(req.json(), dict)
def test_002_pluginslist(self):
"""Plugins list."""
@@ -203,14 +205,12 @@ class TestGlances(unittest.TestCase):
self.assertTrue(len(req.json()['user']) > 1)
print("HTTP RESTful request: %s/cpu/system/%s" % (URL, method))
req = self.http_get("%s/cpu/system/%s" % (URL, method))
- self.assertIsInstance(req.json(), dict)
- self.assertIsInstance(req.json()['system'], list)
- self.assertTrue(len(req.json()['system']) > 0)
+ self.assertIsInstance(req.json(), list)
+ self.assertIsInstance(req.json()[0], list)
print("HTTP RESTful request: %s/cpu/system/%s/3" % (URL, method))
req = self.http_get("%s/cpu/system/%s/3" % (URL, method))
- self.assertIsInstance(req.json(), dict)
- self.assertIsInstance(req.json()['system'], list)
- self.assertTrue(len(req.json()['system']) > 1)
+ self.assertIsInstance(req.json(), list)
+ self.assertIsInstance(req.json()[0], list)
def test_011_issue1401(self):
"""Check issue #1401."""
@@ -229,7 +229,7 @@ class TestGlances(unittest.TestCase):
req = self.http_get("%s/%s" % (URL, method))
self.assertTrue(req.ok)
- self.assertEqual(req.text, "Active")
+ self.assertEqual(req.json(), "Active")
def test_013_top(self):
"""Values."""