Introduction
Relevant source files
The following files were used as context for generating this wiki page:
The faceit-python library provides a type-safe, idiomatic Python interface for interacting with the FACEIT API. It allows developers to access player statistics, match history, and tournament data through both synchronous and asynchronous clients. The library uses Pydantic for data validation and httpx for network communication, ensuring that all data returned from the API conforms to defined Python models.
Sources: README.md:1-20, src/faceit/init.py
Core Architecture
The library organizes its functionality into "Resources" that aggregate various API endpoints. The primary entry point is the DataResource, which currently supports the FACEIT Data API. This resource is available in two flavors: SyncDataResource for synchronous blocking code and AsyncDataResource for asynchronous event-loop based applications.
Resource Aggregation
The library uses a decorator pattern to aggregate specific API modules (players, matches, teams, etc.) into a single resource object. Each sub-resource (e.g., SyncPlayers) can return data in two formats:
- Model Mode: Returns parsed Pydantic models (e.g.,
data.players.get(...)). - Raw Mode: Returns the original dictionary data from the API (e.g.,
data.raw_players.get(...)).
Sources: src/faceit/api/data/init.py:57-119, src/faceit/api/base.py:65-75
The diagram shows the relationship between the top-level resource aggregator, specific sub-resources, and the underlying HTTP layer.
HTTP Client and Authentication
The library manages network requests through SyncClient and AsyncClient wrappers around httpx. It implements robust error handling, retries using the tenacity library, and adaptive rate limiting for asynchronous requests.
Authentication
Interaction with the FACEIT Data API requires a valid API key. The library supports three methods for key handling:
- Automatic: Loading
FACEIT_API_KEYfrom environment variables viapython-decouple. - Manual: Passing the key directly to the resource constructor.
- Custom Variable: Using
FromEnv("VARIABLE_NAME")to specify a different environment key.
Sources: README.md:55-65, src/faceit/http/client.py:61-125
Reliability Features
- Retries: The client automatically retries failed requests caused by timeouts, connection errors, or retryable HTTP status codes (e.g., 5xx).
- Adaptive Rate Limiting: The
AsyncClientmonitors SSL errors and reduces concurrent connections if a threshold is reached, gradually recovering once stability is detected.
Sources: src/faceit/http/client.py:270-350, src/faceit/http/helpers.py
Data Modeling and Pagination
All API responses in model mode are validated against Pydantic models. For endpoints that return lists of data, the library uses the ItemPage collection.
ItemPage Utility
The ItemPage model wraps paginated responses and provides utility methods for data manipulation:
.filter(predicate): Returns a new page with items matching the condition..map(func): Transforms items within the page..find(attr, value): Locates the first item matching a specific attribute value..get_random(): Returns a random item from the collection.
Sources: src/faceit/models/item_page.py:70-135
Advanced Pagination
The library abstracts complex API pagination logic through SyncPageIterator and AsyncPageIterator. It supports two primary modes:
- Offset-based: Standard pagination using
offsetandlimit. - Unix Timestamp-based: Useful for history endpoints where data is queried between time ranges.
Sources: src/faceit/api/pagination.py:175-230, src/faceit/api/pagination.py:465-510
| Feature | Description |
|---|---|
| Type Safety | Full compatibility with mypy and static type checkers. |
| Pydantic Models | Automatic validation and serialization of API data. |
| Sync/Async | Dual support for synchronous and asynchronous environments. |
| Adaptive Limits | Dynamic adjustment of concurrent requests based on API health. |
| Iterators | High-level helpers to collect all pages of a resource automatically. |
Sources: README.md:23-35, src/faceit/http/client.py:355-385
Supported Games and Skill Levels
The library includes built-in constants for FACEIT-supported games and implements logic for skill level (ELO) calculations.
Skill Level Logic
The SkillLevel class provides methods to calculate progress toward the next rank. It includes pre-defined ELO thresholds for games like CS2 and CS:GO.
- Progress Calculation: Determines the percentage of progress within a specific level based on current ELO.
- Level Retrieval: Allows fetching a
SkillLevelobject by either its rank number (1-10) or a specific ELO value.
Sources: src/faceit/constants.py:155-250
Conclusion
The faceit-python library acts as a comprehensive middleware that bridges raw FACEIT API endpoints with structured Python code. By combining Pydantic validation, flexible HTTP clients, and advanced pagination iterators, it simplifies the development of tools requiring high-quality FACEIT data analysis.
Sources: README.md:70-80, src/faceit/api/data/init.py