ItemPage Collection Utilities
Relevant source files
The following files were used as context for generating this wiki page:
ItemPage is a type-safe collection wrapper for paginated FACEIT API responses. It provides a high-level interface to interact with groups of models, offering functional programming utilities like mapping, filtering, and searching directly on the returned data.
The utility system bridges the gap between raw paginated JSON responses and structured Python objects. When the library operates in model mode, paginated endpoints return ItemPage instances instead of raw dictionaries, enabling developers to perform analysis and data manipulation without manual iteration.
Sources: README.md:21-23, src/faceit/models/item_page.py:41-45
Core Data Structures
The utility system uses three primary models to represent paginated data and its associated metadata.
Pagination Models
| Model | Purpose | Key Fields |
|---|---|---|
PaginationTimeRange | Defines a temporal window for the data. | start, to (TimestampMs) |
PaginationMetadata | Contains request parameters for the current page. | offset, limit, time_range |
ItemPage | The main container for a collection of items. | items, offset, limit, time_from, time_to |
Sources: src/faceit/models/item_page.py:27-58
Class Hierarchy
The following diagram illustrates the relationship between ItemPage and its metadata components.
Sources: src/faceit/models/item_page.py:27-65
Collection Operations
ItemPage implements several methods to manipulate and query the collection of items. These methods typically return a new ItemPage instance or a specific item, maintaining type safety throughout the operation.
Functional Utilities
- map: Applies a transformation function to every item in the collection and returns a new
ItemPagecontaining the results. - filter: Filters items based on a predicate function and returns a new
ItemPagewith the matching items. - merge: A class method that combines multiple
ItemPageinstances into a single collection, useful after gathering data from iterators.
Sources: src/faceit/models/item_page.py:108-115, src/faceit/models/item_page.py:121-123
Search and Retrieval
- find: Returns the first item where a specific attribute matches a given value. It supports nested property lookups.
- find_all: Returns a new
ItemPagecontaining all items that match the attribute-value criteria. - get_first / get_last: Safely retrieves the first or last item, returning a default value if the collection is empty.
- get_random: Returns a random item from the collection using non-cryptographic sampling.
Sources: src/faceit/models/item_page.py:75-106
Integration with API Pagination
The ItemPage utilities work in conjunction with SyncPageIterator and AsyncPageIterator to handle large datasets.
Data Flow
When a user calls a collection method (e.g., all_history), the system follows this flow:
- The iterator calls the raw resource method repeatedly.
- The
BaseResourcevalidates the response against a Pydantic model. - The results are wrapped in
ItemPageinstances. gather_from_iteratormerges individual pages into a finalItemPage.
Usage Example
The following logic demonstrates using collection utilities to analyze player performance.
# Fetches all CS2 matches stats as an ItemPage
matches = data.players.all_matches_stats(player_id, GameID.CS2)
# Use .filter() to find matches with positive K/D
positive_matches = matches.filter(lambda m: m.kd_ratio >= 1)
# Use .get_first() to safely get the most recent match
latest_match = matches.get_first()
# Calculate rate using len() on the collections
kd_rate = (len(positive_matches) / len(matches) * 100) if matches else 0
Sources: README.md:46-60, src/faceit/models/item_page.py:84-86, src/faceit/models/item_page.py:114-115
Technical Implementation Details
- Normalization: The
ItemPageclass includes a_normalize_itemsvalidator. It ensures that if the API returns items wrapped in single-key dictionaries (a common FACEIT API pattern), the inner object is extracted automatically. - Immutability: Methods like
filterandmapusemodel_constructvia_construct_without_metadata. This creates new instances without re-validating the underlying data, improving performance for collection-level operations. - Sequence Protocol:
ItemPageimplements__iter__,__len__,__getitem__, and__contains__. It behaves like a standard Python tuple while providing additional utility methods.
Sources: src/faceit/models/item_page.py:125-132, src/faceit/models/item_page.py:146-175, src/faceit/models/item_page.py:177-196
Summary
ItemPage Collection Utilities provide a type-safe and idiomatic way to handle paginated data in the FACEIT Python library. By encapsulating items within a model-aware container, the library allows for complex data filtering and retrieval operations directly on API results, abstracting away the complexities of raw JSON manipulation and manual pagination management.