carbonintensity

package
v0.1.2 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 carbonintensity is the library behind the carbonintensity command line: the HTTP client, request shaping, and the typed data models for the UK Carbon Intensity API (api.carbonintensity.org.uk). No API key required.

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 DefaultUserAgent = "carbonintensity-cli/dev (+https://github.com/tamnd/carbonintensity-cli)"

DefaultUserAgent identifies the client to the Carbon Intensity API.

View Source
const Host = "api.carbonintensity.org.uk"

Host is the API host this client talks to.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client talks to the Carbon Intensity API over HTTP.

func NewClient

func NewClient() *Client

NewClient returns a Client using DefaultConfig.

func NewClientWithConfig

func NewClientWithConfig(cfg Config) *Client

NewClientWithConfig returns a Client configured with cfg.

func (*Client) GetGeneration

func (c *Client) GetGeneration(ctx context.Context) ([]GenerationFuel, error)

GetGeneration returns the current generation mix as one record per fuel type.

func (*Client) GetHistory added in v0.1.1

func (c *Client) GetHistory(ctx context.Context, from, to string) ([]Intensity, error)

GetHistory returns carbon intensity records for the given date range. from and to must be ISO 8601 datetime strings, e.g. "2025-01-01T00:00Z".

func (*Client) GetIntensity

func (c *Client) GetIntensity(ctx context.Context) (*Intensity, error)

GetIntensity returns the current national carbon intensity.

func (*Client) GetRegional

func (c *Client) GetRegional(ctx context.Context) ([]RegionalIntensity, error)

GetRegional returns the current carbon intensity for each of the 18 UK distribution network regions.

type Config

type Config struct {
	BaseURL string
	Rate    time.Duration
	Retries int
	Timeout time.Duration
}

Config holds runtime configuration for the client.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns sensible defaults for the Carbon Intensity API.

type Domain

type Domain struct{}

Domain is the carbonintensity 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 input into the canonical (type, id).

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 returns the live 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 GenerationFuel

type GenerationFuel struct {
	Fuel    string `kit:"id" json:"fuel"`
	Percent string `json:"percent"` // formatted as "%.1f"
}

GenerationFuel is one fuel's share of the current electricity generation mix.

type Intensity

type Intensity struct {
	From     string `kit:"id" json:"from"`
	To       string `json:"to"`
	Forecast int    `json:"forecast"`
	Actual   int    `json:"actual"`
	Index    string `json:"index"`
}

Intensity is the current carbon intensity for Great Britain.

type RegionalIntensity

type RegionalIntensity struct {
	RegionID  int    `kit:"id" json:"region_id"`
	ShortName string `json:"short_name"`
	DNORegion string `json:"dno_region"`
	From      string `json:"from"`
	Forecast  int    `json:"forecast"`
	Index     string `json:"index"`
}

RegionalIntensity is the carbon intensity for one UK distribution network region.

Jump to

Keyboard shortcuts

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