Skip to content

Commit

Permalink
Drop .read/.aread from SyncByteStream/AsyncByteStream (#2407)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchristie committed Nov 7, 2022
1 parent 9f9deea commit 8752e26
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 40 deletions.
8 changes: 2 additions & 6 deletions httpx/_content.py
Expand Up @@ -52,9 +52,7 @@ def __iter__(self) -> Iterator[bytes]:
raise StreamConsumed()

self._is_stream_consumed = True
if hasattr(self._stream, "read") and not isinstance(
self._stream, SyncByteStream
):
if hasattr(self._stream, "read"):
# File-like interfaces should use 'read' directly.
chunk = self._stream.read(self.CHUNK_SIZE) # type: ignore
while chunk:
Expand All @@ -79,9 +77,7 @@ async def __aiter__(self) -> AsyncIterator[bytes]:
raise StreamConsumed()

self._is_stream_consumed = True
if hasattr(self._stream, "aread") and not isinstance(
self._stream, AsyncByteStream
):
if hasattr(self._stream, "aread"):
# File-like interfaces should use 'aread' directly.
chunk = await self._stream.aread(self.CHUNK_SIZE) # type: ignore
while chunk:
Expand Down
32 changes: 0 additions & 32 deletions httpx/_types.py
Expand Up @@ -107,33 +107,7 @@ def close(self) -> None:
"""
Subclasses can override this method to release any network resources
after a request/response cycle is complete.
Streaming cases should use a `try...finally` block to ensure that
the stream `close()` method is always called.
Example:
status_code, headers, stream, extensions = transport.handle_request(...)
try:
...
finally:
stream.close()
"""

def read(self) -> bytes:
"""
Simple cases can use `.read()` as a convenience method for consuming
the entire stream and then closing it.
Example:
status_code, headers, stream, extensions = transport.handle_request(...)
body = stream.read()
"""
try:
return b"".join([part for part in self])
finally:
self.close()


class AsyncByteStream:
Expand All @@ -145,9 +119,3 @@ async def __aiter__(self) -> AsyncIterator[bytes]:

async def aclose(self) -> None:
pass

async def aread(self) -> bytes:
try:
return b"".join([part async for part in self])
finally:
await self.aclose()
4 changes: 2 additions & 2 deletions tests/test_content.py
Expand Up @@ -13,8 +13,8 @@ async def test_empty_content():
assert isinstance(stream, httpx.SyncByteStream)
assert isinstance(stream, httpx.AsyncByteStream)

sync_content = stream.read()
async_content = await stream.aread()
sync_content = b"".join([part for part in stream])
async_content = b"".join([part async for part in stream])

assert headers == {}
assert sync_content == b""
Expand Down

0 comments on commit 8752e26

Please sign in to comment.