Skip to content

Added support for AternateVersions Response group #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion amazon/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def browse_node_lookup(self, ResponseGroup="BrowseNodeInfo", **kwargs):
response = self.api.BrowseNodeLookup(
ResponseGroup=ResponseGroup, **kwargs)
root = objectify.fromstring(response)
if root.BrowseNodes.Request.IsValid == 'False':
if hasattr(root.BrowseNodes.Request, 'Errors'):
code = root.BrowseNodes.Request.Errors.Error.Code
msg = root.BrowseNodes.Request.Errors.Error.Message
raise BrowseNodeLookupException(
Expand Down Expand Up @@ -1140,6 +1140,34 @@ def list_price(self):
else:
return None, None

@property
def alternate_versions(self):
"""AlternateVersions.

Returns a list of dicts of items. Used for a search or lookup
using the `ResponseGroup="AlternateVersions"` keyword argument.

:return:
Returns a list of dicts with the keys 'asin', 'title', and
'binding.
"""

def version_dict(version):
asin = self._safe_get_element_text('ASIN', root=version)
title = self._safe_get_element_text('Title', root=version)
binding = self._safe_get_element_text('Binding', root=version)
return {'asin': asin,
'title': title,
'binding': binding}

alternates_elm = self._safe_get_element('AlternateVersions')
if alternates_elm is not None:
versions = [version_dict(version)
for version in alternates_elm.getchildren()]
return versions
else:
return []

def get_attribute(self, name):
"""Get Attribute

Expand Down
36 changes: 31 additions & 5 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
CartInfoMismatchException,
SearchException,
AmazonSearch,
AsinNotFound)
AsinNotFound,
BrowseNodeLookupException)

_AMAZON_ACCESS_KEY = None
_AMAZON_SECRET_KEY = None
Expand Down Expand Up @@ -214,7 +215,6 @@ def test_search_iterate_pages(self):
pass
assert_true(products.is_last_page)


@flaky(max_runs=3, rerun_filter=delay_rerun)
def test_search_no_results(self):
"""Test Product Search with no results.
Expand Down Expand Up @@ -288,6 +288,10 @@ def test_browse_node_lookup(self):
assert_equals(bn.id, bnid)
assert_equals(bn.name, 'eBook Readers')
assert_equals(bn.is_category_root, False)
invalid_bnid = 1036682
assert_raises(BrowseNodeLookupException,
self.amazon.browse_node_lookup,
BrowseNodeId=invalid_bnid)

@flaky(max_runs=3, rerun_filter=delay_rerun)
def test_obscure_date(self):
Expand Down Expand Up @@ -432,7 +436,7 @@ def test_studio(self):
@flaky(max_runs=3, rerun_filter=delay_rerun)
def test_is_preorder(self):
product = self.amazon.lookup(ItemId="B01NBTSVDN")
assert_equals(product.is_preorder , None)
assert_equals(product.is_preorder, None)

@flaky(max_runs=3, rerun_filter=delay_rerun)
def test_detail_page_url(self):
Expand Down Expand Up @@ -464,7 +468,6 @@ def test_availability_min_max_hours(self):
assert_equals(product.availability_min_hours, '0')
assert_equals(product.availability_max_hours, '0')


def test_kwargs(self):
amazon = AmazonAPI(_AMAZON_ACCESS_KEY, _AMAZON_SECRET_KEY,
_AMAZON_ASSOC_TAG, MaxQPS=0.7)
Expand All @@ -481,6 +484,29 @@ def test_images(self):
assert_equals(type(product.images), list)
assert_equals(len(product.images), 7)

@flaky(max_runs=3, rerun_filter=delay_rerun)
def test_alternate_versions(self):
"""Test alternate_versions property

Test that the images property has a value when using the
AlternateVersions ResponseGroup.

This test tries to be as perminant as possible. It doesn't
depend on any specific results that could be ruined if a new
version is added that ends up being the first one in the list,
instead only checking that an alternate version is returned
and that it has the right properties.
"""
product = self.amazon.lookup(ResponseGroup='AlternateVersions',
ItemId='1491914254')
assert_equals(type(product.alternate_versions), list)
assert_true(len(product.alternate_versions) > 0)
assert_equals(type(product.alternate_versions[0]), dict)
assert_equals(len(product.alternate_versions[0]), 3)
assert_true('asin' in product.alternate_versions[0].keys())
assert_true('title' in product.alternate_versions[0].keys())
assert_true('binding' in product.alternate_versions[0].keys())


class TestAmazonCart(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -606,4 +632,4 @@ def test_cart_delete(self):
assert_raises(KeyError, new_cart.__getitem__, cart_item_id)

if __name__ == '__main__':
unittest.main()
unittest.main()