Skip to content
Myr

Rename 'read/close' to 'aread/aclose' on Response (#674)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Switch to aread/aclose on responses * Linting

authored by Florimond Manca · committed by Tom Christie · parent 25b40db · Browse files · 14 files+53 added−53 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Tom Christie <tom@tomchristie.com>

Diff stats

14 files+53 added−53 removed

  • tests:+39 added−39 removed
  • other:+14 added−14 removed

14 files+53 added−53 removed

modified:docs/api.md+2 added-2 removed
modified:docs/compatibility.md+1 added-1 removed
30 unmodified lines
31* `.aiter_bytes()` - Instead of `response.iter_content()`32* `.aiter_text()` - Instead of `response.iter_content(decode_unicode=True)`33* `.aiter_lines()` - Instead of `response.iter_lines()`34* `.aiter_raw()` - Use this instead of `response.raw`35* `.read()` - Read the entire response body, making `request.text` and `response.content` available.35* `.aread()` - Read the entire response body, making `request.text` and `response.content` available.3637## SSL configuration3839When using a `Client` instance, the `trust_env`, `verify`, and `cert` arguments should always be passed on client instantiation, rather than passed to the request method.
30 unmodified lines
modified:docs/quickstart.md+1 added-1 removed
337 unmodified lines
338339```340>>> async with httpx.stream("GET", "https://www.example.com") as r:341... if r.headers['Content-Length'] < TOO_LONG:342... await r.read()342... await r.aread()343... print(r.text)344```345346## Cookies
113 unmodified lines
modified:httpx/dispatch/proxy_http.py+1 added-1 removed
153 unmodified lines
154 f"origin={origin!r} "155 f"response={proxy_response!r}"156 )157 if not (200 <= proxy_response.status_code <= 299):158 await proxy_response.read()158 await proxy_response.aread()159 raise ProxyError(160 f"Non-2XX response received from HTTP proxy "161 f"({proxy_response.status_code})",162 request=proxy_request,
38 unmodified lines
modified:httpx/client.py+6 added-6 removed
401 unmodified lines
402 )403404 if not stream:405 try:406 await response.read()406 await response.aread()407 finally:408 await response.close()408 await response.aclose()409410 return response411412 def setup_auth(self, request: Request, auth: AuthTypes = None) -> Auth:
43 unmodified lines
456457 if not response.is_redirect:458 return response459460 await response.read()460 await response.aread()461 request = self.build_redirect_request(request, response)462 history = history + [response]463464 if not allow_redirects:
110 unmodified lines
575 next_request = auth_flow.send(response)576 except StopIteration:577 return response578 except BaseException as exc:579 await response.close()579 await response.aclose()580 raise exc from None581 else:582 request = next_request583 await response.close()583 await response.aclose()584585 async def send_single_request(586 self, request: Request, timeout: Timeout,587 ) -> Response:
365 unmodified lines
953 exc_type: typing.Type[BaseException] = None,954 exc_value: BaseException = None,955 traceback: TracebackType = None,956 ) -> None:957 await self.response.close()957 await self.response.aclose()958 if self.close_client:959 await self.client.close()
modified:httpx/models.py+3 added-3 removed
842 unmodified lines
843844 def __repr__(self) -> str:845 return f"<Response [{self.status_code} {self.reason_phrase}]>"846847 async def read(self) -> bytes:847 async def aread(self) -> bytes:848 """849 Read and return the response content.850 """851 if not hasattr(self, "_content"):
61 unmodified lines
913914 self.is_stream_consumed = True915 async for part in self._raw_stream:916 yield part917 await self.close()917 await self.aclose()918919 async def next(self) -> "Response":920 """921 Get the next response from a redirect response.
2 unmodified lines
924 raise NotRedirectResponse()925 assert self.call_next is not None926 return await self.call_next()927928 async def close(self) -> None:928 async def aclose(self) -> None:929 """930 Close the response and release the connection.931 Automatically called if the response body is read to completion.932 """
178 unmodified lines
modified:tests/client/test_async_client.py+1 added-1 removed
51 unmodified lines
52@pytest.mark.usefixtures("async_environment")53async def test_stream_response(server):54 async with httpx.Client() as client:55 async with client.stream("GET", server.url) as response:56 body = await response.read()56 body = await response.aread()5758 assert response.status_code == 20059 assert body == b"Hello, world!"60 assert response.content == b"Hello, world!"
109 unmodified lines
modified:tests/client/test_client.py+1 added-1 removed
57 unmodified lines
58@pytest.mark.asyncio59async def test_stream_response(server):60 async with httpx.Client() as client:61 async with client.stream("GET", server.url) as response:62 content = await response.read()62 content = await response.aread()63 assert response.status_code == 20064 assert content == b"Hello, world!"6566
116 unmodified lines
modified:tests/dispatch/test_connection_pools.py+20 added-20 removed
9 unmodified lines
10 Connections should default to staying in a keep-alive state.11 """12 async with ConnectionPool() as http:13 response = await http.request("GET", server.url)14 await response.read()14 await response.aread()15 assert len(http.active_connections) == 016 assert len(http.keepalive_connections) == 11718 response = await http.request("GET", server.url)19 await response.read()19 await response.aread()20 assert len(http.active_connections) == 021 assert len(http.keepalive_connections) == 12223
3 unmodified lines
27 Keep-alive connections should timeout.28 """29 async with ConnectionPool() as http:30 response = await http.request("GET", server.url)31 await response.read()31 await response.aread()32 assert len(http.active_connections) == 033 assert len(http.keepalive_connections) == 13435 http.next_keepalive_check = 0.0
5 unmodified lines
41 async with ConnectionPool() as http:42 http.KEEP_ALIVE_EXPIRY = 0.04344 response = await http.request("GET", server.url)45 await response.read()45 await response.aread()46 assert len(http.active_connections) == 047 assert len(http.keepalive_connections) == 14849 http.next_keepalive_check = 0.0
9 unmodified lines
59 Connections to differing connection keys should result in multiple connections.60 """61 async with ConnectionPool() as http:62 response = await http.request("GET", server.url)63 await response.read()63 await response.aread()64 assert len(http.active_connections) == 065 assert len(http.keepalive_connections) == 16667 response = await http.request("GET", "http://localhost:8000/")68 await response.read()68 await response.aread()69 assert len(http.active_connections) == 070 assert len(http.keepalive_connections) == 27172
5 unmodified lines
78 pool_limits = httpx.PoolLimits(soft_limit=1)7980 async with ConnectionPool(pool_limits=pool_limits) as http:81 response = await http.request("GET", server.url)82 await response.read()82 await response.aread()83 assert len(http.active_connections) == 084 assert len(http.keepalive_connections) == 18586 response = await http.request("GET", "http://localhost:8000/")87 await response.read()87 await response.aread()88 assert len(http.active_connections) == 089 assert len(http.keepalive_connections) == 19091
6 unmodified lines
98 response = await http.request("GET", server.url)99 assert len(http.active_connections) == 1100 assert len(http.keepalive_connections) == 0101102 await response.read()102 await response.aread()103104 assert len(http.active_connections) == 0105 assert len(http.keepalive_connections) == 1106
11 unmodified lines
118 response_b = await http.request("GET", server.url)119 assert len(http.active_connections) == 2120 assert len(http.keepalive_connections) == 0121122 await response_b.read()122 await response_b.aread()123 assert len(http.active_connections) == 1124 assert len(http.keepalive_connections) == 1125126 await response_a.read()126 await response_a.aread()127 assert len(http.active_connections) == 0128 assert len(http.keepalive_connections) == 2129130
4 unmodified lines
135 """136 headers = [(b"connection", b"close")]137 async with ConnectionPool() as http:138 response = await http.request("GET", server.url, headers=headers)139 await response.read()139 await response.aread()140 assert len(http.active_connections) == 0141 assert len(http.keepalive_connections) == 0142143
3 unmodified lines
147 A standard close should keep the connection open.148 """149 async with ConnectionPool() as http:150 response = await http.request("GET", server.url)151 await response.read()152 await response.close()151 await response.aread()152 await response.aclose()153 assert len(http.active_connections) == 0154 assert len(http.keepalive_connections) == 1155156
3 unmodified lines
160 A premature close should close the connection.161 """162 async with ConnectionPool() as http:163 response = await http.request("GET", server.url)164 await response.close()164 await response.aclose()165 assert len(http.active_connections) == 0166 assert len(http.keepalive_connections) == 0167168
4 unmodified lines
173 should be reestablished.174 """175 async with ConnectionPool() as http:176 response = await http.request("GET", server.url)177 await response.read()177 await response.aread()178179 # Shutdown the server to close the keep-alive connection180 await restart(server)181182 response = await http.request("GET", server.url)183 await response.read()183 await response.aread()184 assert len(http.active_connections) == 0185 assert len(http.keepalive_connections) == 1186187
6 unmodified lines
194 should be reestablished.195 """196 async with ConnectionPool() as http:197 response = await http.request("GET", server.url)198 await response.read()198 await response.aread()199200 # Shutdown the server to close the keep-alive connection201 await restart(server)202203 response = await http.request("GET", server.url)204 await response.read()204 await response.aread()205 assert len(http.active_connections) == 0206 assert len(http.keepalive_connections) == 1207208
4 unmodified lines
213 properly on a disconnected connection.214 """215 async with ConnectionPool(pool_limits=httpx.PoolLimits(hard_limit=1)) as http:216 response = await http.request("GET", server.url)217 await response.read()217 await response.aread()218219 # Close the connection so we're forced to recycle it220 await restart(server)221
2 unmodified lines
modified:tests/dispatch/test_connections.py+3 added-3 removed
6 unmodified lines
7@pytest.mark.usefixtures("async_environment")8async def test_get(server):9 async with HTTPConnection(origin=server.url) as conn:10 response = await conn.request("GET", server.url)11 await response.read()11 await response.aread()12 assert response.status_code == 20013 assert response.content == b"Hello, world!"1415
10 unmodified lines
26 async with HTTPConnection(origin=server.url) as conn:27 response = await conn.request(28 "GET", server.url.copy_with(path="/premature_close")29 )30 await response.read()30 await response.aread()313233@pytest.mark.usefixtures("async_environment")34async def test_https_get_with_ssl(https_server, ca_cert_pem_file):
1 unmodified line
36 An HTTPS request, with SSL configuration set on the client.37 """38 async with HTTPConnection(origin=https_server.url, verify=ca_cert_pem_file) as conn:39 response = await conn.request("GET", https_server.url)40 await response.read()40 await response.aread()41 assert response.status_code == 20042 assert response.content == b"Hello, world!"
modified:tests/dispatch/test_proxy_http.py+2 added-2 removed
117 unmodified lines
118 assert resp.request.method == "GET"119 assert resp.request.url == "https://example.com"120 assert resp.request.headers["Host"] == "example.com"121122 await resp.read()122 await resp.aread()123124 # Make another request to see that the tunnel is re-used.125 resp = await proxy.request("GET", "https://example.com/target")126
3 unmodified lines
130 assert resp.request.method == "GET"131 assert resp.request.url == "https://example.com/target"132 assert resp.request.headers["Host"] == "example.com"133134 await resp.read()134 await resp.aread()135136 recv = raw_io.received_data137 assert len(recv) == 5138 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"
68 unmodified lines
modified:tests/models/test_responses.py+8 added-8 removed
129 unmodified lines
130 assert response.text == "Hello, world!"131 assert response.encoding == "ascii"132 assert response.is_closed133134 content = await response.read()134 content = await response.aread()135136 assert content == b"Hello, world!"137 assert response.content == b"Hello, world!"138 assert response.is_closed
22 unmodified lines
161@pytest.mark.asyncio162async def test_text_interface():163 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)164165 await response.read()165 await response.aread()166167 content = ""168 async for part in response.aiter_text():169 content += part
3 unmodified lines
173@pytest.mark.asyncio174async def test_lines_interface():175 response = httpx.Response(200, content=b"Hello,\nworld!", request=REQUEST)176177 await response.read()177 await response.aread()178179 content = []180 async for line in response.aiter_lines():181 content.append(line)
3 unmodified lines
185@pytest.mark.asyncio186async def test_stream_interface_after_read():187 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)188189 await response.read()189 await response.aread()190191 content = b""192 async for part in response.aiter_bytes():193 content += part
7 unmodified lines
201202 assert response.status_code == 200203 assert not response.is_closed204205 content = await response.read()205 content = await response.aread()206207 assert content == b"Hello, world!"208 assert response.content == b"Hello, world!"209 assert response.is_closed
8 unmodified lines
218 async for part in response.aiter_bytes():219 content += part220221 with pytest.raises(httpx.StreamConsumed):222 await response.read()222 await response.aread()223224225@pytest.mark.asyncio226async def test_cannot_read_after_response_closed():227 stream = AsyncIteratorStream(aiterator=async_streaming_body())228 response = httpx.Response(200, stream=stream, request=REQUEST)229230 await response.close()230 await response.aclose()231232 with pytest.raises(httpx.ResponseClosed):233 await response.read()233 await response.aread()234235236def test_unknown_status_code():237 response = httpx.Response(600, request=REQUEST)
61 unmodified lines
modified:tests/test_api.py+1 added-1 removed
67 unmodified lines
6869@pytest.mark.asyncio70async def test_stream(server):71 async with httpx.stream("GET", server.url) as response:72 await response.read()72 await response.aread()7374 assert response.status_code == 20075 assert response.reason_phrase == "OK"76 assert response.text == "Hello, world!"
7 unmodified lines
modified:tests/test_decoders.py+3 added-3 removed
98 unmodified lines
99 headers = [(b"Content-Encoding", b"gzip")]100 stream = AsyncIteratorStream(aiterator=compress(body))101 response = httpx.Response(200, headers=headers, stream=stream, request=REQUEST)102 assert not hasattr(response, "body")103 assert await response.read() == body103 assert await response.aread() == body104105106@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br", b"identity"))107def test_empty_content(header_value):
48 unmodified lines
156 yield chunk157158 stream = AsyncIteratorStream(aiterator=iterator())159 response = httpx.Response(200, stream=stream, request=REQUEST)160 await response.read()160 await response.aread()161 assert response.text == (b"".join(data)).decode(encoding)162163164@pytest.mark.asyncio
10 unmodified lines
175 stream=stream,176 request=REQUEST,177 )178179 await response.read()179 await response.aread()180 assert "".join(response.text) == "トラベル"181182183def test_text_decoder_empty_cases():
56 unmodified lines