Skip to content

Commit

Permalink
Raise error upon invalid api key (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbede authored Jan 20, 2024
1 parent 00357c8 commit 257fa02
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
14 changes: 13 additions & 1 deletion aiotankerkoenig/aiotankerkoenig.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
import orjson
from yarl import URL

from .exceptions import TankerkoenigConnectionError, TankerkoenigError
from .exceptions import (
TankerkoenigConnectionError,
TankerkoenigError,
TankerkoenigInvalidKeyError,
)
from .models import GasType, PriceInfo, Sort, Station

VERSION = metadata.version(__package__)
Expand Down Expand Up @@ -66,6 +70,14 @@ async def _request(self, path: str, params: dict[str, Any]) -> Any:

obj = orjson.loads(await response.text()) # pylint: disable=maybe-no-member
if not obj["ok"]:
message = obj["message"].lower()
if any(x in message.lower() for x in ("api-key", "apikey")):
msg = "tankerkoenig.de API responded with an invalid key error"
raise TankerkoenigInvalidKeyError(
msg,
{"response": obj},
)

msg = "tankerkoenig.de API responded with an error"
raise TankerkoenigError(
msg,
Expand Down
4 changes: 4 additions & 0 deletions aiotankerkoenig/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ class TankerkoenigError(Exception):

class TankerkoenigConnectionError(TankerkoenigError):
"""Raised when a connection error occurs."""


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

0 comments on commit 257fa02

Please sign in to comment.