swapi

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

Documentation

Overview

Package swapi is the library behind the swapi command line: the HTTP client, request shaping, and typed data models for the Star Wars API.

The Client sets a real User-Agent, paces requests, and retries the transient failures (429 and 5xx) that any public API throws under load. All six resource types are supported: people, films, planets, starships, vehicles, and species.

Index

Constants

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

BaseURL is the root every request is built from.

View Source
const Host = "swapi.py4e.com"

Host is the site this client talks to.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client talks to the Star Wars API over HTTP.

func NewClient

func NewClient(cfg Config) *Client

NewClient returns a Client configured with cfg.

func (*Client) Films

func (c *Client) Films(ctx context.Context) ([]Film, error)

Films returns all 7 canonical Star Wars films.

func (*Client) People

func (c *Client) People(ctx context.Context, search string, limit int) ([]Person, error)

People returns Star Wars characters. search is optional ("" means all). Follows pagination until limit is satisfied or no more pages remain.

func (*Client) Planets

func (c *Client) Planets(ctx context.Context, search string, limit int) ([]Planet, error)

Planets returns planets in the Star Wars universe. search is optional.

func (*Client) Species

func (c *Client) Species(ctx context.Context, search string, limit int) ([]Species, error)

Species returns species/races from the Star Wars universe. search is optional.

func (*Client) Starships

func (c *Client) Starships(ctx context.Context, search string, limit int) ([]Starship, error)

Starships returns starships used in the Star Wars films. search is optional.

func (*Client) Vehicles

func (c *Client) Vehicles(ctx context.Context, search string, limit int) ([]Vehicle, error)

Vehicles returns vehicles from the Star Wars films. search is optional.

type Config

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

Config holds tunable knobs for the HTTP client.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns sensible defaults for production use.

type Domain

type Domain struct{}

Domain is the swapi kit driver. It carries no state; the per-run client is built by the factory Register hands to kit.

func (Domain) Classify

func (Domain) Classify(input string) (string, string, error)

Classify turns a SWAPI URL or resource path into (type, id).

func (Domain) Info

func (Domain) Info() kit.DomainInfo

Info describes the scheme, hostnames, and the identity used in help and version.

func (Domain) Locate

func (Domain) Locate(t, id string) (string, error)

Locate returns the live API URL for a (type, id).

func (Domain) Register

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

Register installs the client factory and all six resource operations onto app.

type Film

type Film struct {
	Title       string `json:"title"`
	EpisodeID   int    `json:"episode_id"`
	Director    string `json:"director"`
	Producer    string `json:"producer"`
	ReleaseDate string `json:"release_date"`
	URL         string `json:"url"`
}

Film is one of the 7 canonical Star Wars films.

type Person

type Person struct {
	Name      string `json:"name"`
	Height    string `json:"height"`
	Mass      string `json:"mass"`
	HairColor string `json:"hair_color"`
	BirthYear string `json:"birth_year"`
	Gender    string `json:"gender"`
	URL       string `json:"url"`
}

Person is a character from the Star Wars films.

type Planet

type Planet struct {
	Name       string `json:"name"`
	Climate    string `json:"climate"`
	Terrain    string `json:"terrain"`
	Population string `json:"population"`
	URL        string `json:"url"`
}

Planet is a planet in the Star Wars universe.

type Species

type Species struct {
	Name            string `json:"name"`
	Classification  string `json:"classification"`
	Language        string `json:"language"`
	AverageLifespan string `json:"average_lifespan"`
	URL             string `json:"url"`
}

Species is a species or race in the Star Wars universe.

type Starship

type Starship struct {
	Name             string `json:"name"`
	Model            string `json:"model"`
	Manufacturer     string `json:"manufacturer"`
	StarshipClass    string `json:"starship_class"`
	HyperdriveRating string `json:"hyperdrive_rating"`
	URL              string `json:"url"`
}

Starship is a starship used in the Star Wars films.

type Vehicle

type Vehicle struct {
	Name         string `json:"name"`
	Model        string `json:"model"`
	Manufacturer string `json:"manufacturer"`
	VehicleClass string `json:"vehicle_class"`
	URL          string `json:"url"`
}

Vehicle is a ground or air vehicle in the Star Wars universe.

Jump to

Keyboard shortcuts

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