|
15 | 15 | from Crypto.Hash import SHA256 as sha256
|
16 | 16 |
|
17 | 17 | import hmac
|
18 |
| -import re |
19 | 18 | import socket
|
20 | 19 | from time import strftime, gmtime, sleep
|
21 | 20 | import urllib2
|
@@ -416,6 +415,271 @@ def browse_node_lookup(self, browse_node_id, response_group=None, **params):
|
416 | 415 | # otherwise re-raise exception
|
417 | 416 | raise # pragma: no cover
|
418 | 417 |
|
| 418 | + def _convert_cart_items(self, items, key='ASIN'): |
| 419 | + """ |
| 420 | + Converts items into correct format for cart operations. |
| 421 | + """ |
| 422 | + result = {} |
| 423 | + # TODO ListItemId |
| 424 | + if type(items) == dict: |
| 425 | + for no, (item_id, quantity) in enumerate(items.items()): |
| 426 | + result['Item.%i.%s' % (no+1, key)] = item_id |
| 427 | + result['Item.%i.Quantity' % (no+1)] = quantity |
| 428 | + return result |
| 429 | + |
| 430 | + def cart_create(self, items, **params): |
| 431 | + """ |
| 432 | + The ``CartCreate`` operation enables you to create a remote shopping |
| 433 | + cart. A shopping cart is the metaphor used by most e-commerce |
| 434 | + solutions. It is a temporary data storage structure that resides on |
| 435 | + Amazon servers. The structure contains the items a customer wants to |
| 436 | + buy. In Product Advertising API, the shopping cart is considered remote |
| 437 | + because it is hosted by Amazon servers. In this way, the cart is remote |
| 438 | + to the vendor's web site where the customer views and selects the items |
| 439 | + they want to purchase. |
| 440 | +
|
| 441 | + Once you add an item to a cart by specifying the item's ListItemId and |
| 442 | + ASIN, or OfferListing ID, the item is assigned a ``CartItemId`` and |
| 443 | + accessible only by that value. That is, in subsequent requests, an item |
| 444 | + in a cart cannot be accessed by its ``ListItemId`` and ``ASIN``, or |
| 445 | + ``OfferListingId``. ``CartItemId`` is returned by ``CartCreate``, |
| 446 | + ``CartGet``, and C``artAdd``. |
| 447 | +
|
| 448 | + Because the contents of a cart can change for different reasons, such |
| 449 | + as item availability, you should not keep a copy of a cart locally. |
| 450 | + Instead, use the other cart operations to modify the cart contents. For |
| 451 | + example, to retrieve contents of the cart, which are represented by |
| 452 | + CartItemIds, use ``CartGet``. |
| 453 | +
|
| 454 | + Available products are added as cart items. Unavailable items, for |
| 455 | + example, items out of stock, discontinued, or future releases, are |
| 456 | + added as SaveForLaterItems. No error is generated. The Amazon database |
| 457 | + changes regularly. You may find a product with an offer listing ID but |
| 458 | + by the time the item is added to the cart the product is no longer |
| 459 | + available. The checkout page in the Order Pipeline clearly lists items |
| 460 | + that are available and those that are SaveForLaterItems. |
| 461 | +
|
| 462 | + It is impossible to create an empty shopping cart. You have to add at |
| 463 | + least one item to a shopping cart using a single ``CartCreate`` |
| 464 | + request. You can add specific quantities (up to 999) of each item. |
| 465 | +
|
| 466 | + ``CartCreate`` can be used only once in the life cycle of a cart. To |
| 467 | + modify the contents of the cart, use one of the other cart operations. |
| 468 | +
|
| 469 | + Carts cannot be deleted. They expire automatically after being unused |
| 470 | + for 7 days. The lifespan of a cart restarts, however, every time a cart |
| 471 | + is modified. In this way, a cart can last for more than 7 days. If, for |
| 472 | + example, on day 6, the customer modifies a cart, the 7 day countdown |
| 473 | + starts over. |
| 474 | + """ |
| 475 | + try: |
| 476 | + params.update(self._convert_cart_items(items)) |
| 477 | + return self.call(Operation='CartCreate', **params) |
| 478 | + except AWSError, e: |
| 479 | + |
| 480 | + if e.code == 'AWS.MissingParameters': |
| 481 | + raise ValueError(e.msg) |
| 482 | + |
| 483 | + if e.code == 'AWS.ParameterOutOfRange': |
| 484 | + raise ValueError(e.msg) |
| 485 | + |
| 486 | + if e.code == 'AWS.ECommerceService.ItemNotEligibleForCart': |
| 487 | + raise InvalidCartItem(e.msg) |
| 488 | + |
| 489 | + # otherwise re-raise exception |
| 490 | + raise # pragma: no cover |
| 491 | + |
| 492 | + def cart_add(self, cart_id, hmac, items, **params): |
| 493 | + """ |
| 494 | + The ``CartAdd`` operation enables you to add items to an existing |
| 495 | + remote shopping cart. ``CartAdd`` can only be used to place a new item |
| 496 | + in a shopping cart. It cannot be used to increase the quantity of an |
| 497 | + item already in the cart. If you would like to increase the quantity of |
| 498 | + an item that is already in the cart, you must use the ``CartModify`` |
| 499 | + operation. |
| 500 | +
|
| 501 | + You add an item to a cart by specifying the item's ``OfferListingId``, |
| 502 | + or ``ASIN`` and ``ListItemId``. Once in a cart, an item can only be |
| 503 | + identified by its ``CartItemId``. That is, an item in a cart cannot be |
| 504 | + accessed by its ASIN or OfferListingId. CartItemId is returned by |
| 505 | + ``CartCreate``, ``CartGet``, and ``CartAdd``. |
| 506 | +
|
| 507 | + To add items to a cart, you must specify the cart using the ``CartId`` |
| 508 | + and ``HMAC`` values, which are returned by the ``CartCreate`` |
| 509 | + operation. |
| 510 | +
|
| 511 | + If the associated CartCreate request specified an AssociateTag, all |
| 512 | + ``CartAdd`` requests must also include a value for Associate Tag |
| 513 | + otherwise the request will fail. |
| 514 | +
|
| 515 | + .. note:: Some manufacturers have a minimum advertised price (MAP) that |
| 516 | + can be displayed on Amazon's retail web site. In these cases, when |
| 517 | + performing a Cart operation, the MAP Is returned instead of the actual |
| 518 | + price. The only way to see the actual price is to add the item to a |
| 519 | + remote shopping cart and follow the PurchaseURL. The actual price will |
| 520 | + be the MAP or lower. |
| 521 | + """ |
| 522 | + try: |
| 523 | + params.update({ |
| 524 | + 'CartId' : cart_id, |
| 525 | + 'HMAC' : hmac, |
| 526 | + }) |
| 527 | + params.update(self._convert_cart_items(items)) |
| 528 | + return self.call(Operation='CartAdd', **params) |
| 529 | + except AWSError, e: |
| 530 | + |
| 531 | + if e.code == 'AWS.ECommerceService.InvalidCartId': |
| 532 | + raise InvalidCartId |
| 533 | + |
| 534 | + if e.code == 'AWS.ECommerceService.CartInfoMismatch': |
| 535 | + raise CartInfoMismatch |
| 536 | + |
| 537 | + if e.code == 'AWS.MissingParameters': |
| 538 | + raise ValueError(e.msg) |
| 539 | + |
| 540 | + if e.code == 'AWS.ParameterOutOfRange': |
| 541 | + raise ValueError(e.msg) |
| 542 | + |
| 543 | + if e.code == 'AWS.ECommerceService.ItemNotEligibleForCart': |
| 544 | + raise InvalidCartItem(e.msg) |
| 545 | + |
| 546 | + if e.code == 'AWS.ECommerceService.ItemAlreadyInCart': |
| 547 | + if self.locale == 'jp': print e.msg |
| 548 | + item = self._reg('already-in-cart').search(e.msg).group('item') |
| 549 | + raise ItemAlreadyInCart(item) |
| 550 | + |
| 551 | + # otherwise re-raise exception |
| 552 | + raise # pragma: no cover |
| 553 | + |
| 554 | + def cart_modify(self, cart_id, hmac, items, **params): |
| 555 | + """ |
| 556 | + The ``CartModify`` operation enables you to change the quantity of |
| 557 | + items that are already in a remote shopping cart and move items from |
| 558 | + the active area of a cart to the SaveForLater area or the reverse. |
| 559 | +
|
| 560 | + To modify the number of items in a cart, you must specify the cart |
| 561 | + using the CartId and HMAC values that are returned in the CartCreate |
| 562 | + operation. A value similar to HMAC, URLEncodedHMAC, is also returned. |
| 563 | + This value is the URL encoded version of the HMAC. This encoding is |
| 564 | + necessary because some characters, such as + and /, cannot be included |
| 565 | + in a URL. Rather than encoding the HMAC yourself, use the |
| 566 | + URLEncodedHMAC value for the HMAC parameter. |
| 567 | +
|
| 568 | + You can use ``CartModify`` to modify the number of items in a remote |
| 569 | + shopping cart by setting the value of the Quantity parameter |
| 570 | + appropriately. You can eliminate an item from a cart by setting the |
| 571 | + value of the Quantity parameter to zero. Or, you can double the number |
| 572 | + of a particular item in the cart by doubling its Quantity . You cannot, |
| 573 | + however, use ``CartModify`` to add new items to a cart. |
| 574 | +
|
| 575 | + If the associated CartCreate request specified an AssociateTag, all |
| 576 | + ``CartModify`` requests must also include a value for Associate Tag |
| 577 | + otherwise the request will fail. |
| 578 | + """ |
| 579 | + # TODO Action=SaveForLater |
| 580 | + try: |
| 581 | + params.update({ |
| 582 | + 'CartId' : cart_id, |
| 583 | + 'HMAC' : hmac, |
| 584 | + }) |
| 585 | + params.update(self._convert_cart_items(items, key='CartItemId')) |
| 586 | + return self.call(Operation='CartModify', **params) |
| 587 | + except AWSError, e: |
| 588 | + |
| 589 | + if e.code == 'AWS.ECommerceService.CartInfoMismatch': |
| 590 | + raise CartInfoMismatch |
| 591 | + |
| 592 | + if e.code == 'AWS.MissingParameters': |
| 593 | + raise ValueError(e.msg) |
| 594 | + |
| 595 | + if e.code == 'AWS.ParameterOutOfRange': |
| 596 | + raise ValueError(e.msg) |
| 597 | + |
| 598 | + if e.code == 'AWS.ECommerceService.ItemNotEligibleForCart': |
| 599 | + raise InvalidCartItem(e.msg) |
| 600 | + |
| 601 | + # otherwise re-raise exception |
| 602 | + raise # pragma: no cover |
| 603 | + |
| 604 | + def cart_get(self, cart_id, hmac, **params): |
| 605 | + """ |
| 606 | + The ``CartGet`` operation enables you to retrieve the IDs, quantities, |
| 607 | + and prices of all of the items, including SavedForLater items in a |
| 608 | + remote shopping cart. |
| 609 | +
|
| 610 | + Because the contents of a cart can change for different reasons, such |
| 611 | + as availability, you should not keep a copy of a cart locally. Instead, |
| 612 | + use ``CartGet`` to retrieve the items in a remote shopping cart. |
| 613 | +
|
| 614 | + To retrieve the items in a cart, you must specify the cart using the |
| 615 | + ``CartId`` and ``HMAC`` values, which are returned in the |
| 616 | + ``CartCreate`` operation. A value similar to HMAC, ``URLEncodedHMAC``, |
| 617 | + is also returned. This value is the URL encoded version of the |
| 618 | + ``HMAC``. This encoding is necessary because some characters, such as |
| 619 | + ``+`` and ``/``, cannot be included in a URL. Rather than encoding the |
| 620 | + ``HMAC`` yourself, use the ``URLEncodedHMAC`` value for the HMAC |
| 621 | + parameter. |
| 622 | +
|
| 623 | + ``CartGet`` does not work after the customer has used the |
| 624 | + ``PurchaseURL`` to either purchase the items or merge them with the |
| 625 | + items in their Amazon cart. |
| 626 | +
|
| 627 | + If the associated ``CartCreate`` request specified an ``AssociateTag``, |
| 628 | + all ``CartGet`` requests must also include a value for ``AssociateTag`` |
| 629 | + otherwise the request will fail. |
| 630 | + """ |
| 631 | + try: |
| 632 | + params.update({ |
| 633 | + 'CartId' : cart_id, |
| 634 | + 'HMAC' : hmac, |
| 635 | + }) |
| 636 | + return self.call(Operation='CartGet', **params) |
| 637 | + except AWSError, e: |
| 638 | + |
| 639 | + if e.code == 'AWS.ECommerceService.CartInfoMismatch': |
| 640 | + raise CartInfoMismatch |
| 641 | + |
| 642 | + # otherwise re-raise exception |
| 643 | + raise # pragma: no cover |
| 644 | + |
| 645 | + def cart_clear(self, cart_id, hmac, **params): |
| 646 | + """ |
| 647 | + The ``CartClear`` operation enables you to remove all of the items in a |
| 648 | + remote shopping cart, including SavedForLater items. To remove only |
| 649 | + some of the items in a cart or to reduce the quantity of one or more |
| 650 | + items, use ``CartModify``. |
| 651 | +
|
| 652 | + To delete all of the items from a remote shopping cart, you must |
| 653 | + specify the cart using the ``CartId`` and ``HMAC`` values, which are |
| 654 | + returned by the ``CartCreate`` operation. A value similar to the |
| 655 | + ``HMAC``, ``URLEncodedHMAC``, is also returned. This value is the URL |
| 656 | + encoded version of the ``HMAC``. This encoding is necessary because |
| 657 | + some characters, such as ``+`` and ``/``, cannot be included in a URL. |
| 658 | + Rather than encoding the ``HMAC`` yourself, use the U``RLEncodedHMAC`` |
| 659 | + value for the HMAC parameter. |
| 660 | +
|
| 661 | + ``CartClear`` does not work after the customer has used the |
| 662 | + ``PurchaseURL`` to either purchase the items or merge them with the |
| 663 | + items in their Amazon cart. |
| 664 | +
|
| 665 | + Carts exist even though they have been emptied. The lifespan of a cart |
| 666 | + is 7 days since the last time it was acted upon. For example, if a cart |
| 667 | + created 6 days ago is modified, the cart lifespan is reset to 7 days. |
| 668 | + """ |
| 669 | + try: |
| 670 | + params.update({ |
| 671 | + 'CartId' : cart_id, |
| 672 | + 'HMAC' : hmac, |
| 673 | + }) |
| 674 | + return self.call(Operation='CartClear', **params) |
| 675 | + except AWSError, e: |
| 676 | + |
| 677 | + if e.code == 'AWS.ECommerceService.CartInfoMismatch': |
| 678 | + raise CartInfoMismatch |
| 679 | + |
| 680 | + # otherwise re-raise exception |
| 681 | + raise # pragma: no cover |
| 682 | + |
419 | 683 | def deprecated_operation(self, *args, **kwargs):
|
420 | 684 | """
|
421 | 685 | Some operations are deprecated and will be answered with HTTP 410. To
|
|
0 commit comments