]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Rename 'read/close' to 'aread/aclose' on Response (#674)
authorFlorimond Manca <florimond.manca@gmail.com>
Sun, 29 Dec 2019 15:14:53 +0000 (16:14 +0100)
committerTom Christie <tom@tomchristie.com>
Sun, 29 Dec 2019 15:14:53 +0000 (15:14 +0000)
* Switch to aread/aclose on responses

* Linting

Co-authored-by: Tom Christie <tom@tomchristie.com>
14 files changed:
docs/api.md
docs/compatibility.md
docs/quickstart.md
httpx/client.py
httpx/dispatch/proxy_http.py
httpx/models.py
tests/client/test_async_client.py
tests/client/test_client.py
tests/dispatch/test_connection_pools.py
tests/dispatch/test_connections.py
tests/dispatch/test_proxy_http.py
tests/models/test_responses.py
tests/test_api.py
tests/test_decoders.py

index 7762c05947a168c7c0e59be7f8f7e91ecfd99377..62380f9126d84a87e91ec90f34b783cde84967a4 100644 (file)
   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`
index 506340c3abc5d1ea10fa2913c2347d7e6790b412..6f67660ba51ec6638cfe5f1745a7c2965e5c4366 100644 (file)
@@ -32,7 +32,7 @@ Within a `stream()` block request data is made available with:
 * `.aiter_text()` - Instead of `response.iter_content(decode_unicode=True)`
 * `.aiter_lines()` - Instead of `response.iter_lines()`
 * `.aiter_raw()` - Use this instead of `response.raw`
-* `.read()` - Read the entire response body, making `request.text` and `response.content` available.
+* `.aread()` - Read the entire response body, making `request.text` and `response.content` available.
 
 ## SSL configuration
 
index 8ef8e959eeb2718a907cccaa2e2e14afe37e48dd..faa34959f01f127357e772da1d0cf5ebc4851ffe 100644 (file)
@@ -339,7 +339,7 @@ If you're using streaming responses in any of these ways then the `response.cont
 ```
 >>> async with httpx.stream("GET", "https://www.example.com") as r:
 ...     if r.headers['Content-Length'] < TOO_LONG:
-...         await r.read()
+...         await r.aread()
 ...         print(r.text)
 ```
 
index 41ddd415cf7ff8187386b0debebbf8dfbb182c99..2596cd531e55c796f0d4f6098287bcdda8b2ca05 100644 (file)
@@ -403,9 +403,9 @@ class Client:
 
         if not stream:
             try:
-                await response.read()
+                await response.aread()
             finally:
-                await response.close()
+                await response.aclose()
 
         return response
 
@@ -457,7 +457,7 @@ class Client:
             if not response.is_redirect:
                 return response
 
-            await response.read()
+            await response.aread()
             request = self.build_redirect_request(request, response)
             history = history + [response]
 
@@ -576,11 +576,11 @@ class Client:
             except StopIteration:
                 return response
             except BaseException as exc:
-                await response.close()
+                await response.aclose()
                 raise exc from None
             else:
                 request = next_request
-                await response.close()
+                await response.aclose()
 
     async def send_single_request(
         self, request: Request, timeout: Timeout,
@@ -954,6 +954,6 @@ class StreamContextManager:
         exc_value: BaseException = None,
         traceback: TracebackType = None,
     ) -> None:
-        await self.response.close()
+        await self.response.aclose()
         if self.close_client:
             await self.client.close()
index 1c2407907bd92484fca58ba42d0d363963d6a855..01aaa7db466e8f9e147b86cc0f4538745e64b73b 100644 (file)
@@ -155,7 +155,7 @@ class HTTPProxy(ConnectionPool):
             f"response={proxy_response!r}"
         )
         if not (200 <= proxy_response.status_code <= 299):
-            await proxy_response.read()
+            await proxy_response.aread()
             raise ProxyError(
                 f"Non-2XX response received from HTTP proxy "
                 f"({proxy_response.status_code})",
index e217f01aee3d2a2637871429e2cf5f7b428f4687..174109511fbe8c983ba6a2ddd560b866f0f6e086 100644 (file)
@@ -844,7 +844,7 @@ class Response:
     def __repr__(self) -> str:
         return f"<Response [{self.status_code} {self.reason_phrase}]>"
 
-    async def read(self) -> bytes:
+    async def aread(self) -> bytes:
         """
         Read and return the response content.
         """
@@ -914,7 +914,7 @@ class Response:
             self.is_stream_consumed = True
             async for part in self._raw_stream:
                 yield part
-            await self.close()
+            await self.aclose()
 
     async def next(self) -> "Response":
         """
@@ -925,7 +925,7 @@ class Response:
         assert self.call_next is not None
         return await self.call_next()
 
-    async def close(self) -> None:
+    async def aclose(self) -> None:
         """
         Close the response and release the connection.
         Automatically called if the response body is read to completion.
index 8180b0dbf72d2ade292f486e9f95e248e6225f6a..20216b6f5d1350745640cfdfb332eb4d66601037 100644 (file)
@@ -53,7 +53,7 @@ async def test_post_json(server):
 async def test_stream_response(server):
     async with httpx.Client() as client:
         async with client.stream("GET", server.url) as response:
-            body = await response.read()
+            body = await response.aread()
 
     assert response.status_code == 200
     assert body == b"Hello, world!"
index 851896146ed07bb73b9e175a305b0edda6c64eff..05abedf1f5ff51b1b218b23fd7b5fff0ec0946f1 100644 (file)
@@ -59,7 +59,7 @@ async def test_post_json(server):
 async def test_stream_response(server):
     async with httpx.Client() as client:
         async with client.stream("GET", server.url) as response:
-            content = await response.read()
+            content = await response.aread()
     assert response.status_code == 200
     assert content == b"Hello, world!"
 
index d108bbd5031b8c34fc26fe181bf4fa95095b705f..5e1fce3e0748fcb2aa88fc5223022d05079ccb23 100644 (file)
@@ -11,12 +11,12 @@ async def test_keepalive_connections(server):
     """
     async with ConnectionPool() as http:
         response = await http.request("GET", server.url)
-        await response.read()
+        await response.aread()
         assert len(http.active_connections) == 0
         assert len(http.keepalive_connections) == 1
 
         response = await http.request("GET", server.url)
-        await response.read()
+        await response.aread()
         assert len(http.active_connections) == 0
         assert len(http.keepalive_connections) == 1
 
@@ -28,7 +28,7 @@ async def test_keepalive_timeout(server):
     """
     async with ConnectionPool() as http:
         response = await http.request("GET", server.url)
-        await response.read()
+        await response.aread()
         assert len(http.active_connections) == 0
         assert len(http.keepalive_connections) == 1
 
@@ -42,7 +42,7 @@ async def test_keepalive_timeout(server):
         http.KEEP_ALIVE_EXPIRY = 0.0
 
         response = await http.request("GET", server.url)
-        await response.read()
+        await response.aread()
         assert len(http.active_connections) == 0
         assert len(http.keepalive_connections) == 1
 
@@ -60,12 +60,12 @@ async def test_differing_connection_keys(server):
     """
     async with ConnectionPool() as http:
         response = await http.request("GET", server.url)
-        await response.read()
+        await response.aread()
         assert len(http.active_connections) == 0
         assert len(http.keepalive_connections) == 1
 
         response = await http.request("GET", "http://localhost:8000/")
-        await response.read()
+        await response.aread()
         assert len(http.active_connections) == 0
         assert len(http.keepalive_connections) == 2
 
@@ -79,12 +79,12 @@ async def test_soft_limit(server):
 
     async with ConnectionPool(pool_limits=pool_limits) as http:
         response = await http.request("GET", server.url)
-        await response.read()
+        await response.aread()
         assert len(http.active_connections) == 0
         assert len(http.keepalive_connections) == 1
 
         response = await http.request("GET", "http://localhost:8000/")
-        await response.read()
+        await response.aread()
         assert len(http.active_connections) == 0
         assert len(http.keepalive_connections) == 1
 
@@ -99,7 +99,7 @@ async def test_streaming_response_holds_connection(server):
         assert len(http.active_connections) == 1
         assert len(http.keepalive_connections) == 0
 
-        await response.read()
+        await response.aread()
 
         assert len(http.active_connections) == 0
         assert len(http.keepalive_connections) == 1
@@ -119,11 +119,11 @@ async def test_multiple_concurrent_connections(server):
         assert len(http.active_connections) == 2
         assert len(http.keepalive_connections) == 0
 
-        await response_b.read()
+        await response_b.aread()
         assert len(http.active_connections) == 1
         assert len(http.keepalive_connections) == 1
 
-        await response_a.read()
+        await response_a.aread()
         assert len(http.active_connections) == 0
         assert len(http.keepalive_connections) == 2
 
@@ -136,7 +136,7 @@ async def test_close_connections(server):
     headers = [(b"connection", b"close")]
     async with ConnectionPool() as http:
         response = await http.request("GET", server.url, headers=headers)
-        await response.read()
+        await response.aread()
         assert len(http.active_connections) == 0
         assert len(http.keepalive_connections) == 0
 
@@ -148,8 +148,8 @@ async def test_standard_response_close(server):
     """
     async with ConnectionPool() as http:
         response = await http.request("GET", server.url)
-        await response.read()
-        await response.close()
+        await response.aread()
+        await response.aclose()
         assert len(http.active_connections) == 0
         assert len(http.keepalive_connections) == 1
 
@@ -161,7 +161,7 @@ async def test_premature_response_close(server):
     """
     async with ConnectionPool() as http:
         response = await http.request("GET", server.url)
-        await response.close()
+        await response.aclose()
         assert len(http.active_connections) == 0
         assert len(http.keepalive_connections) == 0
 
@@ -174,13 +174,13 @@ async def test_keepalive_connection_closed_by_server_is_reestablished(server, re
     """
     async with ConnectionPool() as http:
         response = await http.request("GET", server.url)
-        await response.read()
+        await response.aread()
 
         # Shutdown the server to close the keep-alive connection
         await restart(server)
 
         response = await http.request("GET", server.url)
-        await response.read()
+        await response.aread()
         assert len(http.active_connections) == 0
         assert len(http.keepalive_connections) == 1
 
@@ -195,13 +195,13 @@ async def test_keepalive_http2_connection_closed_by_server_is_reestablished(
     """
     async with ConnectionPool() as http:
         response = await http.request("GET", server.url)
-        await response.read()
+        await response.aread()
 
         # Shutdown the server to close the keep-alive connection
         await restart(server)
 
         response = await http.request("GET", server.url)
-        await response.read()
+        await response.aread()
         assert len(http.active_connections) == 0
         assert len(http.keepalive_connections) == 1
 
@@ -214,7 +214,7 @@ async def test_connection_closed_free_semaphore_on_acquire(server, restart):
     """
     async with ConnectionPool(pool_limits=httpx.PoolLimits(hard_limit=1)) as http:
         response = await http.request("GET", server.url)
-        await response.read()
+        await response.aread()
 
         # Close the connection so we're forced to recycle it
         await restart(server)
index 1606597dfea85b8583273c448235ed86dbb45da1..65eb6c51ddea2d764441ad373a2214381cd8eb49 100644 (file)
@@ -8,7 +8,7 @@ from httpx.dispatch.connection import HTTPConnection
 async def test_get(server):
     async with HTTPConnection(origin=server.url) as conn:
         response = await conn.request("GET", server.url)
-        await response.read()
+        await response.aread()
         assert response.status_code == 200
         assert response.content == b"Hello, world!"
 
@@ -27,7 +27,7 @@ async def test_premature_close(server):
             response = await conn.request(
                 "GET", server.url.copy_with(path="/premature_close")
             )
-            await response.read()
+            await response.aread()
 
 
 @pytest.mark.usefixtures("async_environment")
@@ -37,6 +37,6 @@ async def test_https_get_with_ssl(https_server, ca_cert_pem_file):
     """
     async with HTTPConnection(origin=https_server.url, verify=ca_cert_pem_file) as conn:
         response = await conn.request("GET", https_server.url)
-        await response.read()
+        await response.aread()
         assert response.status_code == 200
         assert response.content == b"Hello, world!"
index 428b86718ecf54aa2c1bbeb363dd1426b5ea5690..5acb6579d069df6d125ca1e8472a209e75ec6ebc 100644 (file)
@@ -119,7 +119,7 @@ async def test_proxy_tunnel_start_tls():
         assert resp.request.url == "https://example.com"
         assert resp.request.headers["Host"] == "example.com"
 
-        await resp.read()
+        await resp.aread()
 
         # Make another request to see that the tunnel is re-used.
         resp = await proxy.request("GET", "https://example.com/target")
@@ -131,7 +131,7 @@ async def test_proxy_tunnel_start_tls():
         assert resp.request.url == "https://example.com/target"
         assert resp.request.headers["Host"] == "example.com"
 
-        await resp.read()
+        await resp.aread()
 
     recv = raw_io.received_data
     assert len(recv) == 5
index 3133e03dd48cc91e49791e76c9d84617a962619d..805b2a6593649b0395c8ad610accd08949c73b0a 100644 (file)
@@ -131,7 +131,7 @@ async def test_read_response():
     assert response.encoding == "ascii"
     assert response.is_closed
 
-    content = await response.read()
+    content = await response.aread()
 
     assert content == b"Hello, world!"
     assert response.content == b"Hello, world!"
@@ -162,7 +162,7 @@ async def test_bytes_interface():
 async def test_text_interface():
     response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)
 
-    await response.read()
+    await response.aread()
 
     content = ""
     async for part in response.aiter_text():
@@ -174,7 +174,7 @@ async def test_text_interface():
 async def test_lines_interface():
     response = httpx.Response(200, content=b"Hello,\nworld!", request=REQUEST)
 
-    await response.read()
+    await response.aread()
 
     content = []
     async for line in response.aiter_lines():
@@ -186,7 +186,7 @@ async def test_lines_interface():
 async def test_stream_interface_after_read():
     response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)
 
-    await response.read()
+    await response.aread()
 
     content = b""
     async for part in response.aiter_bytes():
@@ -202,7 +202,7 @@ async def test_streaming_response():
     assert response.status_code == 200
     assert not response.is_closed
 
-    content = await response.read()
+    content = await response.aread()
 
     assert content == b"Hello, world!"
     assert response.content == b"Hello, world!"
@@ -219,7 +219,7 @@ async def test_cannot_read_after_stream_consumed():
         content += part
 
     with pytest.raises(httpx.StreamConsumed):
-        await response.read()
+        await response.aread()
 
 
 @pytest.mark.asyncio
@@ -227,10 +227,10 @@ async def test_cannot_read_after_response_closed():
     stream = AsyncIteratorStream(aiterator=async_streaming_body())
     response = httpx.Response(200, stream=stream, request=REQUEST)
 
-    await response.close()
+    await response.aclose()
 
     with pytest.raises(httpx.ResponseClosed):
-        await response.read()
+        await response.aread()
 
 
 def test_unknown_status_code():
index 9624842b01e23cb8b2f3bf012ce87fa7e683ccad..1c658e91a024e879e2f3448bbef774856dcad351 100644 (file)
@@ -69,7 +69,7 @@ async def test_delete(server):
 @pytest.mark.asyncio
 async def test_stream(server):
     async with httpx.stream("GET", server.url) as response:
-        await response.read()
+        await response.aread()
 
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
index 307ab5b23e14020e42f9d26e25cce45fbd189dbc..5eb26d305ef70a31b47a534100142e08f49be6d0 100644 (file)
@@ -100,7 +100,7 @@ async def test_streaming():
     stream = AsyncIteratorStream(aiterator=compress(body))
     response = httpx.Response(200, headers=headers, stream=stream, request=REQUEST)
     assert not hasattr(response, "body")
-    assert await response.read() == body
+    assert await response.aread() == body
 
 
 @pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br", b"identity"))
@@ -157,7 +157,7 @@ async def test_text_decoder(data, encoding):
 
     stream = AsyncIteratorStream(aiterator=iterator())
     response = httpx.Response(200, stream=stream, request=REQUEST)
-    await response.read()
+    await response.aread()
     assert response.text == (b"".join(data)).decode(encoding)
 
 
@@ -176,7 +176,7 @@ async def test_text_decoder_known_encoding():
         request=REQUEST,
     )
 
-    await response.read()
+    await response.aread()
     assert "".join(response.text) == "トラベル"