diff --git a/README.md b/README.md index 3c7a6ca9..977bfc60 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,21 @@ with ProoflineClient() as client: print(row) ``` +When page metadata and raw text are not needed, `iter_export_rows` yields decoded rows directly while fetching pages lazily: + +```python +with ProoflineClient() as client: + for row in client.iter_export_rows( + UUID(os.environ["PROOFLINE_ORGANIZATION_ID"]), + "reviews", + from_time="2026-10-01T00:00:00Z", + to_time="2026-10-25T00:00:00Z", + ): + print(row) +``` + +The async client exposes the same method as an async iterator. Both methods keep the page bound and cursor-cycle checks used by `iter_export_pages`. + Use `async for` with `AsyncProoflineClient`. Each yielded page retains its raw text, decoded rows, and metadata; CSV pages retain their individual headers. Pass `cursor=` to resume an export. Iteration stops only when the server omits the next cursor, including when an intermediate page has no rows. Empty or repeated continuation cursors raise `ProoflineError` with type `urn:proofline:sdk:invalid-pagination`, and the page that carries such a cursor is never yielded. The default bound is 100 pages; set `max_pages` to an integer from 1 to 1000. Exhausting that bound while a cursor remains raises `urn:proofline:sdk:pagination-limit`, so a partial export cannot silently appear complete. Stop iteration early when only a prefix is needed. ## Coverage uploads diff --git a/proofline/client.py b/proofline/client.py index dda559a3..5fb0b2e5 100644 --- a/proofline/client.py +++ b/proofline/client.py @@ -335,6 +335,31 @@ def iter_export_pages( return raise _pagination_limit("Export") + def iter_export_rows( + self, + organization_id: UUID, + dataset: ExportDataset, + *, + from_time: str, + to_time: str, + format: ExportFormat = "ndjson", + cursor: str | None = None, + group_by: str | None = None, + max_pages: int = 100, + ) -> Iterator[dict[str, Any]]: + """Stream decoded rows across pages without exposing page boundaries.""" + for page in self.iter_export_pages( + organization_id, + dataset, + from_time=from_time, + to_time=to_time, + format=format, + cursor=cursor, + group_by=group_by, + max_pages=max_pages, + ): + yield from page.rows + def list_reviews(self, organization_id: UUID, **filters: Unpack[ReviewFilters]) -> ReviewPage: return self._call(list_reviews.sync_detailed, ReviewPage, organization_id, **filters) @@ -705,6 +730,32 @@ async def iter_export_pages( return raise _pagination_limit("Export") + async def iter_export_rows( + self, + organization_id: UUID, + dataset: ExportDataset, + *, + from_time: str, + to_time: str, + format: ExportFormat = "ndjson", + cursor: str | None = None, + group_by: str | None = None, + max_pages: int = 100, + ) -> AsyncIterator[dict[str, Any]]: + """Stream decoded rows across pages without exposing page boundaries.""" + async for page in self.iter_export_pages( + organization_id, + dataset, + from_time=from_time, + to_time=to_time, + format=format, + cursor=cursor, + group_by=group_by, + max_pages=max_pages, + ): + for row in page.rows: + yield row + async def list_reviews( self, organization_id: UUID, **filters: Unpack[ReviewFilters] ) -> ReviewPage: diff --git a/tests/test_http.py b/tests/test_http.py index 0af9d60f..7c8741c8 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -1921,6 +1921,52 @@ def respond(request: Request, index: int) -> Reply: assert len(server.requests) == 2 +@MODES +async def test_export_row_iterator_flattens_pages_lazily(mode: str) -> None: + def respond(request: Request, index: int) -> Reply: + query = parse_qs(urlsplit(request.path).query) + assert query.get("cursor") == (["second"] if index else None) + headers = { + "Content-Type": "application/x-ndjson", + "X-Proofline-Export-Version": "1", + "X-Proofline-Export-Settled-Until": "2026-10-25T00:00:00Z", + } + if index == 0: + headers["X-Proofline-Export-Next-Cursor"] = "second" + body = ( + b'{"dataset_version":1,"claim":"first"}\n' + b'{"dataset_version":1,"claim":"second"}\n' + ) + else: + body = b'{"dataset_version":1,"claim":"third"}\n' + return Reply( + body=body, + headers=headers, + ) + + with Server(respond) as server: + async with session(mode, server.url) as client: + rows = client.iter_export_rows( + ORG, + "reviews", + from_time="2026-10-01T00:00:00Z", + to_time="2026-10-25T00:00:00Z", + ) + assert not server.requests + assert (await next_export(rows))["claim"] == "first" + assert len(server.requests) == 1 + assert (await next_export(rows))["claim"] == "second" + assert len(server.requests) == 1 + assert (await next_export(rows))["claim"] == "third" + if mode == "sync": + with pytest.raises(StopIteration): + next(rows) + else: + with pytest.raises(StopAsyncIteration): + await anext(rows) + assert len(server.requests) == 2 + + @MODES @pytest.mark.parametrize("failure", ["empty", "repeat", "resume", "cycle", "bound", "refused"]) async def test_export_iterator_refuses_invalid_continuations_and_bounds(