From ad3cfbe74dea731c0992b3d154530e31226a0afa Mon Sep 17 00:00:00 2001 From: Francois JUMELLE Date: Sun, 2 Nov 2025 23:37:06 +0100 Subject: [PATCH] Improve http get/post with internal retry --- plugin.py | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/plugin.py b/plugin.py index 25f1712..b8dcbcb 100755 --- a/plugin.py +++ b/plugin.py @@ -195,14 +195,12 @@ class BasePlugin: 'X-Gizwits-Application-Id': 'c70a66ff039d41b4a220e198b0fcc8b3', } data = '{ "username": "'+user+'", "password": "'+password+'", "lang": "en" }' + time.sleep(0.5) + url = 'https://euapi.gizwits.com/app/login' try: - time.sleep(0.5) - url = 'https://euapi.gizwits.com/app/login' - response = requests.post(url, headers=headers, data=data, timeout=self._HTTP_TIMEOUT).json() + response = http("post", url, headers, data) except Exception as exc: Domoticz.Error("Cannot open connection to Heatzy API to get the token: " + str(exc)) - #Domoticz.Error("URL: " + str(url)) - #Domoticz.Error("Headers: " + str(headers)) Domoticz.Error("Data: " + str(data)) #Decrease retry self.retry = self.retry - 1 @@ -244,7 +242,7 @@ class BasePlugin: params = (('limit', '20'), ('skip', '0'),) url = 'https://euapi.gizwits.com/app/bindings' try: - response = requests.get(url, headers=headers, params=params, timeout=self._HTTP_TIMEOUT).json() + response = http("get", url, headers, params=params) except Exception as exc: Domoticz.Error("Cannot open connection to Heatzy API to get the device id: " + str(exc)) #Domoticz.Error("URL: " + str(url)) @@ -293,7 +291,7 @@ class BasePlugin: url = f"https://euapi.gizwits.com/app/devdata/{did}/latest" try: - response = requests.get(url, headers=headers, timeout=self._HTTP_TIMEOUT).json() + response = http("get", url, headers) except Exception as exc: message = f"Cannot open connection to Heatzy API to get the mode for {alias} (retry={self.retry}): {exc}" if self.retry <= 0: @@ -398,7 +396,7 @@ class BasePlugin: self.did[deviceid]["command_at"] = time.time() url = f"https://euapi.gizwits.com/app/control/{did}" try: - response = requests.post(url, headers=headers, data=data, timeout=self._HTTP_TIMEOUT).json() + response = http("post", url, headers, data) except Exception as exc: Domoticz.Error("Cannot open connection to Heatzy API to set the mode: " + str(exc)) #Domoticz.Error("URL: " + str(url)) @@ -461,6 +459,33 @@ class BasePlugin: Domoticz.Status("Reset retry counter") self.retry = self._MAX_RETRY_PER_DEVICE * len(self.did) +def http(mode:str, url:str, headers:dict, data:str="", params:tuple|None=None)->dict: + """HTTP GET/POST helper function""" + retries = 3 + timeout = 10 + + retry = 0 + while True: + try: + if mode.upper() == "GET": + if data != "": + raise ValueError("'data' shall be empty.") + response = requests.get(url, headers=headers, params=params, timeout=timeout).json() + Domoticz.Debug("HTTP GET Response:" + str(response)) + else: + if params is not None: + raise ValueError("'params' shall be None.") + response = requests.post(url, headers=headers, data=data, timeout=timeout).json() + Domoticz.Debug("HTTP POST Response:" + str(response)) + break + except Exception: + retry = retry + 1 + if retry >= retries: + raise + time.sleep(0.5) + + return response + _plugin = BasePlugin() def onStart(): #NOSONAR #pylint: disable=invalid-name