Added the possibility to access headers without fetching the whole body (fixes #86).
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Jérémy Bethmont · committed by Jérémy Bethmont · parent d2ff0f4 · Browse files · 1 file·+17 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+17 added−20 removed
- other:+17 added−20 removed
Provenance
- Commit
891e52d- Tree
dcf344d- Author
Jérémy Bethmont <jeremy.bethmont@gmail.com>- Committer
Jérémy Bethmont <jeremy.bethmont@gmail.com>- Parent
d2ff0f4
1 file changed+17 added−20 removed
modified:requests/models.py+17 added-20 removed
149 unmodified lines150 response.status_code = getattr(resp, 'code', None)151 152 try:153 response.headers = CaseInsensitiveDict(getattr(resp.info(), 'dict', None))154- response.content = resp.read()154+ response.read = resp.read155+ response.close = resp.close156 except AttributeError:157 pass158 158- if response.headers['content-encoding'] == 'gzip':159- try:160- response.content = zlib.decompress(response.content, 16+zlib.MAX_WBITS)161- except zlib.error:162- pass163-164- # TODO: Support deflate165-159 response.url = getattr(resp, 'url', None)160 161 return response162 153 unmodified lines316 317 return self.sent318 319 327- def read(self, *args):328- return self.response.read()320 330-331-321 class Response(object):322 """The core :class:`Response <models.Response>` object. All323 :class:`Request <models.Request>` objects contain a324 :class:`response <models.Response>` attribute, which is an instance3 unmodified lines328 def __init__(self):329 #: Raw content of the response, in bytes.330 #: If ``content-encoding`` of response was set to ``gzip``, the331 #: response data will be automatically deflated.343- self.content = None332+ self._content = None333 #: Integer Code of responded HTTP Status.334 self.status_code = None335 #: Case-insensitive Dictionary of Response Headers.336 #: For example, ``headers['content-encoding']`` will return the21 unmodified lines358 """Returns true if :attr:`status_code` is 'OK'."""359 return not self.error360 361 362+ def __getattr__(self, name):363+ """Read and returns the full stream when accessing to :attr: `content`"""364+ if name == 'content':365+ if self._content is not None:366+ return self._content367+ self._content = self.read()368+ if self.headers.get('content-encoding', '') == 'gzip':369+ try:370+ self._content = zlib.decompress(self._content, 16+zlib.MAX_WBITS)371+ except zlib.error:372+ pass373+ return self._content374+375+376 def raise_for_status(self):377 """Raises stored :class:`HTTPError` or :class:`URLError`, if one occured."""378 if self.error:379 raise self.error380 378- def read(self, *args):379- """Returns :attr:`content`. Used for file-like object compatiblity."""381 381- return self.content382-383-384-382 class AuthManager(object):383 """Requests Authentication Manager."""384 385 def __new__(cls):165 unmodified lines