Skip to content

Commit

Permalink
add rate limit error (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
mib1185 authored Feb 12, 2024
1 parent 64a8945 commit 3af3d8f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions aiotankerkoenig/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
TankerkoenigConnectionTimeoutError,
TankerkoenigError,
TankerkoenigInvalidKeyError,
TankerkoenigRateLimitError,
)
from .models import PriceInfo, Station

Expand All @@ -17,6 +18,7 @@
"TankerkoenigConnectionError",
"TankerkoenigInvalidKeyError",
"TankerkoenigConnectionTimeoutError",
"TankerkoenigRateLimitError",
"GasType",
"Sort",
"Status",
Expand Down
5 changes: 4 additions & 1 deletion aiotankerkoenig/aiotankerkoenig.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
import socket
from typing import Any, Self

from aiohttp import ClientError, ClientSession
from aiohttp import ClientError, ClientResponseError, ClientSession
from yarl import URL

from .const import GasType, Sort
from .exceptions import (
TankerkoenigConnectionError,
TankerkoenigConnectionTimeoutError,
TankerkoenigError,
TankerkoenigRateLimitError,
)
from .models import (
PriceInfo,
Expand Down Expand Up @@ -75,6 +76,8 @@ async def _request(self, path: str, params: dict[str, Any]) -> str:
ClientError,
socket.gaierror,
) as exception:
if isinstance(exception, ClientResponseError) and exception.code == 503:
raise TankerkoenigRateLimitError from exception
msg = "Error occurred while communicating with the tankerkoenig.de API"
raise TankerkoenigConnectionError(msg) from exception

Expand Down
4 changes: 4 additions & 0 deletions aiotankerkoenig/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ class TankerkoenigConnectionTimeoutError(TankerkoenigConnectionError):

class TankerkoenigInvalidKeyError(TankerkoenigError):
"""Raised when the API key is invalid."""


class TankerkoenigRateLimitError(TankerkoenigError):
"""Raised when the API rate limit is reached."""
16 changes: 16 additions & 0 deletions tests/test_aiotankerkoenig.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
TankerkoenigConnectionTimeoutError,
TankerkoenigError,
TankerkoenigInvalidKeyError,
TankerkoenigRateLimitError,
)
from tests import load_fixture

Expand Down Expand Up @@ -73,6 +74,21 @@ async def test_non_200_response(
await tankerkoenig_client.station_details(station_id="1")


async def test_api_rate_limit(
responses: aioresponses,
tankerkoenig_client: Tankerkoenig,
) -> None:
"""Test handling api rate limit response."""
responses.get(
f"{TANKERKOENIG_ENDPOINT}/json/detail.php?apikey=abc123&id=1",
status=503,
headers={"Content-Type": "plain/text"},
body="Yes",
)
with pytest.raises(TankerkoenigRateLimitError):
await tankerkoenig_client.station_details(station_id="1")


@pytest.mark.parametrize(
"message",
[
Expand Down

0 comments on commit 3af3d8f

Please sign in to comment.