Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove error message status handling from request handler #105

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading