Skip to content

Commit b034971

Browse files
committed
Replace magic HTTP status codes with HTTPStatus constants
Use http.HTTPStatus instead of raw integers for status code comparisons, eliminating all PLR2004 noqa suppressions.
1 parent 5ecc668 commit b034971

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

pyoverkiz/auth/strategies.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import json
99
import ssl
1010
from collections.abc import Mapping
11+
from http import HTTPStatus
1112
from typing import TYPE_CHECKING, Any, cast
1213

1314
if TYPE_CHECKING:
@@ -49,6 +50,8 @@
4950
)
5051
from pyoverkiz.models import ServerConfig
5152

53+
MIN_JWT_SEGMENTS = 2
54+
5255

5356
class BaseAuthStrategy(AuthStrategy):
5457
"""Base class for authentication strategies."""
@@ -110,13 +113,13 @@ async def _post_login(self, data: Mapping[str, Any]) -> None:
110113
data=data,
111114
ssl=self._ssl,
112115
) as response:
113-
if response.status not in (200, 204):
116+
if response.status not in (HTTPStatus.OK, HTTPStatus.NO_CONTENT):
114117
raise BadCredentialsError(
115118
f"Login failed for {self.server.name}: {response.status}"
116119
)
117120

118121
# A 204 No Content response cannot have a body, so skip JSON parsing.
119-
if response.status == 204: # noqa: PLR2004
122+
if response.status == HTTPStatus.NO_CONTENT:
120123
return
121124

122125
result = await response.json()
@@ -419,7 +422,7 @@ def auth_headers(self, path: str | None = None) -> Mapping[str, str]:
419422
def _decode_jwt_payload(token: str) -> dict[str, Any]:
420423
"""Decode the payload of a JWT token."""
421424
parts = token.split(".")
422-
if len(parts) < 2: # noqa: PLR2004
425+
if len(parts) < MIN_JWT_SEGMENTS:
423426
raise InvalidTokenError("Malformed JWT received.")
424427

425428
payload_segment = parts[1]

pyoverkiz/client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import logging
66
import ssl
77
import urllib.parse
8+
from http import HTTPStatus
89
from pathlib import Path
910
from types import TracebackType
1011
from typing import Any, Self, cast
@@ -728,7 +729,7 @@ async def _delete(self, path: str) -> None:
728729
async def _parse_response(response: ClientResponse) -> Any:
729730
"""Check response status and parse JSON body (returns None for 204)."""
730731
await check_response(response)
731-
if response.status == 204: # noqa: PLR2004
732+
if response.status == HTTPStatus.NO_CONTENT:
732733
return None
733734
return await response.json()
734735

pyoverkiz/response_handler.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
from http import HTTPStatus
56
from json import JSONDecodeError
67

78
from aiohttp import ClientResponse
@@ -113,7 +114,7 @@
113114

114115
async def check_response(response: ClientResponse) -> None:
115116
"""Check the response returned by the OverKiz API."""
116-
if response.status in [200, 204]:
117+
if response.status in (HTTPStatus.OK, HTTPStatus.NO_CONTENT):
117118
return
118119

119120
try:
@@ -124,7 +125,7 @@ async def check_response(response: ClientResponse) -> None:
124125
if "is down for maintenance" in result:
125126
raise MaintenanceError("Server is down for maintenance") from error
126127

127-
if response.status == 503: # noqa: PLR2004
128+
if response.status == HTTPStatus.SERVICE_UNAVAILABLE:
128129
raise ServiceUnavailableError(result) from error
129130

130131
raise OverkizError(

0 commit comments

Comments
 (0)