Skip to content

Commit 1d1e9f6

Browse files
authored
feat: add flatten to dataset stream_items (#1106)
`DatasetClient.stream_items()` and its async twin now accept `flatten`, which `get_items_as_bytes()` already supports on the same export endpoint. Two unit tests check that the value reaches the API as a comma-separated query parameter. Closes: #1100 *✍️ Drafted by Claude Code*
1 parent 8903ea0 commit 1d1e9f6

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

‎src/apify_client/_resource_clients/dataset.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ def stream_items(
402402
skip_hidden: bool | None = None,
403403
xml_root: str | None = None,
404404
xml_row: str | None = None,
405+
flatten: list[str] | None = None,
405406
signature: str | None = None,
406407
timeout: Timeout = 'long',
407408
) -> Iterator[HttpResponse]:
@@ -445,6 +446,7 @@ def stream_items(
445446
xml_root: Overrides default root element name of xml output. By default the root element is items.
446447
xml_row: Overrides default element name that wraps each page or page function result object in xml output.
447448
By default the element name is item.
449+
flatten: A list of fields that should be flattened.
448450
signature: Signature used to access the items.
449451
timeout: Timeout for the API HTTP request.
450452
@@ -469,6 +471,7 @@ def stream_items(
469471
skipHidden=skip_hidden,
470472
xmlRoot=xml_root,
471473
xmlRow=xml_row,
474+
flatten=flatten,
472475
signature=signature,
473476
)
474477

@@ -964,6 +967,7 @@ async def stream_items(
964967
skip_hidden: bool | None = None,
965968
xml_root: str | None = None,
966969
xml_row: str | None = None,
970+
flatten: list[str] | None = None,
967971
signature: str | None = None,
968972
timeout: Timeout = 'long',
969973
) -> AsyncIterator[HttpResponse]:
@@ -1007,6 +1011,7 @@ async def stream_items(
10071011
xml_root: Overrides default root element name of xml output. By default the root element is items.
10081012
xml_row: Overrides default element name that wraps each page or page function result object in xml output.
10091013
By default the element name is item.
1014+
flatten: A list of fields that should be flattened.
10101015
signature: Signature used to access the items.
10111016
timeout: Timeout for the API HTTP request.
10121017
@@ -1031,6 +1036,7 @@ async def stream_items(
10311036
skipHidden=skip_hidden,
10321037
xmlRoot=xml_root,
10331038
xmlRow=xml_row,
1039+
flatten=flatten,
10341040
signature=signature,
10351041
)
10361042

‎tests/unit/test_client_streaming.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,28 @@ async def test_dataset_stream_items_async(
5353
assert await response.aread() == STREAM_CONTENT
5454

5555

56+
def test_dataset_stream_items_flatten_sync(httpserver: HTTPServer) -> None:
57+
"""`flatten` is sent as a comma-separated query parameter."""
58+
httpserver.expect_request(
59+
f'/v2/datasets/{DATASET_ID}/items', query_string={'format': 'json', 'flatten': 'a,b.c'}
60+
).respond_with_data(STREAM_CONTENT)
61+
client = ApifyClient(token='test', api_url=httpserver.url_for('/').removesuffix('/'))
62+
63+
with client.dataset(DATASET_ID).stream_items(flatten=['a', 'b.c']) as response:
64+
assert response.read() == STREAM_CONTENT
65+
66+
67+
async def test_dataset_stream_items_flatten_async(httpserver: HTTPServer) -> None:
68+
"""`flatten` is sent as a comma-separated query parameter."""
69+
httpserver.expect_request(
70+
f'/v2/datasets/{DATASET_ID}/items', query_string={'format': 'json', 'flatten': 'a,b.c'}
71+
).respond_with_data(STREAM_CONTENT)
72+
client = ApifyClientAsync(token='test', api_url=httpserver.url_for('/').removesuffix('/'))
73+
74+
async with client.dataset(DATASET_ID).stream_items(flatten=['a', 'b.c']) as response:
75+
assert await response.aread() == STREAM_CONTENT
76+
77+
5678
def test_key_value_store_stream_record_sync(
5779
httpserver: HTTPServer,
5880
http_client_class: type[HttpClient],

0 commit comments

Comments
 (0)