spec

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package spec fetches, caches and revalidates an OpenAPI document.

Index

Constants

View Source
const (
	StatusFetched     = "fetched"     // the document is new or has changed
	StatusRevalidated = "revalidated" // confirmed unchanged, by 304 or by bytes
	StatusCached      = "cached"      // served from disk, network not consulted
	StatusStale       = "stale"       // the backend was unreachable
)

How a spec came to be in hand, which is worth reporting under --verbose. The distinction that matters is whether the document changed, not whether a full GET happened: a server with no ETag makes every run a full GET.

View Source
const NegativeTTL = 10 * time.Minute

NegativeTTL is how long blip remembers that an environment serves no spec. Without it, a config that declares routes instead of a spec would pay for four failed probes on every single command.

Variables

View Source
var ProbePaths = []string{
	"/openapi/v1.json",
	"/swagger/v1/swagger.json",
	"/openapi/v1.yaml",
	"/swagger/v1/swagger.yaml",
}

ProbePaths are tried in order when an environment does not name a spec_url. The first two cover .NET minimal APIs, from Microsoft.AspNetCore.OpenApi and from Swashbuckle respectively.

Functions

func CacheDir

func CacheDir(apiName, envName string) (string, error)

CacheDir is where the spec for one environment of one API is kept. The environment is part of the key because dev and prod can serve different specs.

func Probe added in v0.2.0

func Probe(ctx context.Context, client *http.Client, base *url.URL, paths []string) (string, bool)

Probe reports which of the given paths serves a spec, for scaffolding a config before one exists. It does not cache anything.

Types

type Fetcher

type Fetcher struct {
	Client *http.Client
	// Strict is used for any candidate that is not the API's own origin, so an
	// insecure granted for a localhost dev certificate cannot follow it away.
	Strict  *http.Client
	Refresh bool
	Offline bool
	Warnf   func(format string, args ...any)

	// Authorize applies credentials to a spec request. It is called only after an
	// unauthenticated attempt has been refused, so a public spec never reaches for
	// a vault.
	Authorize func(ctx context.Context, req *http.Request) error
}

Fetcher loads a spec, using the cache and the network according to the flags.

func (*Fetcher) Load

func (f *Fetcher) Load(ctx context.Context, apiName string, env *config.Environment) (*Spec, error)

Load returns the spec for an environment, from cache or from the network.

type Meta

type Meta struct {
	URL          string    `json:"url"`
	ETag         string    `json:"etag,omitempty"`
	LastModified string    `json:"last_modified,omitempty"`
	FetchedAt    time.Time `json:"fetched_at"`
}

Meta is what blip remembers about a cached spec between runs.

type Spec

type Spec struct {
	Data   []byte
	Meta   Meta
	Path   string
	Status string
}

Spec is a document plus where it came from.

Jump to

Keyboard shortcuts

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