tfl

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package tfl is the library behind the tfl command line: the HTTP client, request shaping, and the typed data models for the Transport for London Unified API (api.tfl.gov.uk).

The Client here is the spine every command shares. It sets a real User-Agent, paces requests so a busy session stays polite, and retries the transient failures (429 and 5xx) that any public API throws under load.

Index

Constants

View Source
const BaseURL = "https://" + Host

BaseURL is the root every request is built from.

View Source
const DefaultUserAgent = "tfl-cli/dev (+https://github.com/tamnd/tfl-cli)"

DefaultUserAgent identifies the client to TfL.

View Source
const Host = "api.tfl.gov.uk"

Host is the TfL Unified API host.

Variables

This section is empty.

Functions

This section is empty.

Types

type AirForecast

type AirForecast struct {
	Period          string `json:"period"`
	ForecastType    string `json:"forecast_type"`
	ForecastBand    string `json:"forecast_band"`
	ForecastSummary string `json:"forecast_summary"`
}

AirForecast is one period's air quality forecast.

type AirQuality

type AirQuality struct {
	UpdatePeriod    string        `json:"update_period" kit:"id"`
	UpdateFreq      string        `json:"update_freq"`
	ForecastURL     string        `json:"forecast_url"`
	CurrentForecast []AirForecast `json:"current_forecast"`
}

AirQuality is the London air quality data.

type Client

type Client struct {
	HTTP      *http.Client
	UserAgent string
	// Rate is the minimum gap between requests. Zero means no pacing.
	Rate    time.Duration
	Retries int
	// contains filtered or unexported fields
}

Client talks to api.tfl.gov.uk over HTTP.

func NewClient

func NewClient() *Client

NewClient returns a Client with sensible defaults: a 30s timeout, a 200ms minimum gap between requests, and five retries on transient errors.

func (*Client) Get

func (c *Client) Get(ctx context.Context, url string) ([]byte, error)

Get fetches url and returns the response body. It paces and retries according to the client's settings.

func (*Client) GetAirQuality

func (c *Client) GetAirQuality(ctx context.Context) (*AirQuality, error)

GetAirQuality fetches the London air quality data.

func (*Client) GetDisruptions

func (c *Client) GetDisruptions(ctx context.Context, mode string) ([]*Disruption, error)

GetDisruptions fetches active disruptions for the given mode.

func (*Client) GetJourneys

func (c *Client) GetJourneys(ctx context.Context, from, to, mode string) ([]*Journey, error)

GetJourneys fetches journey options between two points.

func (*Client) GetLineStatus

func (c *Client) GetLineStatus(ctx context.Context, mode string) ([]*LineStatus, error)

GetLineStatus fetches line statuses for the given mode. mode can be "tube", "bus", "dlr", "overground", "elizabeth-line", or "all".

type Disruption

type Disruption struct {
	Category    string `json:"category"     kit:"id"`
	Type        string `json:"type"`
	Description string `json:"description"`
	CreatedTime string `json:"created_time"`
	ClosureText string `json:"closure_text"`
}

Disruption is an active service disruption on the TfL network.

type Domain

type Domain struct{}

Domain is the TfL driver. It carries no state; the per-run client is built by the factory Register hands kit.

func (Domain) Classify

func (Domain) Classify(input string) (uriType, id string, err error)

Classify turns any accepted input into (type, id). A NaPTAN ID like "940GZZLUASL" is classified as "stop"; a line id like "jubilee" is classified as "line".

func (Domain) Info

func (Domain) Info() kit.DomainInfo

Info describes the scheme, the hostnames a pasted link is matched against, and the identity reused for the binary's help and version.

func (Domain) Locate

func (Domain) Locate(uriType, id string) (string, error)

Locate is the inverse: the live https URL for a (type, id).

func (Domain) Register

func (Domain) Register(app *kit.App)

Register installs the client factory and every operation onto app.

type Journey

type Journey struct {
	Duration int    `json:"duration_min" kit:"id"`
	Legs     int    `json:"legs"`
	Fare     string `json:"fare"`
}

Journey is one journey option returned by the journey planner.

type LineStatus

type LineStatus struct {
	ID         string `json:"id"          kit:"id"`
	Name       string `json:"name"`
	Mode       string `json:"mode"`
	Severity   int    `json:"severity"`
	StatusDesc string `json:"status"`
	Reason     string `json:"reason"`
}

LineStatus is one tube/bus/rail line with its current service status.

Jump to

Keyboard shortcuts

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