Skip to content

Commit 6c22ac3

Browse files
authored
Correction for home-assistant/core#125260 via an injection of httpx_client from homeassistant (#22)
1 parent 62bc0f3 commit 6c22ac3

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

example.py

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ async def main():
2828
await client.get_allmetrics()
2929
print(client.metrics)
3030

31+
await client.close()
3132

3233
if __name__ == "__main__":
3334
asyncio.run(main())

netdata/__init__.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
class 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:

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "netdata"
3-
version = "1.2.0"
3+
version = "1.3.0"
44
description = "Python API for interacting with Netdata"
55
authors = ["Fabian Affolter <[email protected]>"]
66
license = "MIT"

0 commit comments

Comments
 (0)