From f19764baa613a4b9223488e16b0a6711c830a959 Mon Sep 17 00:00:00 2001 From: Jon Connell Date: Sun, 27 Oct 2024 12:55:11 +0000 Subject: [PATCH] Ensure SSL certificate loading is done in a non-blocking fashion --- pyproject.toml | 2 +- src/connectsensor/client.py | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index f392b13..47874d6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,7 +15,7 @@ name = "kingspan-connect-sensor" packages = [{include = "connectsensor", from = "src"}] readme = "README.md" repository = "https://github.com/masaccio/kingspan-connect-sensor" -version = "3.0.4" +version = "3.0.5" [tool.poetry.dependencies] async-property = "^0.2.1" diff --git a/src/connectsensor/client.py b/src/connectsensor/client.py index a0e51c3..f1051ec 100644 --- a/src/connectsensor/client.py +++ b/src/connectsensor/client.py @@ -4,6 +4,7 @@ from zeep import Client as SoapClient from zeep import AsyncClient as AsyncSoapClient +from asyncio import get_running_loop from .tank import Tank, AsyncTank from .exceptions import APIError @@ -70,9 +71,8 @@ def _get_history(self, signalman_no, start_date=None, end_date=None): class AsyncSensorClient: def __init__(self, base=DEFAULT_SERVER): - url = urljoin(base, WSDL_PATH) - self._soap_client = AsyncSoapClient(url) - self._soap_client.set_ns_prefix(None, "http://mobileapp/") + self._soap_url = urljoin(base, WSDL_PATH) + self._soap_client = None async def __aenter__(self): return self @@ -80,7 +80,19 @@ async def __aenter__(self): async def __aexit__(self, exc_t, exc_v, exc_tb): pass + async def _init_zeep(self): + # Zeep.AsyncClient uses httpx which loads SSL cerficates at construction + # time. An alternative would be disabling certificate verification. + loop = get_running_loop() + self._soap_client = await loop.run_in_executor( + None, AsyncSoapClient, self._soap_url + ) + self._soap_client.set_ns_prefix(None, "http://mobileapp/") + async def login(self, username, password): + if self._soap_client is None: + await self._init_zeep() + self._username = username self._password = password