Skip to content

Commit

Permalink
Refactor/error handling (#72)
Browse files Browse the repository at this point in the history
* refactor: handle response status codes within the same method

Signed-off-by: F.N. Claessen <[email protected]>

* refactor: handle response status codes in a more intuitive order

Signed-off-by: F.N. Claessen <[email protected]>

* fix(setup.cfg): add async-timeout to dependencies

Signed-off-by: GustaafL <[email protected]>

---------

Signed-off-by: F.N. Claessen <[email protected]>
Signed-off-by: GustaafL <[email protected]>
Co-authored-by: GustaafL <[email protected]>
  • Loading branch information
Flix6x and GustaafL authored Feb 9, 2024
1 parent 5619db9 commit db29c55
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ install_requires =
aiohttp
pandas
pydantic>=1.10.8,<2.0
async-timeout


[options.packages.find]
Expand Down
13 changes: 5 additions & 8 deletions src/flexmeasures_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ async def request(
response,
polling_step,
reauth_once,
url,
) = await self.request_once(
method=method,
url=url,
Expand All @@ -128,10 +129,6 @@ async def request(
)
if response.status < 300:
break
if response.status == 303:
message = f"Redirect to fallback schedule: {response.headers['location']}" # noqa: E501
logging.debug(message)
url = response.headers["location"]
except asyncio.TimeoutError:
message = f"Client request timeout occurred while connecting to the API. Polling step: {polling_step}. Retrying in {self.polling_interval} seconds..." # noqa: E501
logging.debug(message)
Expand Down Expand Up @@ -160,7 +157,7 @@ async def request_once(
json: dict | None = None,
polling_step: int = 0,
reauth_once: bool = True,
) -> tuple[ClientResponse, int, bool]:
) -> tuple[ClientResponse, int, bool, str]:
url_msg = f"url: {url}"
json_msg = f"payload: {json}"
params_msg = f"params: {params}"
Expand Down Expand Up @@ -195,10 +192,10 @@ async def request_once(
logging.debug(headers_msg)
logging.debug("=" * 14)

polling_step, reauth_once = await check_response(
self, response, polling_step, reauth_once
polling_step, reauth_once, url = await check_response(
self, response, polling_step, reauth_once, url
)
return response, polling_step, reauth_once
return response, polling_step, reauth_once, url

def start_session(self):
"""If there is no session, start one"""
Expand Down
32 changes: 19 additions & 13 deletions src/flexmeasures_client/response_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,34 @@


async def check_response(
self: FlexMeasuresClient, response, polling_step: int, reauth_once: bool
) -> tuple[int, bool]:
self: FlexMeasuresClient, response, polling_step: int, reauth_once: bool, url: str
) -> tuple[int, bool, str]:
"""
<300: passes
303: redirect to new url
400 + custom message: schedule not ready yet
401: reauthenticate
todo: 503 + Retry-After header: poll again
503 + Retry-After header: poll again
otherwise: call error_handler
"""
status = response.status
payload = await response.json()
headers = response.headers
if status < 300:
pass
elif response.status == 303:
message = f"Redirect to fallback schedule: {response.headers['location']}" # noqa: E501
logging.debug(message)
url = response.headers["location"]
elif status == 400 and (
"Scheduling job waiting" in payload.get("message", "")
or "Scheduling job in progress" in payload.get("message", "")
):
# can be removed in a later version GH issue #645 of the FlexMeasures repo
message = f"Server indicated to try again later. Retrying in {self.polling_interval} seconds..." # noqa: E501
logging.debug(message)
polling_step += 1
await asyncio.sleep(self.polling_interval)
elif status == 401 and reauth_once:
message = f"""Authentication failed with"
status: {status}
Expand All @@ -39,15 +54,6 @@ async def check_response(
elif status == 503 and "Retry-After" in headers:
polling_step += 1
await asyncio.sleep(self.polling_interval)
elif status == 400 and (
"Scheduling job waiting" in payload.get("message", "")
or "Scheduling job in progress" in payload.get("message", "")
):
# can be removed in a later version GH issue #645 of the FlexMeasures repo
message = f"Server indicated to try again later. Retrying in {self.polling_interval} seconds..." # noqa: E501
logging.debug(message)
polling_step += 1
await asyncio.sleep(self.polling_interval)
elif payload.get("errors"):
# try to raise any error messages from the response
raise ValueError(" ,".join(payload.get("errors")))
Expand All @@ -60,7 +66,7 @@ async def check_response(
logging.error(message)
# otherwise, raise if the status does not indicate okay
response.raise_for_status()
return polling_step, reauth_once
return polling_step, reauth_once, url


def check_content_type(response):
Expand Down

0 comments on commit db29c55

Please sign in to comment.