Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
sergioteula committed Jul 19, 2021
2 parents f4c694f + 056a469 commit 69bf6b9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
18 changes: 15 additions & 3 deletions amazon_paapi/errors/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -31,3 +31,15 @@ class MalformedRequestException(AmazonException):
class ItemsNotFoudException(AmazonException):
"""Raised if no items are found"""
pass

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

class AssociateValidationException(AmazonException):
"""Raised when credentials are not valid for the selected country."""
pass
20 changes: 14 additions & 6 deletions amazon_paapi/helpers/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
26 changes: 21 additions & 5 deletions amazon_paapi/helpers/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, 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
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -117,3 +117,19 @@ 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')

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.')

elif 'InvalidAssociate' in error.body:
raise AssociateValidationException('Used credentials are not valid for the selected country.')

raise ApiRequestException('Request failed: ' + str(error.reason))
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name='python-amazon-paapi',
version='4.0.0',
version='4.1.0',
author='Sergio Abad',
author_email='[email protected]',
description='Amazon Product Advertising API 5.0 wrapper for Python',
Expand Down

0 comments on commit 69bf6b9

Please sign in to comment.