API improvements
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 14d6fa1 · Browse files · 2 files·+32 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+32 added−16 removed
- other:+32 added−16 removed
Provenance
- Commit
ce64cce- Tree
e66ffb3- Author
Kenneth Reitz <me@kennethreitz.org>- Committer
Kenneth Reitz <me@kennethreitz.org>- Parent
14d6fa1
2 files changed+32 added−16 removed
modified:requests/models.py+8 added
575 unmodified lines576 hooks = hooks or []577 for event in hooks:578 self.register_hook(event, hooks[event])579 580+ def send(self, session):581+ """Sends the given PreparedRequest to the given session."""582+ return session.send(self)583 584+585 class Response(object):586 """The :class:`Response <Response>` object, which contains a587 server's response to an HTTP request.588 """7 unmodified lines596 super(Response, self).__init__()597 598 self._content = False599 self._content_consumed = False600+ self._next = None601 602 #: Integer Code of responded HTTP Status, e.g. 404 or 200.603 self.status_code = None604 108 unmodified lines713 def is_permanent_redirect(self):714 """True if this Response one of the permanent versions of redirect"""715 return ('location' in self.headers and self.status_code in (codes.moved_permanently, codes.permanent_redirect))716 717+ def next(self):718+ if self.is_redirect:719+ return self._next720 @property721 def apparent_encoding(self):722 """The apparent encoding, provided by the chardet library"""723 return chardet.detect(self.content)['encoding']226 unmodified linesmodified:requests/sessions.py+24 added-16 removed
112 unmodified lines113 return to_native_string(location, 'utf8')114 return None115 116 def resolve_redirects(self, resp, req, stream=False, timeout=None,117- verify=True, cert=None, proxies=None, **adapter_kwargs):118- """Receives a Response. Returns a generator of Responses."""117+ verify=True, cert=None, proxies=None, yield_responses=True, **adapter_kwargs):118+ """Receives a Response. Returns a generator of Responses or Requests."""119 120 hist = [] # keep track of history121 122 url = self.get_redirect_target(resp)79 unmodified lines202 203 # Override the original request.204 req = prepared_request205 206- resp = self.send(207- req,208- stream=stream,209- timeout=timeout,210- verify=verify,211- cert=cert,212- proxies=proxies,213- allow_redirects=False,214- **adapter_kwargs215- )206+ if not yield_responses:207+ yield req208+ else:209 217- extract_cookies_to_jar(self.cookies, prepared_request, resp.raw)210+ resp = self.send(211+ req,212+ stream=stream,213+ timeout=timeout,214+ verify=verify,215+ cert=cert,216+ proxies=proxies,217+ allow_redirects=False,218+ **adapter_kwargs219+ )220 219- # extract redirect url, if any, for the next loop220- url = self.get_redirect_target(resp)221- yield resp221+ extract_cookies_to_jar(self.cookies, prepared_request, resp.raw)222 223+ # extract redirect url, if any, for the next loop224+ url = self.get_redirect_target(resp)225+ yield resp226+227 def rebuild_auth(self, prepared_request, response):228 """When being redirected we may want to strip authentication from the229 request to avoid leaking credentials. This method intelligently removes230 and reapplies authentication where possible to avoid credential loss.440 unmodified lines671 # Get the last request made672 r = history.pop()673 r.history = history674 675+ # If redirects aren't being followed, store the response on the Request for Response.next().676+ if not allow_redirects:677+ r._next = self.resolve_redirects(r, request, yield_responses=False, **kwargs).next()678+679 if not stream:680 r.content681 682 return r83 unmodified lines