Skip to content

Commit 69bf6b9

Browse files
committed
Merge branch 'develop'
2 parents f4c694f + 056a469 commit 69bf6b9

File tree

4 files changed

+51
-15
lines changed

4 files changed

+51
-15
lines changed

amazon_paapi/errors/exceptions.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
class AmazonException(Exception):
55
"""Common base class for all Amazon API exceptions."""
6-
def __init__(self, message: str):
6+
def __init__(self, reason: str):
77
super().__init__()
8-
self.message = message
8+
self.reason = reason
99

1010
def __str__(self) -> str:
11-
return '%s' % self.message
11+
return '%s' % self.reason
1212

1313

1414
class InvalidArgumentException(AmazonException):
@@ -31,3 +31,15 @@ class MalformedRequestException(AmazonException):
3131
class ItemsNotFoudException(AmazonException):
3232
"""Raised if no items are found"""
3333
pass
34+
35+
class TooManyRequestsException(AmazonException):
36+
"""Raised if too many requests are made"""
37+
pass
38+
39+
class InvalidPartnerTagException(AmazonException):
40+
"""Raised if the partner tag is not present or invalid"""
41+
pass
42+
43+
class AssociateValidationException(AmazonException):
44+
"""Raised when credentials are not valid for the selected country."""
45+
pass

amazon_paapi/helpers/arguments.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,26 @@ def _check_search_mandatory_args(**kwargs):
3838

3939

4040
def _check_search_pagination_args(**kwargs):
41+
error_message = ('Args item_count and item_page should be integers between 1 and 10.')
4142
pagination_args = [kwargs['item_count'], kwargs['item_page']]
4243
pagination_args = [arg for arg in pagination_args if arg]
43-
if not all(1 <= arg <= 10 and isinstance(arg, int) for arg in pagination_args):
44-
error_message = ('Args item_count and item_page should be integers between 1 and 10.')
44+
45+
if not all(isinstance(arg, int) for arg in pagination_args):
46+
raise InvalidArgumentException(error_message)
47+
48+
if not all(1 <= arg <= 10 for arg in pagination_args):
4549
raise InvalidArgumentException(error_message)
4650

4751

4852
def check_variations_args(**kwargs):
49-
pagination_args = [kwargs['variation_count'], kwargs['variation_page']]
50-
pagination_args = [arg for arg in pagination_args if arg]
51-
if not all(1 <= arg <= 10 and isinstance(arg, int) for arg in pagination_args):
52-
error_message = ('Args variation_count and variation_page should be integers between 1 and 10.')
53+
error_message = ('Args variation_count and variation_page should be integers between 1 and 10.')
54+
variation_args = [kwargs['variation_count'], kwargs['variation_page']]
55+
variation_args = [arg for arg in variation_args if arg]
56+
57+
if not all(isinstance(arg, int) for arg in variation_args):
58+
raise InvalidArgumentException(error_message)
59+
60+
if not all(1 <= arg <= 10 for arg in variation_args):
5361
raise InvalidArgumentException(error_message)
5462

5563

amazon_paapi/helpers/requests.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from ..models.search_result import SearchResult
77
from ..models.variations_result import VariationsResult
88
from ..models.browse_nodes_result import BrowseNode
9-
from ..errors import ApiRequestException, ItemsNotFoudException, MalformedRequestException
9+
from ..errors import ApiRequestException, ItemsNotFoudException, MalformedRequestException, TooManyRequestsException, AssociateValidationException, InvalidArgumentException
1010
from ..sdk.models.partner_type import PartnerType
1111
from ..sdk.models.get_items_resource import GetItemsResource
1212
from ..sdk.models.get_items_request import GetItemsRequest
@@ -36,7 +36,7 @@ def get_items_response(amazon_api, request: GetItemsRequest) -> List[Item]:
3636
try:
3737
response = amazon_api._api.get_items(request)
3838
except ApiException as e:
39-
raise ApiRequestException('Error getting response for get_items from Amazon API: ' + str(e))
39+
_manage_response_exceptions(e)
4040

4141
if response.items_result == None:
4242
raise ItemsNotFoudException('No items have been found')
@@ -59,7 +59,7 @@ def get_search_items_response(amazon_api, request: SearchItemsRequest) -> Search
5959
try:
6060
response = amazon_api._api.search_items(request)
6161
except ApiException as e:
62-
raise ApiRequestException('Error getting response for search_items from Amazon API: ' + str(e))
62+
_manage_response_exceptions(e)
6363

6464
if response.search_result == None:
6565
raise ItemsNotFoudException('No items have been found')
@@ -82,7 +82,7 @@ def get_variations_response(amazon_api, request: GetVariationsRequest) -> Variat
8282
try:
8383
response = amazon_api._api.get_variations(request)
8484
except ApiException as e:
85-
raise ApiRequestException('Error getting response for get_variations from Amazon API: ' + str(e))
85+
_manage_response_exceptions(e)
8686

8787
if response.variations_result == None:
8888
raise ItemsNotFoudException('No variation items have been found')
@@ -105,7 +105,7 @@ def get_browse_nodes_response(amazon_api, request: GetBrowseNodesRequest) -> Lis
105105
try:
106106
response = amazon_api._api.get_browse_nodes(request)
107107
except ApiException as e:
108-
raise ApiRequestException('Error getting response for get_browse_nodes from Amazon API: ' + str(e))
108+
_manage_response_exceptions(e)
109109

110110
if response.browse_nodes_result == None:
111111
raise ItemsNotFoudException('No browse nodes have been found')
@@ -117,3 +117,19 @@ def _get_request_resources(resources) -> List[str]:
117117
resources = inspect.getmembers(resources, lambda a:not(inspect.isroutine(a)))
118118
resources = [x[-1] for x in resources if isinstance(x[-1], str) and x[0][0:2] != '__']
119119
return resources
120+
121+
def _manage_response_exceptions(error) -> None:
122+
if isinstance(error, ApiException):
123+
if error.status == 429:
124+
raise TooManyRequestsException('Requests limit reached, try increasing throttling or wait before trying again')
125+
126+
elif 'InvalidParameterValue' in error.body:
127+
raise InvalidArgumentException('The value provided in the request for atleast one parameter is invalid.')
128+
129+
elif 'InvalidPartnerTag' in error.body:
130+
raise InvalidArgumentException('The partner tag is invalid or not present.')
131+
132+
elif 'InvalidAssociate' in error.body:
133+
raise AssociateValidationException('Used credentials are not valid for the selected country.')
134+
135+
raise ApiRequestException('Request failed: ' + str(error.reason))

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name='python-amazon-paapi',
8-
version='4.0.0',
8+
version='4.1.0',
99
author='Sergio Abad',
1010
author_email='[email protected]',
1111
description='Amazon Product Advertising API 5.0 wrapper for Python',

0 commit comments

Comments
 (0)