Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ClientResponse class support methods iter_chunked and iter_chunks. #7455

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/5324.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ClientResponse class support methods iter_chunked and iter_chunks.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ Eugene Nikolaiev
Eugene Tolmachev
Evan Kepner
Evert Lammerts
fatelei
Felix Yan
Fernanda Guimarães
FichteFoll
Expand Down
13 changes: 13 additions & 0 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from typing import (
TYPE_CHECKING,
Any,
AsyncIterator,
Dict,
Iterable,
List,
Expand Down Expand Up @@ -986,6 +987,18 @@ async def wait_for_close(self) -> None:
self._writer = None
self.release()

async def iter_chunked(self, n) -> AsyncIterator[bytes]:
async for data in self.content.iter_chunked(n):
yield data
for trace in self._traces:
await trace.send_response_chunk_received(self.method, self.url, data)

async def iter_chunks(self) -> AsyncIterator[bytes]:
async for data, _ in self.content.iter_chunks():
yield data
for trace in self._traces:
await trace.send_response_chunk_received(self.method, self.url, data)

async def read(self) -> bytes:
"""Read response payload."""
if self._body is None:
Expand Down
42 changes: 42 additions & 0 deletions tests/test_client_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -1265,3 +1265,45 @@ def test_response_not_closed_after_get_ok(mocker) -> None:
assert not response.ok
assert not response.closed
assert spy.call_count == 0


async def test_iter_chunks(loop: Any, session: Any) -> None:
response = ClientResponse(
"get",
URL("http://def-cl-resp.org"),
request_info=mock.Mock(),
writer=mock.Mock(),
continue100=None,
timer=TimerNoop(),
traces=[],
loop=loop,
session=session,
)

content = response.content = mock.MagicMock()
content.iter_chunks.__aiter__.return_value = [b"payload"]

async for data in response.iter_chunked(1):
assert data == b"payload"
assert response._connection is None


async def test_iter_chunked(loop: Any, session: Any) -> None:
response = ClientResponse(
"get",
URL("http://def-cl-resp.org"),
request_info=mock.Mock(),
writer=mock.Mock(),
continue100=None,
timer=TimerNoop(),
traces=[],
loop=loop,
session=session,
)

content = response.content = mock.MagicMock()
content.iter_chunked.__aiter__.return_value = [b"payload"]

async for data in response.iter_chunked(1):
assert data == b"payload"
assert response._connection is None