Skip to content

Commit 9c2d76d

Browse files
GuyKhfabaff
andauthored
Allow using a custom external httpx client (#5)
* Allow using a custom external `httpx` client * Update __init__.py * Update __init__.py * Update __init__.py * Update formatting Co-authored-by: Fabian Affolter <[email protected]>
1 parent e150b19 commit 9c2d76d

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

glances_api/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def __init__(
2222
verify_ssl=True,
2323
username=None,
2424
password=None,
25+
httpx_client=None,
2526
):
2627
"""Initialize the connection."""
2728
schema = "https" if ssl else "http"
@@ -33,13 +34,20 @@ def __init__(
3334
self.username = username
3435
self.password = password
3536
self.verify_ssl = verify_ssl
37+
self.httpx_client = httpx_client
3638

3739
async def get_data(self, endpoint):
3840
"""Retrieve the data."""
3941
url = "{}/{}".format(self.url, endpoint)
4042

43+
httpx_client = (
44+
self.httpx_client
45+
if self.httpx_client
46+
else httpx.AsyncClient(verify=self.verify_ssl)
47+
)
48+
4149
try:
42-
async with httpx.AsyncClient(verify=self.verify_ssl) as client:
50+
async with httpx_client as client:
4351
if self.password is None:
4452
response = await client.get(str(url))
4553
else:

0 commit comments

Comments
 (0)