-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathecs.py
1074 lines (834 loc) · 37.5 KB
/
ecs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Author: Kun Xi <[email protected]>
# License: Python Software Foundation License
"""
A Python wrapper to access Amazon Web Service(AWS) E-Commerce Serive APIs,
based upon pyamazon (http://www.josephson.org/projects/pyamazon/), enhanced
to meet the latest AWS specification(http://www.amazon.com/webservices).
This module defines the following classes:
- `Bag`, a generic container for the python objects
- `listIterator`, a derived class of list
- `pagedIterator`, a page-based iterator using lazy evaluation
Exception classes:
- `AWSException`
- `NoLicenseKey`
- `NoSecretAccessKey`
- `BadLocale`
- `BadOption`
- `ExactParameterRequirement`
- `ExceededMaximumParameterValues`
- `InsufficientParameterValues`
- `InternalError`
- `InvalidEnumeratedParameter`
- `InvalidISO8601Time`
- `InvalidOperationForMarketplace`
- `InvalidOperationParameter`
- `InvalidParameterCombination`
- `InvalidParameterValue`
- `InvalidResponseGroup`
- `InvalidServiceParameter`
- `InvalidSubscriptionId`
- `InvalidXSLTAddress`
- `MaximumParameterRequirement`
- `MinimumParameterRequirement`
- `MissingOperationParameter`
- `MissingParameterCombination`
- `MissingParameters`
- `MissingParameterValueCombination`
- `MissingServiceParameter`
- `ParameterOutOfRange`
- `ParameterRepeatedInRequest`
- `RestrictedParameterValueCombination`
- `XSLTTransformationError`
Functions:
- `setLocale`
- `getLocale`
- `setLicenseKey`
- `getLicenseKey`
- `getVersion`
- `setOptions`
- `getOptions`
- `buildRequest`
- `buildException`
- `query`
- `rawObject`
- `rawIterator`
- `pagedWrapper`
- `unmarshal`
- `ItemLookup`
- `XMLItemLookup`
- `ItemSearch`
- `XMLItemSearch`
- `SimilarityLookup`
- `XMLSimilarityLookup`
- `ListLookup`
- `XMLListLookup`
- `ListSearch`
- `XMLListSearch`
- `CartCreate`
- `XMLCartCreate`
- `CartAdd`
- `XMLCartAdd`
- `CartGet`
- `XMLCartGet`
- `CartModify`
- `XMLCartModify`
- `CartClear`
- `XMLCartClear`
- `SellerLookup`
- `XMLSellerLookup`
- `SellerListingLookup`
- `XMLSellerListingLookup`
- `SellerListingSearch`
- `XMLSellerListingSearch`
- `CustomerContentSearch`
- `XMLCustomerContentSearch`
- `CustomerContentLookup`
- `XMLCustomerContentLookup`
- `BrowseNodeLookup`
- `XMLBrowseNodeLookup`
- `Help`
- `XMLHelp`
- `TransactionLookup`
- `XMLTransactionLookup`
Accroding to the ECS specification, there are two implementation foo and XMLfoo, for example, `ItemLookup` and `XMLItemLookup`. foo returns a Python object, XMLfoo returns the raw XML file.
How To Use This Module
======================
(See the individual classes, methods, and attributes for details.)
1. Apply for a Amazon Web Service API key from Amazon Web Service:
https://aws-portal.amazon.com/gp/aws/developer/registration/index.html
2. Import it: ``import pyaws.ecs``
3. Setup the license key: ``ecs.setLicenseKey('YOUR-KEY-FROM-AWS')``
or you could use the environment variable AMAZON_LICENSE_KEY
Optional:
a) setup other options, like AssociateTag, MerchantID, Validate
b) export the http_proxy environment variable if you want to use proxy
c) setup the locale if your locale is not ``us``
4. Send query to the AWS, and manupilate the returned python object.
"""
__author__ = "Kun Xi < [email protected] >"
__version__ = "0.2.0"
__license__ = "Python Software Foundation"
__docformat__ = 'restructuredtext'
import os, urllib, string, hmac, hashlib, sys
from datetime import datetime
from xml.dom import minidom
# python 2.4 compat for hashes
try:
from hashlib import sha1 as sha
from hashlib import sha256 as sha256
if sys.version[:3] == "2.4":
# we are using an hmac that expects a .new() method.
class Faker:
def __init__(self, which):
self.which = which
self.digest_size = self.which().digest_size
def new(self, *args, **kwargs):
return self.which(*args, **kwargs)
sha = Faker(sha)
sha256 = Faker(sha256)
except ImportError:
import sha
sha256 = None
# Package-wide variables:
LICENSE_KEY = None
SECRET_ACCESS_KEY = None
LOCALE = "us"
VERSION = "2009-06-01"
OPTIONS = {}
__supportedLocales = {
None : "ecs.amazonaws.com",
"us" : "ecs.amazonaws.com",
"uk" : "ecs.amazonaws.co.uk",
"de" : "ecs.amazonaws.de",
"jp" : "ecs.amazonaws.jp",
"fr" : "ecs.amazonaws.fr",
"ca" : "ecs.amazonaws.ca",
}
__licenseKeys = (
(lambda key: key),
(lambda key: LICENSE_KEY),
(lambda key: os.environ.get('AWS_LICENSE_KEY', None)),
(lambda key: os.environ.get('AWS_ACCESS_KEY_ID', None))
)
__secretAccessKeys = (
(lambda key: key),
(lambda key: SECRET_ACCESS_KEY),
(lambda key: os.environ.get('AWS_SECRET_ACCESS_KEY', None))
)
def __buildPlugins():
"""
Build plugins used in unmarshal
Return the dict like:
Operation => { 'isByPassed'=>(...), 'isPivoted'=>(...),
'isCollective'=>(...), 'isCollected'=>(...),
isPaged=> { key1: (...), key2: (...), ... }
"""
"""
ResponseGroups heirachy:
Parent => children,
The benefit of this layer is to reduce the redundency, when
the child ResponseGroup change, it propaged to the parent
automatically
"""
rgh = {
'CustomerFull': ('CustomerInfo', 'CustomerLists', 'CustomerReviews'),
'Large': ('Accessories', 'BrowseNodes', 'ListmaniaLists', 'Medium', 'Offers', 'Reviews', 'Similarities', 'Tracks'),
'ListFull': ('ListInfo', 'ListItems'),
'ListInfo': ('ListMinimum', ),
'ListItems': ('ListMinimum', ),
'Medium': ('EditorialReview', 'Images', 'ItemAttributes', 'OfferSummary', 'Request', 'SalesRank', 'Small'),
'OfferFull': ('Offers',),
'Offers': ('OfferSummary',),
'Variations': ('VariationMinimum', 'VariationSummary')
}
"""
ResponseGroup and corresponding plugins:
ResponseGroup=>(isBypassed, isPivoted, isCollective, isCollected, isPaged)
isPaged is defined as:
{ kwItems : (kwPage, kwTotalResults, pageSize) }
- kwItems: string, the tagname of collection
- kwPage: string, the tagname of page
- kwTotalResults: string, the tagname of length
- pageSize: constant integer, the size of each page
CODE DEBT:
- Do we need to remove the ResponseGroup in rgh.keys()? At least, Medium does not
introduce any new attributes.
"""
rgps = {
'Accessories': ((), (), ('Accessories',), ('Accessory',), {}),
'AlternateVersions': ((), (), (), (), {}),
'BrowseNodeInfo': ((), (), ('Children', 'Ancestors'), ('BrowseNode',), {}),
'BrowseNodes': ((), (), ('Children', 'Ancestors', 'BrowseNodes'), ('BrowseNode',), {}),
'Cart': ((), (), (), (), {}),
'CartNewReleases': ((), (), (), (), {}),
'CartTopSellers': ((), (), (), (), {}),
'CartSimilarities': ((), (), (), (), {}),
'Collections': ((), (), (), (), {}),
'CustomerFull': ((), (), (), (), {}),
'CustomerInfo': ((), (), ('Customers',), ('Customer',), {}),
'CustomerLists': ((), (), ('Customers',), ('Customer',), {}),
'CustomerReviews': ((), (), ('Customers',),('Customer', 'Review'),
{'CustomerReviews': ('ReviewPage', 'TotalReviews', 10)}),
'EditorialReview': ((), (), ('EditorialReviews',), ('EditorialReview',), {}),
'Help': ((), (), ('RequiredParameters', 'AvailableParameters',
'DefaultResponseGroups', 'AvailableResponseGroups'),
('Parameter', 'ResponseGroup'), {}),
'Images': ((), (), ('ImageSets',), ('ImageSet',), {}),
'ItemAttributes': ((), ('ItemAttributes',), (), (), {}),
'ItemIds': ((), (), (), (), {}),
'ItemLookup.Small': ((), ('ItemAttributes',), (), ('Item',),
{'Items': ('OfferPage', 'TotalResults', 10) }),
'ItemSearch.Small': ((), ('ItemAttributes',), (), ('Item',),
{'Items': ('ItemPage', 'TotalResults', 10) }),
'Large': ((), (), (), (), {}),
'ListFull': ((), (), (), (), {}),
'ListInfo': ((), (), (), (), {}),
'ListItems': ((), (), (), (), {}),
'ListmaniaLists': ((), (), ('ListmaniaLists', ), ('ListmaniaList',), {}),
'ListMinimum': ((), (), (), (), {}),
'Medium': ((), (), (), (), {}),
'MerchantItemAttributes': ((), (), (), (), {}),
'NewReleases': ((), (), ('NewReleases',), ('NewRelease',), {}),
'OfferFull': ((), (), (), (), {}),
'OfferListings': ((), (), (), (), {}),
'Offers': ((), (), (), ('Offer',), {'Offers': ('OfferPage', 'TotalOffers', 10)}),
'OfferSummary': ((), (), (), (), {}),
'Request': (('Request',), (), (), (), {}),
'Reviews': ((), (), (),('Review',),
{'CustomerReviews': ('ReviewPage', 'TotalReviews', 10)}),
'SalesRank': ((), (), (), (), {}),
'SearchBins': ((), (), ('SearchBinSets',), ('SearchBinSet',), {}),
'Seller': ((), (), (), (), {}),
'SellerListing': ((), (), (), (), {}),
'Similarities': ((), (), ('SimilarProducts',), ('SimilarProduct',), {}),
'Small': ((), (), (), (), {}),
'Subjects': ((), (), ('Subjects',), ('Subject',), {}),
'TopSellers': ((), (), ('TopSellers',), ('TopSeller',), {}),
'Tracks': ((), (), (), (), {}),
'TransactionDetails': ((), (), ('Transactions', 'TransactionItems', 'Shipments'),
('Transaction', 'TransactionItem', 'Shipment'), {}),
'Variations': ((), (), (), (), {}),
'VariationMinimum': ((), (), ('Variations',), ('Variation',), {}),
'VariationImages': ((), (), (), (), {}),
'VariationSummary':((), (), (), (), {})
}
"""
Operation=>ResponseGroups
"""
orgs = {
'BrowseNodeLookup': ('Request', 'BrowseNodeInfo', 'NewReleases', 'TopSellers'),
'CartAdd': ('Cart', 'Request', 'CartSimilarities', 'CartTopSellers', 'NewReleases'),
'CartClear': ('Cart', 'Request'),
'CartCreate': ('Cart', 'Request', 'CartSimilarities', 'CartTopSellers', 'CartNewReleases'),
'CartGet': ('Cart', 'Request', 'CartSimilarities', 'CartTopSellers', 'CartNewReleases'),
'CartModify': ('Cart', 'Request', 'CartSimilarities', 'CartTopSellers', 'CartNewReleases'),
'CustomerContentLookup': ('Request', 'CustomerInfo', 'CustomerReviews', 'CustomerLists', 'CustomerFull'),
'CustomerContentSearch': ('Request', 'CustomerInfo'),
'Help': ('Request', 'Help'),
'ItemLookup': ('Request', 'ItemLookup.Small', 'Accessories', 'BrowseNodes', 'EditorialReview', 'Images', 'ItemAttributes', 'ItemIds', 'Large', 'ListmaniaLists', 'Medium', 'MerchantItemAttributes', 'OfferFull', 'Offers', 'OfferSummary', 'Reviews', 'SalesRank', 'Similarities', 'Subjects', 'Tracks', 'VariationImages', 'VariationMinimum', 'Variations', 'VariationSummary'),
'ItemSearch': ('Request', 'ItemSearch.Small', 'Accessories', 'BrowseNodes', 'EditorialReview', 'ItemAttributes', 'ItemIds', 'Large', 'ListmaniaLists', 'Medium', 'MerchantItemAttributes', 'OfferFull', 'Offers', 'OfferSummary', 'Reviews', 'SalesRank', 'SearchBins', 'Similarities', 'Subjects', 'Tracks', 'VariationMinimum', 'Variations', 'VariationSummary'),
'ListLookup': ('Request', 'ListInfo', 'Accessories', 'BrowseNodes', 'EditorialReview', 'Images', 'ItemAttributes', 'ItemIds', 'Large', 'ListFull', 'ListItems', 'ListmaniaLists', 'Medium', 'Offers', 'OfferSummary', 'Reviews', 'SalesRank', 'Similarities', 'Subjects', 'Tracks', 'VariationMinimum', 'Variations', 'VariationSummary'),
'ListSearch': ('Request', 'ListInfo', 'ListMinimum'),
'SellerListingLookup': ('Request', 'SellerListing'),
'SellerListingSearch': ('Request', 'SellerListing'),
'SellerLookup': ('Request', 'Seller'),
'SimilarityLookup': ('Request', 'Small', 'Accessories', 'BrowseNodes', 'EditorialReview', 'Images', 'ItemAttributes', 'ItemIds', 'Large', 'ListmaniaLists', 'Medium', 'Offers', 'OfferSummary', 'Reviews', 'SalesRank', 'Similarities', 'Tracks', 'VariationMinimum', 'Variations', 'VariationSummary'),
'TransactionLookup':('Request', 'TransactionDetails')
}
def collapse(responseGroups):
l = []
for x in responseGroups:
l.append(x)
if x in rgh.keys():
l.extend( collapse(rgh[x]) )
return l
def mergePlugins(responseGroups, index):
#return reduce(lambda x, y: x.update(set(rgps[y][index])), responseGroups, set())
# this magic reduce does not work, using the primary implementation first.
# CODEDEBT: magic number !
if index == 4:
s = dict()
else:
s = set()
map(lambda x: s.update(rgps[x][index]), responseGroups)
return s
def unionPlugins(responseGroups):
return dict( [ (key, mergePlugins(collapse(responseGroups), index)) for index, key in enumerate(['isBypassed', 'isPivoted', 'isCollective', 'isCollected', 'isPaged']) ])
return dict( [ (k, unionPlugins(v)) for k, v in orgs.items() ] )
__plugins = __buildPlugins()
# Wrapper class for ECS
class Bag :
"""A generic container for the python objects"""
def __repr__(self):
return '<Bag instance: ' + self.__dict__.__repr__() + '>'
def rawObject(XMLSearch, arguments, kwItem, plugins=None):
"""Return simple object from `unmarshal`"""
dom = XMLSearch(** arguments)
return unmarshal(XMLSearch, arguments, dom.getElementsByTagName(kwItem).item(0), plugins)
def rawIterator(XMLSearch, arguments, kwItems, plugins=None):
"""Return list of objects from `unmarshal`"""
dom = XMLSearch(** arguments)
return unmarshal(XMLSearch, arguments, dom.getElementsByTagName(kwItems).item(0), plugins, listIterator())
class listIterator(list):
"""List with extended attributes"""
pass
def pagedWrapper(XMLSearch, arguments, keywords, plugins):
"""Wrapper of pagedIterator
Parameters:
- `XMLSearch`: a function, the query to get the DOM
- `arguments`: a dictionary, `XMLSearch`'s arguments
- `keywords`: a tuple, (kwItems, (kwPage, kwTotalResults, pageSize) )
- `plugins`: a dictionary, collection of plugged objects
Note: it is possible to have more than one pagedIterators
in plugins, but only one pagedIterator is returned in
pagedWrapper.
"""
return pagedIterator(XMLSearch, arguments, keywords,
XMLSearch(** arguments).getElementsByTagName(keywords[0]).item(0),
plugins)
class pagedIterator:
"""
A page-based iterator using lazy evaluation.
In some service, such as ItemSearch, AWS returns the result based on
pages, the pagedIterator keeps track of the current page, and send
the request only if necessary.
Bugs:
- list slicing is still missing.
"""
def __init__(self, XMLSearch, arguments, keywords, element, plugins):
"""
Initialize a `pagedIterator` object.
Parameters:
- `XMLSearch`: a function, the query to get the DOM
- `arguments`: a dictionary, `XMLSearch`'s arguments
- `keywords`: a tuple, (kwItems, (kwPage, kwTotalResults, pageSize) )
- `element`: a DOM element, the root of the collection
- `plugins`: a dictionary, collection of plugged objects
"""
kwItems, (kwPage, kwTotalResults, pageSize) = keywords
self.__search = XMLSearch
self.__arguments = arguments
self.__plugins = plugins
self.__keywords ={'Items':kwItems, 'Page':kwPage}
self.__page = arguments[kwPage] or 1
"""Current page"""
self.__index = 0
"""Current index"""
self.__pageSize = pageSize
self.__items = unmarshal(XMLSearch, arguments, element, plugins, listIterator())
"""Cached items"""
try:
self.__len = int(element.getElementsByTagName(kwTotalResults).item(0).firstChild.data)
except AttributeError, e:
self.__len = len(self.__items)
def __len__(self):
return self.__len
def __iter__(self):
return self
def next(self):
if self.__index < self.__len:
self.__index = self.__index + 1
return self.__getitem__(self.__index-1)
else:
raise StopIteration
def __getitem__(self, key):
num = int(key)
if num >= self.__len:
raise IndexError
page = num / self.__pageSize + 1
index = num % self.__pageSize
if page != self.__page:
self.__arguments[self.__keywords['Page']] = page
dom = self.__search(** self.__arguments)
self.__items = unmarshal(self.__search, self.__arguments, dom.getElementsByTagName(self.__keywords['Items']).item(0), self.__plugins, listIterator())
self.__page = page
return self.__items[index]
# Exception classes
class AWSException(Exception) : pass
class NoLicenseKey(AWSException) : pass
class NoSecretAccessKey(AWSException) : pass
class BadLocale(AWSException) : pass
class BadOption(AWSException): pass
# Runtime exception
class ExactParameterRequirement(AWSException): pass
class ExceededMaximumParameterValues(AWSException): pass
class InsufficientParameterValues(AWSException): pass
class InternalError(AWSException): pass
class InvalidEnumeratedParameter(AWSException): pass
class InvalidISO8601Time(AWSException): pass
class InvalidOperationForMarketplace(AWSException): pass
class InvalidOperationParameter(AWSException): pass
class InvalidParameterCombination(AWSException): pass
class InvalidParameterValue(AWSException): pass
class InvalidResponseGroup(AWSException): pass
class InvalidServiceParameter(AWSException): pass
class InvalidSubscriptionId(AWSException): pass
class InvalidXSLTAddress(AWSException): pass
class MaximumParameterRequirement(AWSException): pass
class MinimumParameterRequirement(AWSException): pass
class MissingOperationParameter(AWSException): pass
class MissingParameterCombination(AWSException): pass
class MissingParameters(AWSException): pass
class MissingParameterValueCombination(AWSException): pass
class MissingServiceParameter(AWSException): pass
class ParameterOutOfRange(AWSException): pass
class ParameterRepeatedInRequest(AWSException): pass
class RestrictedParameterValueCombination(AWSException): pass
class XSLTTransformationError(AWSException): pass
# Utilities functions
def setLocale(locale):
"""Set the locale
if unsupported locale is set, BadLocale is raised."""
global LOCALE
if not __supportedLocales.has_key(locale):
raise BadLocale, ("Unsupported locale. Locale must be one of: %s" %
', '.join([x for x in __supportedLocales.keys() if x]))
LOCALE = locale
def getLocale():
"""Get the locale"""
return LOCALE
def setLicenseKey(license_key=None):
"""Set AWS license key.
If license_key is not specified, the license key is set using the
environment variable: AMAZON_LICENSE_KEY; if no license key is
set, NoLicenseKey exception is raised."""
global LICENSE_KEY
for get in __licenseKeys:
rc = get(license_key)
if rc:
LICENSE_KEY = rc;
return;
raise NoLicenseKey, ("Please get the license key from http://www.amazon.com/webservices")
def getLicenseKey():
"""Get license key.
If no license key is specified, NoLicenseKey is raised."""
if not LICENSE_KEY:
setLicenseKey()
return LICENSE_KEY
def setSecretAccessKey(secret_access_key=None):
"""Sets your secret AWS key.
If secret_access_key is not specified, we look for the
environment variable: AMAZON_SECRET_ACCESS_KEY.
Raises NoSecretAccessKey if we can't get it to work."""
global SECRET_ACCESS_KEY
for get in __secretAccessKeys:
rc = get(secret_access_key)
if rc:
SECRET_ACCESS_KEY = rc;
return;
raise NoSecretAccessKey, ("Please get your secret key from http://www.amazon.com/webservices")
def getSecretAccessKey():
"""Get the secret access key.
If no key is specified, NoSecretAccessKey is raised."""
if not SECRET_ACCESS_KEY:
setSecretAccessKey()
return SECRET_ACCESS_KEY
def getVersion():
"""Get the version of ECS specification"""
return VERSION
def setOptions(options):
"""
Set the general optional parameter, available options are:
- AssociateTag
- MerchantID
- Version
- Validate
"""
if set(options.keys()).issubset( set(['AssociateTag', 'MerchantID', 'Validate']) ):
global OPTIONS
OPTIONS.update(options)
else:
raise BadOption, ('Unsupported option')
def getOptions():
"""Get options"""
return OPTIONS
def buildSignature(netloc,query_string):
secret_key = getSecretAccessKey()
string_to_sign = 'GET\n%s\n%s\n%s' % (netloc,'/onca/xml',query_string)
return urllib.quote_plus(hmac.new(secret_key,string_to_sign,sha256).digest().encode('base64').strip())
def buildQuery(argv):
# 1. Filter any key set to 'None'
# 2. Sort the dict by key
# 3. Quote everything and build the query string
query_string = "&".join("%s=%s" % (k, urllib.quote(str(argv[k]))) for (k) in sorted(argv.keys()) if argv[k])
netloc = __supportedLocales[getLocale()]
signature = buildSignature(netloc, query_string)
return 'http://' + netloc + '/onca/xml?' + query_string + '&Signature=' + signature
def buildRequest(argv):
"""Adds some standard keys (like Timestamp and Version) to the request,
then builds and returns the request-url."""
if not argv['AWSAccessKeyId']:
argv['AWSAccessKeyId'] = getLicenseKey()
argv.update(getOptions())
argv.update({'Service':'AWSECommerceService',
'Timestamp':datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ'),
'Version':VERSION})
return buildQuery(argv)
def buildException(els):
"""Build the exception from the returned DOM node
Note: only the first exception is raised."""
error = els[0]
class_name = error.childNodes[0].firstChild.data[4:]
msg = error.childNodes[1].firstChild.data
e = globals()[ class_name ](msg)
return e
def query(url):
"""Send the query url and return the DOM
Exception is raised if there are errors"""
u = urllib.FancyURLopener()
usock = u.open(url)
dom = minidom.parse(usock)
usock.close()
errors = dom.getElementsByTagName('Error')
if errors:
e = buildException(errors)
raise e
return dom
def unmarshal(XMLSearch, arguments, element, plugins=None, rc=None):
"""Return the `Bag` / `listIterator` object with attributes
populated using DOM element.
Parameters:
- `XMLSearch`: callback function, used when construct pagedIterator
- `arguments`: arguments of `XMLSearch`
- `element`: DOM object, the DOM element interested in
- `plugins`: a dictionary, collection of plugged objects to fine-tune
the object attributes
- `rc`: Bag object, parent object
This core function is inspired by Mark Pilgrim ([email protected])
with some enhancement. Each node.tagName is evalued by plugins' callback
functions:
- if tagname in plugins['isBypassed']
this elment is ignored
- if tagname in plugins['isPivoted']
this children of this elment is moved to grandparents
this object is ignored.
- if tagname in plugins['isCollective']
this elment is mapped to []
- if tagname in plugins['isCollected']
this children of elment is appended to grandparent
this object is ignored.
- if tagname in plugins['isPaged'].keys():
this pagedIterator is constructed for the object
CODE DEBT:
- Use optimal search for optimization if necessary
"""
if(rc == None):
rc = Bag()
childElements = [e for e in element.childNodes if isinstance(e, minidom.Element)]
if childElements:
for child in childElements:
key = child.tagName
if hasattr(rc, key):
attr = getattr(rc, key)
if type(attr) <> type([]):
setattr(rc, key, [attr])
setattr(rc, key, getattr(rc, key) + [unmarshal(XMLSearch, arguments, child, plugins)])
elif isinstance(child, minidom.Element):
if child.tagName in plugins['isCollected']:
rc.append(unmarshal(XMLSearch, arguments, child, plugins))
elif child.tagName in plugins['isCollective']:
setattr(rc, key, unmarshal(XMLSearch, arguments, child, plugins, listIterator([])))
elif child.tagName in plugins['isPaged'].keys():
setattr(rc, key, pagedIterator(XMLSearch, arguments, (child.tagName, plugins['isPaged'][child.tagName]), child, plugins))
elif child.tagName in plugins['isPivoted']:
unmarshal(XMLSearch, arguments, child, plugins, rc)
elif child.tagName in plugins['isBypassed']:
continue
else:
setattr(rc, key, unmarshal(XMLSearch, arguments, child, plugins))
else:
rc = "".join([e.data for e in element.childNodes if isinstance(e, minidom.Text)])
return rc
# User interfaces
def ItemLookup(ItemId, IdType=None, SearchIndex=None, MerchantId=None, Condition=None, DeliveryMethod=None, ISPUPostalCode=None, OfferPage=None, ReviewPage=None, ReviewSort=None, VariationPage=None, ResponseGroup=None, AWSAccessKeyId=None):
'''ItemLookup in ECS'''
return pagedWrapper(XMLItemLookup, vars(),
('Items', __plugins['ItemLookup']['isPaged']['Items']), __plugins['ItemLookup'])
def XMLItemLookup(ItemId, IdType=None, SearchIndex=None, MerchantId=None, Condition=None, DeliveryMethod=None, ISPUPostalCode=None, OfferPage=None, ReviewPage=None, ReviewSort=None, VariationPage=None, ResponseGroup=None, AWSAccessKeyId=None):
'''DOM representation of ItemLookup in ECS'''
Operation = "ItemLookup"
return query(buildRequest(vars()))
def ItemSearch(Keywords, SearchIndex="Blended", Availability=None, Title=None, Power=None, BrowseNode=None, Artist=None, Author=None, Actor=None, Director=None, AudienceRating=None, Manufacturer=None, MusicLabel=None, Composer=None, Publisher=None, Brand=None, Conductor=None, Orchestra=None, TextStream=None, ItemPage=None, OfferPage=None, ReviewPage=None, Sort=None, City=None, Cuisine=None, Neighborhood=None, MinimumPrice=None, MaximumPrice=None, MerchantId=None, Condition=None, DeliveryMethod=None, ResponseGroup=None, AWSAccessKeyId=None):
'''ItemSearch in ECS'''
return pagedWrapper(XMLItemSearch, vars(),
('Items', __plugins['ItemSearch']['isPaged']['Items']), __plugins['ItemSearch'])
def XMLItemSearch(Keywords, SearchIndex="Blended", Availability=None, Title=None, Power=None, BrowseNode=None, Artist=None, Author=None, Actor=None, Director=None, AudienceRating=None, Manufacturer=None, MusicLabel=None, Composer=None, Publisher=None, Brand=None, Conductor=None, Orchestra=None, TextStream=None, ItemPage=None, OfferPage=None, ReviewPage=None, Sort=None, City=None, Cuisine=None, Neighborhood=None, MinimumPrice=None, MaximumPrice=None, MerchantId=None, Condition=None, DeliveryMethod=None, ResponseGroup=None, AWSAccessKeyId=None):
'''DOM representation of ItemSearch in ECS'''
Operation = "ItemSearch"
return query(buildRequest(vars()))
def SimilarityLookup(ItemId, SimilarityType=None, MerchantId=None, Condition=None, DeliveryMethod=None, ResponseGroup=None, AWSAccessKeyId=None):
'''SimilarityLookup in ECS'''
argv = vars()
plugins = {
'isBypassed': (),
'isPivoted': ('ItemAttributes',),
'isCollective': ('Items',),
'isCollected': ('Item',),
'isPaged': {}
}
return rawIterator(XMLSimilarityLookup, argv, 'Items', plugins)
def XMLSimilarityLookup(ItemId, SimilarityType=None, MerchantId=None, Condition=None, DeliveryMethod=None, ResponseGroup=None, AWSAccessKeyId=None):
'''DOM representation of SimilarityLookup in ECS'''
Operation = "SimilarityLookup"
return query(buildRequest(vars()))
# List Operations
def ListLookup(ListType, ListId, ProductPage=None, ProductGroup=None, Sort=None, MerchantId=None, Condition=None, DeliveryMethod=None, ResponseGroup=None, AWSAccessKeyId=None):
'''ListLookup in ECS'''
argv = vars()
plugins = {
'isBypassed': (),
'isPivoted': ('ItemAttributes',),
'isCollective': ('Lists',),
'isCollected': ('List',),
'isPaged' : { 'Lists': ('ProductPage', 'TotalResults', 10) }
}
return pagedWrapper(XMLListLookup, argv,
('Lists', plugins['isPaged']['Lists']), plugins)
def XMLListLookup(ListType, ListId, ProductPage=None, ProductGroup=None, Sort=None, MerchantId=None, Condition=None, DeliveryMethod=None, ResponseGroup=None, AWSAccessKeyId=None):
'''DOM representation of ListLookup in ECS'''
Operation = "ListLookup"
return query(buildRequest(vars()))
def ListSearch(ListType, Name=None, FirstName=None, LastName=None, Email=None, City=None, State=None, ListPage=None, ResponseGroup=None, AWSAccessKeyId=None):
'''ListSearch in ECS'''
argv = vars()
plugins = {
'isBypassed': (),
'isPivoted': ('ItemAttributes',),
'isCollective': ('Lists',),
'isCollected': ('List',),
'isPaged' : { 'Lists': ('ListPage', 'TotalResults', 10) }
}
return pagedWrapper(XMLListSearch, argv,
('Lists', plugins['isPaged']['Lists']), plugins)
def XMLListSearch(ListType, Name=None, FirstName=None, LastName=None, Email=None, City=None, State=None, ListPage=None, ResponseGroup=None, AWSAccessKeyId=None):
'''DOM representation of ListSearch in ECS'''
Operation = "ListSearch"
return query(buildRequest(vars()))
#Remote Shopping Cart Operations
def CartCreate(Items, Quantities, ResponseGroup=None, AWSAccessKeyId=None):
'''CartCreate in ECS'''
return __cartOperation(XMLCartCreate, vars())
def XMLCartCreate(Items, Quantities, ResponseGroup=None, AWSAccessKeyId=None):
'''DOM representation of CartCreate in ECS'''
Operation = "CartCreate"
argv = vars()
for x in ('Items', 'Quantities'):
del argv[x]
__fromListToItems(argv, Items, 'ASIN', Quantities)
return query(buildRequest(argv))
def CartAdd(Cart, Items, Quantities, ResponseGroup=None, AWSAccessKeyId=None):
'''CartAdd in ECS'''
return __cartOperation(XMLCartAdd, vars())
def XMLCartAdd(Cart, Items, Quantities, ResponseGroup=None, AWSAccessKeyId=None):
'''DOM representation of CartAdd in ECS'''
Operation = "CartAdd"
CartId = Cart.CartId
HMAC = Cart.HMAC
argv = vars()
for x in ('Items', 'Cart', 'Quantities'):
del argv[x]
__fromListToItems(argv, Items, 'ASIN', Quantities)
return query(buildRequest(argv))
def CartGet(Cart, ResponseGroup=None, AWSAccessKeyId=None):
'''CartGet in ECS'''
return __cartOperation(XMLCartGet, vars())
def XMLCartGet(Cart, ResponseGroup=None, AWSAccessKeyId=None):
'''DOM representation of CartGet in ECS'''
Operation = "CartGet"
CartId = Cart.CartId
HMAC = Cart.HMAC
argv = vars()
del argv['Cart']
return query(buildRequest(argv))
def CartModify(Cart, Items, Actions, ResponseGroup=None, AWSAccessKeyId=None):
'''CartModify in ECS'''
return __cartOperation(XMLCartModify, vars())
def XMLCartModify(Cart, Items, Actions, ResponseGroup=None, AWSAccessKeyId=None):
'''DOM representation of CartModify in ECS'''
Operation = "CartModify"
CartId = Cart.CartId
HMAC = Cart.HMAC
argv = vars()
for x in ('Cart', 'Items', 'Actions'):
del argv[x]
__fromListToItems(argv, Items, 'CartItemId', Actions)
return query(buildRequest(argv))
def CartClear(Cart, ResponseGroup=None, AWSAccessKeyId=None):
'''CartClear in ECS'''
return __cartOperation(XMLCartClear, vars())
def XMLCartClear(Cart, ResponseGroup=None, AWSAccessKeyId=None):
'''DOM representation of CartClear in ECS'''
Operation = "CartClear"
CartId = Cart.CartId
HMAC = Cart.HMAC
argv = vars()
del argv['Cart']
return query(buildRequest(argv))
def __fromListToItems(argv, items, id, actions):
'''Convert list to AWS REST arguments'''
for i in range(len(items)):
argv["Item.%d.%s" % (i+1, id)] = getattr(items[i], id);
action = actions[i]
if isinstance(action, int):
argv["Item.%d.Quantity" % (i+1)] = action
else:
argv["Item.%d.Action" % (i+1)] = action
def __cartOperation(XMLSearch, arguments):
'''Generic cart operation'''
plugins = {
'isBypassed': ('Request',),
'isPivoted': (),
'isCollective': ('CartItems', 'SavedForLaterItems'),
'isCollected': ('CartItem', 'SavedForLaterItem'),
'isPaged': {}
}
return rawObject(XMLSearch, arguments, 'Cart', plugins)
# Seller Operation
def SellerLookup(Sellers, FeedbackPage=None, ResponseGroup=None, AWSAccessKeyId=None):
'''SellerLookup in AWS'''
argv = vars()
plugins = {
'isBypassed': ('Request',),
'isPivoted': (),
'isCollective': ('Sellers',),
'isCollected': ('Seller',),
'isPaged': {}
}
return rawIterator(XMLSellerLookup, argv, 'Sellers', plugins)
def XMLSellerLookup(Sellers, FeedbackPage=None, ResponseGroup=None, AWSAccessKeyId=None):
'''DOM representation of SellerLookup in AWS'''
Operation = "SellerLookup"
SellerId = ",".join(Sellers)
argv = vars()
del argv['Sellers']
return query(buildRequest(argv))
def SellerListingLookup(SellerId, Id, IdType="Listing", ResponseGroup=None, AWSAccessKeyId=None):
'''SellerListingLookup in AWS
Notice: although the repsonse includes TotalPage, TotalResults,
there is no ListingPage in the request, so we have to use rawIterator
instead of pagedIterator. Hope Amazaon would fix this inconsistance'''
argv = vars()
plugins = {
'isBypassed': ('Request',),
'isPivoted': (),
'isCollective': ('SellerListings',),
'isCollected': ('SellerListing',),
'isPaged': {}
}
return rawIterator(XMLSellerListingLookup, argv, "SellerListings", plugins)
def XMLSellerListingLookup(SellerId, Id, IdType="Listing", ResponseGroup=None, AWSAccessKeyId=None):
'''DOM representation of SellerListingLookup in AWS'''
Operation = "SellerListingLookup"
return query(buildRequest(vars()))
def SellerListingSearch(SellerId, Title=None, Sort=None, ListingPage=None, OfferStatus=None, ResponseGroup=None, AWSAccessKeyId=None):
'''SellerListingSearch in AWS'''
argv = vars()
plugins = {
'isBypassed': ('Request',),
'isPivoted': (),
'isCollective': ('SellerListings',),
'isCollected': ('SellerListing',),
'isPaged' : { 'SellerListings': ('ListingPage', 'TotalResults', 10) }
}
return pagedWrapper(XMLSellerListingSearch, argv,
('SellerListings', plugins['isPaged']['SellerListings']), plugins)
def XMLSellerListingSearch(SellerId, Title=None, Sort=None, ListingPage=None, OfferStatus=None, ResponseGroup=None, AWSAccessKeyId=None):
'''DOM representation of SellerListingSearch in AWS'''
Operation = "SellerListingSearch"
return query(buildRequest(vars()))
def CustomerContentSearch(Name=None, Email=None, CustomerPage=1, ResponseGroup=None, AWSAccessKeyId=None):
'''CustomerContentSearch in AWS'''
return rawIterator(XMLCustomerContentSearch, vars(), 'Customers', __plugins['CustomerContentSearch'])
def XMLCustomerContentSearch(Name=None, Email=None, CustomerPage=1, ResponseGroup=None, AWSAccessKeyId=None):
'''DOM representation of CustomerContentSearch in AWS'''
Operation = "CustomerContentSearch"
argv = vars()