thetrivia

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

Package thetrivia is the library behind the thetrivia command line: the HTTP client, request shaping, and the typed data models for The Trivia API (the-trivia-api.com).

The Client paces requests, retries transient failures (429 and 5xx), and sets an honest User-Agent. All endpoint methods decode JSON into the typed Question record.

Index

Constants

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

BaseURL is the root every request is built from.

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

DefaultUserAgent identifies the client to The Trivia API.

View Source
const Host = "the-trivia-api.com"

Host is the site this client talks to, claimed by the URI driver in domain.go.

Variables

This section is empty.

Functions

This section is empty.

Types

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

func NewClient

func NewClient() *Client

NewClient returns a Client using DefaultConfig values.

func (*Client) GetQuestions

func (c *Client) GetQuestions(ctx context.Context, limit int, categories []string, difficulty string) ([]Question, error)

GetQuestions fetches trivia questions from the API.

limit is the number of questions to return (1-50; default 10 when 0). categories is a list of category slugs (e.g. "science", "history"); omit to get questions from all categories. difficulty is one of "easy", "medium", "hard"; omit to get any difficulty.

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, polite defaults for The Trivia API.

type Domain

type Domain struct{}

Domain is the thetrivia 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 the canonical (type, id). A numeric string (bare question id) becomes ("id", input). Anything else becomes ("query", input), treated as a search term.

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) pair. For "id", that is the question endpoint on the-trivia-api.com.

func (Domain) Register

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

Register installs the client factory and every operation onto app.

type Question

type Question struct {
	ID               string   `kit:"id" json:"id"`
	Category         string   `json:"category"`
	Difficulty       string   `json:"difficulty"`
	Question         string   `json:"question"`
	CorrectAnswer    string   `json:"correct_answer"`
	IncorrectAnswers []string `json:"incorrect_answers"`
	IsNiche          bool     `json:"is_niche"`
	Tags             []string `json:"tags"`
}

Question is one trivia question record, shaped from the API response. The ID field carries kit:"id" so a host can mint a thetrivia://id/<id> URI.

Jump to

Keyboard shortcuts

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