Skip to content

Commit b49f288

Browse files
committed
refactor: Enhance type safety for generic resource handling and improve documentation for API client context manager usage and resource extraction.
1 parent 160ac76 commit b49f288

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

amazon_creatorsapi/aio/api.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
import asyncio
99
import time
10-
from typing import TYPE_CHECKING, Any
10+
from enum import Enum
11+
from typing import TYPE_CHECKING, Any, TypeVar
1112

1213
from typing_extensions import Self
1314

@@ -40,7 +41,6 @@
4041
from creatorsapi_python_sdk.models.search_items_resource import SearchItemsResource
4142

4243
if TYPE_CHECKING:
43-
from enum import Enum
4444
from types import TracebackType
4545

4646
from amazon_creatorsapi.core.marketplaces import CountryCode
@@ -59,6 +59,9 @@
5959
ENDPOINT_GET_VARIATIONS = "/catalog/v1/getVariations"
6060
ENDPOINT_GET_BROWSE_NODES = "/catalog/v1/getBrowseNodes"
6161

62+
# TypeVar for generic resource handling
63+
ResourceT = TypeVar("ResourceT", bound=Enum)
64+
6265

6366
class AsyncAmazonCreatorsApi:
6467
"""Async version of Amazon Creators API wrapper.
@@ -89,6 +92,13 @@ class AsyncAmazonCreatorsApi:
8992
The context manager approach is more efficient when making multiple
9093
requests in quick succession due to HTTP connection pooling.
9194
95+
Note:
96+
Using without context manager creates a new HTTP connection for each
97+
request, which is less efficient and may impact performance. For
98+
production code making multiple API calls, always use the context
99+
manager (async with) to benefit from connection pooling and reduced
100+
overhead.
101+
92102
Args:
93103
credential_id: Your Creators API credential ID.
94104
credential_secret: Your Creators API credential secret.
@@ -524,8 +534,16 @@ def _handle_error_response(self, status_code: int, body: str) -> None:
524534
msg = f"Request failed with status {status_code}{body_info}"
525535
raise RequestError(msg)
526536

527-
def _get_all_resources(self, resource_class: type[Enum]) -> list[Any]:
528-
"""Extract all resource values from a resource enum class."""
537+
def _get_all_resources(self, resource_class: type[ResourceT]) -> list[ResourceT]:
538+
"""Extract all resource values from a resource enum class.
539+
540+
Args:
541+
resource_class: Enum class containing resource definitions.
542+
543+
Returns:
544+
List of all enum members from the resource class.
545+
546+
"""
529547
return list(resource_class)
530548

531549
def _deserialize_items(self, items_data: list[dict[str, Any]]) -> list[Item]:

0 commit comments

Comments
 (0)