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 ¶
const BaseURL = "https://" + Host
BaseURL is the root every request is built from.
const DefaultUserAgent = "thetrivia/dev (+https://github.com/tamnd/thetrivia-cli)"
DefaultUserAgent identifies the client to The Trivia API.
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 (*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 ¶
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 ¶
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.
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.