Skip to content

Commit ac5e934

Browse files
committed
Removed GetBrowseNodes, updated exception handling and cleaned args
1 parent c4e852b commit ac5e934

2 files changed

Lines changed: 43 additions & 54 deletions

File tree

amazon/constant.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from paapi5_python_sdk.get_items_resource import GetItemsResource
44
from paapi5_python_sdk.search_items_resource import SearchItemsResource
55
from paapi5_python_sdk.get_variations_resource import GetVariationsResource
6-
from paapi5_python_sdk.get_browse_nodes_resource import GetBrowseNodesResource
76
from paapi5_python_sdk.condition import Condition
87

98
"""Available regions for the Amazon API."""
@@ -229,9 +228,3 @@
229228
GetVariationsResource.VARIATIONSUMMARY_PRICE_LOWESTPRICE,
230229
GetVariationsResource.VARIATIONSUMMARY_VARIATIONDIMENSION
231230
]
232-
233-
"""Browse node resources to get from Amazon API."""
234-
BROWSE_RESOURCES = [
235-
GetBrowseNodesResource.ANCESTOR,
236-
GetBrowseNodesResource.CHILDREN
237-
]

amazon/paapi.py

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class AmazonAPI:
3333
throttling (float, optional): Reduce this value to wait longer
3434
between API calls.
3535
"""
36-
def __init__(self, key: str, secret: str, tag: str, country: str, throttling=0.9):
36+
def __init__(self, key: str, secret: str, tag: str, country: str, throttling=0.8):
3737
self.key = key
3838
self.secret = secret
3939
self.tag = tag
@@ -54,7 +54,8 @@ def __init__(self, key: str, secret: str, tag: str, country: str, throttling=0.9
5454
self.api = DefaultApi(access_key=self.key, secret_key=self.secret, host=self.host,
5555
region=self.region)
5656

57-
def get_products(self, product_ids: [str, list], condition='Any', async_req=False):
57+
def get_products(self, product_ids: [str, list], condition='Any', merchant='All',
58+
async_req=False):
5859
"""Find product information for multiple products on Amazon.
5960
6061
Args:
@@ -63,6 +64,9 @@ def get_products(self, product_ids: [str, list], condition='Any', async_req=Fals
6364
condition (str, optional): Specify the product condition.
6465
Allowed values: Any, Collectible, New, Refurbished, Used.
6566
Defaults to Any.
67+
merchant (str, optional): Filters search results to return items
68+
having at least one offer sold by target merchant. Allowed values:
69+
All, Amazon. Defaults to All.
6670
async_req (bool, optional): Specify if a thread should be created to
6771
run the request. Defaults to False.
6872
@@ -85,11 +89,14 @@ def get_products(self, product_ids: [str, list], condition='Any', async_req=Fals
8589
request = GetItemsRequest(partner_tag=self.tag,
8690
partner_type=PartnerType.ASSOCIATES,
8791
marketplace=self.marketplace,
92+
merchant=merchant,
8893
condition=CONDITION[condition],
8994
item_ids=asin_list,
9095
resources=PRODUCT_RESOURCES)
91-
except Exception as exception:
92-
raise AmazonException(exception.status, exception.reason)
96+
except KeyError:
97+
raise AmazonException('KeyError', 'Invalid condition value')
98+
except Exception as e:
99+
raise AmazonException('GetItemsError', e)
93100

94101
for x in range(3):
95102
try:
@@ -114,22 +121,26 @@ def get_products(self, product_ids: [str, list], condition='Any', async_req=Fals
114121
if len(response.items_result.items) > 0:
115122
for item in response.items_result.items:
116123
results.append(parse_product(item))
117-
except Exception as exception:
118-
raise AmazonException(exception.status, exception.reason)
124+
except Exception as e:
125+
raise AmazonException('ResponseError', e)
119126

120127
if results:
121128
return results
122129
else:
123130
return None
124131

125-
def get_product(self, product_id: str, condition='Any', async_req=False):
132+
def get_product(self, product_id: str, condition='Any', merchant='All',
133+
async_req=False):
126134
"""Find product information for a specific product on Amazon.
127135
128136
Args:
129137
product_id (str): One item ID like ASIN or product URL.
130138
condition (str, optional): Specify the product condition.
131139
Allowed values: Any, Collectible, New, Refurbished, Used.
132140
Defaults to Any.
141+
merchant (str, optional): Filters search results to return items
142+
having at least one offer sold by target merchant. Allowed values:
143+
All, Amazon. Defaults to All.
133144
async_req (bool, optional): Specify if a thread should be created to
134145
run the request. Defaults to False.
135146
@@ -138,20 +149,20 @@ def get_product(self, product_id: str, condition='Any', async_req=False):
138149
for the product or None if no results.
139150
"""
140151
if isinstance(product_id, list):
141-
raise AmazonException('TypeError', 'Product ID should be string, not list')
152+
raise AmazonException('TypeError', 'Arg product_id should be string')
142153
if isinstance(product_id, str):
143154
check_product_id = product_id.split(',')
144155
if len(check_product_id) > 1:
145156
raise AmazonException('ValueError', 'Only 1 product ID is allowed, use '
146157
'get_products for multiple requests')
147-
return self.get_products(product_id, condition=condition, async_req=async_req)[0]
148-
149-
def search_products(self, item_count=10, item_page=1, items_per_page=10, actor=None,
150-
artist=None, author=None, availability=None, brand=None, browse_node=None,
151-
condition='Any', currency=None, delivery=None, keywords=None,
152-
languages=None, max_price=None, min_price=None, min_rating=None,
153-
min_discount=None, merchant=None, search_index=None, sort_by=None,
154-
title=None, async_req=False):
158+
return self.get_products(product_id, condition=condition, merchant=merchant,
159+
async_req=async_req)[0]
160+
161+
def search_products(self, item_count=10, item_page=1, items_per_page=10, keywords=None,
162+
actor=None, artist=None, author=None, brand=None, title=None,
163+
availability='Available', browse_node=None, condition='Any', delivery=None,
164+
max_price=None, min_price=None, min_rating=None, min_discount=None,
165+
merchant='All', search_index='All', sort_by=None, async_req=False):
155166
"""Search products on Amazon using different parameters. At least one of the
156167
following parameters should be used: keywords, actor, artist, author, brand,
157168
title.
@@ -163,27 +174,23 @@ def search_products(self, item_count=10, item_page=1, items_per_page=10, actor=N
163174
1 and 10. Defaults to 1.
164175
items_per_page (int, optional): Products on each page. Should be between
165176
1 and 10. Defaults to 10.
177+
keywords (str, optional): A word or phrase that describes an item.
166178
actor (str, optional): Actor name associated with the item.
167179
artist (str, optional): Artist name associated with the item.
168180
author (str, optional): Author name associated with the item.
169-
availability (str, optional): Filters available items on Amazon. By
170-
default, all requests returns available items only.
171-
Allowed values: Available, IncludeOutOfStock.
172181
brand (str, optional): Brand name associated with the item.
182+
title (str, optional): Title associated with the item.
183+
availability (str, optional): Filters available items on Amazon. Allowed values:
184+
Available, IncludeOutOfStock. Defaults to Available.
173185
browse_node (str, optional): A unique ID assigned by Amazon that
174186
identifies a product category or subcategory.
175187
condition (str, optional): The condition parameter filters offers by
176-
condition type. By default, condition equals Any.
177-
Allowed values: Any, Collectible, New, Refurbished, Used.
178-
currency (str, optional): Currency of preference in which the prices
179-
information should be returned in response.
188+
condition type. Allowed values: Any, Collectible, New, Refurbished, Used.
189+
Defaults to Any.
180190
delivery (list, optional): The delivery flag filters items which
181191
satisfy a certain delivery program promoted by the specific
182192
Amazon Marketplace. Allowed values: AmazonGlobal, FreeShipping,
183193
FulfilledByAmazon, Prime.
184-
keywords (str, optional): A word or phrase that describes an item.
185-
languages (list, optional): Languages in order of preference in which
186-
the item information should be returned in response.
187194
max_price (int, optional): Filters search results to items with at
188195
least one offer price below the specified value.
189196
min_price (int, optional): Filters search results to items with at
@@ -194,14 +201,13 @@ def search_products(self, item_count=10, item_page=1, items_per_page=10, actor=N
194201
at least one offer having saving percentage above the specified
195202
value.
196203
merchant (str, optional): Filters search results to return items
197-
having at least one offer sold by target merchant. By default
198-
the value All is passed. Allowed values: All, Amazon.
204+
having at least one offer sold by target merchant. Allowed values:
205+
All, Amazon. Defaults to All.
199206
search_index (str, optional): Indicates the product category to
200-
search.
207+
search. Defaults to All.
201208
sort_by (str, optional): The way in which items in the response
202209
are sorted. Allowed values: AvgCustomerReviews, Featured,
203210
NewestArrivals, Price:HighToLow, Price:LowToHigh, Relevance.
204-
title (str, optional): Title associated with the item.
205211
async_req (bool, optional): Specify if a thread should be created to
206212
run the request. Defaults to False.
207213
@@ -219,7 +225,6 @@ def search_products(self, item_count=10, item_page=1, items_per_page=10, actor=N
219225
raise AmazonException('ValueError', 'At least one of the following args must be '
220226
'provided: keywords, actor, artist, author, brand,'
221227
'title')
222-
223228
results = []
224229
while len(results) < item_count:
225230
try:
@@ -233,12 +238,10 @@ def search_products(self, item_count=10, item_page=1, items_per_page=10, actor=N
233238
brand=brand,
234239
browse_node_id=browse_node,
235240
condition=CONDITION[condition],
236-
currency_of_preference=currency,
237241
delivery_flags=delivery,
238242
item_count=items_per_page,
239243
item_page=item_page,
240244
keywords=keywords,
241-
languages_of_preference=languages,
242245
max_price=max_price,
243246
merchant=merchant,
244247
min_price=min_price,
@@ -251,10 +254,8 @@ def search_products(self, item_count=10, item_page=1, items_per_page=10, actor=N
251254
title=title)
252255
except KeyError:
253256
raise AmazonException('KeyError', 'Invalid condition value')
254-
except ValueError as e:
255-
raise AmazonException('ValueError', e)
256257
except Exception as e:
257-
raise AmazonException('Error', e)
258+
raise AmazonException('SearchItemsError', e)
258259

259260
for x in range(3):
260261
try:
@@ -284,8 +285,8 @@ def search_products(self, item_count=10, item_page=1, items_per_page=10, actor=N
284285
break
285286
if response.errors is not None:
286287
raise AmazonException(response.errors[0].code, response.errors[0].message)
287-
except Exception as exception:
288-
raise AmazonException(exception.reason, exception.body)
288+
except Exception as e:
289+
raise AmazonException('ResponseError', e)
289290
item_page += 1
290291
if item_page > 10:
291292
break
@@ -296,7 +297,7 @@ def search_products(self, item_count=10, item_page=1, items_per_page=10, actor=N
296297
return None
297298

298299
def get_variations(self, asin, item_count=10, item_page=1, items_per_page=10, condition='Any',
299-
currency=None, languages=None, merchant=None, async_req=False):
300+
merchant='All', async_req=False):
300301

301302
if items_per_page > 10 or items_per_page < 1:
302303
raise AmazonException('ValueError', 'Arg items_per_page should be between 1 and 10')
@@ -314,19 +315,15 @@ def get_variations(self, asin, item_count=10, item_page=1, items_per_page=10, co
314315
marketplace=self.marketplace,
315316
asin=asin,
316317
condition=CONDITION[condition],
317-
currency_of_preference=currency,
318-
languages_of_preference=languages,
319318
merchant=merchant,
320319
offer_count=1,
321320
variation_count=items_per_page,
322321
variation_page=item_page,
323322
resources=VARIATION_RESOURCES)
324323
except KeyError:
325324
raise AmazonException('KeyError', 'Invalid condition value')
326-
except ValueError as e:
327-
raise AmazonException('ValueError', e)
328325
except Exception as e:
329-
raise AmazonException('Error', e)
326+
raise AmazonException('GetVariationsError', e)
330327

331328
for x in range(3):
332329
try:
@@ -356,9 +353,8 @@ def get_variations(self, asin, item_count=10, item_page=1, items_per_page=10, co
356353
break
357354
if response.errors is not None:
358355
raise AmazonException(response.errors[0].code, response.errors[0].message)
359-
360-
except Exception as exception:
361-
raise AmazonException(exception.reason, exception.body)
356+
except Exception as e:
357+
raise AmazonException('ResponseError', e)
362358
item_page += 1
363359
if item_page > 10:
364360
break

0 commit comments

Comments
 (0)