Advanced Pagination System
Relevant source files
The following files were used as context for generating this wiki page:
The Advanced Pagination System manages data retrieval for large datasets from the FACEIT API. It provides high-level abstractions for offset-based and Unix timestamp-based iteration, supporting both synchronous and asynchronous execution. The system ensures type safety by wrapping API responses in Pydantic-based models and offers utility methods for collection processing, such as filtering and mapping.
Sources: src/faceit/api/pagination.py:126-140, README.md:20-25
Core Architecture
The system centers on BasePageIterator, which implements the logic for traversing paginated results. It uses metadata from FACEIT resource methods to determine limits and offsets.
Pagination Components
The system consists of several key classes and types that handle the iteration state and data conversion.
| Component | Description |
|---|---|
BasePageIterator | The abstract base class managing iteration state, limits, and exhausted status. |
SyncPageIterator | A final class for synchronous iteration and collection of results. |
AsyncPageIterator | A final class for asynchronous iteration using __anext__. |
ItemPage | A Pydantic model representing a single page of results with utility methods. |
pages | A specialized integer type requiring a value greater than 1 to specify page counts. |
Sources: src/faceit/api/pagination.py:75-87, src/faceit/api/pagination.py:141-160, src/faceit/models/item_page.py:34-45
Data Flow for Offset Pagination
The following diagram illustrates how the system requests consecutive pages using offset and limit parameters.
Unix Timestamp Pagination
For endpoints that support time-based ranges (like player match history), the system implements a unix method. This method automatically manages start and to parameters by extracting timestamps from the last item of the previous page to fetch subsequent results.
Timestamp Configuration
To enable Unix pagination, a TimestampPaginationConfig must be defined for the specific resource method.
# Example configuration for player match statistics
_matches_stats_timestamp_cfg = TimestampPaginationConfig(
key="stats.Match Finished At",
attr="finished_at"
)
Sources: src/faceit/api/data/players.py:76-81, src/faceit/api/pagination.py:58-62
Unix Iteration Logic
The sequence diagram below shows how the iterator updates time boundaries between requests.
ItemPage Collection Utilities
The ItemPage class serves as a container for paginated items. It provides a suite of methods to interact with the data without converting it to a standard list first.
Key Methods
map(func): Transforms every item in the page using the provided function.filter(predicate): Returns a newItemPagecontaining only items that satisfy the predicate.find(attr, value): Searches for the first item where the specified attribute matches the value.get_first()/get_last(): Retrieves items at the boundaries of the page.merge(pages): A class method that combines multipleItemPageinstances into one.
Sources: src/faceit/models/item_page.py:92-125, src/faceit/models/item_page.py:133-140
Pagination Constraints and Validation
The system strictly validates parameters to prevent API errors, specifically ensuring the offset is a multiple of the limit.
Limit and Offset Rules
| Parameter | Validation Rule | Source |
|---|---|---|
limit | Must be a positive integer within the resource's allowed range (e.g., 1-100). | src/faceit/api/data/players.py:140 |
offset | Must be a non-negative integer and a multiple of the limit. | src/faceit/api/pagination.py:241-255 |
max_items | Defaults to 2000. Warns if "safe" limit (100 pages) is exceeded. | src/faceit/api/pagination.py:155-156 |
The system calculates an _effective_limit for the last page to ensure the offset % limit == 0 constraint required by the FACEIT API is met.
Sources: src/faceit/api/pagination.py:241-260, src/faceit/api/pagination.py:307-320
Result Collection and Deduplication
The collect() method on iterators gathers all pages into a final structure. By default, it performs deduplication of items, which is critical when using Unix pagination as overlapping timestamps can occur at page boundaries.
# Synchronous collection with deduplication
iterator = SyncPageIterator(data.players.bans, player_id, max_items=100)
results = iterator.collect(deduplicate=True)
Sources: src/faceit/api/pagination.py:393-415, src/faceit/api/pagination.py:355-365
The system uses deduplicate_unhashable to handle complex API objects and models during the collection process.
Sources: src/faceit/api/pagination.py:367-372, tests/test_pagination.py:140-145
The Advanced Pagination System enables robust data fetching by handling the complexities of API state, time-based boundaries, and result merging. This allows developers to focus on data processing using the provided ItemPage utility methods.
Conclusion Sources: README.md:20-25, src/faceit/api/pagination.py:141-145