|
| 1 | +# Pydantic Examples |
| 2 | + |
| 3 | +This folder contains examples demonstrating how to use PlayerDataPy with generated Pydantic types via the `PlayerDataAPI` class. This approach provides type safety and autocomplete support through code-generated models. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The `PlayerDataAPI` class wraps the GraphQL client and provides a high-level interface for building queries using Pydantic models. These models are auto-generated by `ariadne-codegen` and provide type-safe query construction. |
| 8 | + |
| 9 | +## Quick Start |
| 10 | + |
| 11 | +The simplest way to get started is with the `quick_start.py` example: |
| 12 | + |
| 13 | +```bash |
| 14 | +python examples/pydantic/quick_start.py |
| 15 | +``` |
| 16 | + |
| 17 | +Before running, set the following environment variables: |
| 18 | + |
| 19 | +```bash |
| 20 | +export CLIENT_ID=your_client_id |
| 21 | +export CLIENT_SECRET=your_client_secret |
| 22 | +export CLUB_ID=your_club_id |
| 23 | +``` |
| 24 | + |
| 25 | +## Structure |
| 26 | + |
| 27 | +- **`quick_start.py`**: A simple example demonstrating basic query execution |
| 28 | +- **`example_use.ipynb`**: A comprehensive Jupyter notebook with detailed examples covering: |
| 29 | + - Session details queries |
| 30 | + - Session metrics queries |
| 31 | + - Raw data retrieval and processing |
| 32 | +- **`queries/`**: Reusable query functions built using Pydantic models |
| 33 | + - `club_sessions_filtered_by_time_range.py`: Query sessions filtered by time range |
| 34 | + - `session_details.py`: Query detailed session information including participants and segments |
| 35 | + - `session_metrics.py`: Query session and participation metrics |
| 36 | + - `session_participations_urls.py`: Query datafile URLs for session participations |
| 37 | +- **`raw_data_utils/`**: Utilities for processing raw data |
| 38 | + - `url_to_csv.py`: Functions to fetch raw data from URLs and convert to CSV format |
| 39 | + |
| 40 | +## Basic Usage Pattern |
| 41 | + |
| 42 | +```python |
| 43 | +from playerdatapy.playerdata_api import PlayerDataAPI |
| 44 | +from playerdatapy.gqlauth import AuthenticationType |
| 45 | +import asyncio |
| 46 | + |
| 47 | +# Create a PlayerDataAPI instance |
| 48 | +api = PlayerDataAPI( |
| 49 | + client_id=CLIENT_ID, |
| 50 | + client_secret=CLIENT_SECRET, |
| 51 | + authentication_type=AuthenticationType.CLIENT_CREDENTIALS_FLOW, |
| 52 | +) |
| 53 | + |
| 54 | +# Build queries using Pydantic models |
| 55 | +from queries.club_sessions_filtered_by_time_range import club_sessions_filtered_by_time_range |
| 56 | + |
| 57 | +query = club_sessions_filtered_by_time_range( |
| 58 | + club_id=CLUB_ID, |
| 59 | + start_time_gteq=START_TIME, |
| 60 | + end_time_lteq=END_TIME, |
| 61 | +) |
| 62 | + |
| 63 | +# Execute queries |
| 64 | +response = asyncio.run( |
| 65 | + api.run_queries("ClubSessionsFilteredByTimeRangeQuery", query) |
| 66 | +) |
| 67 | +``` |
| 68 | + |
| 69 | +## Query Functions |
| 70 | + |
| 71 | +The `queries/` folder contains reusable query functions that demonstrate common use cases: |
| 72 | + |
| 73 | +### Club Sessions Filtered by Time Range |
| 74 | + |
| 75 | +Query sessions for a club within a specific time range: |
| 76 | + |
| 77 | +```python |
| 78 | +from queries.club_sessions_filtered_by_time_range import club_sessions_filtered_by_time_range |
| 79 | + |
| 80 | +query = club_sessions_filtered_by_time_range( |
| 81 | + club_id="your_club_id", |
| 82 | + start_time_gteq=datetime.now() - timedelta(days=30), |
| 83 | + end_time_lteq=datetime.now(), |
| 84 | +) |
| 85 | +``` |
| 86 | + |
| 87 | +### Session Details |
| 88 | + |
| 89 | +Query detailed information about a session including participants, athletes, and segments: |
| 90 | + |
| 91 | +```python |
| 92 | +from queries.session_details import session_details |
| 93 | + |
| 94 | +query = session_details(session_id="your_session_id") |
| 95 | +``` |
| 96 | + |
| 97 | +### Session Metrics |
| 98 | + |
| 99 | +Query metrics for a session, including aggregated session metrics and per-participation metrics: |
| 100 | + |
| 101 | +```python |
| 102 | +from queries.session_metrics import session_metrics |
| 103 | + |
| 104 | +query = session_metrics(session_id="your_session_id") |
| 105 | +``` |
| 106 | + |
| 107 | +### Session Participation URLs |
| 108 | + |
| 109 | +Query datafile URLs for session participations: |
| 110 | + |
| 111 | +```python |
| 112 | +from queries.session_participations_urls import session_participations_urls |
| 113 | + |
| 114 | +query = session_participations_urls( |
| 115 | + session_participation_ids=["id1", "id2", "id3"] |
| 116 | +) |
| 117 | +``` |
| 118 | + |
| 119 | +## Raw Data Processing |
| 120 | + |
| 121 | +The `raw_data_utils/` folder contains utilities for processing raw data retrieved from the API: |
| 122 | + |
| 123 | +### Converting Raw Data to CSV |
| 124 | + |
| 125 | +The `url_to_csv.py` module provides functions to: |
| 126 | +- Fetch JSON data from URLs |
| 127 | +- Extract GPS data, IMU acceleration data, and IMU orientation data |
| 128 | +- Write data to CSV files organized by session participation ID |
| 129 | + |
| 130 | +Example usage: |
| 131 | + |
| 132 | +```python |
| 133 | +from raw_data_utils.url_to_csv import url_to_csv |
| 134 | + |
| 135 | +# Fetch and convert raw data to CSV files |
| 136 | +url_to_csv( |
| 137 | + url="https://datafile_url.com", |
| 138 | + session_participation_id="participation_id" |
| 139 | +) |
| 140 | +``` |
| 141 | + |
| 142 | +This will create a directory named after the `session_participation_id` containing: |
| 143 | +- `gps_data_{session_participation_id}.csv` |
| 144 | +- `imu_acceleration_data_{session_participation_id}.csv` |
| 145 | +- `imu_orientation_data_{session_participation_id}.csv` |
| 146 | + |
| 147 | +## Authentication Types |
| 148 | + |
| 149 | +The examples use `AuthenticationType.CLIENT_CREDENTIALS_FLOW` by default, which is suitable for backend-to-backend communication. You can also use: |
| 150 | + |
| 151 | +- `AuthenticationType.AUTHORISATION_CODE_FLOW`: For confidential client credentials |
| 152 | +- `AuthenticationType.AUTHORISATION_CODE_FLOW_PKCE`: For non-confidential client credentials |
| 153 | + |
| 154 | +## Further Reading |
| 155 | + |
| 156 | +For more detailed examples and use cases, see the `example_use.ipynb` notebook which provides comprehensive demonstrations of querying session details, metrics, and raw data processing. |
0 commit comments