Drop `.next()`/`.anext()` in favour of `response.next_request` (#1339)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Drop response.next()/response.anext() in favour of response.next_request * Drop NotRedirectResponse
authored by Tom Christie · committed by GitHub · parent 2a2bbe5 · Browse files · 8 files·+12 added−109 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+12 added−109 removed
- tests:+12 added−49 removed
- other:+0 added−60 removed
Provenance
- Commit
0eed6a3- Tree
4b2394f- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
2a2bbe5
8 files changed+12 added−109 removed
modified:docs/exceptions.md-4 removed
74 unmodified lines75767778797980818269 unmodified lines15215315415515715815915615715815913 unmodified lines74 unmodified lines * DecodingError * TooManyRedirects * HTTPStatusError* InvalidURL* NotRedirectResponse* CookieConflict* StreamError * StreamConsumed * ResponseNotRead69 unmodified lines
::: httpx.InvalidURL :docstring:
::: httpx.NotRedirectResponse :docstring:
::: httpx.CookieConflict :docstring:
::: httpx.StreamError13 unmodified linesmodified:httpx/__init__.py-2 removed
12 unmodified lines13 HTTPStatusError,14 InvalidURL,15 LocalProtocolError,16 NetworkError,17- NotRedirectResponse,17 PoolTimeout,18 ProtocolError,19 ProxyError,20 ReadError,44 unmodified lines65 "InvalidURL",66 "Limits",67 "LocalProtocolError",68 "NetworkError",70- "NotRedirectResponse",69 "options",70 "patch",71 "PoolTimeout",72 "post",35 unmodified linesmodified:httpx/_client.py-15 removed
1 import datetime2 import enum3-import functools3 import typing4 import warnings5 from types import TracebackType6 839 unmodified lines846 history = history + [response]847 848 if not allow_redirects:849 response.next_request = request851- response.call_next = functools.partial(852- self._send_handling_redirects,853- request=request,854- timeout=timeout,855- allow_redirects=False,856- history=history,857- )850 return response851 852 def _send_single_request(self, request: Request, timeout: Timeout) -> Response:853 """631 unmodified lines1485 history = history + [response]1486 1487 if not allow_redirects:1488 response.next_request = request1497- response.call_next = functools.partial(1498- self._send_handling_redirects,1499- request=request,1500- timeout=timeout,1501- allow_redirects=False,1502- history=history,1503- )1489 return response1490 1491 async def _send_single_request(1492 self, request: Request, timeout: Timeout354 unmodified linesmodified:httpx/_exceptions.py-13 removed
22 unmodified lines23 + TooManyRedirects24 + RequestBodyUnavailable25 x HTTPStatusError26 * InvalidURL27-* NotRedirectResponse27 * CookieConflict28 * StreamError29 x StreamConsumed30 x ResponseNotRead200 unmodified lines231 def __init__(self, message: str) -> None:232 super().__init__(message)233 234 236-class NotRedirectResponse(Exception):237- """238- Response was not a redirect response.239-240- May be raised if `response.next()` is called without first241- properly checking `response.is_redirect`.242- """243-244- def __init__(self, message: str) -> None:245- super().__init__(message)246-247-235 class CookieConflict(Exception):236 """237 Attempted to lookup a cookie by name, but multiple cookies existed.238 124 unmodified linesmodified:httpx/_models.py-26 removed
25 unmodified lines26 CookieConflict,27 DecodingError,28 HTTPStatusError,29 InvalidURL,30- NotRedirectResponse,30 RequestNotRead,31 ResponseClosed,32 ResponseNotRead,33 StreamConsumed,1147 unmodified lines1181 self._num_bytes_downloaded += len(part)1182 yield part1183 self.close()1184 1186- def next(self) -> "Response":1187- """1188- Get the next response from a redirect response.1189- """1190- if not self.is_redirect:1191- message = (1192- "Called .next(), but the response was not a redirect. "1193- "Calling code should check `response.is_redirect` first."1194- )1195- raise NotRedirectResponse(message)1196- assert self.call_next is not None1197- return self.call_next()1198-1185 def close(self) -> None:1186 """1187 Close the response and release the connection.1188 Automatically called if the response body is read to completion.64 unmodified lines1253 self._num_bytes_downloaded += len(part)1254 yield part1255 await self.aclose()1256 1271- async def anext(self) -> "Response":1272- """1273- Get the next response from a redirect response.1274- """1275- if not self.is_redirect:1276- raise NotRedirectResponse(1277- "Called .anext(), but the response was not a redirect. "1278- "Calling code should check `response.is_redirect` first."1279- )1280- assert self.call_next is not None1281- return await self.call_next()1282-1257 async def aclose(self) -> None:1258 """1259 Close the response and release the connection.1260 Automatically called if the response body is read to completion.186 unmodified linesmodified:tests/client/test_async_client.py-3 removed
17 unmodified lines18 assert response.headers19 assert repr(response) == "<Response [200 OK]>"20 assert response.elapsed > timedelta(seconds=0)21 22- with pytest.raises(httpx.NotRedirectResponse):23- await response.anext()22 25-23 @pytest.mark.parametrize(24 "url",25 [26 pytest.param("invalid://example.org", id="scheme-not-http(s)"),218 unmodified linesmodified:tests/client/test_client.py-3 removed
21 unmodified lines22 assert response.is_redirect is False23 assert repr(response) == "<Response [200 OK]>"24 assert response.elapsed > timedelta(0)25 26- with pytest.raises(httpx.NotRedirectResponse):27- response.next()26 29-27 @pytest.mark.parametrize(28 "url",29 [30 pytest.param("invalid://example.org", id="scheme-not-http(s)"),243 unmodified linesmodified:tests/client/test_redirects.py+12 added-43 removed
9 unmodified lines10 raise httpcore.UnsupportedProtocol(11 f"Scheme {request.url.scheme!r} not supported."12 )13 14- if request.url.path == "/no_redirect":15- return httpx.Response(200)16-17- elif request.url.path == "/redirect_301":14+ if request.url.path == "/redirect_301":15 status_code = httpx.codes.MOVED_PERMANENTLY16 content = b"<a href='https://example.org/'>here</a>"17 headers = {"location": "https://example.org/"}18 return httpx.Response(status_code, headers=headers, content=content)95 unmodified lines114 115 return httpx.Response(200, html="<html><body>Hello, world!</body></html>")116 117 121-def test_no_redirect():122- client = httpx.Client(transport=MockTransport(redirects))123- url = "https://example.com/no_redirect"124- response = client.get(url)125- assert response.status_code == 200126- with pytest.raises(httpx.NotRedirectResponse):127- response.next()128-129-118 def test_redirect_301():119 client = httpx.Client(transport=MockTransport(redirects))120 response = client.post("https://example.org/redirect_301")121 assert response.status_code == httpx.codes.OK16 unmodified lines138 assert response.url == "https://example.org/"139 assert len(response.history) == 1140 141 154-def test_disallow_redirects():142+def test_next_request():143 client = httpx.Client(transport=MockTransport(redirects))156- response = client.post("https://example.org/redirect_303", allow_redirects=False)144+ request = client.build_request("POST", "https://example.org/redirect_303")145+ response = client.send(request, allow_redirects=False)146 assert response.status_code == httpx.codes.SEE_OTHER147 assert response.url == "https://example.org/redirect_303"159- assert response.is_redirect is True160- assert len(response.history) == 0148+ assert response.next_request is not None149 162- response = response.next()150+ response = client.send(response.next_request, allow_redirects=False)151 assert response.status_code == httpx.codes.OK152 assert response.url == "https://example.org/"165- assert response.is_redirect is False166- assert len(response.history) == 1153+ assert response.next_request is None154 155 156+@pytest.mark.usefixtures("async_environment")169-def test_next_request():170- client = httpx.Client(transport=MockTransport(redirects))157+async def test_async_next_request():158+ client = httpx.AsyncClient(transport=MockTransport(redirects))159 request = client.build_request("POST", "https://example.org/redirect_303")172- response = client.send(request, allow_redirects=False)160+ response = await client.send(request, allow_redirects=False)161 assert response.status_code == httpx.codes.SEE_OTHER162 assert response.url == "https://example.org/redirect_303"163 assert response.next_request is not None164 177- response = client.send(response.next_request, allow_redirects=False)165+ response = await client.send(response.next_request, allow_redirects=False)166 assert response.status_code == httpx.codes.OK167 assert response.url == "https://example.org/"168 assert response.next_request is None169 68 unmodified lines238 with pytest.raises(httpx.TooManyRedirects):239 await client.get("https://example.org/multiple_redirects?count=21")240 241 254-@pytest.mark.usefixtures("async_environment")255-async def test_async_too_many_redirects_calling_next():256- async with httpx.AsyncClient(transport=MockTransport(redirects)) as client:257- url = "https://example.org/multiple_redirects?count=21"258- response = await client.get(url, allow_redirects=False)259- with pytest.raises(httpx.TooManyRedirects):260- while response.is_redirect:261- response = await response.anext()262-263-242 def test_sync_too_many_redirects():243 client = httpx.Client(transport=MockTransport(redirects))244 with pytest.raises(httpx.TooManyRedirects):245 client.get("https://example.org/multiple_redirects?count=21")246 247 270-def test_sync_too_many_redirects_calling_next():271- client = httpx.Client(transport=MockTransport(redirects))272- url = "https://example.org/multiple_redirects?count=21"273- response = client.get(url, allow_redirects=False)274- with pytest.raises(httpx.TooManyRedirects):275- while response.is_redirect:276- response = response.next()277-278-248 def test_redirect_loop():249 client = httpx.Client(transport=MockTransport(redirects))250 with pytest.raises(httpx.TooManyRedirects):251 client.get("https://example.org/redirect_loop")148 unmodified lines