cleanups, response.fo
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parent 850e1c7 · Browse files · 1 file·+42 added−10 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+42 added−10 removed
- other:+42 added−10 removed
Provenance
- Commit
882ea76- Tree
769155f- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
850e1c7
1 file changed+42 added−10 removed
modified:requests/models.py+42 added-10 removed
9 unmodified lines10 import urllib211 import socket12 import zlib13 14+15 from urllib2 import HTTPError16 from urlparse import urlparse, urlunparse, urljoin17 from datetime import datetime18 1 unmodified line20 from .monkeys import Request as _Request, HTTPBasicAuthHandler, HTTPForcedBasicAuthHandler, HTTPDigestAuthHandler, HTTPRedirectHandler21 from .structures import CaseInsensitiveDict22 from .packages.poster.encode import multipart_encode23 from .packages.poster.streaminghttp import register_openers, get_handlers23-from .utils import dict_from_cookiejar24-from .exceptions import RequestException, AuthenticationError, Timeout, URLRequired, InvalidMethod, TooManyRedirects24+from .utils import dict_from_cookiejar, get_unicode_from_response25 from .status_codes import codes26+from .exceptions import RequestException, AuthenticationError, Timeout, URLRequired, InvalidMethod, TooManyRedirects27 28 29 REDIRECT_STATI = (codes.moved, codes.found, codes.other, codes.temporary_moved)30 101 unmodified lines132 if self.cookiejar is not None:133 _handlers.append(urllib2.HTTPCookieProcessor(self.cookiejar))134 135 if self.auth:135- if not isinstance(self.auth.handler, (urllib2.AbstractBasicAuthHandler, urllib2.AbstractDigestAuthHandler)):136+ if not isinstance(self.auth.handler,137+ (urllib2.AbstractBasicAuthHandler,138+ urllib2.AbstractDigestAuthHandler)):139+140 # TODO: REMOVE THIS COMPLETELY137- auth_manager.add_password(self.auth.realm, self.url, self.auth.username, self.auth.password)141+ auth_manager.add_password(142+ self.auth.realm, self.url,143+ self.auth.username,144+ self.auth.password)145+146 self.auth.handler = self.auth.handler(auth_manager)147 auth_manager.add_auth(self.url, self.auth)148 149 _handlers.append(self.auth.handler)23 unmodified lines173 return opener.open174 175 176 def _build_response(self, resp, is_error=False):169- """Build internal :class:`Response <models.Response>` object from given response."""177+ """Build internal :class:`Response <models.Response>` object178+ from given response.179+ """180 181+182 def build(resp):183 184 response = Response()185 response.status_code = getattr(resp, 'code', None)186 187 try:188 response.headers = CaseInsensitiveDict(getattr(resp.info(), 'dict', None))189 response.read = resp.read179- response.readline = resp.readline180- response._resp = resp190+ response.fo = resp191 response._close = resp.close192 193 if self.cookiejar:194 233 unmodified lines428 429 430 def __nonzero__(self):431 """Returns true if :attr:`status_code` is 'OK'."""432+433 return not self.error434 435 436 def __getattr__(self, name):426- """Read and returns the full stream when accessing to :attr: `content`"""437+ """Read and returns the full stream when accessing to438+ :attr: `content`439+ """440+441 if name == 'content':442 if self._content is not None:443 return self._content444+445+ # Read the contents.446 self._content = self.read()447+448+ # Decode GZip'd content.449 if self.headers.get('content-encoding', '') == 'gzip':450 try:451 self._content = zlib.decompress(self._content, 16+zlib.MAX_WBITS)452 except zlib.error:453 pass454+455+ # Decode unicode content.456+ try:457+ self._content = get_unicode_from_response(self)458+ # Don't trust this stuff.459+ except UserWarning, e:460+ print e461+462+463 return self._content464+465 else:466 raise AttributeError467 468+469+470 def raise_for_status(self):471 """Raises stored :class:`HTTPError` or :class:`URLError`, if one occured."""472 if self.error:473 raise self.error474 475 476 def close(self):447- if self._resp.fp is not None and hasattr(self._resp.fp, '_sock'):448- self._resp.fp._sock.recv = None477+ if self.fo.fp is not None and hasattr(self.fo.fp, '_sock'):478+ self.fo.fp._sock.recv = None479 self._close()480 481+482+483 class AuthManager(object):484 """Requests Authentication Manager."""485 486 def __new__(cls):170 unmodified lines