本文整理汇总了Python中amazon.api.AmazonAPI.cart_add方法的典型用法代码示例。如果您正苦于以下问题:Python AmazonAPI.cart_add方法的具体用法?Python AmazonAPI.cart_add怎么用?Python AmazonAPI.cart_add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类amazon.api.AmazonAPI
的用法示例。
在下文中一共展示了AmazonAPI.cart_add方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: createCart
# 需要导入模块: from amazon.api import AmazonAPI [as 别名]
# 或者: from amazon.api.AmazonAPI import cart_add [as 别名]
def createCart(products):
pdict = {}
for item in products:
if item in pdict:
pdict[item] = pdict[item] + 1
else:
pdict[item] = 1
amazon = AmazonAPI(ACCESS_KEY, SECRET_KEY, ASSOC_ID)
for index, key in enumerate(pdict.items()):
product = amazon.lookup(ItemId=key[0])
item = {'offer_id': product.offer_id, 'quantity': key[1]}
if index == 0:
cart = amazon.cart_create(item)
else:
cart = amazon.cart_add(item, cart.cart_id, cart.hmac)
return cart
示例2: TestAmazonCart
# 需要导入模块: from amazon.api import AmazonAPI [as 别名]
# 或者: from amazon.api.AmazonAPI import cart_add [as 别名]
class TestAmazonCart(unittest.TestCase):
def setUp(self):
self.amazon = AmazonAPI(
AMAZON_ACCESS_KEY,
AMAZON_SECRET_KEY,
AMAZON_ASSOC_TAG,
CacheReader=cache_reader,
CacheWriter=cache_writer,
MaxQPS=0.5
)
def test_cart_clear_required_params(self):
self.assertRaises(CartException, self.amazon.cart_clear, None, None)
self.assertRaises(CartException, self.amazon.cart_clear, 'NotNone', None)
self.assertRaises(CartException, self.amazon.cart_clear, None, 'NotNone')
def build_cart_object(self):
product = self.amazon.lookup(ItemId="B0016J8AOC")
return self.amazon.cart_create(
{
'offer_id': product._safe_get_element('Offers.Offer.OfferListing.OfferListingId'),
'quantity': 1
}
)
def test_cart_create_single_item(self):
cart = self.build_cart_object()
assert_equals(len(cart), 1)
def test_cart_create_multiple_item(self):
product1 = self.amazon.lookup(ItemId="B0016J8AOC")
product2 = self.amazon.lookup(ItemId="B007HCCNJU")
asins = [product1.asin, product2.asin]
cart = self.amazon.cart_create([
{
'offer_id': product1._safe_get_element('Offers.Offer.OfferListing.OfferListingId'),
'quantity': 1
},
{
'offer_id': product2._safe_get_element('Offers.Offer.OfferListing.OfferListingId'),
'quantity': 1
},
])
assert_equals(len(cart), 2)
for item in cart:
assert_true(item.asin in asins)
def test_cart_clear(self):
cart = self.build_cart_object()
new_cart = self.amazon.cart_clear(cart.cart_id, cart.hmac)
assert_true(new_cart._safe_get_element('Cart.Request.IsValid'))
def test_cart_clear_wrong_hmac(self):
cart = self.build_cart_object()
# never use urlencoded hmac, as library encodes as well. Just in case hmac = url_encoded_hmac we add some noise
hmac = cart.url_encoded_hmac + '%3d'
self.assertRaises(CartInfoMismatchException, self.amazon.cart_clear, cart.cart_id, hmac)
def test_cart_attributes(self):
cart = self.build_cart_object()
for attribute in CART_ATTRIBUTES:
getattr(cart, attribute)
def test_cart_item_attributes(self):
cart = self.build_cart_object()
for item in cart:
for attribute in CART_ITEM_ATTRIBUTES:
getattr(item, attribute)
def test_cart_get(self):
# We need to flush the cache here so we will get a new cart that has not been used in test_cart_clear
cache_clear()
cart = self.build_cart_object()
fetched_cart = self.amazon.cart_get(cart.cart_id, cart.hmac)
assert_equals(fetched_cart.cart_id, cart.cart_id)
assert_equals(len(fetched_cart), len(cart))
def test_cart_get_wrong_hmac(self):
# We need to flush the cache here so we will get a new cart that has not been used in test_cart_clear
cache_clear()
cart = self.build_cart_object()
self.assertRaises(CartInfoMismatchException, self.amazon.cart_get, cart.cart_id, cart.hmac + '%3d')
def test_cart_add(self):
cart = self.build_cart_object()
product = self.amazon.lookup(ItemId="B007HCCNJU")
item = {
'offer_id': product._safe_get_element('Offers.Offer.OfferListing.OfferListingId'),
'quantity': 1
}
new_cart = self.amazon.cart_add(item, cart.cart_id, cart.hmac)
assert_true(len(new_cart) > len(cart))
def test_cart_modify(self):
cart = self.build_cart_object()
cart_item_id = None
for item in cart:
cart_item_id = item.cart_item_id
#.........这里部分代码省略.........