hotfix: fix epic api error handling (#74)

* hotfix: fix epic api error handling

* much cleaner way to handle the retry
This commit is contained in:
JP Compagnone
2023-11-28 18:39:57 -05:00
committed by GitHub
parent 87016670d4
commit 68c4dadb63

View File

@@ -23,7 +23,6 @@ from pyasic import settings
from pyasic.web import BaseWebAPI from pyasic.web import BaseWebAPI
from pyasic.errors import APIError, APIWarning from pyasic.errors import APIError, APIWarning
class ePICWebAPI(BaseWebAPI): class ePICWebAPI(BaseWebAPI):
def __init__(self, ip: str) -> None: def __init__(self, ip: str) -> None:
super().__init__(ip) super().__init__(ip)
@@ -36,28 +35,24 @@ class ePICWebAPI(BaseWebAPI):
command: Union[str, bytes], command: Union[str, bytes],
ignore_errors: bool = False, ignore_errors: bool = False,
allow_warning: bool = True, allow_warning: bool = True,
post: bool = False,
**parameters: Union[str, int, bool], **parameters: Union[str, int, bool],
) -> dict: ) -> dict:
async with httpx.AsyncClient() as client: if post or parameters != {}:
is_get = False post = True
for i in range(settings.get("get_data_retries", 1)):
async with httpx.AsyncClient(transport=settings.transport()) as client:
for i in range(settings.get("get_data_retries", 1) + 1):
try: try:
if parameters.get("post"): if post:
parameters.pop("post") epic_param = {"param": parameters.get("parameters"),
epic_param = {"param": parameters.get("parameters"), "password": self.pwd} "password": self.pwd}
response = await client.post( response = await client.post(
f"http://{self.ip}:4028/{command}", f"http://{self.ip}:4028/{command}",
timeout=5, timeout=5,
json=epic_param, json=epic_param,
) )
elif not parameters == {}:
response = await client.post(
f"http://{self.ip}:4028/{command}",
timeout=5,
json=parameters,
)
else: else:
is_get = True
response = await client.get( response = await client.get(
f"http://{self.ip}:4028/{command}", f"http://{self.ip}:4028/{command}",
timeout=5, timeout=5,
@@ -68,15 +63,14 @@ class ePICWebAPI(BaseWebAPI):
json_data = response.json() json_data = response.json()
if json_data: if json_data:
# The API can return a fail status if the miner cannot return the requested data. Catch this and pass # The API can return a fail status if the miner cannot return the requested data. Catch this and pass
if "result" in json_data and json_data["result"] is False and is_get and not ignore_errors: if "result" in json_data and json_data["result"] is False and not post:
if not i > settings.get("get_data_retries", 1):
continue
if not ignore_errors:
raise APIError(json_data["error"]) raise APIError(json_data["error"])
return json_data return json_data
return {"success": True} return {"success": True}
except httpx.HTTPError: except (httpx.HTTPError, json.JSONDecodeError, AttributeError):
pass
except json.JSONDecodeError:
pass
except AttributeError:
pass pass
async def multicommand( async def multicommand(