Working with cursor-based pagination in the Avala SDK
The Avala SDK uses cursor-based pagination for list endpoints that may return large result sets. The CursorPage class provides a clean interface for iterating through paginated results.
The CursorPage class is a generic container defined in avala/_pagination.py:11-28 that wraps paginated API responses:
from avala import Clientclient = Client(api_key="your_api_key")page = client.projects.list(limit=10)print(f"Items in this page: {len(page)}")print(f"Has more results: {page.has_more}")print(f"Next cursor: {page.next_cursor}")
For fine-grained control, manually fetch pages using cursors:
from avala import Clientclient = Client(api_key="your_api_key")# Fetch the first pagepage = client.datasets.list(limit=25)while True: # Process items in the current page for dataset in page: print(f"Dataset: {dataset.name}") # Check if there are more results if not page.has_more: break # Fetch the next page using the cursor page = client.datasets.list( limit=25, cursor=page.next_cursor )
The cursor parameter is passed to the list method to fetch the next page. The SDK handles encoding and decoding of cursors automatically.
from avala import Clientclient = Client(api_key="your_api_key")all_tasks = []page = client.tasks.list(project_id="proj_123", limit=100)while True: all_tasks.extend(page.items) if not page.has_more: break page = client.tasks.list( project_id="proj_123", limit=100, cursor=page.next_cursor )print(f"Total tasks: {len(all_tasks)}")
Be cautious when fetching all results for large datasets. Consider using pagination with smaller page sizes and processing items incrementally to avoid memory issues.
from avala import Clientdef process_batch(items): """Process a batch of items.""" for item in items: # Your processing logic here print(f"Processing: {item.id}")client = Client(api_key="your_api_key")page = client.exports.list(project_id="proj_123", limit=50)while True: # Process the current page process_batch(page.items) # Move to the next page if not page.has_more: break page = client.exports.list( project_id="proj_123", limit=50, cursor=page.next_cursor )
The actual number of items returned may be less than the requested limit, even if there are more results available. Always check has_more to determine if pagination should continue.
def find_first_match(client, project_id, condition): page = client.tasks.list(project_id=project_id, limit=50) while True: for task in page: if condition(task): return task if not page.has_more: return None page = client.tasks.list( project_id=project_id, limit=50, cursor=page.next_cursor )