Merge pull request #1918 from zackw/response-is-redirect
Before adoption: this commit came in with the history the project adopted, so no landing records it.
New Response property, .is_redirect.
authored by Kenneth Reitz · committed by Kenneth Reitz · parents 4ecd746, 0caa243 · Browse files · 5 files·+35 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+35 added−12 removed
- other:+35 added−12 removed
Provenance
- Commit
3c88e52- Tree
cf441cd- Author
Kenneth Reitz <me@kennethreitz.org>- Committer
Kenneth Reitz <me@kennethreitz.org>- Parent 1
4ecd746- Parent 2
0caa243
5 files changed+35 added−12 removed
modified:requests/__init__.py+2 added-2 removed
40 unmodified lines41 42 """43 44 __title__ = 'requests'45-__version__ = '2.2.1'46-__build__ = 0x02020145+__version__ = '2.3.0'46+__build__ = 0x02030047 __author__ = 'Kenneth Reitz'48 __license__ = 'Apache 2.0'49 __copyright__ = 'Copyright 2014 Kenneth Reitz'50 27 unmodified linesmodified:requests/models.py+17 added
28 unmodified lines29 iter_slices, guess_json_utf, super_len, to_native_string)30 from .compat import (31 cookielib, urlunparse, urlsplit, urlencode, str, bytes, StringIO,32 is_py2, chardet, json, builtin_str, basestring, IncompleteRead)33+from .status_codes import codes34 35+#: The set of HTTP status codes that indicate an automatically36+#: processable redirect.37+REDIRECT_STATI = (38+ codes.moved, # 30139+ codes.found, # 30240+ codes.other, # 30341+ codes.temporary_moved, # 30742+)43+DEFAULT_REDIRECT_LIMIT = 3044 CONTENT_CHUNK_SIZE = 10 * 102445 ITER_CHUNK_SIZE = 51246 47 551 unmodified lines599 return False600 return True601 602 @property603+ def is_redirect(self):604+ """True if this Response is a well-formed HTTP redirect that could have605+ been processed automatically (by :meth:`Session.resolve_redirects`).606+ """607+ return ('location' in self.headers and self.status_code in REDIRECT_STATI)608+609+ @property610 def apparent_encoding(self):611 """The apparent encoding, provided by the chardet library"""612 return chardet.detect(self.content)['encoding']613 187 unmodified linesmodified:requests/sessions.py+5 added-10 removed
13 unmodified lines14 15 from .compat import cookielib, OrderedDict, urljoin, urlparse, builtin_str16 from .cookies import (17 cookiejar_from_dict, extract_cookies_to_jar, RequestsCookieJar, merge_cookies)18-from .models import Request, PreparedRequest18+from .models import Request, PreparedRequest, DEFAULT_REDIRECT_LIMIT19 from .hooks import default_hooks, dispatch_hook20 from .utils import to_key_val_list, default_headers, to_native_string21 from .exceptions import TooManyRedirects, InvalidSchema22 from .structures import CaseInsensitiveDict2 unmodified lines25 26 from .utils import requote_uri, get_environ_proxies, get_netrc_auth27 28 from .status_codes import codes29-REDIRECT_STATI = (30- codes.moved, # 30131- codes.found, # 30232- codes.other, # 30333- codes.temporary_moved, # 30734-)35-DEFAULT_REDIRECT_LIMIT = 3029 30+# formerly defined here, reexposed here for backward compatibility31+from .models import REDIRECT_STATI32 33+34 def merge_setting(request_setting, session_setting, dict_class=OrderedDict):35 """36 Determines appropriate setting for a given request, taking into account the37 explicit setting on that request, and the setting in the session. If a46 unmodified lines84 """Receives a Response. Returns a generator of Responses."""85 86 i = 087 92- # ((resp.status_code is codes.see_other))93- while ('location' in resp.headers and resp.status_code in REDIRECT_STATI):88+ while resp.is_redirect:89 prepared_request = req.copy()90 91 resp.content # Consume socket so it can be released92 477 unmodified linesmodified:HISTORY.rst+9 added
1 unmodified line2 3 Release History4 ---------------5 6+2.3.0 (YYYY-MM-DD)7+++++++++++++++++++8+9+**API Changes**10+11+- New ``Response`` property ``is_redirect``, which is true when the12+ library could have processed this response as a redirection (whether13+ or not it actually did).14+15 2.2.1 (2014-01-23)16 ++++++++++++++++++17 18 **Bugfixes**762 unmodified linesmodified:test_requests.py+2 added
113 unmodified lines114 115 def test_HTTP_302_ALLOW_REDIRECT_GET(self):116 r = requests.get(httpbin('redirect', '1'))117 assert r.status_code == 200118+ assert r.history[0].status_code == 302119+ assert r.history[0].is_redirect120 121 # def test_HTTP_302_ALLOW_REDIRECT_POST(self):122 # r = requests.post(httpbin('status', '302'), data={'some': 'data'})123 # self.assertEqual(r.status_code, 200)1052 unmodified lines