deckofcards

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

Documentation

Overview

Package deckofcards is the library behind the deckofcards command line: the HTTP client, request shaping, and the typed data models for deckofcardsapi.com (free, no API key required).

The Client 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 (5xx) that any public API throws under load.

Index

Constants

View Source
const Host = "deckofcardsapi.com"

Host is the site this client talks to.

Variables

This section is empty.

Functions

This section is empty.

Types

type Card

type Card struct {
	Code  string `json:"code"`
	Value string `json:"value"`
	Suit  string `json:"suit"`
	Image string `json:"image"`
}

Card is one card drawn from a deck.

type Client

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

Client talks to deckofcardsapi.com over HTTP.

func NewClient

func NewClient(cfg Config) *Client

NewClient returns a Client configured from cfg.

func (*Client) Draw

func (c *Client) Draw(ctx context.Context, deckID string, n int) (*DrawResult, error)

Draw draws n cards from the deck identified by deckID. n defaults to 1 if <= 0.

func (*Client) New

func (c *Client) New(ctx context.Context, count int, shuffle bool) (*Deck, error)

New creates a new deck. count is the number of 52-card packs to combine (defaults to 1 if <= 0). If shuffle is true, uses the /new/shuffle/ endpoint; otherwise uses /new/ which creates an unshuffled ordered deck.

func (*Client) Shuffle

func (c *Client) Shuffle(ctx context.Context, deckID string) (*Deck, error)

Shuffle reshuffles the existing deck identified by deckID. All drawn cards are returned to the deck before reshuffling.

type Config

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

Config holds all tuneable parameters for a Client.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the production configuration for deckofcardsapi.com.

type Deck

type Deck struct {
	ID        string `json:"deck_id"`
	Shuffled  bool   `json:"shuffled"`
	Remaining int    `json:"remaining"`
	Success   bool   `json:"success"`
}

Deck represents a deck created or reshuffled via the API.

type Domain

type Domain struct{}

Domain is the deckofcards 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).

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 DrawResult

type DrawResult struct {
	DeckID    string `json:"deck_id"`
	Cards     []Card `json:"cards"`
	Remaining int    `json:"remaining"`
	Success   bool   `json:"success"`
}

DrawResult is the response from a draw operation.

Jump to

Keyboard shortcuts

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