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
Provenance
- Commit
f9d18a8- Tree
4fbcd2c- Author
Florimond Manca <florimond.manca@gmail.com>- Committer
Tom Christie <tom@tomchristie.com>- Parent
25b40db
14 files changed+53 added−53 removed
modified:docs/api.md+2 added-2 removed
60 unmodified lines6162636465656667686970707172737486 unmodified lines60 unmodified lines [total_seconds()](https://docs.python.org/3/library/datetime.html#datetime.timedelta.total_seconds) to correctly get the total elapsed seconds.* `def .raise_for_status()` - **None*** `def .json()` - **Any*** `def .read()` - **bytes*** `def .aread()` - **bytes*** `def .aiter_raw()` - **async bytes iterator*** `def .aiter_bytes()` - **async bytes iterator*** `def .aiter_text()` - **async text iterator*** `def .aiter_lines()` - **async text iterator*** `def .close()` - **None*** `def .aclose()` - **None*** `def .next()` - **Response**
## `Request`
86 unmodified linesmodified:docs/compatibility.md+1 added-1 removed
30 unmodified lines31 * `.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.36 37 ## SSL configuration38 39 When 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 linesmodified:docs/quickstart.md+1 added-1 removed
337 unmodified lines338 339 ```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 ```345 346 ## Cookies113 unmodified linesmodified:httpx/dispatch/proxy_http.py+1 added-1 removed
153 unmodified lines154 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 linesmodified:httpx/client.py+6 added-6 removed
401 unmodified lines402 )403 404 if not stream:405 try:406- await response.read()406+ await response.aread()407 finally:408- await response.close()408+ await response.aclose()409 410 return response411 412 def setup_auth(self, request: Request, auth: AuthTypes = None) -> Auth:43 unmodified lines456 457 if not response.is_redirect:458 return response459 460- await response.read()460+ await response.aread()461 request = self.build_redirect_request(request, response)462 history = history + [response]463 464 if not allow_redirects:110 unmodified lines575 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()584 585 async def send_single_request(586 self, request: Request, timeout: Timeout,587 ) -> Response:365 unmodified lines953 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 lines843 844 def __repr__(self) -> str:845 return f"<Response [{self.status_code} {self.reason_phrase}]>"846 847- 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 lines913 914 self.is_stream_consumed = True915 async for part in self._raw_stream:916 yield part917- await self.close()917+ await self.aclose()918 919 async def next(self) -> "Response":920 """921 Get the next response from a redirect response.2 unmodified lines924 raise NotRedirectResponse()925 assert self.call_next is not None926 return await self.call_next()927 928- 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 linesmodified:tests/client/test_async_client.py+1 added-1 removed
51 unmodified lines52 @pytest.mark.usefixtures("async_environment")53 async 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()57 58 assert response.status_code == 20059 assert body == b"Hello, world!"60 assert response.content == b"Hello, world!"109 unmodified linesmodified:tests/client/test_client.py+1 added-1 removed
57 unmodified lines58 @pytest.mark.asyncio59 async 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!"65 66 116 unmodified linesmodified:tests/dispatch/test_connection_pools.py+20 added-20 removed
9 unmodified lines10 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) == 117 18 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) == 122 23 3 unmodified lines27 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) == 134 35 http.next_keepalive_check = 0.05 unmodified lines41 async with ConnectionPool() as http:42 http.KEEP_ALIVE_EXPIRY = 0.043 44 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) == 148 49 http.next_keepalive_check = 0.09 unmodified lines59 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) == 166 67 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) == 271 72 5 unmodified lines78 pool_limits = httpx.PoolLimits(soft_limit=1)79 80 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) == 185 86 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) == 190 91 6 unmodified lines98 response = await http.request("GET", server.url)99 assert len(http.active_connections) == 1100 assert len(http.keepalive_connections) == 0101 102- await response.read()102+ await response.aread()103 104 assert len(http.active_connections) == 0105 assert len(http.keepalive_connections) == 1106 11 unmodified lines118 response_b = await http.request("GET", server.url)119 assert len(http.active_connections) == 2120 assert len(http.keepalive_connections) == 0121 122- await response_b.read()122+ await response_b.aread()123 assert len(http.active_connections) == 1124 assert len(http.keepalive_connections) == 1125 126- await response_a.read()126+ await response_a.aread()127 assert len(http.active_connections) == 0128 assert len(http.keepalive_connections) == 2129 130 4 unmodified lines135 """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) == 0142 143 3 unmodified lines147 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) == 1155 156 3 unmodified lines160 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) == 0167 168 4 unmodified lines173 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()178 179 # Shutdown the server to close the keep-alive connection180 await restart(server)181 182 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) == 1186 187 6 unmodified lines194 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()199 200 # Shutdown the server to close the keep-alive connection201 await restart(server)202 203 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) == 1207 208 4 unmodified lines213 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()218 219 # Close the connection so we're forced to recycle it220 await restart(server)221 2 unmodified linesmodified:tests/dispatch/test_connections.py+3 added-3 removed
6 unmodified lines7 @pytest.mark.usefixtures("async_environment")8 async 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!"14 15 10 unmodified lines26 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()31 32 33 @pytest.mark.usefixtures("async_environment")34 async def test_https_get_with_ssl(https_server, ca_cert_pem_file):1 unmodified line36 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 lines118 assert resp.request.method == "GET"119 assert resp.request.url == "https://example.com"120 assert resp.request.headers["Host"] == "example.com"121 122- await resp.read()122+ await resp.aread()123 124 # Make another request to see that the tunnel is re-used.125 resp = await proxy.request("GET", "https://example.com/target")126 3 unmodified lines130 assert resp.request.method == "GET"131 assert resp.request.url == "https://example.com/target"132 assert resp.request.headers["Host"] == "example.com"133 134- await resp.read()134+ await resp.aread()135 136 recv = raw_io.received_data137 assert len(recv) == 5138 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"68 unmodified linesmodified:tests/models/test_responses.py+8 added-8 removed
129 unmodified lines130 assert response.text == "Hello, world!"131 assert response.encoding == "ascii"132 assert response.is_closed133 134- content = await response.read()134+ content = await response.aread()135 136 assert content == b"Hello, world!"137 assert response.content == b"Hello, world!"138 assert response.is_closed22 unmodified lines161 @pytest.mark.asyncio162 async def test_text_interface():163 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)164 165- await response.read()165+ await response.aread()166 167 content = ""168 async for part in response.aiter_text():169 content += part3 unmodified lines173 @pytest.mark.asyncio174 async def test_lines_interface():175 response = httpx.Response(200, content=b"Hello,\nworld!", request=REQUEST)176 177- await response.read()177+ await response.aread()178 179 content = []180 async for line in response.aiter_lines():181 content.append(line)3 unmodified lines185 @pytest.mark.asyncio186 async def test_stream_interface_after_read():187 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)188 189- await response.read()189+ await response.aread()190 191 content = b""192 async for part in response.aiter_bytes():193 content += part7 unmodified lines201 202 assert response.status_code == 200203 assert not response.is_closed204 205- content = await response.read()205+ content = await response.aread()206 207 assert content == b"Hello, world!"208 assert response.content == b"Hello, world!"209 assert response.is_closed8 unmodified lines218 async for part in response.aiter_bytes():219 content += part220 221 with pytest.raises(httpx.StreamConsumed):222- await response.read()222+ await response.aread()223 224 225 @pytest.mark.asyncio226 async def test_cannot_read_after_response_closed():227 stream = AsyncIteratorStream(aiterator=async_streaming_body())228 response = httpx.Response(200, stream=stream, request=REQUEST)229 230- await response.close()230+ await response.aclose()231 232 with pytest.raises(httpx.ResponseClosed):233- await response.read()233+ await response.aread()234 235 236 def test_unknown_status_code():237 response = httpx.Response(600, request=REQUEST)61 unmodified linesmodified:tests/test_api.py+1 added-1 removed
67 unmodified lines68 69 @pytest.mark.asyncio70 async def test_stream(server):71 async with httpx.stream("GET", server.url) as response:72- await response.read()72+ await response.aread()73 74 assert response.status_code == 20075 assert response.reason_phrase == "OK"76 assert response.text == "Hello, world!"7 unmodified linesmodified:tests/test_decoders.py+3 added-3 removed
98 unmodified lines99 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() == body104 105 106 @pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br", b"identity"))107 def test_empty_content(header_value):48 unmodified lines156 yield chunk157 158 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)162 163 164 @pytest.mark.asyncio10 unmodified lines175 stream=stream,176 request=REQUEST,177 )178 179- await response.read()179+ await response.aread()180 assert "".join(response.text) == "トラベル"181 182 183 def test_text_decoder_empty_cases():56 unmodified lines