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:
@@ -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)
|
||||||
@@ -32,32 +31,28 @@ class ePICWebAPI(BaseWebAPI):
|
|||||||
self.token = None
|
self.token = None
|
||||||
|
|
||||||
async def send_command(
|
async def send_command(
|
||||||
self,
|
self,
|
||||||
command: Union[str, bytes],
|
command: Union[str, bytes],
|
||||||
ignore_errors: bool = False,
|
ignore_errors: bool = False,
|
||||||
allow_warning: bool = True,
|
allow_warning: bool = True,
|
||||||
**parameters: Union[str, int, bool],
|
post: bool = False,
|
||||||
|
**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:
|
||||||
raise APIError(json_data["error"])
|
if not i > settings.get("get_data_retries", 1):
|
||||||
|
continue
|
||||||
|
if not ignore_errors:
|
||||||
|
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(
|
||||||
|
|||||||
Reference in New Issue
Block a user