guardian

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

Documentation

Overview

Package guardian is the library behind the guardian command line: the HTTP client, request shaping, and the typed data models for The Guardian newspaper API.

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://content.guardianapis.com"

BaseURL is the root every request is built from.

View Source
const Host = "content.guardianapis.com"

Host is the API host this client talks to.

Variables

This section is empty.

Functions

This section is empty.

Types

type Article

type Article struct {
	ID          string `json:"id" kit:"id"`
	Title       string `json:"title"`
	Section     string `json:"section"`
	PublishedAt string `json:"published_at"`
	Author      string `json:"author"`
	Trail       string `json:"trail"`
	URL         string `json:"url"`
}

Article is a single piece of content from The Guardian.

type Client

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

Client talks to The Guardian content API over HTTPS.

func NewClient

func NewClient() *Client

NewClient returns a Client with DefaultConfig applied.

func NewClientWithConfig

func NewClientWithConfig(cfg Config) *Client

NewClientWithConfig returns a Client using the provided Config.

func (*Client) ListSections

func (c *Client) ListSections(ctx context.Context) ([]Section, error)

ListSections returns all content sections available on The Guardian.

func (*Client) ListTags

func (c *Client) ListTags(ctx context.Context, section string, limit int) ([]Tag, error)

ListTags returns tags from The Guardian, optionally filtered by section.

func (*Client) SearchArticles

func (c *Client) SearchArticles(ctx context.Context, query, section, from, to string, limit int) ([]Article, error)

SearchArticles queries The Guardian content search API.

type Config

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

Config holds all tunable knobs for the client.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns sensible defaults using the official free demo key.

type Domain

type Domain struct{}

Domain is the guardian 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 a canonical (type, id). - "technology/2024/abc" (contains "/") → ("article", id) - "technology" (single token) → ("section", id) - "climate change" (contains space) → ("query", q)

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 Section

type Section struct {
	ID    string `json:"id" kit:"id"`
	Title string `json:"title"`
	URL   string `json:"url"`
}

Section is a content section on The Guardian website.

type Tag

type Tag struct {
	ID      string `json:"id" kit:"id"`
	Type    string `json:"type"`
	Title   string `json:"title"`
	Section string `json:"section"`
}

Tag is a topic tag used to classify Guardian content.

Jump to

Keyboard shortcuts

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