thecatapi

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 thecatapi is the library behind the thecatapi command line: the HTTP client, request shaping, and the typed data models for The Cat 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. Build your endpoint calls and JSON decoding on top of it.

Index

Constants

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

BaseURL is the root every request is built from.

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

DefaultUserAgent identifies the client to The Cat API.

View Source
const Host = "api.thecatapi.com"

Host is the API host this client talks to.

Variables

This section is empty.

Functions

This section is empty.

Types

type Breed

type Breed struct {
	ID           string `kit:"id" json:"id"`
	Name         string `json:"name"`
	Origin       string `json:"origin"`
	Description  string `json:"description"`
	Temperament  string `json:"temperament"`
	LifeSpan     string `json:"life_span"`
	WeightMetric string `json:"weight_metric"`
	Indoor       int    `json:"indoor"`
	Adaptability int    `json:"adaptability"`
	Intelligence int    `json:"intelligence"`
	EnergyLevel  int    `json:"energy_level"`
	Affection    int    `json:"affection_level"`
	WikipediaURL string `json:"wikipedia_url"`
}

Breed represents a cat breed from The Cat API.

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 The Cat API over HTTP.

func NewClient

func NewClient() *Client

NewClient returns a Client with sensible defaults: a 30s timeout, a 300ms 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.

func (*Client) GetBreed

func (c *Client) GetBreed(ctx context.Context, id string) (*Breed, error)

GetBreed fetches a single breed by its ID.

func (*Client) GetImage

func (c *Client) GetImage(ctx context.Context, id string) (*Image, error)

GetImage fetches a single image by its ID.

func (*Client) ListBreeds

func (c *Client) ListBreeds(ctx context.Context, limit int) ([]*Breed, error)

ListBreeds returns up to limit breeds from the API.

func (*Client) ListImages

func (c *Client) ListImages(ctx context.Context, limit int, breedID string) ([]*Image, error)

ListImages returns up to limit cat images, optionally filtered by breed ID.

func (*Client) SearchBreeds

func (c *Client) SearchBreeds(ctx context.Context, query string) ([]*Breed, error)

SearchBreeds searches breeds by name query.

type Domain

type Domain struct{}

Domain is the thecatapi driver.

func (Domain) Classify

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

Classify turns any accepted input into the canonical (type, id). Short breed-like IDs (4 chars, lowercase letters) are classified as "breed"; everything else is classified as "query".

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 Image

type Image struct {
	ID     string `kit:"id" json:"id"`
	URL    string `json:"url"`
	Width  int    `json:"width"`
	Height int    `json:"height"`
}

Image represents a cat image from The Cat API.

Jump to

Keyboard shortcuts

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