From c443170f78a5ac00b5e77c8d5d080ccb8d79c1af Mon Sep 17 00:00:00 2001 From: b-rowan Date: Sat, 27 Jan 2024 09:42:35 -0700 Subject: [PATCH] refactor: improve epic web send_command implementation. --- pyasic/web/epic.py | 63 ++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/pyasic/web/epic.py b/pyasic/web/epic.py index e567914a..8442a905 100644 --- a/pyasic/web/epic.py +++ b/pyasic/web/epic.py @@ -44,40 +44,37 @@ class ePICWebAPI(BaseWebAPI): post = privileged or not parameters == {} async with httpx.AsyncClient(transport=settings.transport()) as client: - for i in range(settings.get("get_data_retries", 1) + 1): - try: - if post: - response = await client.post( - f"http://{self.ip}:{self.port}/{command}", - timeout=5, - json={ - **parameters, - "password": self.pwd, - }, + try: + if post: + response = await client.post( + f"http://{self.ip}:{self.port}/{command}", + timeout=5, + json={ + **parameters, + "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: - response = await client.get( - f"http://{self.ip}:{self.port}/{command}", - timeout=5, - ) - if not response.status_code == 200: - continue - json_data = response.json() - if json_data: - # 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 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 + return {} + json_data = response.json() + if json_data: + # 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 ignore_errors: + raise APIError(json_data["error"]) + return json_data + return {"success": True} + except (httpx.HTTPError, json.JSONDecodeError, AttributeError): + pass async def multicommand( self, *commands: str, ignore_errors: bool = False, allow_warning: bool = True