Added the possibility to access headers without fetching the whole body (fixes #86).
authorJérémy Bethmont <jeremy.bethmont@gmail.com>
Wed, 29 Jun 2011 09:10:39 +0000 (11:10 +0200)
committerJérémy Bethmont <jeremy.bethmont@gmail.com>
Wed, 29 Jun 2011 09:10:39 +0000 (11:10 +0200)
requests/models.py

index 9b8f614f63fd221a1856af7bcf7f6b3cc2e01a19..a67691dd714d145d0e8b25c1a918623f5b8dbab9 100644 (file)
@@ -151,18 +151,11 @@ class Request(object):
 
             try:
                 response.headers = CaseInsensitiveDict(getattr(resp.info(), 'dict', None))
-                response.content = resp.read()
+                response.read = resp.read
+                response.close = resp.close
             except AttributeError:
                 pass
 
-            if response.headers['content-encoding'] == 'gzip':
-                try:
-                    response.content = zlib.decompress(response.content, 16+zlib.MAX_WBITS)
-                except zlib.error:
-                    pass
-
-            # TODO: Support deflate
-
             response.url = getattr(resp, 'url', None)
 
             return response
@@ -324,10 +317,6 @@ class Request(object):
         return self.sent
 
 
-    def read(self, *args):
-        return self.response.read()
-
-
 
 class Response(object):
     """The core :class:`Response <models.Response>` object. All
@@ -340,7 +329,7 @@ class Response(object):
         #: Raw content of the response, in bytes.
         #: If ``content-encoding`` of response was set to ``gzip``, the
         #: response data will be automatically deflated.
-        self.content = None
+        self._content = None
         #: Integer Code of responded HTTP Status.
         self.status_code = None
         #: Case-insensitive Dictionary of Response Headers.
@@ -370,17 +359,25 @@ class Response(object):
         return not self.error
 
 
+    def __getattr__(self, name):
+        """Read and returns the full stream when accessing to :attr: `content`"""
+        if name == 'content':
+            if self._content is not None:
+                return self._content
+            self._content = self.read()
+            if self.headers.get('content-encoding', '') == 'gzip':
+                try:
+                    self._content = zlib.decompress(self._content, 16+zlib.MAX_WBITS)
+                except zlib.error:
+                    pass
+            return self._content
+
+
     def raise_for_status(self):
         """Raises stored :class:`HTTPError` or :class:`URLError`, if one occured."""
         if self.error:
             raise self.error
 
-    def read(self, *args):
-        """Returns :attr:`content`. Used for file-like object compatiblity."""
-
-        return self.content
-
-
 
 class AuthManager(object):
     """Requests Authentication Manager."""