Skip to content

Commit

Permalink
Correction for home-assistant/core#125260 via an injection of httpx_c…
Browse files Browse the repository at this point in the history
…lient from homeassistant (#22)
  • Loading branch information
cnico authored Dec 2, 2024
1 parent 62bc0f3 commit 6c22ac3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ async def main():
await client.get_allmetrics()
print(client.metrics)

await client.close()

if __name__ == "__main__":
asyncio.run(main())
16 changes: 13 additions & 3 deletions netdata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
class Netdata(object):
"""A class for handling connections with a Netdata instance."""

def __init__(self, host, port=19999, tls=None, path=None, timeout=5.0):
def __init__(self, host, port=19999, tls=None, path=None, timeout=5.0, httpx_client:httpx.AsyncClient=None):
"""Initialize the connection to the Netdata instance."""
self.host = host
self.port = port
Expand All @@ -38,12 +38,22 @@ def __init__(self, host, port=19999, tls=None, path=None, timeout=5.0):
self.base_url = URL.build(
scheme=self.scheme, host=host, port=port, path=path
)

if httpx_client is None:
self.client = httpx.AsyncClient()
else:
# This gives the possibility to use HomeAssistant implementation that has no blocking operation.
# See : https://developers.home-assistant.io/docs/asyncio_blocking_operations/#load_default_certs
self.client = httpx_client

async def close(self):
"""Close the client session."""
await self.client.aclose()

async def get_data(self, url) -> Dict:
"""Execute a request to a data endpoint."""
try:
async with httpx.AsyncClient() as client:
response = await client.get(str(url), timeout = self.timeout)
response = await self.client.get(str(url), timeout = self.timeout)
except httpx.TimeoutException:
raise
except httpx.TransportError:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "netdata"
version = "1.2.0"
version = "1.3.0"
description = "Python API for interacting with Netdata"
authors = ["Fabian Affolter <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit 6c22ac3

Please sign in to comment.