redtoad / python-amazon-product-api

A Python wrapper for the Amazon Product Advertising API. This module offers a light-weight access to the latest version of the Amazon Product Advertising API without getting in your way.
https://pypi.org/project/python-amazon-product-api/
BSD 3-Clause "New" or "Revised" License
3 stars 4 forks source link

item_lookup with multiple ItemId's #31

Closed redtoad closed 10 years ago

redtoad commented 12 years ago

Originally reported by rkatcher (Bitbucket: rkatcher, GitHub: rkatcher)


Hi, I've been using this API for a few days now and love it, thanks for the great work!

I recently encountered behavior that may be an issue and hoping someone here may know ...

I am doing ItemLookup with multiple ItemId's, but if any of the ItemId's are not found on the Amazon site than the whole request fails. I should mention that I added an IdType parameter to item_lookup so I could lookup an item with UPC (pretty straightforward).
prodResult = api.item_lookup(UPCstr, IdType='UPC', SearchIndex=searchIndex, ResponseGroup='Offers,ItemAttributes')

So if I send it the following UPC's (644209004461, 009800895250, 024100440771) everything works fine and prodResult is set. But if I add 301357583001 to the list than the entire item_lookup fails and prodResult is never set. Is this expected? I would think it would return an error for that Item but return results for the rest. I can do the item_lookup's individually but that makes it way too slow when you have anything but a small number of items (sometimes I need to lookup a 15-20 items at a time).

For the record, this has nothing to do with locale as a lot of others have experienced with the InvalidParameterValue error (my locale is 'us' fwiw). That is the error I would expect per Amazon's documentation for this lookup (UPC not found on ItemLookup) http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.html

But again my question is whether the entire item_lookup call should fail with InvalidParameterValue when 1 of n ItemId's are invalid.

Thanks for any info you can provide.

Rob


redtoad commented 10 years ago

Original comment by Oussama Bnet (Bitbucket: OussamaJ, GitHub: OussamaJ)


Used the dev and it is working perfect, thank you

redtoad commented 10 years ago

Original comment by Sebastian Rahlf (Bitbucket: basti, GitHub: basti)


Try out latest (=dev) version. If problem persists, please provide code for me to reproduce.

redtoad commented 10 years ago

Original comment by Oussama Bnet (Bitbucket: OussamaJ, GitHub: OussamaJ)


I am having the same issue with v0.2.7.

Thank you for the great API !

redtoad commented 10 years ago

Original comment by Jannis Gebauer (Bitbucket: jageb, GitHub: jageb)


Nice. I just forked the repo and made a fix for this (needed to pass the xml on reraise), but #42 seems to be a more general approach, so I'll wait for your release.

Do you have a ETA on when you will push this to the dev branch?

redtoad commented 10 years ago

Original comment by Sebastian Rahlf (Bitbucket: basti, GitHub: basti)


This requires #42 to be implemented. I've got a working prototype already.

redtoad commented 10 years ago

Original comment by Jannis Gebauer (Bitbucket: jageb, GitHub: jageb)


Im still struggling with this one on 0.2.7.

#!python

cfg = {"access_key":"", "secret_key":"","associate_tag":""}
api = API(locale="de", cfg=cfg)
params = {"ResponseGroup": "Large", "IdType":"ASIN"}
keywords = ["B00CX7C0FQ", "B00F8ZEAX0"]
try:
    result = api.item_lookup(*keywords, **params)
except InvalidParameterValue, e:
    result = e

result
>>> InvalidParameterValue('ItemId', 'B00CX7C0FQ')
result.__class__
>>> <class 'amazonproduct.errors.InvalidParameterValue'>
result.__dict__
>>> {'msg': None, 'xml': None, 'code': None}
redtoad commented 11 years ago

Original comment by Jannis Gebauer (Bitbucket: jageb, GitHub: jageb)


api = API(AWS_KEY, SECRET_KEY, 'de',ASSOCIATE_TAG)

params = {
    'ResponseGroup' : 'Large',
    'SearchIndex' : 'All',
    'IdType' : 'EAN',
    }

try:
    root = api.item_lookup('5051890006479', '886976038892', '4010884258875', **params)
except InvalidParameterValue,e:

    print "e is",e
    root = e.xml

print type(root)

output is: e is ('ItemId', '886976038892') <type 'NoneType'>

redtoad commented 11 years ago

Original comment by Sebastian Rahlf (Bitbucket: basti, GitHub: basti)


Can you post en example please, so I can reproduce the problem?

redtoad commented 11 years ago

Original comment by Jannis Gebauer (Bitbucket: jageb, GitHub: jageb)


Hey Sebastian,

this seems to no longer work.

I query the api with EANs. If one EAN is not found, "e" from your example returns only: ('ItemId', '#replace_with_bad_item_id').

Is this working as expected and the example is just outdated?

redtoad commented 12 years ago

Original comment by Sebastian Rahlf (Bitbucket: basti, GitHub: basti)


Added example of how to access the original XML response (closes #31).

redtoad commented 12 years ago

Original comment by Sebastian Rahlf (Bitbucket: basti, GitHub: basti)


Bugfix: Keep original exception arguments (see #31).

redtoad commented 12 years ago

Original comment by rkatcher (Bitbucket: rkatcher, GitHub: rkatcher)


Sebastian, so it turns out Amazon is returning valid results for all the other lookups like you suspected. I am working around this for now ... I simply modified the call function to not raise the InvalidParameter exception ... probably not a good long term solution :), but it works for my purposes for now ... perhaps longer term it could attach the error to the results and let the caller check for it and determine how to handle that Amazon error code based on the situation since Amazon seems to apply it very liberally. Hope that is helpful to you when you get around to this down the road. Thanks again for the great API!!

redtoad commented 12 years ago

Original comment by rkatcher (Bitbucket: rkatcher, GitHub: rkatcher)


OK, thanks again Sebastian. I'm sure you realize it, but this issue is pretty important for performance reasons ... at roughly a second per Amazon call, doing individual lookups on 15-20 UPC's is too long for an end user to wait. I can prefetch some of it, but Amazon changes their offers pretty frequently so I can't cache offers for very long. I will take a look at the code as well, but will likely need a more experienced developer to figure it out.

Rob

redtoad commented 12 years ago

Original comment by Sebastian Rahlf (Bitbucket: basti, GitHub: basti)


Well, at the moment this is just a figment of my imagination. Let's see what I can do about this in the next release.

Keep nagging me, if you don't see this feature in version 0.2.6!

redtoad commented 12 years ago

Original comment by rkatcher (Bitbucket: rkatcher, GitHub: rkatcher)


Thanks Sebastian. That's great news that Amazon is returning more xml that might be useful.

Unless I'm mistaken though, e does not contain xml at that point. e seems to only contain the UPC that was not found along with the ItemId identifier.
('ItemId', '301357583001')

So the question is really how do I get at the results from Amazon? Please pardon me if this is an elementary question, as I am still coming up to speed on python. Thanks.

Rob

redtoad commented 12 years ago

Original comment by Sebastian Rahlf (Bitbucket: basti, GitHub: basti)


From the looks of it there is information about the items inside the returned XML. Hmmm.

The question is how would you make this information available and raise the exception at the same time? Maybe something like

#!python

try:
    prodResult = api.item_lookup(UPCstr, IdType='UPC')
except InvalidParameterValue, e:
    print "There was an invalid ItemId!"
    prodResult = e.xml
redtoad commented 12 years ago

Original comment by rkatcher (Bitbucket: rkatcher, GitHub: rkatcher)


Thanks Sebastian. Yes, InvalidParameterValue is the exception. Let me know if you need any info.

redtoad commented 12 years ago

Original comment by Sebastian Rahlf (Bitbucket: basti, GitHub: basti)


I quite agree with you. One bad itemId should not make the whole operation fail. The problem here, however, is that Amazon's XML response will contain an error message which this module picks up and throws the appropriate Python exception. It would be interesting to see if the rest of the XML contained item information.

Presumeably you are getting a amazonproduct.errors.InvalidParameterValue exception. If not please provide traceback and API version.