retry on any exception

This commit is contained in:
Stefan Regnery 2023-09-27 16:17:44 +02:00
parent ed9fdf0994
commit dda662f3ca
2 changed files with 13 additions and 1 deletions

View File

@ -3,6 +3,7 @@ from typing import Optional
import requests
from requests import Response
from retrying import retry
class CloudConnection:
@ -13,6 +14,12 @@ class CloudConnection:
self._refresh_token = str
self._auth_header = Optional[str]
@staticmethod
def _retry_one_any_exception(exception):
logging.error(f"An Exception occurred: \n{exception}")
return True
@retry(retry_on_exception=_retry_one_any_exception, wait_exponential_multiplier=1000, wait_exponential_max=10000)
def generate_user_token(self) -> None:
url = "https://app.api.apsystemsema.com:9223/api/token/generateToken/user/login"
@ -29,6 +36,7 @@ class CloudConnection:
self._refresh_token = post.json()["data"]["refresh_token"]
self._auth_header = {"authorization": f"Bearer {user_token}"}
@retry(retry_on_exception=_retry_one_any_exception, wait_exponential_multiplier=1000, wait_exponential_max=10000)
def refresh_user_token(self):
url = "https://app.api.apsystemsema.com:9223/api/token/refreshToken"
data = {
@ -39,6 +47,7 @@ class CloudConnection:
user_token = result.json()["data"]["access_token"]
self._auth_header = {"authorization": f"Bearer {user_token}"}
@retry(retry_on_exception=_retry_one_any_exception, wait_exponential_multiplier=1000, wait_exponential_max=10000)
def get_realtime_data(self, inverter_id) -> dict:
url = f"https://app.api.apsystemsema.com:9223/aps-api-web/api/v2/data/device/ezInverter/realTime/{inverter_id}"
while True:
@ -49,6 +58,7 @@ class CloudConnection:
break
return data
@retry(retry_on_exception=_retry_one_any_exception, wait_exponential_multiplier=1000, wait_exponential_max=10000)
def get_statistic_data(self, inverter_id):
url = f"https://app.api.apsystemsema.com:9223/aps-api-web/api/v2/data/device/ezInverter/statistic/{inverter_id}"
while True:
@ -59,6 +69,7 @@ class CloudConnection:
break
return data
@retry(retry_on_exception=_retry_one_any_exception, wait_exponential_multiplier=1000, wait_exponential_max=10000)
def get_inverter(self):
url = f"https://app.api.apsystemsema.com:9223/aps-api-web/api/v2/data/device/ezInverter/list/{self._user_id}"
inverter = []

View File

@ -1,3 +1,4 @@
toml==0.10.2
requests~=2.31.0
paho-mqtt~=1.6.1
paho-mqtt~=1.6.1
retrying~=1.3.4