commit e742badc9c064d98c9c253019ad268848397463d
Author: Stavros Sachtouris <saxtouri@admin.grnet.gr>
Date:   Fri Jul 6 15:29:17 2012 +0300

    Simplified  Client code
    
    Added set_header method then replaced code that pushes headers to the query with calls to this much simpler method

diff --git a/kamaki/cli.py b/kamaki/cli.py
index 66bf368..a04d06c 100755
--- a/kamaki/cli.py
+++ b/kamaki/cli.py
@@ -801,7 +801,6 @@ class store_list(_store_container_command):
             size = format_size(container['bytes'])
             print('%s (%s, %s objects)' % (container['name'], size, container['count']))
             
-
     def main(self, container=None, object=None):
         super(store_list, self).main()
         if container is None:
diff --git a/kamaki/clients/__init__.py b/kamaki/clients/__init__.py
index 2a7dc29..91ee57d 100644
--- a/kamaki/clients/__init__.py
+++ b/kamaki/clients/__init__.py
@@ -5,11 +5,11 @@
 # conditions are met:
 #
 #   1. Redistributions of source code must retain the above
-#      copyright notice, this list of conditions and the following
+#      copyright notice, self.list of conditions and the following
 #      disclaimer.
 #
 #   2. Redistributions in binary form must reproduce the above
-#      copyright notice, this list of conditions and the following
+#      copyright notice, self.list of conditions and the following
 #      disclaimer in the documentation and/or other materials
 #      provided with the distribution.
 #
@@ -61,46 +61,47 @@ class Client(object):
     def __init__(self, base_url, token):
         self.base_url = base_url
         self.token = token
+        self.headers = {}
 
     def raise_for_status(self, r):
         message = "%d %s" % (r.status_code, r.status)
         details = r.text
         raise ClientError(message, r.status_code, details)
 
+    def set_header(self, name, value):
+        #If the header exists, replace the value
+        #otherwise create a new header
+        self.headers[unicode(name)] = unicode(value)
+
     def request(self, method, path, **kwargs):
         raw = kwargs.pop('raw', False)
         success = kwargs.pop('success', 200)
         directory = kwargs.pop('directory', False)
-        meta = kwargs.pop('meta', False)
+        #meta = kwargs.pop('meta', False)
 
         data = kwargs.pop('data', None)
-        headers = kwargs.pop('headers', {})
-        headers.setdefault('X-Auth-Token', self.token)
+        #headers = kwargs.pop('headers', {})
+        self.headers.setdefault('X-Auth-Token', self.token)
         publish = kwargs.pop('publish', None)
 
         if directory:
-            headers.setdefault('Content-Type', 'application/directory')
-            headers.setdefault('Content-length', '0')
+            self.headers.setdefault('Content-Type', 'application/directory')
+            self.headers.setdefault('Content-length', '0')
         else:
             if 'json' in kwargs:
                 data = json.dumps(kwargs.pop('json'))
-                headers.setdefault('Content-Type', 'application/json')
+                self.headers.setdefault('Content-Type', 'application/json')
             if data:
-                headers.setdefault('Content-Length', unicode(len(data)))
+                self.headers.setdefault('Content-Length', unicode(len(data)))
 
-        if meta:
-            for key in meta.keys():
-                headers[key] = meta[key]
+        #if meta:
+        #    for key in meta.keys():
+        #        self.headers[key] = meta[key]
 
-        if publish is not None:
-            headers.setdefault('X-object-public', unicode(publish))
-            
         url = self.base_url + path
         kwargs.setdefault('verify', False)  # Disable certificate verification
-        print('HAVE WE BEEN OVER THIS? '+unicode(headers))
-        r = requests.request(method, url, headers=headers, data=data, **kwargs)
+        r = requests.request(method, url, headers=self.headers, data=data, **kwargs)
 
-        print('HAVE WE BEEN OVER THIS?')
         req = r.request
         sendlog.info('%s %s', req.method, req.url)
         for key, val in req.headers.items():
diff --git a/kamaki/clients/pithos.py b/kamaki/clients/pithos.py
index ffdb7b2..55e8c19 100644
--- a/kamaki/clients/pithos.py
+++ b/kamaki/clients/pithos.py
@@ -51,17 +51,14 @@ class PithosClient(StorageClient):
 
     def purge_container(self, container):
         self.assert_account()
-        path = '/%s/%s' % (self.account, container)
-        params = {'until': int(time())}
-        self.delete(path, params=params, success=204)
+        path = path4url(self.account, container)+params4url({'until': unicode(time())})
+        self.delete(path, success=204)
 
     def put_block(self, data, hash):
-        path = '/%s/%s' % (self.account, self.container)
-        params = {'update': ''}
-        headers = {'Content-Type': 'application/octet-stream',
-                   'Content-Length': str(len(data))}
-        r = self.post(path, params=params, data=data, headers=headers,
-                      success=202)
+        path = path4url(self.account, self.container)+params4url({'update':None})
+        self.set_header('Content-Type', 'application/octet-stream')
+        self.set_header('Content-Length', len(data))
+        r = self.post(path, data=data, success=202)
         assert r.text.strip() == hash, 'Local hash does not match server'
 
     def create_object(self, object, f, size=None, hash_cb=None,
@@ -78,7 +75,6 @@ class PithosClient(StorageClient):
         self.assert_container()
 
         meta = self.get_container_info(self.container)
-        print(unicode(meta))
         blocksize = int(meta['x-container-block-size'])
         blockhash = meta['x-container-block-hash']
 
@@ -105,12 +101,11 @@ class PithosClient(StorageClient):
 
         assert offset == size
 
-        path = '/%s/%s/%s' % (self.account, self.container, object)
-        params = dict(format='json', hashmap='')
+        path = path4url(self.account, self.container, object)+params4url(dict(format='json', hashmap=''))
+        
         hashmap = dict(bytes=size, hashes=hashes)
-        headers = {'Content-Type': 'application/octet-stream'}
-        r = self.put(path, params=params, headers=headers, json=hashmap,
-                     success=(201, 409))
+        self.set_header('Content-Type', 'application/octet-stream')
+        r = self.put(path, json=hashmap, success=(201, 409))
 
         if r.status_code == 201:
             return
@@ -129,7 +124,7 @@ class PithosClient(StorageClient):
             if upload_cb:
                 upload_gen.next()
 
-        self.put(path, params=params, headers=headers, json=hashmap,
+        self.put(path, json=hashmap,
                  success=201)
 
     def get_account_policy(self):
@@ -142,8 +137,9 @@ class PithosClient(StorageClient):
         assert(type(metapairs) is dict)
         self.assert_account()
         path = path4url(self.account)+params4url({'update':None})
-        meta = prefix_keys(metapairs, 'X-Account-Meta-')
-        self.post(path, meta=meta, success=202)
+        for key, val in metapairs.items():
+            self.set_header('X-Account-Meta-'+key, val)
+        self.post(path, success=202)
 
     def get_container_policy(self, container):
         return filter_in(self.get_container_info(container), 'X-Container-Policy-')
@@ -158,39 +154,44 @@ class PithosClient(StorageClient):
         assert(type(metapairs) is dict)
         self.assert_container()
         path=path4url(self.account, self.container)+params4url({'update':None})
-        meta = prefix_keys(metapairs, 'X-Container-Meta-')
-        self.post(path, meta=meta, success=202)
+        for key, val in metapairs.items():
+            self.set_header('X-Container-Meta-'+key, val)
+        self.post(path, success=202)
 
     def delete_container_meta(self, metakey):
         headers = self.get_container_info(self.container)
-        new_headers = filter_out(headers, 'x-container-meta-'+metakey, exactMatch = True)
-        if len(new_headers) == len(headers):
+        self.headers = filter_out(headers, 'x-container-meta-'+metakey, exactMatch = True)
+        if len(self.headers) == len(headers):
             raise ClientError('X-Container-Meta-%s not found' % metakey, 404)
         path = path4url(self.account, self.container)
-        self.post(path, headers=new_headers, success = 202)
+        self.post(path, success = 202)
 
     def replace_container_meta(self, metapairs):
         self.assert_container()
         path=path4url(self.account, self.container)
-        meta = prefix_keys(metapairs, 'X-Container-Meta-')
-        self.post(path, meta=meta, success=202)
+        for key, val in metapairs.items():
+            self.set_header('X-Container-Meta-'+key, val)
+        self.post(path, success=202)
 
     def set_object_meta(self, object, metapairs):
         assert(type(metapairs) is dict)
         self.assert_container()
         path=path4url(self.account, self.container, object)+params4url({'update':None})
-        meta = prefix_keys(metapairs, 'X-Object-Meta-')
-        self.post(path, meta=meta, success=202)
+        for key, val in metapairs.items():
+            self.set_header('X-Object-Meta-'+key, val)
+        self.post(path, success=202)
 
     def publish_object(self, object):
         self.assert_container()
         path = path4url(self.account, self.container, object)+params4url({'update':None})
-        self.post(path, publish=True, success=202)
+        self.set_header('X-Object-Public', True)
+        self.post(path, success=202)
 
     def unpublish_object(self, object):
         self.assert_container()
         path = path4url(self.account, self.container, object)+params4url({'update':None})
-        self.post(path, publish=False, success=202)
+        self.set_header('X-Object-Public', False)
+        self.post(path, success=202)
         
         
         
diff --git a/kamaki/clients/storage.py b/kamaki/clients/storage.py
index 8bb5201..6391e10 100644
--- a/kamaki/clients/storage.py
+++ b/kamaki/clients/storage.py
@@ -62,16 +62,17 @@ class StorageClient(Client):
     def replace_account_meta(self, metapairs):
         self.assert_account()
         path = path4url(self.account)
-        meta = prefix_keys(metapairs, 'X-Account-Meta-')
-        self.post(path, meta=meta, success=202)
+        for key, val in  metapairs:
+            self.set_header('X-Account-Meta-'+key, val)
+        self.post(path, success=202)
 
     def delete_account_meta(self, metakey):
         headers = self.get_account_info()
-        new_headers = filter_out(headers, 'X-Account-Meta-'+metakey, exactMatch = True)
-        if len(new_headers) == len(headers):
+        self.headers = filter_out(headers, 'X-Account-Meta-'+metakey, exactMatch = True)
+        if len(self.headers) == len(headers):
             raise ClientError('X-Account-Meta-%s not found' % metakey, 404)
         path = path4url(self.account)
-        self.post(path, headers=new_headers, success = 202)
+        self.post(path, success = 202)
 
     def create_container(self, container):
         self.assert_account()
@@ -86,8 +87,7 @@ class StorageClient(Client):
         r = self.head(path, success=(204, 404))
         if r.status_code == 404:
             raise ClientError("Container does not exist", r.status_code)
-        reply = r.headers
-        return reply
+        return r.headers
 
     def delete_container(self, container):
         self.assert_account()
@@ -100,9 +100,8 @@ class StorageClient(Client):
 
     def list_containers(self):
         self.assert_account()
-        path = path4url(self.account) 
-        params = dict(format='json')
-        r = self.get(path, params = params, success = (200, 204))
+        path = path4url(self.account)+params4url(dict(format='json'))
+        r = self.get(path, success = (200, 204))
         return r.json
 
     def create_object(self, object, f, size=None, hash_cb=None,
@@ -130,20 +129,18 @@ class StorageClient(Client):
 
     def delete_object_meta(self, metakey, object):
         headers = self.get_object_info(object)
-        new_headers = filter_out(headers, 'X-Object-Meta-'+metakey, exactMatch = True)
-        if len(new_headers) == len(headers):
+        self.headers = filter_out(headers, 'X-Object-Meta-'+metakey, exactMatch = True)
+        if len(self.headers) == len(headers):
             raise ClientError('X-Object-Meta-%s not found' % metakey, 404)
         path = path4url(self.account, self.container, object)
-        print('HAVE WE BEEN OVER THIS?')
-        print(unicode(new_headers))
-        print('HAVE WE BEEN OVER THIS?')
-        self.post(path, headers=new_headers, success = 202)
+        self.post(path, success = 202)
 
-    def replace_object(self, metapairs):
+    def replace_object_meta(self, metapairs):
         self.assert_container()
         path=path4url(self.account, self.container)
-        meta = prefix_keys(metapairs, 'X-Object-Meta-')
-        self.post(path, meta=meta, success=202)
+        for key, val in metapairs:
+            self.set_header('X-Object-Meta-'+key, val)
+        self.post(path, success=202)
 
     def get_object(self, object):
         self.assert_container()
@@ -162,16 +159,17 @@ class StorageClient(Client):
     def list_objects(self):
         self.assert_container()
         path = path4url(self.account, self.container)
-        params = dict(format='json')
+        params = dict(format='json') #request parameters
         r = self.get(path, params=params, success=(200, 204, 404))
         if r.status_code == 404:
             raise ClientError("Incorrect account (%s) for that container"%self.account, r.status_code)
+        print(unicode(r.json))
         return r.json
 
     def list_objects_in_path(self, path_prefix):
         self.assert_container()
         path = path4url(self.account, self.container)
-        params = dict(format='json', path=path_prefix)
+        params = dict(format='json', path=path_prefix) #request parameters
         r = self.get(path, params=params, success=(200, 204, 404))
         if r.status_code == 404:
             raise ClientError("Incorrect account (%s) for that container"%self.account, r.status_code)
diff --git a/kamaki/clients/utils.py b/kamaki/clients/utils.py
index d178c1d..32d8e66 100644
--- a/kamaki/clients/utils.py
+++ b/kamaki/clients/utils.py
@@ -69,10 +69,10 @@ def params4url(params):
     """
     assert(type(params) is dict)
     result = ''
-    delimiter = '?'
+    dlmtr = '?'
     for name in params:
-        result = result + delimiter + name
+        result = result + dlmtr + name
         result = result + '=' + params[name] if params[name] is not None else result
-        delimeter = '&'
+        dlmtr = '&'
     return result
 
