deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@apache.org
Subject [1/8] git commit: Python client: simplify making requests
Date Tue, 21 Aug 2012 11:41:45 GMT
Updated Branches:
  refs/heads/master 64e01be9a -> 286eb19da


Python client: simplify making requests

This removes the code duplication in the GET, POST, DELETE and PUT
methods and adds a generic `do_request` method that where the caller can
specify the HTTP method.

We will need that in order to respect the HTTP methods returned by the
Deltacloud API calls instead of hardcoding them.

Signed-off-by: Tomas Sedovic <tomas@sedovic.cz>


Project: http://git-wip-us.apache.org/repos/asf/deltacloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltacloud/commit/8ad1c7ac
Tree: http://git-wip-us.apache.org/repos/asf/deltacloud/tree/8ad1c7ac
Diff: http://git-wip-us.apache.org/repos/asf/deltacloud/diff/8ad1c7ac

Branch: refs/heads/master
Commit: 8ad1c7ac74eb72a1f9afa3cb46190efa301ec410
Parents: 0e44bdb
Author: Tomas Sedovic <tomas@sedovic.cz>
Authored: Wed Aug 8 15:26:26 2012 +0200
Committer: Tomas Sedovic <tomas@sedovic.cz>
Committed: Tue Aug 21 12:06:56 2012 +0200

----------------------------------------------------------------------
 clients/python/deltacloud.py |   42 +++++++++++++-----------------------
 1 files changed, 15 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/8ad1c7ac/clients/python/deltacloud.py
----------------------------------------------------------------------
diff --git a/clients/python/deltacloud.py b/clients/python/deltacloud.py
index d2b58df..55812d0 100644
--- a/clients/python/deltacloud.py
+++ b/clients/python/deltacloud.py
@@ -17,6 +17,7 @@
 
 from httplib2 import Http
 from urllib import urlencode
+from urlparse import urljoin
 import libxml2
 
 
@@ -29,40 +30,27 @@ class SimpleRestClient:
         self.client.follow_all_redirect = True
         self.client.add_credentials(self.user, self.password)
 
-    def GET(self, uri):
-        if uri.startswith('http://'):
-            current_url = ''
-        else:
-            current_url = self.url
-        status, response = self.client.request('{url}{uri}'.format(url=current_url, uri=uri),
'GET', headers={'accept':'application/xml'})
+    def do_request(self, uri, method='GET', params=None):
+        if params:
+            params = urlencode(params)
+        status, response = self.client.request(urljoin(self.url, uri),
+                method=method.upper(),
+                body=params,
+                headers={'accept':'application/xml'})
         response = self.parse_xml(response)
         return status, response
 
-    def POST(self, uri, params={}):
-        if uri.startswith('http://'):
-            current_url = ''
-        else:
-            current_url = self.url
-        if not params:
-            params = {}
-        status, response = self.client.request('{url}{uri}'.format(url=current_url, uri=uri),
'POST',
-                                               urlencode(params), headers={'accept':'application/xml'})
-        response = self.parse_xml(response)
-        return status, response
+    def GET(self, uri):
+        return self.do_request(uri, 'GET')
+
+    def POST(self, uri, params=None):
+        return self.do_request(uri, 'POST', params)
 
     def DELETE(self, uri):
-        if uri.startswith('http://'):
-            current_url = ''
-        else:
-            current_url = self.url
-        return self.client.request('{url}{uri}'.format(url=current_url, uri=uri), 'DELETE')
+        return self.do_request(uri, 'DELETE')
 
     def PUT(self, uri):
-        if uri.startswith('http://'):
-            current_url = ''
-        else:
-            current_url = self.url
-        return self.client.request('{url}{uri}'.format(url=current_url, uri=uri), 'PUT')
+        return self.do_request(uri, 'PUT')
 
     def parse_xml(self, response):
         return libxml2.parseDoc(response)


Mime
View raw message