21
21
class Netdata (object ):
22
22
"""A class for handling connections with a Netdata instance."""
23
23
24
- def __init__ (self , host , port = 19999 , tls = None , path = None , timeout = 5.0 ):
24
+ def __init__ (self , host , port = 19999 , tls = None , path = None , timeout = 5.0 , httpx_client : httpx . AsyncClient = None ):
25
25
"""Initialize the connection to the Netdata instance."""
26
26
self .host = host
27
27
self .port = port
@@ -38,12 +38,22 @@ def __init__(self, host, port=19999, tls=None, path=None, timeout=5.0):
38
38
self .base_url = URL .build (
39
39
scheme = self .scheme , host = host , port = port , path = path
40
40
)
41
+
42
+ if httpx_client is None :
43
+ self .client = httpx .AsyncClient ()
44
+ else :
45
+ # This gives the possibility to use HomeAssistant implementation that has no blocking operation.
46
+ # See : https://developers.home-assistant.io/docs/asyncio_blocking_operations/#load_default_certs
47
+ self .client = httpx_client
48
+
49
+ async def close (self ):
50
+ """Close the client session."""
51
+ await self .client .aclose ()
41
52
42
53
async def get_data (self , url ) -> Dict :
43
54
"""Execute a request to a data endpoint."""
44
55
try :
45
- async with httpx .AsyncClient () as client :
46
- response = await client .get (str (url ), timeout = self .timeout )
56
+ response = await self .client .get (str (url ), timeout = self .timeout )
47
57
except httpx .TimeoutException :
48
58
raise
49
59
except httpx .TransportError :
0 commit comments