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

Fix AsyncResponse.iter_lines() #182

Merged
merged 6 commits into from
Nov 22, 2024
Merged
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
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Release History
===============

3.11.1 (2024-11-22)
-------------------

**Fixed**
- async version of ``iter_line``. (#182)

3.11.0 (2024-11-20)
-------------------

Expand Down
16 changes: 16 additions & 0 deletions docs/user/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,22 @@ Delaying the content consumption in an async context can be easily achieved usin

asyncio.run(main())

Or using the ``iter_line`` method as such::

import niquests
import asyncio

async def main() -> None:

async with niquests.AsyncSession() as s:
r = await s.get("https://pie.dev/get", stream=True)

async for chunk in r.iter_line():
print(chunk)

if __name__ == "__main__":
asyncio.run(main())

Or simply by doing::

import niquests
Expand Down
4 changes: 2 additions & 2 deletions src/niquests/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
__url__: str = "https://niquests.readthedocs.io"

__version__: str
__version__ = "3.11.0"
__version__ = "3.11.1"

__build__: int = 0x031100
__build__: int = 0x031101
__author__: str = "Kenneth Reitz"
__author_email__: str = "[email protected]"
__license__: str = "Apache-2.0"
Expand Down
4 changes: 2 additions & 2 deletions src/niquests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,7 @@ async def iter_lines( # type: ignore[misc]

pending = None

async for chunk in self.iter_content( # type: ignore[call-overload]
async for chunk in await self.iter_content( # type: ignore[call-overload]
chunk_size=chunk_size, decode_unicode=decode_unicode
):
if pending is not None:
Expand All @@ -1816,7 +1816,7 @@ async def iter_lines( # type: ignore[misc]
else:
pending = None

async for line in lines:
for line in lines:
yield line

if pending is not None:
Expand Down
45 changes: 23 additions & 22 deletions tests/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,28 +106,29 @@ async def callback_on_early(early_resp) -> None:
assert resp.status_code == 200
assert received_early_response is True

# async def test_http_trailer_preload(self) -> None:
# async with AsyncSession() as s:
# r = await s.get("https://httpbingo.org/trailers?foo=baz")
#
# assert r.ok
# assert r.trailers
# assert "foo" in r.trailers
# assert r.trailers["foo"] == "baz"
#
# async def test_http_trailer_no_preload(self) -> None:
# async with AsyncSession() as s:
# r = await s.get("https://httpbingo.org/trailers?foo=baz", stream=True)
#
# assert r.ok
# assert not r.trailers
# assert "foo" not in r.trailers
#
# await r.content
#
# assert r.trailers
# assert "foo" in r.trailers
# assert r.trailers["foo"] == "baz"
async def test_iter_line(self) -> None:
async with AsyncSession() as s:
r = await s.get("https://httpbingo.org/html", stream=True)
content = b""

async for line in r.iter_lines():
assert isinstance(line, bytes)
content += line

assert content
assert b"Herman Melville - Moby-Dick" in content

async def test_iter_line_decode(self) -> None:
async with AsyncSession() as s:
r = await s.get("https://httpbingo.org/html", stream=True)
content = ""

async for line in r.iter_lines(decode_unicode=True):
assert isinstance(line, str)
content += line

assert content
assert "Herman Melville - Moby-Dick" in content


@pytest.mark.usefixtures("requires_wan")
Expand Down
Loading