Skip to content
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
30 changes: 29 additions & 1 deletion docs/02_concepts/08_pagination.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import IterateItemsAsyncExample from '!!raw-loader!./code/08_iterate_items_async
import IterateItemsSyncExample from '!!raw-loader!./code/08_iterate_items_sync.py';
import IterateCollectionAsyncExample from '!!raw-loader!./code/08_iterate_collection_async.py';
import IterateCollectionSyncExample from '!!raw-loader!./code/08_iterate_collection_sync.py';
import StoreSearchAsyncExample from '!!raw-loader!./code/08_store_search_async.py';
import StoreSearchSyncExample from '!!raw-loader!./code/08_store_search_sync.py';

Most methods named `list` or `list_something` in the Apify client return a page model — a [Pydantic](https://docs.pydantic.dev/latest/) model such as <ApiLink to="class/ListOfActors">`ListOfActors`</ApiLink> or <ApiLink to="class/ListOfDatasets">`ListOfDatasets`</ApiLink>. Unstructured dataset items use the <ApiLink to="class/DatasetItemsPage">`DatasetItemsPage`</ApiLink> dataclass instead. All page models share a consistent interface for working with paginated data and expose the following fields:

Expand All @@ -27,7 +29,7 @@ Most methods named `list` or `list_something` in the Apify client return a page

On <ApiLink to="class/DatasetItemsPage">`DatasetItemsPage`</ApiLink>, `count` reports how many dataset rows the API scanned for the page. Filters drop items from a page and `unwind` multiplies them, so a page can hold fewer or more items than `count`.

Some methods paginate differently. For example, <ApiLink to="class/RequestQueueClient#list_requests">`RequestQueueClient.list_requests`</ApiLink> returns a cursor-based <ApiLink to="class/ListOfRequests">`ListOfRequests`</ApiLink> without the `total`, `offset`, and `count` fields. To fetch the next page, pass its `next_cursor` value back as the `cursor` parameter. Other examples include `list_keys` and `list_head`. Regardless, the primary results are always stored under the `items` field, and the `limit` field can be used to control the number of results returned.
Some methods paginate differently. For example, <ApiLink to="class/RequestQueueClient#list_requests">`RequestQueueClient.list_requests`</ApiLink> returns a cursor-based <ApiLink to="class/ListOfRequests">`ListOfRequests`</ApiLink> without the `total`, `offset`, and `count` fields. To fetch the next page, pass its `next_cursor` value back as the `cursor` parameter. Other examples include `list_keys` and `list_head`. <ApiLink to="class/StoreCollectionClient#search">`StoreCollectionClient.search`</ApiLink> doesn't paginate at all. For details, see [Search Apify Store](#search-apify-store). Regardless, the primary results are always stored under the `items` field, and the `limit` field can be used to control the number of results returned.

The following example shows how to fetch all items from a dataset using pagination:

Expand Down Expand Up @@ -89,3 +91,29 @@ The next example uses `iterate_items` on a dataset client to stream items past a
</Tabs>

To read the items of a run that's still going, use <ApiLink to="class/RunClient#iterate_dataset_items">`RunClient.iterate_dataset_items`</ApiLink>. Its `offset` and `limit` also count dataset rows. For details, see [Retrieve Actor data](/api/client/python/docs/guides/retrieve-actor-data#read-items-while-the-run-is-going).

## Search Apify Store

<ApiLink to="class/StoreCollectionClient#list">`StoreCollectionClient.list`</ApiLink> and <ApiLink to="class/StoreCollectionClient#iterate">`StoreCollectionClient.iterate`</ApiLink> go through all Actors in [Apify Store](https://apify.com/store), page by page, in Store order. To find Actors by a text query, use <ApiLink to="class/StoreCollectionClient#search">`StoreCollectionClient.search`</ApiLink> instead.

A search isn't paginated. The API returns the Actors that match the query best as a single page of at most 100 Actors, ordered by relevance. There is no next page and no `offset`, and the API doesn't report how many Actors match in total. The method therefore returns a <ApiLink to="class/StoreSearchResult">`StoreSearchResult`</ApiLink> with two fields:

- `items` - The matching Actors, best match first.
- `limit` - The limit the API applied. It's the `limit` you passed, or 100 if you passed more or none.

When `items` holds as many Actors as `limit`, more Actors may match than were returned. To narrow the result, use a more specific query or the `category`, `username`, and `pricing_model` filters.

<Tabs>
<TabItem value="AsyncExample" label="Async client" default>
<CodeBlock className="language-python">
{StoreSearchAsyncExample}
</CodeBlock>
</TabItem>
<TabItem value="SyncExample" label="Sync client">
<CodeBlock className="language-python">
{StoreSearchSyncExample}
</CodeBlock>
</TabItem>
</Tabs>

The `search` and `sort_by` arguments of `list` and `iterate` are deprecated and will be removed in v4. The API ignores `sort_by`, and `search` moved to the `search` method.
17 changes: 17 additions & 0 deletions docs/02_concepts/code/08_store_search_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from apify_client import ApifyClientAsync

TOKEN = 'MY-APIFY-TOKEN'


async def main() -> None:
apify_client = ApifyClientAsync(TOKEN)

# Search returns the best matches as a single page of at most 100 Actors.
result = await apify_client.store().search('instagram posts', limit=10)

for actor in result.items:
print(f'{actor.username}/{actor.name}')

# With as many Actors as the applied limit, more Actors may match the query.
if len(result.items) == result.limit:
print('The result may be cut off. Narrow the search with a filter.')
21 changes: 21 additions & 0 deletions docs/02_concepts/code/08_store_search_sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from apify_client import ApifyClient

TOKEN = 'MY-APIFY-TOKEN'


def main() -> None:
apify_client = ApifyClient(TOKEN)

# Search returns the best matches as a single page of at most 100 Actors.
result = apify_client.store().search('instagram posts', limit=10)

for actor in result.items:
print(f'{actor.username}/{actor.name}')

# With as many Actors as the applied limit, more Actors may match the query.
if len(result.items) == result.limit:
print('The result may be cut off. Narrow the search with a filter.')


if __name__ == '__main__':
main()
4 changes: 2 additions & 2 deletions src/apify_client/_apify_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ def user(self, user_id: str | None = None) -> UserClient:
return UserClient(resource_id=user_id, **self._base_kwargs)

def store(self) -> StoreCollectionClient:
"""Get the sub-client for the Apify Store, allowing to list Actors published in the store."""
"""Get the sub-client for the Apify Store, allowing to list and search Actors published in the Store."""
return StoreCollectionClient(**self._base_kwargs)


Expand Down Expand Up @@ -854,5 +854,5 @@ def user(self, user_id: str | None = None) -> UserClientAsync:
return UserClientAsync(resource_id=user_id, **self._base_kwargs)

def store(self) -> StoreCollectionClientAsync:
"""Get the sub-client for the Apify Store, allowing to list Actors published in the store."""
"""Get the sub-client for the Apify Store, allowing to list and search Actors published in the Store."""
return StoreCollectionClientAsync(**self._base_kwargs)
Loading
Loading