Skip to content

Commit

Permalink
remove error message status handling from request handler (#105)
Browse files Browse the repository at this point in the history
* remove error message status handling

* fix linting
  • Loading branch information
geo-martino authored Jun 27, 2024
1 parent df0bc16 commit c9df3f3
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions musify/api/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from collections.abc import Mapping, Callable
from datetime import datetime, timedelta
from http import HTTPStatus
from time import sleep
from typing import Any, Self
from urllib.parse import unquote

Expand Down Expand Up @@ -113,7 +112,6 @@ def __init__(self, connector: Callable[[], ClientSession], authoriser: APIAuthor
self.wait_max = 1
self._wait_start_logged = False


async def __aenter__(self) -> Self:
if self.closed:
self._session = self._connector()
Expand Down Expand Up @@ -175,7 +173,7 @@ async def request(self, method: str, url: str | URL, **kwargs) -> dict[str, Any]
if handled or waited:
continue

if await self._response_is_ok(response):
if response.ok:
data = await self._get_json_response(response)
break

Expand Down Expand Up @@ -244,13 +242,6 @@ def log(

self.logger.log(level=level, msg=format_url_log(method=method, url=url, messages=log))

async def _response_is_ok(self, response: ClientResponse) -> bool:
response_json = await self._get_json_response(response)
error_status = response_json.get("error", {}).get("status")
if error_status:
return int(error_status) < 400
return response.ok

def _log_backoff_start(self) -> None:
if self._backoff_start_logged:
return
Expand Down Expand Up @@ -285,9 +276,7 @@ async def _log_response(self, response: ClientResponse, method: str, url: str |
async def _handle_bad_response(self, response: ClientResponse) -> bool:
"""Handle bad responses by extracting message and handling status codes that should raise an exception."""
response_json = await self._get_json_response(response)

error_message = response_json.get("error", {}).get("message")
error_status = int(response_json.get("error", {}).get("status", 0))
if error_message is None:
status = HTTPStatus(error_status or response.status)
error_message = f"{status.phrase} | {status.description}"
Expand All @@ -297,11 +286,11 @@ async def _handle_bad_response(self, response: ClientResponse) -> bool:
def _log_bad_response(message: str) -> None:
self.logger.debug(f"Status code: {response.status} | {error_message} | {message}")

if response.status == 401 or error_status == 401:
if response.status == 401:
_log_bad_response("Re-authorising...")
await self.authorise()
handled = True
elif response.status == 429 or error_status == 429:
elif response.status == 429:
if self.wait_time < self.wait_max:
self.wait_time += self.wait_increment
_log_bad_response(f"Rate limit hit. Increasing wait time between requests to {self.wait_time}s")
Expand Down

0 comments on commit c9df3f3

Please sign in to comment.