api

package
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package api wraps the Ergast F1 REST API with an optional file-based cache.

Index

Constants

View Source
const (
	BaseURL         = "https://ergast.com/api/f1"
	FallbackBaseURL = "https://api.jolpi.ca/ergast/f1"
)

Variables

View Source
var (
	ErrAPIUnreachable   = errors.New("Ergast API unreachable")
	ErrResourceNotFound = errors.New("resource not found in Ergast dataset")
	ErrInvalidSeason    = errors.New("season is outside the supported range")
)

Sentinel errors the caller can test with errors.Is.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client wraps the Ergast API with automatic primary→fallback retry and an optional file-based response cache. Pass nil for cache to disable caching.

func NewClient

func NewClient(cache *FileCache) *Client

NewClient returns a Client ready for use. cache may be nil.

func (*Client) GetDriverStandings

func (c *Client) GetDriverStandings(year, round int) ([]DriverStanding, error)

GetDriverStandings returns the championship table after round in year.

func (*Client) GetPointsEarnedInRound

func (c *Client) GetPointsEarnedInRound(year, round int) ([]RaceResult, error)

GetPointsEarnedInRound returns only the drivers who scored points.

func (*Client) GetRaceInfo

func (c *Client) GetRaceInfo(year, round int) (Race, error)

GetRaceInfo returns metadata for one race.

func (*Client) GetRaceResults

func (c *Client) GetRaceResults(year, round int) ([]RaceResult, error)

GetRaceResults returns the full race classification for (year, round).

func (*Client) GetSeasonSchedule

func (c *Client) GetSeasonSchedule(year int) ([]Race, error)

GetSeasonSchedule returns the race calendar for year.

func (*Client) GetStandingsBeforeRound

func (c *Client) GetStandingsBeforeRound(year, round int) ([]DriverStanding, error)

GetStandingsBeforeRound returns the championship state entering round. Returns an empty slice for round 1 (no prior data exists).

func (*Client) GetTotalRounds

func (c *Client) GetTotalRounds(year int) (int, error)

GetTotalRounds returns the number of rounds in year's season.

type DriverStanding

type DriverStanding struct {
	Position   int     `json:"position"`
	Points     float64 `json:"points"`
	Wins       int     `json:"wins"`
	DriverID   string  `json:"driver_id"`
	Code       string  `json:"code"`
	GivenName  string  `json:"given_name"`
	FamilyName string  `json:"family_name"`
	DriverName string  `json:"driver_name"`
	TeamName   string  `json:"team_name"`
}

DriverStanding is one row in the drivers' championship table.

type FileCache

type FileCache struct {
	// contains filtered or unexported fields
}

FileCache stores one JSON file per URL under a directory. The cache key is the SHA-256 hex of the full request URL.

func NewFileCache

func NewFileCache(dir string) (*FileCache, error)

NewFileCache initialises the cache directory and returns a FileCache.

func (*FileCache) Get

func (c *FileCache) Get(url string) ([]byte, bool)

Get returns the cached body for url if valid and unexpired. The second return is false on a cache miss or expired entry.

func (*FileCache) Set

func (c *FileCache) Set(url string, body []byte) error

Set writes body to the cache with the appropriate TTL for url.

type Race

type Race struct {
	Season      int    `json:"season"`
	Round       int    `json:"round"`
	RaceName    string `json:"race_name"`
	Date        string `json:"date"`
	Time        string `json:"time,omitempty"`
	CircuitID   string `json:"circuit_id"`
	CircuitName string `json:"circuit_name"`
	Locality    string `json:"locality"`
	Country     string `json:"country"`
}

Race holds metadata for a single Grand Prix weekend.

type RaceResult

type RaceResult struct {
	Position     int     `json:"position"`
	PositionText string  `json:"position_text"`
	Points       float64 `json:"points"`
	Grid         int     `json:"grid"`
	Laps         int     `json:"laps"`
	Status       string  `json:"status"`
	RaceTime     string  `json:"race_time,omitempty"`
	DriverID     string  `json:"driver_id"`
	DriverName   string  `json:"driver_name"`
	TeamName     string  `json:"team_name"`
}

RaceResult is one row in the race classification table.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL