From 891e52d72348d3bb2e5dfc243076992320b65f5d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Je=CC=81re=CC=81my=20Bethmont?= Date: Wed, 29 Jun 2011 11:10:39 +0200 Subject: [PATCH] Added the possibility to access headers without fetching the whole body (fixes #86). --- requests/models.py | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/requests/models.py b/requests/models.py index 9b8f614..a67691d 100644 --- a/requests/models.py +++ b/requests/models.py @@ -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 ` 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.""" -- 2.34.1