openlibrary

package
v0.1.5 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 openlibrary is the library behind the openlibrary command line: the HTTP client, request shaping, and the typed data models for Open Library.

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

Index

Constants

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

BaseURL is the root every request is built from.

View Source
const DefaultUserAgent = "openlibrary-cli/0.1 (tamnd87@gmail.com)"

DefaultUserAgent identifies the client to Open Library.

View Source
const Host = "openlibrary.org"

Host is the site this client talks to.

Variables

This section is empty.

Functions

This section is empty.

Types

type Author added in v0.1.1

type Author struct {
	Key       string `kit:"id" json:"key"`
	Name      string `json:"name"`
	BirthDate string `json:"birth_date"`
	DeathDate string `json:"death_date"`
	Bio       string `json:"bio"`
}

Author is an author record from /authors/{key}.json.

type Book

type Book struct {
	Key              string `kit:"id" json:"key"`
	Title            string `json:"title"`
	Authors          string `json:"authors"` // comma-joined author names
	FirstPublishYear int    `json:"first_publish_year"`
	ISBN             string `json:"isbn"`     // first ISBN if any
	Subjects         string `json:"subjects"` // comma-joined first 3 subjects
	Pages            int    `json:"pages"`    // number_of_pages_median (search) or number_of_pages (isbn)
}

Book is a search result or ISBN lookup record from Open Library.

type Client

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

Client talks to Open Library over HTTP.

func NewClient

func NewClient(cfg Config) *Client

NewClient returns a Client using cfg.

func (*Client) GetAuthor added in v0.1.1

func (c *Client) GetAuthor(ctx context.Context, olid string) (*Author, error)

GetAuthor fetches the full author record by OL ID (e.g. "OL34184A"). The /authors/ prefix is stripped from olid if present.

func (*Client) GetBookByISBN added in v0.1.2

func (c *Client) GetBookByISBN(ctx context.Context, isbn string) (*Book, error)

GetBookByISBN fetches a book by ISBN using the /api/books endpoint. The isbn argument may contain hyphens; they are stripped automatically.

func (*Client) GetEditions added in v0.1.4

func (c *Client) GetEditions(ctx context.Context, olid string, limit int) ([]Edition, error)

GetEditions fetches the editions list for a work by OL ID (e.g. "OL45804W").

func (*Client) GetWork added in v0.1.1

func (c *Client) GetWork(ctx context.Context, olid string) (*Work, error)

GetWork fetches the full work record by OL ID (e.g. "OL45804W"). The /works/ prefix is stripped from olid if present.

func (*Client) SearchAuthors added in v0.1.1

func (c *Client) SearchAuthors(ctx context.Context, query string, limit int) ([]Author, error)

SearchAuthors searches Open Library for authors matching query.

func (*Client) SearchBooks

func (c *Client) SearchBooks(ctx context.Context, query string, limit int) ([]Book, error)

SearchBooks searches Open Library for books matching query.

type Config

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

Config holds all tunable client parameters.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns sensible defaults for a polite CLI client.

type Domain

type Domain struct{}

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

  • starts with "OL" and ends in "A" (like OL34184A) -> ("author", input)
  • starts with "OL" and ends in "W" (like OL45804W) -> ("work", input)
  • all digits, 10 or 13 chars -> ("isbn", input)
  • otherwise -> ("query", input)

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 is the inverse: 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 Edition added in v0.1.3

type Edition struct {
	Key       string `kit:"id" json:"key"`
	Title     string `json:"title"`
	Publisher string `json:"publisher"` // first publisher
	Published string `json:"published"`
	ISBN13    string `json:"isbn_13"` // first isbn_13
}

Edition is one edition entry from /works/{key}/editions.json.

type Work added in v0.1.1

type Work struct {
	Key          string `kit:"id" json:"key"`
	Title        string `json:"title"`
	Description  string `json:"description"`
	Subjects     string `json:"subjects"` // comma-joined first 5 subjects
	FirstPublish string `json:"first_publish"`
}

Work is a full work record from /works/{key}.json.

Jump to

Keyboard shortcuts

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