2026-03-03 00:20:09 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type OpenF1Client struct {
|
|
|
|
|
url string
|
2026-03-27 00:30:53 -04:00
|
|
|
apiKey string
|
2026-03-03 00:20:09 -05:00
|
|
|
httpClient *http.Client
|
2026-03-27 00:30:53 -04:00
|
|
|
cache *Cache
|
2026-03-03 00:20:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewOpenF1Client(url string, timeout time.Duration) *OpenF1Client {
|
|
|
|
|
return &OpenF1Client{
|
|
|
|
|
url: url,
|
|
|
|
|
httpClient: &http.Client{Timeout: timeout},
|
2026-03-27 00:30:53 -04:00
|
|
|
cache: NewCache(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewOpenF1ClientWithKey creates a client that authenticates with a Bearer token.
|
|
|
|
|
// This allows access during live sessions (paid tier).
|
|
|
|
|
func NewOpenF1ClientWithKey(url string, timeout time.Duration, apiKey string) *OpenF1Client {
|
|
|
|
|
return &OpenF1Client{
|
|
|
|
|
url: url,
|
|
|
|
|
apiKey: apiKey,
|
|
|
|
|
httpClient: &http.Client{Timeout: timeout},
|
|
|
|
|
cache: NewCache(),
|
2026-03-03 00:20:09 -05:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-03 02:29:34 -05:00
|
|
|
|
|
|
|
|
// CacheStats returns the cache hit/miss statistics.
|
|
|
|
|
func (c *OpenF1Client) CacheStats() CacheStats {
|
|
|
|
|
return c.cache.Stats()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CacheSize returns the number of cached entries and total size in bytes.
|
|
|
|
|
func (c *OpenF1Client) CacheSize() (int, int64) {
|
|
|
|
|
return c.cache.Size()
|
|
|
|
|
}
|
2026-03-27 00:30:53 -04:00
|
|
|
|
|
|
|
|
// Close releases resources held by the client (closes the cache database).
|
|
|
|
|
func (c *OpenF1Client) Close() error {
|
|
|
|
|
return c.cache.Close()
|
|
|
|
|
}
|