Skip to main content

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:
From results.py:20-26

Making API Requests

Standard Request Method

The make_request() method is the core pattern used across all fetchers:
From 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

From team_points.py:61-86

Function-Based Request Pattern

For simpler scripts without classes:
From driver_points.py:38-53

Common API Endpoints

Get Race Information

From results.py:60-71

Get Race Results

From results.py:73-76

Get Qualifying Results

From quali_results.py:77-80

Get Sprint Results

From sprint_results.py:107-110

Get Constructor Standings

From team_points.py:101-104

Get Driver Standings

From driver_points.py:56-59

Pagination Handling

Fetching Large Datasets

For endpoints with large amounts of data (like lap times and pitstops), use pagination:
From laptimes.py:23-63

Pitstops Pagination

From 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

From sprint_results.py:112-122

Simplified Save Method

From team_points.py:106-112

Race Name Formatting

Converting Race Names to Folder Names

From sprint_results.py:28-30

Slugify Function

From events.py:11-14

Error Handling Patterns

Reading Local Files

From pitstops.py:71-99

Validating API Responses

From quali_results.py:63-75

Complete Fetcher Example

Here’s a complete example combining all patterns:

Best Practices

  1. Always use rate limiting - Never make requests without delay
  2. Handle 429 responses - Implement automatic retry with backoff
  3. Validate responses - Check for expected JSON structure
  4. Use logging - Track all requests and errors
  5. Create directories - Use exist_ok=True to avoid errors
  6. Format consistently - Convert race names to lowercase with hyphens
  7. Handle exceptions - Wrap file operations in try-except blocks
  8. Use pagination - For large datasets like lap times and pitstops
  9. Track request counts - Monitor hourly limits for long operations
  10. Return meaningful values - Return None on errors, data on success