Skip to content

Commit

Permalink
 Works in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolargo committed Dec 9, 2023
1 parent 5fa7ae4 commit 5d054e1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
9 changes: 5 additions & 4 deletions glances/outputs/glances_restful_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ def __init__(self, config=None, args=None):

# 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)

Expand Down Expand Up @@ -509,9 +511,8 @@ def _api_history(self, plugin, nb: int = 0):
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,
Expand Down Expand Up @@ -610,7 +611,7 @@ def _api_item_history(self, plugin, item, nb: int = 0):

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,
Expand Down
28 changes: 14 additions & 14 deletions unitest-restful.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ def setUp(self):
"""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'})
Expand Down Expand Up @@ -76,16 +76,18 @@ def test_001_all(self):
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."""
Expand Down Expand Up @@ -203,14 +205,12 @@ def test_010_history(self):
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."""
Expand All @@ -229,7 +229,7 @@ def test_012_status(self):
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."""
Expand Down

0 comments on commit 5d054e1

Please sign in to comment.