waqi

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: 12 Imported by: 0

Documentation

Overview

domain.go exposes waqi as a kit Domain: a driver that a multi-domain host (ant) enables with a single blank import,

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

exactly as a database/sql program enables a driver with `import _ "github.com/lib/pq"`. The init below registers it; the host then dereferences waqi:// URIs by routing to the operations Register installs. The same Domain also builds the standalone waqi binary (see cmd/waqi), so the binary and a host share one source of truth.

Package waqi is the library behind the waqi command line: the HTTP client, request shaping, and typed data models for api.waqi.info.

The Client sets a real User-Agent, paces requests, and retries transient failures (429 and 5xx) with exponential backoff. Two operations are provided: get real-time air quality for a city (Feed) and search stations by keyword.

Index

Constants

View Source
const Host = "api.waqi.info"

Host is the site 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 api.waqi.info over HTTP.

func NewClient

func NewClient(cfg Config) *Client

NewClient returns a Client configured with cfg.

func (*Client) Feed

func (c *Client) Feed(ctx context.Context, location string) (*Station, error)

Feed returns real-time air quality data for the given location. location can be a city name ("beijing"), a station UID ("@8076"), or "lat,lon" coordinates ("48.8566,2.3522").

func (*Client) Search

func (c *Client) Search(ctx context.Context, keyword string) ([]SearchResult, error)

Search searches for monitoring stations matching the keyword.

type Config

type Config struct {
	BaseURL   string
	UserAgent string
	Token     string // default "demo"
	Rate      time.Duration
	Timeout   time.Duration
	Retries   int
}

Config holds tunable knobs for the HTTP client.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns sensible defaults for production use.

type Domain

type Domain struct{}

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

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) Register

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

Register installs the client factory and every operation onto app.

type SearchResult

type SearchResult struct {
	UID  int    `kit:"id" json:"uid"`
	Name string `json:"name"`
	AQI  string `json:"aqi"`
	Lat  string `json:"lat"`
	Lon  string `json:"lon"`
}

SearchResult is one entry returned by the search endpoint.

type Station

type Station struct {
	City     string `kit:"id" json:"city"`
	AQI      int    `json:"aqi"`
	Dominant string `json:"dominant_pollutant"` // dominentpol field
	PM25     string `json:"pm25"`               // iaqi.pm25.v, formatted "%.1f" or "-"
	PM10     string `json:"pm10"`               // iaqi.pm10.v
	O3       string `json:"o3"`                 // iaqi.o3.v
	NO2      string `json:"no2"`                // iaqi.no2.v
	SO2      string `json:"so2"`                // iaqi.so2.v
	Temp     string `json:"temp_c"`             // iaqi.t.v
	Updated  string `json:"updated"`            // time.s
}

Station holds the parsed air quality data for one monitoring station. String fields use "%.1f" formatting or "-" when the measurement is absent.

Jump to

Keyboard shortcuts

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