jokeapi

package
v0.1.2 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: 10 Imported by: 0

Documentation

Overview

Package jokeapi is the library behind the jokeapi command line: the HTTP client, request shaping, and the typed data models for v2.jokeapi.dev.

The Client sets a real User-Agent, paces requests so a busy session stays polite, and retries the transient failures (429 and 5xx) that any public site throws under load.

Index

Constants

View Source
const Host = "v2.jokeapi.dev"

Host is the site this client talks to.

Variables

This section is empty.

Functions

This section is empty.

Types

type Category added in v0.1.2

type Category struct {
	Name string `kit:"id" json:"name"`
}

Category is a joke category from v2.jokeapi.dev.

type Client

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

Client talks to v2.jokeapi.dev over HTTP.

func NewClient

func NewClient(cfg Config) *Client

NewClient returns a Client configured with cfg.

func (*Client) Categories added in v0.1.1

func (c *Client) Categories(ctx context.Context) ([]Category, error)

Categories returns the list of available joke categories from the API.

func (*Client) Joke added in v0.1.2

func (c *Client) Joke(ctx context.Context, category, jType, lang string, safe bool, blacklist string) (*Joke, error)

Joke fetches a single joke from the given category. jType may be "single", "twopart", or "" for any type. lang sets the language code (defaults to "en"). If safe is true, only family-safe jokes are returned. blacklist is a comma-separated list of flags to exclude.

func (*Client) Jokes added in v0.1.1

func (c *Client) Jokes(ctx context.Context, category string, safe bool, count int) ([]Joke, error)

Jokes fetches count jokes from the given category. If safe is true, only family-safe jokes are returned. Category defaults to "Any" if empty.

type Config added in v0.1.1

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

Config holds all tunable parameters for the Client.

func DefaultConfig added in v0.1.1

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults.

type Domain

type Domain struct{}

Domain is the jokeapi driver.

func (Domain) Classify

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

Classify turns an input into the canonical (type, id).

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 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 Joke added in v0.1.1

type Joke struct {
	ID        int    `kit:"id" json:"id"`
	Category  string `json:"category"`
	Type      string `json:"type"`     // "single" or "twopart"
	Joke      string `json:"joke"`     // for single-type
	Setup     string `json:"setup"`    // for twopart-type
	Delivery  string `json:"delivery"` // for twopart-type
	Safe      bool   `json:"safe"`
	Lang      string `json:"lang"`
	NSFW      bool   `json:"nsfw"`
	Religious bool   `json:"religious"`
	Political bool   `json:"political"`
	Racist    bool   `json:"racist"`
	Sexist    bool   `json:"sexist"`
	Explicit  bool   `json:"explicit"`
}

Joke is one joke from v2.jokeapi.dev.

Jump to

Keyboard shortcuts

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