Merge pull request #4066 from kennethreitz/next
Before adoption: this commit came in with the history the project adopted, so no landing records it.
API improvements
authored by Kenneth Reitz · committed by GitHub · parents 60041f5, c3367d1 · Browse files · 2 files·+36 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
2 files+36 added−20 removed
- other:+36 added−20 removed
Provenance
- Commit
380d130- Tree
689f8b0- Author
Kenneth Reitz <me@kennethreitz.org>- Committer
GitHub <noreply@github.com>- Parent 1
60041f5- Parent 2
c3367d1
2 files changed+36 added−20 removed
modified:requests/models.py+8 added-2 removed
591 unmodified lines592 super(Response, self).__init__()593 594 self._content = False595 self._content_consumed = False596+ self._next = None597 598 #: Integer Code of responded HTTP Status, e.g. 404 or 200.599 self.status_code = None600 105 unmodified lines706 return ('location' in self.headers and self.status_code in REDIRECT_STATI)707 708 @property709 def is_permanent_redirect(self):709- """True if this Response one of the permanent versions of redirect"""710+ """True if this Response one of the permanent versions of redirect."""711 return ('location' in self.headers and self.status_code in (codes.moved_permanently, codes.permanent_redirect))712 713 @property714+ def next(self):715+ """Returns a PreparedRequest for the next request in a redirect chain, if there is one."""716+ return self._next717+718+ @property719 def apparent_encoding(self):714- """The apparent encoding, provided by the chardet library"""720+ """The apparent encoding, provided by the chardet library."""721 return chardet.detect(self.content)['encoding']722 723 def iter_content(self, chunk_size=1, decode_unicode=False):724 """Iterates over the response data. When stream=True is set on the223 unmodified linesmodified:requests/sessions.py+28 added-18 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_requests=False, **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 yield_requests: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.369 unmodified lines600 601 return self.request('DELETE', url, **kwargs)602 603 def send(self, request, **kwargs):600- """601- Send a given PreparedRequest.604+ """Send a given PreparedRequest.605 606 :rtype: requests.Response607 """608 # Set defaults that the hooks can utilize to ensure they always have61 unmodified lines670 # Get the last request made671 r = history.pop()672 r.history = history673 674+ # If redirects aren't being followed, store the response on the Request for Response.next().675+ if not allow_redirects:676+ try:677+ r._next = self.resolve_redirects(r, request, yield_requests=True, **kwargs).next()678+ except StopIteration:679+ pass680+681 if not stream:682 r.content683 684 return r83 unmodified lines