Commits

ben...@localhost.nymphormation.net  committed c7b656c

per request from gawel (twitter), add possibility to set default headers
on init.

  • Participants
  • Parent commits 8e1bcfb

Comments (0)

Files changed (4)

File restclient/rest.py

     `restclient.http.HTTPClient`.
 
     """
-    def __init__(self, uri, transport=None):
+    def __init__(self, uri, transport=None, headers=None):
         """Constructor for a `Resource` object.
 
         Resource represent an HTTP resource.
                 `Httplib2 <http://code.google.com/p/httplib2/>`_ or make your
                 own depending of the option you need to access to the serve
                 (authentification, proxy, ....).
+        :param headers: dict, optionnal headers that will
+            be added to HTTP request.
         """
 
-        self.client = RestClient(transport)
+        self.client = RestClient(transport, headers=headers)
         self.uri = uri
-        self.transport = self.client.transport
+        self.transport = self.client.transport 
+        self._headers = headers
 
     def __repr__(self):
         return '<%s %s>' % (self.__class__.__name__, self.uri)
             be added to HTTP request.
         :param params: Optionnal parameterss added to the request
         """
-
+        _headers = self._headers or {}
+        _headers.update(headers or {})
         return self.client.request(method, self.uri, path=path,
-                body=payload, headers=headers, **params)
+                body=payload, headers=_headers, **params)
 
     def get_response(self):
         return self.client.get_response()
     encode_keys = True
     safe = "/:"
 
-    def __init__(self, transport=None):
+    def __init__(self, transport=None, headers=None):
         """Constructor for a `RestClient` object.
 
         RestClient represent an HTTP client.
                 `Httplib2 <http://code.google.com/p/httplib2/>`_ or make your
                 own depending of the option you need to access to the serve
                 (authentification, proxy, ....).
+        :param headers: dict, optionnal headers that will
+            be added to HTTP request.
         """ 
 
         if transport is None:
 
         self.status = None
         self.response = None
+        self._headers = headers
+
 
     def get(self, uri, path=None, headers=None, **params):
         """ HTTP GET         
         
         :return: str.
         """
-        headers = headers or {}
+
+        # init headers
+        _headers = self._headers or {}
+        _headers.update(headers or {})
+        
         is_unicode = True
         if hasattr(body, 'read'):
             if not 'Content-Length' in headers:
 
         try:
             resp, data = self.transport.request(self.make_uri(uri, path, **params), 
-                method=method, body=body, headers=headers)
+                method=method, body=body, headers=_headers)
         except TransportError, e:
             raise RequestError(e)
 

File restclient/transport.py

     def add_credentials(self, user, password):
         super(HTTPLib2Transport, self).add_credentials(user, password)
         self.http.add_credentials(user, password)
-

File tests/resource_test.py

 
     def setUp(self):
         run_server_test()
-        transport = HTTPLib2Transport()
+        self.transport = transport = HTTPLib2Transport()
         self.url = 'http://%s:%s' % (HOST, PORT)
         self.res = Resource(self.url, transport)
 
             result = self.res.get('/json', headers={'Content-Type': 'text/plain'})
         self.assertRaises(RequestFailed, bad_get) 
 
+    def testGetWithContentType2(self):
+        res = Resource(self.url, self.transport, 
+                headers={'Content-Type': 'application/json'})
+        result = res.get('/json')
+        self.assert_(res.response.status == 200)
+        
+
     def testNotFound(self):
         def bad_get():
             result = self.res.get("/unknown")

File tests/transports_test.py

             result = self.res.get('/json', headers={'Content-Type': 'text/plain'})
         self.assertRaises(RequestFailed, bad_get) 
 
+    def testGetWithContentType2(self):
+        res = Resource(self.url, self.httptransport, 
+                headers={'Content-Type': 'application/json'})
+        result = res.get('/json')
+        self.assert_(res.response.status == 200)
+
     def testNotFound(self):
         def bad_get():
             result = self.res.get("/unknown")