2121class Netdata (object ):
2222 """A class for handling connections with a Netdata instance."""
2323
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 ):
2525 """Initialize the connection to the Netdata instance."""
2626 self .host = host
2727 self .port = port
@@ -38,12 +38,22 @@ def __init__(self, host, port=19999, tls=None, path=None, timeout=5.0):
3838 self .base_url = URL .build (
3939 scheme = self .scheme , host = host , port = port , path = path
4040 )
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 ()
4152
4253 async def get_data (self , url ) -> Dict :
4354 """Execute a request to a data endpoint."""
4455 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 )
4757 except httpx .TimeoutException :
4858 raise
4959 except httpx .TransportError :
0 commit comments