Skip to content

Commit 35aaaad

Browse files
committed
Renamed exceptions
1 parent f800106 commit 35aaaad

8 files changed

Lines changed: 73 additions & 73 deletions

File tree

amazon_paapi/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import List, Union
88

99
from . import models
10-
from .errors import InvalidArgumentException
10+
from .errors import InvalidArgument
1111
from .helpers import arguments, requests
1212
from .helpers.generators import get_list_chunks
1313
from .helpers.items import sort_items
@@ -50,7 +50,7 @@ def __init__(
5050
self.region = models.regions.REGIONS[country]
5151
self.marketplace = "www.amazon." + models.regions.DOMAINS[country]
5252
except KeyError as error:
53-
raise InvalidArgumentException("Country code is not correct") from error
53+
raise InvalidArgument("Country code is not correct") from error
5454

5555
self.api = DefaultApi(key, secret, self._host, self.region)
5656

amazon_paapi/errors/__init__.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
from .exceptions import (
2-
AmazonException,
3-
ApiRequestException,
4-
AsinNotFoundException,
5-
AssociateValidationException,
6-
InvalidArgumentException,
7-
InvalidPartnerTagException,
8-
ItemsNotFoundException,
9-
MalformedRequestException,
10-
TooManyRequestsException,
2+
AmazonError,
3+
AsinNotFound,
4+
AssociateValidationError,
5+
InvalidArgument,
6+
InvalidPartnerTag,
7+
ItemsNotFound,
8+
MalformedRequest,
9+
RequestError,
10+
TooManyRequests,
1111
)
1212

1313
__all__ = [
14-
"AmazonException",
15-
"ApiRequestException",
16-
"AsinNotFoundException",
17-
"AssociateValidationException",
18-
"InvalidArgumentException",
19-
"InvalidPartnerTagException",
20-
"ItemsNotFoundException",
21-
"MalformedRequestException",
22-
"TooManyRequestsException",
14+
"AmazonError",
15+
"AsinNotFound",
16+
"AssociateValidationError",
17+
"InvalidArgument",
18+
"InvalidPartnerTag",
19+
"ItemsNotFound",
20+
"MalformedRequest",
21+
"RequestError",
22+
"TooManyRequests",
2323
]

amazon_paapi/errors/exceptions.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Custom exceptions module"""
22

33

4-
class AmazonException(Exception):
4+
class AmazonError(Exception):
55
"""Common base class for all Amazon API exceptions."""
66

77
def __init__(self, reason: str):
@@ -12,33 +12,33 @@ def __str__(self) -> str:
1212
return self.reason
1313

1414

15-
class InvalidArgumentException(AmazonException):
16-
"""Raised when arguments are not correct."""
15+
class AsinNotFound(AmazonError):
16+
"""Raised if the ASIN for an item is not found."""
1717

1818

19-
class AsinNotFoundException(AmazonException):
20-
"""Raised if the ASIN for an item is not found."""
19+
class AssociateValidationError(AmazonError):
20+
"""Raised when credentials are not valid for the selected country."""
2121

2222

23-
class ApiRequestException(AmazonException):
24-
"""Raised if the request to Amazon API fails"""
23+
class InvalidArgument(AmazonError):
24+
"""Raised when arguments are not correct."""
2525

2626

27-
class MalformedRequestException(AmazonException):
28-
"""Raised if the request for Amazon API is not correctly formed"""
27+
class InvalidPartnerTag(AmazonError):
28+
"""Raised if the partner tag is not present or invalid."""
2929

3030

31-
class ItemsNotFoundException(AmazonException):
32-
"""Raised if no items are found"""
31+
class ItemsNotFound(AmazonError):
32+
"""Raised if no items are found."""
3333

3434

35-
class TooManyRequestsException(AmazonException):
36-
"""Raised when requests limit is reached"""
35+
class MalformedRequest(AmazonError):
36+
"""Raised if the request for Amazon API is not correctly formed."""
3737

3838

39-
class InvalidPartnerTagException(AmazonException):
40-
"""Raised if the partner tag is not present or invalid"""
39+
class RequestError(AmazonError):
40+
"""Raised if the request to Amazon API fails."""
4141

4242

43-
class AssociateValidationException(AmazonException):
44-
"""Raised when credentials are not valid for the selected country"""
43+
class TooManyRequests(AmazonError):
44+
"""Raised when requests limit is reached."""

amazon_paapi/helpers/arguments.py

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

44
from typing import List, Union
55

6-
from ..errors import AsinNotFoundException, InvalidArgumentException
6+
from ..errors import AsinNotFound, InvalidArgument
77
from ..tools import get_asin
88

99

1010
def get_items_ids(items: Union[str, List[str]]) -> List[str]:
1111
if not isinstance(items, str) and not isinstance(items, List):
12-
raise InvalidArgumentException(
12+
raise InvalidArgument(
1313
"Invalid items argument, it should be a string or List of strings"
1414
)
1515

@@ -23,7 +23,7 @@ def get_items_ids(items: Union[str, List[str]]) -> List[str]:
2323
if items_ids:
2424
return items_ids
2525

26-
raise AsinNotFoundException("No ASIN codes have been found.")
26+
raise AsinNotFound("No ASIN codes have been found.")
2727

2828

2929
def check_search_args(**kwargs):
@@ -47,7 +47,7 @@ def _check_search_mandatory_args(**kwargs):
4747
"At least one of the following args should be provided: keywords, actor,"
4848
" artist, author, brand, title, browse_node_id or search_index."
4949
)
50-
raise InvalidArgumentException(error_message)
50+
raise InvalidArgument(error_message)
5151

5252

5353
def _check_search_pagination_args(**kwargs):
@@ -56,10 +56,10 @@ def _check_search_pagination_args(**kwargs):
5656
pagination_args = [arg for arg in pagination_args if arg]
5757

5858
if not all(isinstance(arg, int) for arg in pagination_args):
59-
raise InvalidArgumentException(error_message)
59+
raise InvalidArgument(error_message)
6060

6161
if not all(1 <= arg <= 10 for arg in pagination_args):
62-
raise InvalidArgumentException(error_message)
62+
raise InvalidArgument(error_message)
6363

6464

6565
def check_variations_args(**kwargs):
@@ -70,13 +70,13 @@ def check_variations_args(**kwargs):
7070
variation_args = [arg for arg in variation_args if arg]
7171

7272
if not all(isinstance(arg, int) for arg in variation_args):
73-
raise InvalidArgumentException(error_message)
73+
raise InvalidArgument(error_message)
7474

7575
if not all(1 <= arg <= 10 for arg in variation_args):
76-
raise InvalidArgumentException(error_message)
76+
raise InvalidArgument(error_message)
7777

7878

7979
def check_browse_nodes_args(**kwargs):
8080
if not isinstance(kwargs["browse_node_ids"], List):
8181
error_message = "Argument browse_node_ids should be a List of strings."
82-
raise InvalidArgumentException(error_message)
82+
raise InvalidArgument(error_message)

amazon_paapi/helpers/requests.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
from typing import List
66

77
from ..errors import (
8-
ApiRequestException,
9-
AssociateValidationException,
10-
InvalidArgumentException,
11-
ItemsNotFoundException,
12-
MalformedRequestException,
13-
TooManyRequestsException,
8+
AssociateValidationError,
9+
InvalidArgument,
10+
ItemsNotFound,
11+
MalformedRequest,
12+
RequestError,
13+
TooManyRequests,
1414
)
1515
from ..models.browse_nodes_result import BrowseNode
1616
from ..models.item_result import Item
@@ -39,7 +39,7 @@ def get_items_request(amazon_api, asin_chunk: List[str], **kwargs) -> GetItemsRe
3939
**kwargs,
4040
)
4141
except TypeError as exc:
42-
raise MalformedRequestException(
42+
raise MalformedRequest(
4343
f"Parameters for get_items request are not correct: {exc}"
4444
) from exc
4545

@@ -51,7 +51,7 @@ def get_items_response(amazon_api, request: GetItemsRequest) -> List[Item]:
5151
_manage_response_exceptions(exc)
5252

5353
if response.items_result is None:
54-
raise ItemsNotFoundException("No items have been found")
54+
raise ItemsNotFound("No items have been found")
5555

5656
return response.items_result.items
5757

@@ -66,7 +66,7 @@ def get_search_items_request(amazon_api, **kwargs) -> SearchItemsRequest:
6666
**kwargs,
6767
)
6868
except TypeError as exc:
69-
raise MalformedRequestException(
69+
raise MalformedRequest(
7070
f"Parameters for search_items request are not correct: {exc}"
7171
) from exc
7272

@@ -78,7 +78,7 @@ def get_search_items_response(amazon_api, request: SearchItemsRequest) -> Search
7878
_manage_response_exceptions(exc)
7979

8080
if response.search_result is None:
81-
raise ItemsNotFoundException("No items have been found")
81+
raise ItemsNotFound("No items have been found")
8282

8383
return response.search_result
8484

@@ -93,7 +93,7 @@ def get_variations_request(amazon_api, **kwargs) -> GetVariationsRequest:
9393
**kwargs,
9494
)
9595
except TypeError as exc:
96-
raise MalformedRequestException(
96+
raise MalformedRequest(
9797
f"Parameters for get_variations request are not correct: {exc}"
9898
) from exc
9999

@@ -107,7 +107,7 @@ def get_variations_response(
107107
_manage_response_exceptions(exc)
108108

109109
if response.variations_result is None:
110-
raise ItemsNotFoundException("No variation items have been found")
110+
raise ItemsNotFound("No variation items have been found")
111111

112112
return response.variations_result
113113

@@ -122,7 +122,7 @@ def get_browse_nodes_request(amazon_api, **kwargs) -> GetBrowseNodesRequest:
122122
**kwargs,
123123
)
124124
except TypeError as exc:
125-
raise MalformedRequestException(
125+
raise MalformedRequest(
126126
f"Parameters for get_browse_nodes request are not correct: {exc}"
127127
) from exc
128128

@@ -136,7 +136,7 @@ def get_browse_nodes_response(
136136
_manage_response_exceptions(exc)
137137

138138
if response.browse_nodes_result is None:
139-
raise ItemsNotFoundException("No browse nodes have been found")
139+
raise ItemsNotFound("No browse nodes have been found")
140140

141141
return response.browse_nodes_result.browse_nodes
142142

@@ -152,23 +152,23 @@ def _get_request_resources(resources) -> List[str]:
152152
def _manage_response_exceptions(error) -> None:
153153
if isinstance(error, ApiException):
154154
if error.status == 429:
155-
raise TooManyRequestsException(
155+
raise TooManyRequests(
156156
"Requests limit reached, try increasing throttling or wait before"
157157
" trying again"
158158
)
159159

160160
if "InvalidParameterValue" in error.body:
161-
raise InvalidArgumentException(
161+
raise InvalidArgument(
162162
"The value provided in the request for atleast one parameter is"
163163
" invalid."
164164
)
165165

166166
if "InvalidPartnerTag" in error.body:
167-
raise InvalidArgumentException("The partner tag is invalid or not present.")
167+
raise InvalidArgument("The partner tag is invalid or not present.")
168168

169169
if "InvalidAssociate" in error.body:
170-
raise AssociateValidationException(
170+
raise AssociateValidationError(
171171
"Used credentials are not valid for the selected country."
172172
)
173173

174-
raise ApiRequestException("Request failed: " + str(error.reason))
174+
raise RequestError("Request failed: " + str(error.reason))

amazon_paapi/tools/asin.py

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

33
import re
44

5-
from ..errors import AsinNotFoundException
5+
from ..errors import AsinNotFound
66

77

88
def get_asin(text: str) -> str:
@@ -16,4 +16,4 @@ def get_asin(text: str) -> str:
1616
if asin:
1717
return asin.group(2).upper()
1818

19-
raise AsinNotFoundException("Asin not found: " + text)
19+
raise AsinNotFound("Asin not found: " + text)

tests/test_helpers_arguments.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22

3-
from amazon_paapi.errors import AsinNotFoundException, InvalidArgumentException
3+
from amazon_paapi.errors import AsinNotFound, InvalidArgument
44
from amazon_paapi.helpers.arguments import get_items_ids
55

66

@@ -22,9 +22,9 @@ def test_get_items_ids(self):
2222
)
2323

2424
def test_get_items_ids_asin_not_found(self):
25-
with self.assertRaises(AsinNotFoundException):
25+
with self.assertRaises(AsinNotFound):
2626
get_items_ids("https://www.amazon.es/gp/")
2727

2828
def test_get_items_ids_invalid_argument(self):
29-
with self.assertRaises(InvalidArgumentException):
29+
with self.assertRaises(InvalidArgument):
3030
get_items_ids(34)

tests/test_tools.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22

3-
from amazon_paapi.errors import AsinNotFoundException
3+
from amazon_paapi.errors import AsinNotFound
44
from amazon_paapi.tools import get_asin
55

66

@@ -28,8 +28,8 @@ def test_get_asin(self):
2828
)
2929

3030
def test_asin_not_found(self):
31-
with self.assertRaises(AsinNotFoundException):
31+
with self.assertRaises(AsinNotFound):
3232
get_asin("https://www.amazon.es/gp/")
3333

34-
with self.assertRaises(AsinNotFoundException):
34+
with self.assertRaises(AsinNotFound):
3535
get_asin("this is not even a URL")

0 commit comments

Comments
 (0)