22import asyncio
33import logging
44
5- import aiohttp
6- import async_timeout
5+ import httpx
76
87from . import exceptions
98
@@ -16,8 +15,6 @@ class Glances(object):
1615
1716 def __init__ (
1817 self ,
19- loop ,
20- session ,
2118 host = "localhost" ,
2219 port = 61208 ,
2320 version = 2 ,
@@ -27,67 +24,50 @@ def __init__(
2724 ):
2825 """Initialize the connection."""
2926 schema = "https" if ssl else "http"
30- self ._loop = loop
31- self ._session = session
3227 self .url = _RESOURCE .format (
3328 schema = schema , host = host , port = port , version = version
3429 )
35- self .data = None
36- self .values = None
3730 self .plugins = None
31+ self .values = None
3832 self .username = username
3933 self .password = password
4034
41- async def get_data (self ):
35+ async def get_data (self , endpoint ):
4236 """Retrieve the data."""
43- url = "{}/{}" .format (self .url , "all" )
37+ url = "{}/{}" .format (self .url , endpoint )
4438
4539 try :
46- with async_timeout . timeout ( 5 ) :
40+ async with httpx . AsyncClient () as client :
4741 if self .password is None :
48- response = await self . _session . get (url )
42+ response = await client . get (str ( url ) )
4943 else :
50- auth = aiohttp .BasicAuth (self .username , self .password )
51- response = await self ._session .get (url , auth = auth )
44+ response = await client .get (
45+ str (url ), auth = (self .username , self .password )
46+ )
47+ except httpx .ConnectError :
48+ raise exceptions .GlancesApiConnectionError (f"Connection to { url } failed" )
5249
53- _LOGGER .debug ("Response from Glances API: %s" , response .status )
54- response .raise_for_status ()
55- print (response .status )
56- print (response .text )
57- self .data = await response .json ()
58- _LOGGER .debug (self .data )
59- except aiohttp .ClientResponseError as err :
60- _LOGGER .error (err .message )
61- raise exceptions .GlancesApiAuthorizationError from err
62- except (asyncio .TimeoutError , aiohttp .ClientError ) as err :
63- _LOGGER .error ("Can not load data from Glances API" )
64- raise exceptions .GlancesApiConnectionError
50+ if response .status_code == httpx .codes .OK :
51+ try :
52+ _LOGGER .debug (response .json ())
53+ # print(self.data)
54+ # self.data = response.json()
55+ if endpoint == "all" :
56+ self .data = response .json ()
57+ if endpoint == "pluginslist" :
58+ self .plugins = response .json ()
59+ except TypeError :
60+ _LOGGER .error ("Can not load data from Glances" )
61+ raise exceptions .GlancesApiConnectionError (
62+ "Unable to get the data from Glances"
63+ )
6564
6665 async def get_metrics (self , element ):
6766 """Get all the metrics for a monitored element."""
68- await self .get_data ()
69- await self .get_plugins ( )
67+ await self .get_data ("all" )
68+ await self .get_data ( "pluginslist" )
7069
7170 if element in self .plugins :
7271 self .values = self .data [element ]
7372 else :
7473 raise exceptions .GlancesApiError ("Element data not available" )
75-
76- async def get_plugins (self ):
77- """Retrieve the available plugins."""
78- url = "{}/{}" .format (self .url , "pluginslist" )
79-
80- try :
81- with async_timeout .timeout (5 ):
82- if self .password is None :
83- response = await self ._session .get (url )
84- else :
85- auth = aiohttp .BasicAuth (self .username , self .password )
86- response = await self ._session .get (url , auth = auth )
87-
88- _LOGGER .debug ("Response from Glances API: %s" , response .status )
89- self .plugins = await response .json ()
90- _LOGGER .debug (self .plugins )
91- except (asyncio .TimeoutError , aiohttp .ClientError ):
92- _LOGGER .error ("Can not load plugins from Glances API" )
93- raise exceptions .GlancesApiConnectionError ()
0 commit comments