client

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package client is a thin JSON:API client for catalog-api, typed against catalog-objects.go value objects. It covers list/get/create/patch/delete for every entity type plus the /search endpoints used for name resolution.

Index

Constants

View Source
const (
	AssetKindCover  = "cover"
	AssetKindSample = "sample"
)

Asset kinds the CLI writes. These are the live kinds: catalog-api's finalize-session flow promotes staged assets to cover/<volumeID> and sample/<volumeID>-<n>, so uploading straight to the live kind plus a PATCH produces the same end state without an edit session (sessions have no REST API - they are written directly to Redis by catalog-api/catalog-web).

View Source
const MediaType = "application/vnd.api+json"

MediaType is the JSON:API content type catalog-api serves.

Variables

View Source
var Entities = map[string]EntityType{
	"volume":       {Name: "volume", Plural: "volumes", Searchable: false},
	"publisher":    {Name: "publisher", Plural: "publishers", Searchable: true},
	"studio":       {Name: "studio", Plural: "studios", Searchable: true},
	"person":       {Name: "person", Plural: "persons", Searchable: true},
	"system":       {Name: "system", Plural: "systems", Searchable: true},
	"license":      {Name: "license", Plural: "licenses", Searchable: true},
	"review":       {Name: "review", Plural: "reviews", Searchable: false},
	"contribution": {Name: "contribution", Plural: "contributions", Searchable: false},
}

Entities is the full registry of entity types the CLI can address.

Functions

func ContentTypeForFilename

func ContentTypeForFilename(name string) (string, error)

ContentTypeForFilename maps an image file extension to the MIME type assets-web accepts, or an error naming the supported types.

func Create

func Create[T any](ctx context.Context, c *Client, plural string, entity *T) (*T, error)

Create posts the entity as plain JSON (catalog-api binds VOs directly, not as JSON:API requests) and returns the created record from the JSON:API response document.

func CreateVolume added in v0.2.0

func CreateVolume(ctx context.Context, c *Client, fields VolumeCreateFields) (*vo.VolumeVO, error)

CreateVolume posts fields to POST /volumes and returns the created, live volume record.

func Delete

func Delete(ctx context.Context, c *Client, plural, id string) error

Delete issues the soft-delete request. Success is 204 No Content.

func Get

func Get[T any](ctx context.Context, c *Client, plural, id string) (*T, error)

Get fetches one entity by ID into out (a *vo.X). A 404 yields an *APIError with StatusCode 404 (see IsNotFound).

func IsAuthError

func IsAuthError(err error) bool

IsAuthError reports whether err indicates missing/insufficient credentials.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound reports whether err is a 404 from the server.

func List

func List[T any](ctx context.Context, c *Client, plural string, opts ListOptions) ([]*T, error)

List fetches entities matching opts. The result is never nil; an empty page is an empty slice.

func Search[T any](ctx context.Context, c *Client, plural, q string) ([]*T, error)

Search hits the type's /search endpoint. Only Searchable entity types support it.

Types

type APIError

type APIError struct {
	Service    string
	StatusCode int
	Code       string
	Message    string
}

APIError carries a non-2xx response's status and parsed body. Body shapes vary by handler ({error,message}, {message}, or {error}); anything unparseable keeps the raw text in Message so operators see what the server said.

func (*APIError) Error

func (e *APIError) Error() string

type AssetsClient

type AssetsClient struct {
	BaseURL *url.URL
	HTTP    *http.Client
	Tokens  TokenSource
}

AssetsClient talks to one assets-web deployment.

func NewAssetsClient

func NewAssetsClient(baseURL string, tokens TokenSource) (*AssetsClient, error)

NewAssetsClient parses baseURL (must be absolute http(s)) and returns an AssetsClient using the default HTTP transport.

func (*AssetsClient) Upload

func (c *AssetsClient) Upload(ctx context.Context, kind, id string, data []byte, contentType string) error

Upload stores data at assets-web's /asset/<kind>/<id>. The wire format is a multipart form with one "file" part carrying the image bytes; the server requires a non-empty filename and an image/png, image/jpeg, or image/webp content type. Success is 201 Created.

type Client

type Client struct {
	BaseURL *url.URL
	HTTP    *http.Client
	Tokens  TokenSource
}

Client talks to one catalog-api deployment.

func New

func New(baseURL string, tokens TokenSource) (*Client, error)

New parses baseURL (must be absolute http(s)) and returns a Client using the default HTTP transport.

type EntityType

type EntityType struct {
	Name       string
	Plural     string
	Searchable bool
}

EntityType describes one catalog entity's URL shape. Searchable marks types that expose the /search?q= endpoint backing name resolution.

func Lookup

func Lookup(name string) (EntityType, error)

Lookup returns the entity type for a CLI name, erroring on unknown types so typos fail before any network call.

type Filter

type Filter struct {
	Field  string
	Values []string
}

Filter is one equality filter: filter[field]=v1,v2 on the wire. catalog-api maps these to Mongo $eq/$in queries.

type ListOptions

type ListOptions struct {
	Filters []Filter
	Start   int
	Limit   int
}

ListOptions narrows a list request.

type TokenSource

type TokenSource func(ctx context.Context) (string, error)

TokenSource supplies the bearer token per request. Returning an empty string sends no Authorization header.

type VolumeCreateFields added in v0.2.0

type VolumeCreateFields struct {
	Title        string                 `json:"title"`
	Description  string                 `json:"description,omitempty"`
	Notes        string                 `json:"notes,omitempty"`
	Format       string                 `json:"format,omitempty"`
	Tags         []string               `json:"tags,omitempty"`
	Properties   []modelcore.PropertyVO `json:"properties,omitempty"`
	PublisherIDs []string               `json:"publisherIds,omitempty"`
}

VolumeCreateFields is the POST /volumes wire body. It is deliberately not vo.VolumeVO: catalog-api's create endpoint (matching PATCH /volumes/:id) takes tags as plain names ([]string), not the {name,value} TagVO shape GET/list responses return - posting a VolumeVO's Tags field as-is 400s with "cannot unmarshal object into ... of type string".

type WriteDisposition

type WriteDisposition struct {
	Submitted bool
	Version   int
	State     string
	Message   string
}

WriteDisposition reports how the server treated a patch: applied live, or recorded as a proposed change awaiting review.

func Patch

func Patch[T any](ctx context.Context, c *Client, plural, id string, fields map[string]any) (*T, WriteDisposition, error)

Patch sends flat field updates ({name: "...", tags: [...]}) and returns the live record plus how the server disposed of the change. When disposition. Submitted is true the live record is nil and Version/State/Message describe the proposed change.

func SetVolumeCover

func SetVolumeCover(ctx context.Context, c *Client, assets *AssetsClient, volumeID string, image []byte, contentType string) (*vo.VolumeVO, WriteDisposition, error)

SetVolumeCover uploads image as the volume's live cover asset (cover/<volumeID> on assets-web) and links it via PATCH. Admins/editors get a live write back; submitters get a submitted change (the server rejects coverAssetId from submitter roles, surfacing that as an *APIError).

func SetVolumeSamples

func SetVolumeSamples(ctx context.Context, c *Client, assets *AssetsClient, volumeID string, samples [][]byte, contentType string) (*vo.VolumeVO, WriteDisposition, error)

SetVolumeSamples uploads each image as sample/<volumeID>-<i> (order preserved) and replaces the volume's sample list in one PATCH. An empty slice clears all samples.

Jump to

Keyboard shortcuts

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