Skip to content

Commit b2de270

Browse files
committed
feat: Add API version validation to AsyncAmazonCreatorsApi initialization and a test for invalid versions.
1 parent ec8717e commit b2de270

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

amazon_creatorsapi/aio/api.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
)
2525

2626
try:
27-
from .auth import AsyncOAuth2TokenManager
27+
from .auth import VERSION_ENDPOINTS, AsyncOAuth2TokenManager
2828
from .client import AsyncHttpClient
2929
except ImportError as exc: # pragma: no cover
3030
msg = (
@@ -110,6 +110,7 @@ class AsyncAmazonCreatorsApi:
110110
111111
Raises:
112112
InvalidArgumentError: If neither country nor marketplace is provided.
113+
ValueError: If version is not supported (valid versions: 2.1, 2.2, 2.3).
113114
114115
"""
115116

@@ -124,6 +125,9 @@ def __init__(
124125
throttling: float = DEFAULT_THROTTLING,
125126
) -> None:
126127
"""Initialize the async Amazon Creators API client."""
128+
# Validate version early to fail fast (before token manager initialization)
129+
self._validate_version(version)
130+
127131
self._credential_id = credential_id
128132
self._credential_secret = credential_secret
129133
self._version = version
@@ -153,6 +157,21 @@ def __init__(
153157
)
154158
self._owns_client = False
155159

160+
def _validate_version(self, version: str) -> None:
161+
"""Validate that the API version is supported.
162+
163+
Args:
164+
version: API version to validate.
165+
166+
Raises:
167+
ValueError: If version is not in the list of supported versions.
168+
169+
"""
170+
if version not in VERSION_ENDPOINTS:
171+
supported = ", ".join(VERSION_ENDPOINTS.keys())
172+
msg = f"Unsupported version: {version}. Supported versions are: {supported}"
173+
raise ValueError(msg)
174+
156175
async def __aenter__(self) -> Self:
157176
"""Enter async context manager, creating a persistent HTTP client."""
158177
self._http_client = AsyncHttpClient(host=API_HOST)

tests/amazon_creatorsapi/aio/api_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,23 @@ def test_raises_error_for_invalid_country(
9999

100100
self.assertIn("Country code", str(context.exception))
101101

102+
@patch("amazon_creatorsapi.aio.api.AsyncOAuth2TokenManager")
103+
def test_raises_error_for_invalid_version(
104+
self, mock_token_manager: MagicMock
105+
) -> None:
106+
"""Test raises ValueError for unsupported API version."""
107+
with self.assertRaises(ValueError) as context:
108+
AsyncAmazonCreatorsApi(
109+
credential_id="test_id",
110+
credential_secret="test_secret",
111+
version="9.9", # Invalid version
112+
tag="test-tag",
113+
country="ES",
114+
)
115+
116+
self.assertIn("Unsupported version: 9.9", str(context.exception))
117+
self.assertIn("Supported versions are:", str(context.exception))
118+
102119

103120
class TestAsyncAmazonCreatorsApiContextManager(unittest.IsolatedAsyncioTestCase):
104121
"""Tests for AsyncAmazonCreatorsApi async context manager."""

0 commit comments

Comments
 (0)