Skip to content

Commit

Permalink
feat: preserve status code on content type exceptions (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
NiceAesth authored Feb 29, 2024
1 parent b84363e commit 9b2429c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
20 changes: 10 additions & 10 deletions aiosu/utils/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ async def process_code(
:rtype: aiosu.models.oauthtoken.OAuthToken
"""
url = f"{base_url}/oauth/token"
headers = {"Content-Type": "application/x-www-form-urlencoded"}
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
}
data = {
"client_id": client_id,
"client_secret": client_secret,
Expand All @@ -54,15 +57,12 @@ async def process_code(

async with aiohttp.ClientSession(headers=headers) as temp_session:
async with temp_session.post(url, data=data) as resp:
try:
body = await resp.read()
json = orjson.loads(body)
if resp.status != 200:
raise APIException(resp.status, json.get("error", ""))
token = OAuthToken.model_validate(json)
return token
except aiohttp.client_exceptions.ContentTypeError:
raise APIException(403, "Invalid code specified.")
body = await resp.read()
json = orjson.loads(body)
if resp.status != 200:
raise APIException(resp.status, json.get("error", ""))
token = OAuthToken.model_validate(json)
return token


def generate_url(
Expand Down
5 changes: 4 additions & 1 deletion aiosu/v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ async def _request(
return BytesIO(body)
if content_type == "text/plain":
return body.decode()
raise APIException(415, "Unhandled Content Type")
raise APIException(
resp.status,
f"Unhandled Content Type '{content_type}'",
)

async def get_user(self, user_query: Union[str, int], **kwargs: Any) -> User:
r"""Gets a user by a query.
Expand Down
44 changes: 22 additions & 22 deletions aiosu/v2/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,10 @@ async def _request(
return BytesIO(body)
if content_type == "text/plain":
return body.decode()
raise APIException(415, f"Unhandled Content Type '{content_type}'")
raise APIException(
resp.status,
f"Unhandled Content Type '{content_type}'",
)

async def _refresh(self) -> None:
r"""INTERNAL: Refreshes the client's token
Expand All @@ -380,28 +383,25 @@ async def _refresh(self) -> None:
async with aiohttp.ClientSession() as temp_session:
async with self._limiter:
async with temp_session.post(url, json=data) as resp:
try:
body = await resp.read()
content_type = get_content_type(
resp.headers.get("content-type", ""),
)
if content_type != "application/json":
raise APIException(
415,
f"Unhandled Content Type '{content_type}'",
)
json = orjson.loads(body)
if resp.status != 200:
raise APIException(resp.status, json.get("error", ""))
if self._session:
await self._session.close()
new_token = OAuthToken.model_validate(json)
await self._update_token(new_token)
self._session = aiohttp.ClientSession(
headers=await self._get_headers(),
body = await resp.read()
content_type = get_content_type(
resp.headers.get("content-type", ""),
)
if content_type != "application/json":
raise APIException(
resp.status,
f"Unhandled Content Type '{content_type}'",
)
except aiohttp.client_exceptions.ContentTypeError:
raise APIException(403, "Invalid token specified.")
json = orjson.loads(body)
if resp.status != 200:
raise APIException(resp.status, json.get("error", ""))
if self._session:
await self._session.close()
new_token = OAuthToken.model_validate(json)
await self._update_token(new_token)
self._session = aiohttp.ClientSession(
headers=await self._get_headers(),
)

await self._process_event(
ClientUpdateEvent(client=self, old_token=old_token, new_token=new_token),
Expand Down

0 comments on commit 9b2429c

Please sign in to comment.