Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@
the total elapsed seconds.
* `def .raise_for_status()` - **None**
* `def .json()` - **Any**
* `def .read()` - **bytes**
* `def .aread()` - **bytes**
* `def .stream_raw()` - **async bytes iterator**
* `def .stream_bytes()` - **async bytes iterator**
* `def .stream_text()` - **async text iterator**
* `def .stream_lines()` - **async text iterator**
* `def .close()` - **None**
* `def .aclose()` - **None**
* `def .next()` - **Response**

## `Request`
Expand Down
2 changes: 1 addition & 1 deletion docs/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

Expand Down
12 changes: 6 additions & 6 deletions httpx/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,9 @@ async def send(

if not stream:
try:
await response.read()
await response.aread()
finally:
await response.close()
await response.aclose()

return response

Expand Down Expand Up @@ -470,7 +470,7 @@ async def send_handling_redirects(
if not response.is_redirect:
return response

await response.read()
await response.aread()
request = self.build_redirect_request(request, response)
history = history + [response]

Expand Down Expand Up @@ -596,11 +596,11 @@ async def send_handling_auth(
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,
Expand Down Expand Up @@ -980,6 +980,6 @@ async def __aexit__(
exc_value: BaseException = None,
traceback: TracebackType = None,
) -> None:
await self.response.close()
await self.response.aclose()
if self.close_client:
await self.client.close()
2 changes: 1 addition & 1 deletion httpx/dispatch/proxy_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ async def request_tunnel_proxy_connection(self, origin: Origin) -> HTTPConnectio
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})",
Expand Down
29 changes: 24 additions & 5 deletions httpx/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,15 @@ def links(self) -> typing.Dict[typing.Optional[str], typing.Dict[str, str]]:
def __repr__(self) -> str:
return f"<Response [{self.status_code} {self.reason_phrase}]>"

async def read(self) -> bytes:
@property
def read(self) -> typing.Callable:
warnings.warn(
"Response.read() is due to be deprecated. Use Response.aread() instead.",
category=DeprecationWarning,
)
return self.aread

async def aread(self) -> bytes:
"""
Read and return the response content.
"""
Expand All @@ -862,15 +870,17 @@ async def read(self) -> bytes:
def stream(self): # type: ignore
warnings.warn(
"Response.stream() is due to be deprecated. "
"Use Response.aiter_bytes() instead."
"Use Response.aiter_bytes() instead.",
category=DeprecationWarning,
)
return self.aiter_bytes

@property
def raw(self): # type: ignore
warnings.warn(
"Response.raw() is due to be deprecated. "
"Use Response.aiter_raw() instead."
"Use Response.aiter_raw() instead.",
category=DeprecationWarning,
)
return self.aiter_raw

Expand Down Expand Up @@ -920,7 +930,7 @@ async def aiter_raw(self) -> typing.AsyncIterator[bytes]:
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":
"""
Expand All @@ -931,7 +941,16 @@ async def next(self) -> "Response":
assert self.call_next is not None
return await self.call_next()

async def close(self) -> None:
@property
def close(self) -> typing.Callable:
warnings.warn(
"Response.close() is due to be deprecated. "
"Use Response.aclose() instead.",
category=DeprecationWarning,
)
return self.aclose

async def aclose(self) -> None:
"""
Close the response and release the connection.
Automatically called if the response body is read to completion.
Expand Down
2 changes: 1 addition & 1 deletion tests/client/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand Down
2 changes: 1 addition & 1 deletion tests/client/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!"

Expand Down
40 changes: 20 additions & 20 deletions tests/dispatch/test_connection_pools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions tests/dispatch/test_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
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!"

Expand All @@ -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")
Expand All @@ -37,7 +37,7 @@ async def test_https_get_with_ssl_defaults(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!"

Expand All @@ -49,6 +49,6 @@ async def test_https_get_with_sll_overrides(https_server, ca_cert_pem_file):
"""
async with HTTPConnection(origin=https_server.url) as conn:
response = await conn.request("GET", https_server.url, verify=ca_cert_pem_file)
await response.read()
await response.aread()
assert response.status_code == 200
assert response.content == b"Hello, world!"
4 changes: 2 additions & 2 deletions tests/dispatch/test_proxy_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down
Loading