Works in progress

This commit is contained in:
nicolargo 2023-12-09 10:09:17 +01:00
parent 5fa7ae4264
commit 5d054e12e1
2 changed files with 19 additions and 18 deletions

View File

@ -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,

View File

@ -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."""