Skip to content

Commit 16e4156

Browse files
committed
Migrate to httpx
1 parent 3196211 commit 16e4156

File tree

2 files changed

+36
-59
lines changed

2 files changed

+36
-59
lines changed

example.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
"""Sample script to use the Python API client for Glances."""
22
import asyncio
33

4-
import aiohttp
5-
64
from glances_api import Glances
75

86
HOST = "127.0.0.1"
@@ -11,20 +9,19 @@
119

1210
async def main():
1311
"""The main part of the example script."""
14-
async with aiohttp.ClientSession() as session:
15-
data = Glances(loop, session, version=VERSION)
12+
data = Glances(version=VERSION)
1613

17-
# Get the metrics for the memory
18-
await data.get_metrics("mem")
14+
# Get the metrics for the memory
15+
await data.get_metrics("mem")
1916

20-
# Print the values
21-
print("Memory values:", data.values)
17+
# Print the values
18+
print("Memory values:", data.values)
2219

23-
# Get the metrics about the disks
24-
await data.get_metrics("diskio")
20+
# Get the metrics about the disks
21+
await data.get_metrics("diskio")
2522

26-
# Print the values
27-
print("Disk values:", data.values)
23+
# Print the values
24+
print("Disk values:", data.values)
2825

2926

3027
loop = asyncio.get_event_loop()

glances_api/__init__.py

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
import asyncio
33
import logging
44

5-
import aiohttp
6-
import async_timeout
5+
import httpx
76

87
from . 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

Comments
 (0)