Overview
All data fetchers in the F1 Stats Archive follow consistent patterns for making API requests, handling errors, parsing JSON responses, and saving data to disk. This page documents the common utilities and best practices.Core Components
Base URL Configuration
All scripts use the Ergast API hosted at jolpi.ca:Fetcher Class Structure
Most scripts follow a class-based architecture:results.py:20-26
Making API Requests
Standard Request Method
Themake_request() method is the core pattern used across all fetchers:
sprint_results.py:32-58
This method combines rate limiting, error handling, automatic retries, and JSON parsing in a single reusable utility.
Enhanced Request Method with Exception Handling
team_points.py:61-86
Function-Based Request Pattern
For simpler scripts without classes:driver_points.py:38-53
Common API Endpoints
Get Race Information
results.py:60-71
Get Race Results
results.py:73-76
Get Qualifying Results
quali_results.py:77-80
Get Sprint Results
sprint_results.py:107-110
Get Constructor Standings
team_points.py:101-104
Get Driver Standings
driver_points.py:56-59
Pagination Handling
Fetching Large Datasets
For endpoints with large amounts of data (like lap times and pitstops), use pagination:laptimes.py:23-63
Pitstops Pagination
pitstops.py:106-181
Pagination is critical for lap times and pitstops data, which can contain thousands of records per race.
File Operations
Directory Creation
All scripts ensure directories exist before writing files:Saving JSON Data
sprint_results.py:112-122
Simplified Save Method
team_points.py:106-112
Race Name Formatting
Converting Race Names to Folder Names
sprint_results.py:28-30
Slugify Function
events.py:11-14
Error Handling Patterns
Reading Local Files
pitstops.py:71-99
Validating API Responses
quali_results.py:63-75
Complete Fetcher Example
Here’s a complete example combining all patterns:Best Practices
- Always use rate limiting - Never make requests without delay
- Handle 429 responses - Implement automatic retry with backoff
- Validate responses - Check for expected JSON structure
- Use logging - Track all requests and errors
- Create directories - Use
exist_ok=Trueto avoid errors - Format consistently - Convert race names to lowercase with hyphens
- Handle exceptions - Wrap file operations in try-except blocks
- Use pagination - For large datasets like lap times and pitstops
- Track request counts - Monitor hourly limits for long operations
- Return meaningful values - Return
Noneon errors, data on success