From 4b63e529fac089cbe488c8fd0d17c9e14ca46ffd Mon Sep 17 00:00:00 2001 From: Sergio Abad Date: Mon, 19 Jul 2021 18:12:33 +0200 Subject: [PATCH 1/6] Added TooManyRequestsException --- amazon_paapi/errors/exceptions.py | 4 ++++ amazon_paapi/helpers/requests.py | 17 ++++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/amazon_paapi/errors/exceptions.py b/amazon_paapi/errors/exceptions.py index a90e0d8..29f4041 100644 --- a/amazon_paapi/errors/exceptions.py +++ b/amazon_paapi/errors/exceptions.py @@ -31,3 +31,7 @@ class MalformedRequestException(AmazonException): class ItemsNotFoudException(AmazonException): """Raised if no items are found""" pass + +class TooManyRequestsException(AmazonException): + """Raised if too many requests are made""" + pass diff --git a/amazon_paapi/helpers/requests.py b/amazon_paapi/helpers/requests.py index 2e9fd9f..a2e03b4 100644 --- a/amazon_paapi/helpers/requests.py +++ b/amazon_paapi/helpers/requests.py @@ -6,7 +6,7 @@ from ..models.search_result import SearchResult from ..models.variations_result import VariationsResult from ..models.browse_nodes_result import BrowseNode -from ..errors import ApiRequestException, ItemsNotFoudException, MalformedRequestException +from ..errors import ApiRequestException, ItemsNotFoudException, MalformedRequestException, TooManyRequestsException from ..sdk.models.partner_type import PartnerType from ..sdk.models.get_items_resource import GetItemsResource from ..sdk.models.get_items_request import GetItemsRequest @@ -36,7 +36,7 @@ def get_items_response(amazon_api, request: GetItemsRequest) -> List[Item]: try: response = amazon_api._api.get_items(request) except ApiException as e: - raise ApiRequestException('Error getting response for get_items from Amazon API: ' + str(e)) + _manage_response_exceptions(e) if response.items_result == None: raise ItemsNotFoudException('No items have been found') @@ -59,7 +59,7 @@ def get_search_items_response(amazon_api, request: SearchItemsRequest) -> Search try: response = amazon_api._api.search_items(request) except ApiException as e: - raise ApiRequestException('Error getting response for search_items from Amazon API: ' + str(e)) + _manage_response_exceptions(e) if response.search_result == None: raise ItemsNotFoudException('No items have been found') @@ -82,7 +82,7 @@ def get_variations_response(amazon_api, request: GetVariationsRequest) -> Variat try: response = amazon_api._api.get_variations(request) except ApiException as e: - raise ApiRequestException('Error getting response for get_variations from Amazon API: ' + str(e)) + _manage_response_exceptions(e) if response.variations_result == None: raise ItemsNotFoudException('No variation items have been found') @@ -105,7 +105,7 @@ def get_browse_nodes_response(amazon_api, request: GetBrowseNodesRequest) -> Lis try: response = amazon_api._api.get_browse_nodes(request) except ApiException as e: - raise ApiRequestException('Error getting response for get_browse_nodes from Amazon API: ' + str(e)) + _manage_response_exceptions(e) if response.browse_nodes_result == None: raise ItemsNotFoudException('No browse nodes have been found') @@ -117,3 +117,10 @@ def _get_request_resources(resources) -> List[str]: resources = inspect.getmembers(resources, lambda a:not(inspect.isroutine(a))) resources = [x[-1] for x in resources if isinstance(x[-1], str) and x[0][0:2] != '__'] return resources + +def _manage_response_exceptions(error) -> None: + if isinstance(error, ApiException): + if error.status == 429: + raise TooManyRequestsException('Requests limit reached, try increasing throttling or wait before trying again') + + raise ApiRequestException('Request failed: ' + str(error)) From 57f07406b5ffc3d1bc251b9530c33c706965f653 Mon Sep 17 00:00:00 2001 From: Sergio Abad Date: Mon, 19 Jul 2021 18:21:30 +0200 Subject: [PATCH 2/6] Added invalid parameters exception handling on request --- amazon_paapi/errors/exceptions.py | 6 +++--- amazon_paapi/helpers/requests.py | 6 +++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/amazon_paapi/errors/exceptions.py b/amazon_paapi/errors/exceptions.py index 29f4041..bc4c2d8 100644 --- a/amazon_paapi/errors/exceptions.py +++ b/amazon_paapi/errors/exceptions.py @@ -3,12 +3,12 @@ class AmazonException(Exception): """Common base class for all Amazon API exceptions.""" - def __init__(self, message: str): + def __init__(self, reason: str): super().__init__() - self.message = message + self.reason = reason def __str__(self) -> str: - return '%s' % self.message + return '%s' % self.reason class InvalidArgumentException(AmazonException): diff --git a/amazon_paapi/helpers/requests.py b/amazon_paapi/helpers/requests.py index a2e03b4..98730ef 100644 --- a/amazon_paapi/helpers/requests.py +++ b/amazon_paapi/helpers/requests.py @@ -1,6 +1,7 @@ """Module with helper functions for creating requests.""" +from amazon_paapi.errors.exceptions import InvalidArgumentException from typing import List from ..models.item_result import Item from ..models.search_result import SearchResult @@ -123,4 +124,7 @@ def _manage_response_exceptions(error) -> None: if error.status == 429: raise TooManyRequestsException('Requests limit reached, try increasing throttling or wait before trying again') - raise ApiRequestException('Request failed: ' + str(error)) + elif 'InvalidParameterValue' in error.body: + raise InvalidArgumentException('The value provided in the request for atleast one parameter is invalid.') + + raise ApiRequestException('Request failed: ' + str(error.reason)) From 1d61f4d2abec37bddb995b1bbfbc45f04ceee1de Mon Sep 17 00:00:00 2001 From: Sergio Abad Date: Mon, 19 Jul 2021 18:25:15 +0200 Subject: [PATCH 3/6] Solved bug with pagination args validation --- amazon_paapi/helpers/arguments.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/amazon_paapi/helpers/arguments.py b/amazon_paapi/helpers/arguments.py index 510b265..0c4bf3d 100644 --- a/amazon_paapi/helpers/arguments.py +++ b/amazon_paapi/helpers/arguments.py @@ -38,18 +38,26 @@ def _check_search_mandatory_args(**kwargs): def _check_search_pagination_args(**kwargs): + error_message = ('Args item_count and item_page should be integers between 1 and 10.') pagination_args = [kwargs['item_count'], kwargs['item_page']] pagination_args = [arg for arg in pagination_args if arg] - if not all(1 <= arg <= 10 and isinstance(arg, int) for arg in pagination_args): - error_message = ('Args item_count and item_page should be integers between 1 and 10.') + + if not all(isinstance(arg, int) for arg in pagination_args): + raise InvalidArgumentException(error_message) + + if not all(1 <= arg <= 10 for arg in pagination_args): raise InvalidArgumentException(error_message) def check_variations_args(**kwargs): - pagination_args = [kwargs['variation_count'], kwargs['variation_page']] - pagination_args = [arg for arg in pagination_args if arg] - if not all(1 <= arg <= 10 and isinstance(arg, int) for arg in pagination_args): - error_message = ('Args variation_count and variation_page should be integers between 1 and 10.') + error_message = ('Args variation_count and variation_page should be integers between 1 and 10.') + variation_args = [kwargs['variation_count'], kwargs['variation_page']] + variation_args = [arg for arg in variation_args if arg] + + if not all(isinstance(arg, int) for arg in variation_args): + raise InvalidArgumentException(error_message) + + if not all(1 <= arg <= 10 for arg in variation_args): raise InvalidArgumentException(error_message) From 64f5a44537b669299b1effe4d914e1ab6f4bf401 Mon Sep 17 00:00:00 2001 From: Sergio Abad Date: Mon, 19 Jul 2021 19:11:34 +0200 Subject: [PATCH 4/6] Added invalid partner tag exception --- amazon_paapi/errors/exceptions.py | 4 ++++ amazon_paapi/helpers/requests.py | 3 +++ 2 files changed, 7 insertions(+) diff --git a/amazon_paapi/errors/exceptions.py b/amazon_paapi/errors/exceptions.py index bc4c2d8..cb078d6 100644 --- a/amazon_paapi/errors/exceptions.py +++ b/amazon_paapi/errors/exceptions.py @@ -35,3 +35,7 @@ class ItemsNotFoudException(AmazonException): class TooManyRequestsException(AmazonException): """Raised if too many requests are made""" pass + +class InvalidPartnerTagException(AmazonException): + """Raised if the partner tag is not present or invalid""" + pass diff --git a/amazon_paapi/helpers/requests.py b/amazon_paapi/helpers/requests.py index 98730ef..a85da14 100644 --- a/amazon_paapi/helpers/requests.py +++ b/amazon_paapi/helpers/requests.py @@ -127,4 +127,7 @@ def _manage_response_exceptions(error) -> None: elif 'InvalidParameterValue' in error.body: raise InvalidArgumentException('The value provided in the request for atleast one parameter is invalid.') + elif 'InvalidPartnerTag' in error.body: + raise InvalidArgumentException('The partner tag is invalid or not present.') + raise ApiRequestException('Request failed: ' + str(error.reason)) From 0bc349cbfbb07bec2bf0616460be3aaec0938a6d Mon Sep 17 00:00:00 2001 From: Sergio Abad Date: Mon, 19 Jul 2021 19:24:22 +0200 Subject: [PATCH 5/6] Added exception for country availabiliy --- amazon_paapi/errors/exceptions.py | 4 ++++ amazon_paapi/helpers/requests.py | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/amazon_paapi/errors/exceptions.py b/amazon_paapi/errors/exceptions.py index cb078d6..dd3ef8f 100644 --- a/amazon_paapi/errors/exceptions.py +++ b/amazon_paapi/errors/exceptions.py @@ -39,3 +39,7 @@ class TooManyRequestsException(AmazonException): class InvalidPartnerTagException(AmazonException): """Raised if the partner tag is not present or invalid""" pass + +class AssociateValidationException(AmazonException): + """Raised when credentials are not valid for the selected country.""" + pass diff --git a/amazon_paapi/helpers/requests.py b/amazon_paapi/helpers/requests.py index a85da14..05a894d 100644 --- a/amazon_paapi/helpers/requests.py +++ b/amazon_paapi/helpers/requests.py @@ -1,13 +1,12 @@ """Module with helper functions for creating requests.""" -from amazon_paapi.errors.exceptions import InvalidArgumentException from typing import List from ..models.item_result import Item from ..models.search_result import SearchResult from ..models.variations_result import VariationsResult from ..models.browse_nodes_result import BrowseNode -from ..errors import ApiRequestException, ItemsNotFoudException, MalformedRequestException, TooManyRequestsException +from ..errors import ApiRequestException, ItemsNotFoudException, MalformedRequestException, TooManyRequestsException, AssociateValidationException, InvalidArgumentException from ..sdk.models.partner_type import PartnerType from ..sdk.models.get_items_resource import GetItemsResource from ..sdk.models.get_items_request import GetItemsRequest @@ -130,4 +129,7 @@ def _manage_response_exceptions(error) -> None: elif 'InvalidPartnerTag' in error.body: raise InvalidArgumentException('The partner tag is invalid or not present.') + elif 'InvalidAssociate' in error.body: + raise AssociateValidationException('Used credentials are not valid for the selected country.') + raise ApiRequestException('Request failed: ' + str(error.reason)) From 056a46978818e70a5042ac20547290d781f91933 Mon Sep 17 00:00:00 2001 From: Sergio Abad Date: Mon, 19 Jul 2021 19:27:22 +0200 Subject: [PATCH 6/6] Updated version to 4.1.0 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 30f5bce..7b15f8e 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name='python-amazon-paapi', - version='4.0.0', + version='4.1.0', author='Sergio Abad', author_email='sergio.abad@bytelix.com', description='Amazon Product Advertising API 5.0 wrapper for Python',