Improve http get/post with internal retry

This commit is contained in:
2025-11-02 23:37:06 +01:00
parent 122615dffe
commit ad3cfbe74d

View File

@@ -195,14 +195,12 @@ class BasePlugin:
'X-Gizwits-Application-Id': 'c70a66ff039d41b4a220e198b0fcc8b3', 'X-Gizwits-Application-Id': 'c70a66ff039d41b4a220e198b0fcc8b3',
} }
data = '{ "username": "'+user+'", "password": "'+password+'", "lang": "en" }' data = '{ "username": "'+user+'", "password": "'+password+'", "lang": "en" }'
time.sleep(0.5)
url = 'https://euapi.gizwits.com/app/login'
try: try:
time.sleep(0.5) response = http("post", url, headers, data)
url = 'https://euapi.gizwits.com/app/login'
response = requests.post(url, headers=headers, data=data, timeout=self._HTTP_TIMEOUT).json()
except Exception as exc: except Exception as exc:
Domoticz.Error("Cannot open connection to Heatzy API to get the token: " + str(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)) Domoticz.Error("Data: " + str(data))
#Decrease retry #Decrease retry
self.retry = self.retry - 1 self.retry = self.retry - 1
@@ -244,7 +242,7 @@ class BasePlugin:
params = (('limit', '20'), ('skip', '0'),) params = (('limit', '20'), ('skip', '0'),)
url = 'https://euapi.gizwits.com/app/bindings' url = 'https://euapi.gizwits.com/app/bindings'
try: 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: except Exception as exc:
Domoticz.Error("Cannot open connection to Heatzy API to get the device id: " + str(exc)) Domoticz.Error("Cannot open connection to Heatzy API to get the device id: " + str(exc))
#Domoticz.Error("URL: " + str(url)) #Domoticz.Error("URL: " + str(url))
@@ -293,7 +291,7 @@ class BasePlugin:
url = f"https://euapi.gizwits.com/app/devdata/{did}/latest" url = f"https://euapi.gizwits.com/app/devdata/{did}/latest"
try: try:
response = requests.get(url, headers=headers, timeout=self._HTTP_TIMEOUT).json() response = http("get", url, headers)
except Exception as exc: except Exception as exc:
message = f"Cannot open connection to Heatzy API to get the mode for {alias} (retry={self.retry}): {exc}" message = f"Cannot open connection to Heatzy API to get the mode for {alias} (retry={self.retry}): {exc}"
if self.retry <= 0: if self.retry <= 0:
@@ -398,7 +396,7 @@ class BasePlugin:
self.did[deviceid]["command_at"] = time.time() self.did[deviceid]["command_at"] = time.time()
url = f"https://euapi.gizwits.com/app/control/{did}" url = f"https://euapi.gizwits.com/app/control/{did}"
try: try:
response = requests.post(url, headers=headers, data=data, timeout=self._HTTP_TIMEOUT).json() response = http("post", url, headers, data)
except Exception as exc: except Exception as exc:
Domoticz.Error("Cannot open connection to Heatzy API to set the mode: " + str(exc)) Domoticz.Error("Cannot open connection to Heatzy API to set the mode: " + str(exc))
#Domoticz.Error("URL: " + str(url)) #Domoticz.Error("URL: " + str(url))
@@ -461,6 +459,33 @@ class BasePlugin:
Domoticz.Status("Reset retry counter") Domoticz.Status("Reset retry counter")
self.retry = self._MAX_RETRY_PER_DEVICE * len(self.did) 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() _plugin = BasePlugin()
def onStart(): #NOSONAR #pylint: disable=invalid-name def onStart(): #NOSONAR #pylint: disable=invalid-name