Skip to content
Myr

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

8 files+12 added−109 removed

modified:docs/exceptions.md-4 removed
modified:httpx/__init__.py-2 removed
12 unmodified lines
13 HTTPStatusError,14 InvalidURL,15 LocalProtocolError,16 NetworkError,17 NotRedirectResponse,17 PoolTimeout,18 ProtocolError,19 ProxyError,20 ReadError,
44 unmodified lines
65 "InvalidURL",66 "Limits",67 "LocalProtocolError",68 "NetworkError",70 "NotRedirectResponse",69 "options",70 "patch",71 "PoolTimeout",72 "post",
35 unmodified lines
modified:httpx/_client.py-15 removed
1import datetime2import enum3import functools3import typing4import warnings5from types import TracebackType6
839 unmodified lines
846 history = history + [response]847848 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 response851852 def _send_single_request(self, request: Request, timeout: Timeout) -> Response:853 """
631 unmodified lines
1485 history = history + [response]14861487 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 response14901491 async def _send_single_request(1492 self, request: Request, timeout: Timeout
354 unmodified lines
modified:httpx/_exceptions.py-13 removed
22 unmodified lines
23 + TooManyRedirects24 + RequestBodyUnavailable25 x HTTPStatusError26* InvalidURL27* NotRedirectResponse27* CookieConflict28* StreamError29 x StreamConsumed30 x ResponseNotRead
200 unmodified lines
231 def __init__(self, message: str) -> None:232 super().__init__(message)233234236class NotRedirectResponse(Exception):237 """238 Response was not a redirect response.239240 May be raised if `response.next()` is called without first241 properly checking `response.is_redirect`.242 """243244 def __init__(self, message: str) -> None:245 super().__init__(message)246247235class CookieConflict(Exception):236 """237 Attempted to lookup a cookie by name, but multiple cookies existed.238
124 unmodified lines
modified:httpx/_models.py-26 removed
25 unmodified lines
26 CookieConflict,27 DecodingError,28 HTTPStatusError,29 InvalidURL,30 NotRedirectResponse,30 RequestNotRead,31 ResponseClosed,32 ResponseNotRead,33 StreamConsumed,
1147 unmodified lines
1181 self._num_bytes_downloaded += len(part)1182 yield part1183 self.close()11841186 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()11981185 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 lines
1253 self._num_bytes_downloaded += len(part)1254 yield part1255 await self.aclose()12561271 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()12821257 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 lines
modified:tests/client/test_async_client.py-3 removed
17 unmodified lines
18 assert response.headers19 assert repr(response) == "<Response [200 OK]>"20 assert response.elapsed > timedelta(seconds=0)2122 with pytest.raises(httpx.NotRedirectResponse):23 await response.anext()222523@pytest.mark.parametrize(24 "url",25 [26 pytest.param("invalid://example.org", id="scheme-not-http(s)"),
218 unmodified lines
modified:tests/client/test_client.py-3 removed
21 unmodified lines
22 assert response.is_redirect is False23 assert repr(response) == "<Response [200 OK]>"24 assert response.elapsed > timedelta(0)2526 with pytest.raises(httpx.NotRedirectResponse):27 response.next()262927@pytest.mark.parametrize(28 "url",29 [30 pytest.param("invalid://example.org", id="scheme-not-http(s)"),
243 unmodified lines
modified:tests/client/test_redirects.py+12 added-43 removed
9 unmodified lines
10 raise httpcore.UnsupportedProtocol(11 f"Scheme {request.url.scheme!r} not supported."12 )1314 if request.url.path == "/no_redirect":15 return httpx.Response(200)1617 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 lines
114115 return httpx.Response(200, html="<html><body>Hello, world!</body></html>")116117121def 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()128129118def 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.OK
16 unmodified lines
138 assert response.url == "https://example.org/"139 assert len(response.history) == 1140141154def test_disallow_redirects():142def 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 None149162 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 None154155156@pytest.mark.usefixtures("async_environment")169def test_next_request():170 client = httpx.Client(transport=MockTransport(redirects))157async 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 None164177 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 lines
238 with pytest.raises(httpx.TooManyRedirects):239 await client.get("https://example.org/multiple_redirects?count=21")240241254@pytest.mark.usefixtures("async_environment")255async 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()262263242def 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")246247270def 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()277278248def 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