refactor: improve epic web send_command implementation.

This commit is contained in:
b-rowan
2024-01-27 09:42:35 -07:00
parent a2c2aa2377
commit c443170f78

View File

@@ -44,40 +44,37 @@ class ePICWebAPI(BaseWebAPI):
post = privileged or not parameters == {} post = privileged or not parameters == {}
async with httpx.AsyncClient(transport=settings.transport()) as client: async with httpx.AsyncClient(transport=settings.transport()) as client:
for i in range(settings.get("get_data_retries", 1) + 1): try:
try: if post:
if post: response = await client.post(
response = await client.post( f"http://{self.ip}:{self.port}/{command}",
f"http://{self.ip}:{self.port}/{command}", timeout=5,
timeout=5, json={
json={ **parameters,
**parameters, "password": self.pwd,
"password": self.pwd, },
}, )
else:
response = await client.get(
f"http://{self.ip}:{self.port}/{command}",
timeout=5,
)
if not response.status_code == 200:
if not ignore_errors:
raise APIError(
f"Web command {command} failed with status code {response.status_code}"
) )
else: return {}
response = await client.get( json_data = response.json()
f"http://{self.ip}:{self.port}/{command}", if json_data:
timeout=5, # The API can return a fail status if the miner cannot return the requested data. Catch this and pass
) if not json_data.get("result", True) and not post:
if not response.status_code == 200: if not ignore_errors:
continue raise APIError(json_data["error"])
json_data = response.json() return json_data
if json_data: return {"success": True}
# The API can return a fail status if the miner cannot return the requested data. Catch this and pass except (httpx.HTTPError, json.JSONDecodeError, AttributeError):
if ( pass
"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"])
return json_data
return {"success": True}
except (httpx.HTTPError, json.JSONDecodeError, AttributeError):
pass
async def multicommand( async def multicommand(
self, *commands: str, ignore_errors: bool = False, allow_warning: bool = True self, *commands: str, ignore_errors: bool = False, allow_warning: bool = True