Commits

Anonymous committed 44c45cf

Refactored buildRequest and its unittest

  • Participants
  • Parent commits d168f5a

Comments (0)

Files changed (2)

    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 < kunxi@kunxi.org >"
     
     return urllib.quote_plus(hmac.new(secret_key,string_to_sign,hashlib.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 = urllib.urlencode([(k, argv[k]) for (k) in sorted(argv.keys()) if argv[k]])
+
+    netloc = __supportedLocales[getLocale()]
+    signature = buildSignature(netloc, query_string)
+    url = 'http://' + netloc + '/onca/xml?' + query_string + '&Signature=' + signature
+    return url
+
 def buildRequest(argv):
-    """Build the REST request URL from 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['Service'] = 'AWSECommerceService'
-    if 'Timestamp' not in argv:
-        argv['Timestamp'] = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ') 
-    argv['Version'] = VERSION
+    argv.update({'Service':'AWSECommerceService',
+                 'Timestamp':datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ'),
+                 'Version':VERSION})
 
-    # 1. Filter any key set to 'None'
-    # 2. Sort the dict by key
-    # 3. Quote everything and build the query string 
-    query_string = urllib.urlencode([(k,argv[k]) for (k) in sorted(argv.keys()) if argv[k]])
-
-    netloc = __supportedLocales[getLocale()]
-    
-    signature = buildSignature(netloc, query_string)
-    url = 'http://' + netloc + '/onca/xml?' + query_string + '&Signature=' + signature
-    return url
+    return buildQuery(argv)
 
 def buildException(els):
     """Build the exception from the returned DOM node

File tests/authentication.py

         expected_signature = 'Nace%2BU3Az4OhN7tISqgs1vdLBHBEijWcBeCqL5xN9xg%3D'
         self.assertEqual(ecs.buildSignature(netloc, query),expected_signature)
                 
-    def testBuildRequest(self):
+    def testBuildQuery(self):
         '''example taken from the api-documentation'''
         ecs.setSecretAccessKey('1234567890')
-        ecs.setLicenseKey('00000000000000000000')
-        ecs.VERSION = '2009-01-01'
         
         args = {
+                'Service':'AWSECommerceService',
                 'AWSAccessKeyId':'00000000000000000000',
                 'Timestamp':'2009-01-01T12:00:00Z',
                 'Operation':'CartCreate',
                 'Item.1.OfferListingId':'j8ejq9wxDfSYWf2OCp6XQGDsVrWhl08GSQ9m5j+e8MS449BN1XGUC3DfU5Zw4nt/FBt87cspLow1QXzfvZpvzg==',
                 'Item.1.Quantity':'3',
-                'AssociateTag':'mytag-20'
+                'AssociateTag':'mytag-20',
+                'Version':'2009-01-01'
         }
         expepected_url = 'http://ecs.amazonaws.com/onca/xml?AWSAccessKeyId=00000000000000000000&AssociateTag=mytag-20&Item.1.OfferListingId=j8ejq9wxDfSYWf2OCp6XQGDsVrWhl08GSQ9m5j%2Be8MS449BN1XGUC3DfU5Zw4nt%2FFBt87cspLow1QXzfvZpvzg%3D%3D&Item.1.Quantity=3&Operation=CartCreate&Service=AWSECommerceService&Timestamp=2009-01-01T12%3A00%3A00Z&Version=2009-01-01&Signature=cF3UtjbJb1%2BxDh387C%2FEmS1BCtS%2FZ01taykBCGemvUU%3D'
-        self.assertEqual(ecs.buildRequest(args),expepected_url)
+        self.assertEqual(ecs.buildQuery(args),expepected_url)
 
 if __name__ == "__main__" :
     unittest.main()