Implement auto-pagination iterators for all endpoints
Implementation:
- Added iter_all() method to all sync endpoints
- PagesEndpoint.iter_all() - automatic pagination for pages
- UsersEndpoint.iter_all() - automatic pagination for users
- GroupsEndpoint.iter_all() - iterate over all groups
- AssetsEndpoint.iter_all() - iterate over all assets
- Added async iter_all() to all async endpoints
- AsyncPagesEndpoint - async generator with pagination
- AsyncUsersEndpoint - async generator with pagination
- AsyncGroupsEndpoint - async iterator
- AsyncAssetsEndpoint - async iterator
Features:
- Automatic batch fetching (configurable batch size, default: 50)
- Transparent pagination - users don't manage offsets
- Memory efficient - fetches data in chunks
- Filtering support - pass through all filter parameters
- Consistent interface across all endpoints
Usage:
# Sync iteration
for page in client.pages.iter_all(batch_size=100):
print(page.title)
# Async iteration
async for user in client.users.iter_all():
print(user.name)
Tests:
- 7 comprehensive pagination tests
- Single batch, multiple batch, and empty result scenarios
- Both sync and async iterator testing
- All tests passing (100%)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -112,7 +112,11 @@ class UsersEndpoint(BaseEndpoint):
|
||||
# Make request
|
||||
response = self._post(
|
||||
"/graphql",
|
||||
json_data={"query": query, "variables": variables} if variables else {"query": query},
|
||||
json_data=(
|
||||
{"query": query, "variables": variables}
|
||||
if variables
|
||||
else {"query": query}
|
||||
),
|
||||
)
|
||||
|
||||
# Parse response
|
||||
@@ -568,3 +572,46 @@ class UsersEndpoint(BaseEndpoint):
|
||||
normalized["groups"] = []
|
||||
|
||||
return normalized
|
||||
|
||||
def iter_all(
|
||||
self,
|
||||
batch_size: int = 50,
|
||||
search: Optional[str] = None,
|
||||
order_by: str = "name",
|
||||
order_direction: str = "ASC",
|
||||
):
|
||||
"""Iterate over all users with automatic pagination.
|
||||
|
||||
Args:
|
||||
batch_size: Number of users to fetch per request (default: 50)
|
||||
search: Search term to filter users
|
||||
order_by: Field to sort by
|
||||
order_direction: Sort direction (ASC or DESC)
|
||||
|
||||
Yields:
|
||||
User objects one at a time
|
||||
|
||||
Example:
|
||||
>>> for user in client.users.iter_all():
|
||||
... print(f"{user.name} ({user.email})")
|
||||
"""
|
||||
offset = 0
|
||||
while True:
|
||||
batch = self.list(
|
||||
limit=batch_size,
|
||||
offset=offset,
|
||||
search=search,
|
||||
order_by=order_by,
|
||||
order_direction=order_direction,
|
||||
)
|
||||
|
||||
if not batch:
|
||||
break
|
||||
|
||||
for user in batch:
|
||||
yield user
|
||||
|
||||
if len(batch) < batch_size:
|
||||
break
|
||||
|
||||
offset += batch_size
|
||||
|
||||
Reference in New Issue
Block a user