Skip to content
Myr

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

5 files+35 added−12 removed

modified:requests/__init__.py+2 added-2 removed
40 unmodified lines
4142"""4344__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 lines
modified:requests/models.py+17 added
28 unmodified lines
29 iter_slices, guess_json_utf, super_len, to_native_string)30from .compat import (31 cookielib, urlunparse, urlsplit, urlencode, str, bytes, StringIO,32 is_py2, chardet, json, builtin_str, basestring, IncompleteRead)33from .status_codes import codes3435#: The set of HTTP status codes that indicate an automatically36#: processable redirect.37REDIRECT_STATI = (38 codes.moved, # 30139 codes.found, # 30240 codes.other, # 30341 codes.temporary_moved, # 30742)43DEFAULT_REDIRECT_LIMIT = 3044CONTENT_CHUNK_SIZE = 10 * 102445ITER_CHUNK_SIZE = 5124647
551 unmodified lines
599 return False600 return True601602 @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)608609 @property610 def apparent_encoding(self):611 """The apparent encoding, provided by the chardet library"""612 return chardet.detect(self.content)['encoding']613
187 unmodified lines
modified:requests/sessions.py+5 added-10 removed
13 unmodified lines
1415from .compat import cookielib, OrderedDict, urljoin, urlparse, builtin_str16from .cookies import (17 cookiejar_from_dict, extract_cookies_to_jar, RequestsCookieJar, merge_cookies)18from .models import Request, PreparedRequest18from .models import Request, PreparedRequest, DEFAULT_REDIRECT_LIMIT19from .hooks import default_hooks, dispatch_hook20from .utils import to_key_val_list, default_headers, to_native_string21from .exceptions import TooManyRedirects, InvalidSchema22from .structures import CaseInsensitiveDict
2 unmodified lines
2526from .utils import requote_uri, get_environ_proxies, get_netrc_auth2728from .status_codes import codes29REDIRECT_STATI = (30 codes.moved, # 30131 codes.found, # 30232 codes.other, # 30333 codes.temporary_moved, # 30734)35DEFAULT_REDIRECT_LIMIT = 302930# formerly defined here, reexposed here for backward compatibility31from .models import REDIRECT_STATI323334def 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 a
46 unmodified lines
84 """Receives a Response. Returns a generator of Responses."""8586 i = 08792 # ((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()9091 resp.content # Consume socket so it can be released92
477 unmodified lines
modified:HISTORY.rst+9 added
1 unmodified line
23Release History4---------------562.3.0 (YYYY-MM-DD)7++++++++++++++++++89**API Changes**1011- 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).14152.2.1 (2014-01-23)16++++++++++++++++++1718**Bugfixes**
762 unmodified lines
modified:test_requests.py+2 added
113 unmodified lines
114115 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_redirect120121 # 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