Browse Source

[desktop] Add content-type support to REST library

bc Wong 13 years ago
parent
commit
171dacf0e9

+ 12 - 3
desktop/core/src/desktop/lib/rest/http_client.py

@@ -119,13 +119,20 @@ class HttpClient(object):
   def logger(self):
   def logger(self):
     return self._logger
     return self._logger
 
 
-  def execute(self, http_method, path, params=None, data=None):
+  def _get_headers(self, headers):
+    res = self._headers.copy()
+    if headers:
+      res.update(headers)
+    return res
+
+  def execute(self, http_method, path, params=None, data=None, headers=None):
     """
     """
     Submit an HTTP request.
     Submit an HTTP request.
     @param http_method: GET, POST, PUT, DELETE
     @param http_method: GET, POST, PUT, DELETE
     @param path: The path of the resource.
     @param path: The path of the resource.
     @param params: Key-value parameter data.
     @param params: Key-value parameter data.
     @param data: The data to attach to the body of the request.
     @param data: The data to attach to the body of the request.
+    @param headers: The headers to set for this request.
 
 
     @return: The result of urllib2.urlopen()
     @return: The result of urllib2.urlopen()
     """
     """
@@ -141,7 +148,9 @@ class HttpClient(object):
     request = urllib2.Request(url, data)
     request = urllib2.Request(url, data)
     # Hack/workaround because urllib2 only does GET and POST
     # Hack/workaround because urllib2 only does GET and POST
     request.get_method = lambda: http_method
     request.get_method = lambda: http_method
-    for k, v in self._headers.items():
+
+    headers = self._get_headers(headers)
+    for k, v in headers.items():
       request.add_header(k, v)
       request.add_header(k, v)
 
 
     # Call it
     # Call it
@@ -154,7 +163,7 @@ class HttpClient(object):
   def _make_url(self, path, params):
   def _make_url(self, path, params):
     res = self._base_url
     res = self._base_url
     if path:
     if path:
-      res += posixpath.normpath('/' + path)
+      res += posixpath.normpath('/' + path.lstrip('/'))
     if params:
     if params:
       param_str = urllib.urlencode(params)
       param_str = urllib.urlencode(params)
       res += '?' + param_str
       res += '?' + param_str

+ 20 - 7
desktop/core/src/desktop/lib/rest/resource.py

@@ -45,14 +45,17 @@ class Resource(object):
       return self._path
       return self._path
     return self._path + posixpath.normpath('/' + relpath)
     return self._path + posixpath.normpath('/' + relpath)
 
 
-  def invoke(self, method, relpath=None, params=None, data=None):
+  def invoke(self, method, relpath=None, params=None, data=None, headers=None):
     """
     """
     Invoke an API method.
     Invoke an API method.
     @return: Raw body or JSON dictionary (if response content type is JSON).
     @return: Raw body or JSON dictionary (if response content type is JSON).
     """
     """
     path = self._join_uri(relpath)
     path = self._join_uri(relpath)
-    resp = self._client.execute(method, path, params=params, data=data)
-
+    resp = self._client.execute(method,
+                                path,
+                                params=params,
+                                data=data,
+                                headers=headers)
     try:
     try:
       body = resp.read()
       body = resp.read()
     except Exception, ex:
     except Exception, ex:
@@ -99,25 +102,35 @@ class Resource(object):
     return self.invoke("DELETE", relpath, params)
     return self.invoke("DELETE", relpath, params)
 
 
 
 
-  def post(self, relpath=None, params=None, data=None):
+  def post(self, relpath=None, params=None, data=None, contenttype=None):
     """
     """
     Invoke the POST method on a resource.
     Invoke the POST method on a resource.
     @param relpath: Optional. A relative path to this resource's path.
     @param relpath: Optional. A relative path to this resource's path.
     @param params: Key-value data.
     @param params: Key-value data.
     @param data: Optional. Body of the request.
     @param data: Optional. Body of the request.
+    @param contenttype: Optional. 
 
 
     @return: A dictionary of the JSON result.
     @return: A dictionary of the JSON result.
     """
     """
-    return self.invoke("POST", relpath, params, data)
+    return self.invoke("POST", relpath, params, data,
+                       self._make_headers(contenttype))
 
 
 
 
-  def put(self, relpath=None, params=None, data=None):
+  def put(self, relpath=None, params=None, data=None, contenttype=None):
     """
     """
     Invoke the PUT method on a resource.
     Invoke the PUT method on a resource.
     @param relpath: Optional. A relative path to this resource's path.
     @param relpath: Optional. A relative path to this resource's path.
     @param params: Key-value data.
     @param params: Key-value data.
     @param data: Optional. Body of the request.
     @param data: Optional. Body of the request.
+    @param contenttype: Optional. 
 
 
     @return: A dictionary of the JSON result.
     @return: A dictionary of the JSON result.
     """
     """
-    return self.invoke("PUT", relpath, params, data)
+    return self.invoke("PUT", relpath, params, data,
+                       self._make_headers(contenttype))
+
+
+  def _make_headers(self, contenttype=None):
+    if contenttype:
+      return { 'Content-Type': contenttype }
+    return None