Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ repos:
- id: debug-statements
- id: forbid-submodules
- repo: "https://github.com/zizmorcore/zizmor-pre-commit"
rev: v1.29.0
rev: v1.30.0
hooks:
- id: zizmor
args:
Expand Down
719 changes: 353 additions & 366 deletions pylock.toml

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dev = [
"mdformat >= 1.0.0",
"mdformat-gfm >= 1.0.0",
"mdformat-ruff >= 0.1.0",
"prek >= 0.4.0",
"prek >= 0.5.0",
"ruff >= 0.16.0",
"toml-sort >= 0.24.0",
"ty >= 0.0.70",
Expand All @@ -20,7 +20,7 @@ tests = [
"pytest >= 9.1.0",
"pytest-cov >= 7.1.0",
"responses >= 0.26.0",
"tox >= 4.60.0",
"tox >= 4.61.0",
"tox-uv >= 1.36.0",
]

Expand Down Expand Up @@ -111,6 +111,7 @@ ignore = [
"PLR2004",
"TCH",
"TRY003",
"TRY300",
]
select = ["ALL"]

Expand Down
9 changes: 7 additions & 2 deletions simyan/comicvine.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@

T = TypeVar("T")
HttpMethod = Literal["GET"]
STATUS_MAPPING = {100: AuthenticationError, 107: RateLimitError}


class CachedLimiterSession(CacheMixin, LimiterMixin, Session):
Expand Down Expand Up @@ -125,7 +126,7 @@ def __init__(
self._session.params.update({"api_key": api_key, "format": "json"})
self._timeout = timeout

def _request(
def _request( # noqa: C901
self, method: HttpMethod, endpoint: str, params: dict[str, str] | None = None
) -> dict[str, Any]:
url = f"{self._base_url}{endpoint}"
Expand All @@ -135,7 +136,11 @@ def _request(
try:
response = self._session.request(method=method, url=url, **kwargs)
response.raise_for_status()
return response.json()
body = response.json()
if body.get("status_code") != 1:
err = STATUS_MAPPING.get(body.get("status_code"), ServiceError)
raise err(body.get("error"))
return body
except HTTPError as err:
status_code = (
HTTPStatus.INTERNAL_SERVER_ERROR
Expand Down
25 changes: 25 additions & 0 deletions tests/errors_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,28 @@ def test_ratelimit(
with pytest.raises(RateLimitError):
mock_session.get_publisher(publisher_id=1)
mock.assert_call_count(f"{url}?{mock_params_str}", 1)


@pytest.mark.parametrize(
("status_code", "error_class"),
[(100, AuthenticationError), (101, ServiceError), (107, RateLimitError), (None, ServiceError)],
ids=["100-status", "101-status", "107-status", "None-status"],
)
def test_comicvine_error(
mock_session: Comicvine,
mock_params: dict[str, str],
mock_params_str: str,
status_code: int | None,
error_class: type[ServiceError],
) -> None:
with Mocker(assert_all_requests_are_fired=True) as mock:
url = f"https://comicvine.gamespot.mock/api{PUBLISHER.singular_endpoint(id_=1)}"
mock.get(
url=url,
match=[query_param_matcher(mock_params)],
status=200,
json={"error": "Generic Error Message", "status_code": status_code},
)
with pytest.raises(error_class):
mock_session.get_publisher(publisher_id=1)
mock.assert_call_count(f"{url}?{mock_params_str}", 1)
Loading
Loading