-
Notifications
You must be signed in to change notification settings - Fork 16
feat: Make list methods of clients iterable #769
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
Open
Pijukatel
wants to merge
1
commit into
master
Choose a base branch
from
make-list-methods-iterable
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| from typing import TYPE_CHECKING, Any, Generic, Protocol, TypeVar | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import AsyncIterator, Awaitable, Callable, Coroutine, Generator, Iterator | ||
|
|
||
| T = TypeVar('T') | ||
|
|
||
|
|
||
| class HasItems(Protocol[T]): | ||
| items: list[T] | ||
|
|
||
|
|
||
| def _min_for_limit_param(a: int | None, b: int | None) -> int | None: | ||
| """Return minimum of two limit parameters, treating `None` or `0` as infinity. | ||
|
|
||
| The Apify API treats `0` as no limit for the `limit` parameter, so `0` here means infinity. | ||
| Returns `None` when both inputs represent infinity. | ||
| """ | ||
| if a == 0: | ||
| a = None | ||
| if b == 0: | ||
| b = None | ||
| if a is None: | ||
| return b | ||
| if b is None: | ||
| return a | ||
| return min(a, b) | ||
|
|
||
|
|
||
| class _LazyTask(Generic[T]): | ||
| """Task that is created lazily upon awaiting. | ||
|
|
||
| This allows to reuse the same Task multiple times without the need to schedule the task when it is created. | ||
| """ | ||
|
|
||
| def __init__(self, awaitable: Coroutine[Any, Any, T]) -> None: | ||
| self._awaitable = awaitable | ||
| self._task: asyncio.Task[T] | None = None | ||
|
|
||
| def __await__(self) -> Generator[Any, None, T]: | ||
| if self._task is None: | ||
| self._task = asyncio.create_task(self._awaitable) | ||
| return (yield from self._task.__await__()) | ||
|
|
||
|
|
||
| def build_get_iterator( | ||
| callback: Callable[..., HasItems[T]], | ||
| first_page: HasItems[T], | ||
| **kwargs: Any, | ||
| ) -> Callable[[], Iterator[T]]: | ||
| """Build a factory for `Iterator` to yield items across paginated API calls. | ||
|
|
||
| The callback is invoked to lazy fetch items from API. | ||
|
|
||
| There are several optional kwargs that control the pagination, but not all are accepted on each paginated endpoint. | ||
| Some endpoints do not return all paginated metadata, so the implementation should be resilient to missing fields, | ||
| but it can use them if available. | ||
|
|
||
| The `total` field from the first page is not trusted for stopping iteration because it may change between calls; | ||
| iteration stops when a page has no items or when the user-requested `limit` has been reached. | ||
|
|
||
| The `count` field does not count objects returned, but objects scanned by the API. For example when using filters, | ||
| returned items can be smaller than `count`. Therefore, `count` should be used for correct offset calculation if | ||
| available. | ||
|
|
||
| Iteration relevant kwargs: | ||
| chunk_size: Maximum number of items requested per API call during iteration. Pass `0` | ||
| or `None` to let the API decide (effectively infinity). | ||
| limit: User-requested total item limit. Stops iteration once this many items are yielded. | ||
| offset: Starting offset for the first page. | ||
| **other: Passed through to the callback unchanged. | ||
| """ | ||
| chunk_size = kwargs.pop('chunk_size', 0) or 0 | ||
| offset = kwargs.get('offset') or 0 | ||
| limit = kwargs.get('limit') or 0 | ||
|
|
||
| def get_iterator() -> Iterator[T]: | ||
| current_page = first_page | ||
| yield from current_page.items | ||
|
|
||
| fetched_items = getattr(current_page, 'count', len(current_page.items)) | ||
| while current_page.items and (not limit or (limit > fetched_items)): | ||
| new_kwargs = { | ||
| **kwargs, | ||
| 'offset': offset + fetched_items, | ||
| 'limit': chunk_size if not limit else _min_for_limit_param(limit - fetched_items, chunk_size), | ||
| } | ||
| current_page = callback(**new_kwargs) | ||
| yield from current_page.items | ||
| fetched_items += getattr(current_page, 'count', len(current_page.items)) | ||
|
|
||
| return get_iterator | ||
|
|
||
|
|
||
| def build_get_iterator_async( | ||
| callback: Callable[..., Coroutine[Any, Any, HasItems[T]]], | ||
| fetch_first_page: Awaitable[HasItems[T]], | ||
| **kwargs: Any, | ||
| ) -> Callable[[], AsyncIterator[T]]: | ||
| """Build a factory for `AsyncIterator` to yield items across paginated API calls. | ||
|
|
||
| Mirrors `build_get_iterator` but for async callbacks. | ||
| """ | ||
| chunk_size = kwargs.pop('chunk_size', 0) or 0 | ||
| offset = kwargs.get('offset') or 0 | ||
| limit = kwargs.get('limit') or 0 | ||
|
|
||
| async def get_async_iterator() -> AsyncIterator[T]: | ||
| current_page = await fetch_first_page | ||
| for item in current_page.items: | ||
| yield item | ||
|
|
||
| fetched_items = getattr(current_page, 'count', len(current_page.items)) | ||
| while current_page.items and (not limit or (limit > fetched_items)): | ||
| new_kwargs = { | ||
| **kwargs, | ||
| 'offset': offset + fetched_items, | ||
| 'limit': chunk_size if not limit else _min_for_limit_param(limit - fetched_items, chunk_size), | ||
| } | ||
| current_page = await callback(**new_kwargs) | ||
| for item in current_page.items: | ||
| yield item | ||
| fetched_items += getattr(current_page, 'count', len(current_page.items)) | ||
|
|
||
| return get_async_iterator | ||
|
|
||
|
|
||
| def build_get_cursor_iterator( | ||
| callback: Callable[..., HasItems[T]], | ||
| first_page: HasItems[T], | ||
| *, | ||
| cursor_param: str, | ||
| limit: int | None = None, | ||
| chunk_size: int | None = None, | ||
| **kwargs: Any, | ||
| ) -> Callable[[], Iterator[T]]: | ||
| """Build a factory for `Iterator` to yield items across paginated API calls. | ||
|
|
||
| Mirrors `build_get_iterator` but with cursor based pagination. | ||
|
|
||
| The caller is responsible for fetching the first page (typically by calling `callback` with | ||
| the initial cursor). After each page, `getattr(page, f'next_{cursor_param}')` is consulted | ||
| to obtain the next cursor; returning `None` ends iteration. The iteration also stops when a | ||
| page is empty or when the caller-requested `limit` has been reached. | ||
| """ | ||
| effective_chunk = chunk_size or 0 | ||
| user_limit = limit or 0 | ||
|
|
||
| def get_iterator() -> Iterator[T]: | ||
| current_page = first_page | ||
| yield from current_page.items | ||
|
|
||
| fetched = len(current_page.items) | ||
| next_cursor = getattr(current_page, f'next_{cursor_param}') | ||
|
|
||
| while current_page.items and next_cursor is not None and (not user_limit or user_limit > fetched): | ||
| remaining = (user_limit - fetched) if user_limit else 0 | ||
| next_limit = effective_chunk if not user_limit else _min_for_limit_param(remaining, effective_chunk) | ||
| current_page = callback(**{**kwargs, cursor_param: next_cursor, 'limit': next_limit}) | ||
| yield from current_page.items | ||
| fetched += len(current_page.items) | ||
| next_cursor = getattr(current_page, f'next_{cursor_param}') | ||
|
|
||
| return get_iterator | ||
|
|
||
|
|
||
| def build_get_cursor_iterator_async( | ||
| callback: Callable[..., Coroutine[Any, Any, HasItems[T]]], | ||
| fetch_first_page: Awaitable[HasItems[T]], | ||
| *, | ||
| cursor_param: str, | ||
| limit: int | None = None, | ||
| chunk_size: int | None = None, | ||
| **kwargs: Any, | ||
| ) -> Callable[[], AsyncIterator[T]]: | ||
| """Build a factory for `Iterator` to yield items across paginated API calls. | ||
|
|
||
| Mirrors `build_get_cursor_iterator` but for async callbacks. | ||
| """ | ||
| effective_chunk = chunk_size or 0 | ||
| user_limit = limit or 0 | ||
|
|
||
| async def get_async_iterator() -> AsyncIterator[T]: | ||
| current_page = await fetch_first_page | ||
| for item in current_page.items: | ||
| yield item | ||
|
|
||
| fetched = len(current_page.items) | ||
| next_cursor = getattr(current_page, f'next_{cursor_param}') | ||
|
|
||
| while current_page.items and next_cursor is not None and (not user_limit or user_limit > fetched): | ||
| remaining = (user_limit - fetched) if user_limit else 0 | ||
| next_limit = effective_chunk if not user_limit else _min_for_limit_param(remaining, effective_chunk) | ||
| current_page = await callback(**{**kwargs, cursor_param: next_cursor, 'limit': next_limit}) | ||
| for item in current_page.items: | ||
| yield item | ||
| fetched += len(current_page.items) | ||
| next_cursor = getattr(current_page, f'next_{cursor_param}') | ||
|
|
||
| return get_async_iterator |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still have
ListPage? Could you ask Claude to also review and update the docs?