Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise error upon invalid api key #3

Merged
merged 1 commit into from
Jan 20, 2024
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
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."""
Loading