diff --git a/src/fr24sdk/__init__.py b/src/fr24sdk/__init__.py index 9bd0b1c..ef2de58 100644 --- a/src/fr24sdk/__init__.py +++ b/src/fr24sdk/__init__.py @@ -13,6 +13,7 @@ TransportError, ApiError, AuthenticationError, + NoApiKeyError, RateLimitError, PaymentRequiredError, BadRequestError, @@ -26,6 +27,7 @@ "TransportError", "ApiError", "AuthenticationError", + "NoApiKeyError", "RateLimitError", "PaymentRequiredError", "BadRequestError", diff --git a/src/fr24sdk/exceptions.py b/src/fr24sdk/exceptions.py index 98e0c99..76d66a9 100644 --- a/src/fr24sdk/exceptions.py +++ b/src/fr24sdk/exceptions.py @@ -66,6 +66,21 @@ class AuthenticationError(ApiError): pass +class NoApiKeyError(Fr24SdkError): + """Indicates that no API key was provided for authentication. + + This is a specific type of authentication error that occurs when the SDK + is used without providing an API token, either through the constructor + or the FR24_API_TOKEN environment variable. + + Unlike other ApiError subclasses, this error is raised before any HTTP + request is made, so it doesn't include request/response context. + """ + + def __init__(self, message: str): + super().__init__(message) + + class RateLimitError(ApiError): """Indicates that the API rate limit has been exceeded (402 Payment Required or 429 Too Many Requests).""" diff --git a/src/fr24sdk/transport.py b/src/fr24sdk/transport.py index ef31727..72bab1f 100644 --- a/src/fr24sdk/transport.py +++ b/src/fr24sdk/transport.py @@ -12,6 +12,7 @@ from .exceptions import ( ApiError, AuthenticationError, + NoApiKeyError, RateLimitError, TransportError, PaymentRequiredError, @@ -73,6 +74,14 @@ def request( ) -> httpx.Response: """Makes an HTTP request to the API.""" + # Check if API token is available before making the request + if not self.api_token: + raise NoApiKeyError( + "No API key provided. Please set the FR24_API_TOKEN environment variable " + "or pass an api_token parameter when creating the Client. " + "For more information, see https://fr24api.flightradar24.com/docs" + ) + request_headers = self._get_default_headers() if headers: request_headers.update(headers) diff --git a/uv.lock b/uv.lock index 9fe854a..943b4ce 100644 --- a/uv.lock +++ b/uv.lock @@ -606,7 +606,7 @@ wheels = [ [[package]] name = "fr24sdk" -version = "0.0.1" +version = "0.1.0" source = { editable = "." } dependencies = [ { name = "exceptiongroup", marker = "python_full_version < '3.11'" },