carb

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 carb is the library behind the carb 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.

Package carb exposes the UK Carbon Intensity API as a kit Domain driver.

A multi-domain host (ant) enables it with a single blank import:

import _ "github.com/tamnd/carb-cli/carb"

The same Domain also builds the standalone carb binary (see cli.NewApp).

Index

Constants

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(cfg Config) *Client

NewClient returns a Client configured with cfg.

func (*Client) Current

func (c *Client) Current(ctx context.Context) ([]Intensity, error)

Current fetches the current national carbon intensity.

func (*Client) Date

func (c *Client) Date(ctx context.Context, date string) ([]Intensity, error)

Date fetches all 30-min intensity periods for a given date (YYYY-MM-DD).

func (*Client) Factors

func (c *Client) Factors(ctx context.Context) ([]Factor, error)

Factors fetches carbon intensity factors by fuel type.

func (*Client) Generation

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

Generation fetches the current generation mix.

type Config

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

Config holds all tunable parameters for the Client.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults.

type Domain

type Domain struct{}

Domain is the carb driver.

func (Domain) Classify

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

Classify turns an 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 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 Factor

type Factor struct {
	Fuel   string  `json:"fuel"`
	Factor float64 `json:"factor"`
}

Factor is a carbon intensity factor for a fuel type.

type GenFuel

type GenFuel struct {
	Fuel string  `json:"fuel"`
	Perc float64 `json:"perc"`
}

GenFuel is one fuel type in the generation mix.

type Generation

type Generation struct {
	From string    `json:"from"`
	To   string    `json:"to"`
	Mix  []GenFuel `json:"mix"`
}

Generation is the current generation mix.

type Intensity

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

Intensity is one carbon intensity reading.

Jump to

Keyboard shortcuts

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