operations

package
v3.1.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package operations provides HTTP contracts for asynchronous API operations.

It standardizes 202 Accepted responses, pollable operation resources, repository contracts, and lifecycle transition helpers while leaving queues, workers, persistence, and retry policy to application code or contrib adapters.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidState reports an unknown operation state.
	ErrInvalidState = errors.New("invalid operation state")
	// ErrInvalidTransition reports a disallowed operation lifecycle transition.
	ErrInvalidTransition = errors.New("invalid operation transition")
)

Functions

func CanTransition

func CanTransition(from, to State) bool

CanTransition reports whether an operation may move from one state to another.

func IsTerminal

func IsTerminal(state State) bool

IsTerminal reports whether state is a completed operation state.

func PollHandler

func PollHandler[T any](config PollConfig[T]) http.Handler

PollHandler returns an HTTP handler for polling operation state.

func ValidateState

func ValidateState(state State) error

ValidateState reports whether state is a known operation lifecycle state.

func WriteAccepted

func WriteAccepted(w http.ResponseWriter, config AcceptedConfig)

WriteAccepted writes a 202 Accepted response with Location and Retry-After headers when configured.

func WriteOperation

func WriteOperation[T any](w http.ResponseWriter, status int, operation Operation[T])

WriteOperation writes an operation resource as JSON.

Types

type Accepted

type Accepted struct {
	ID       string `json:"id,omitempty"`
	State    State  `json:"state"`
	Location string `json:"location,omitempty"`
}

Accepted is the JSON body for a 202 Accepted operation response.

type AcceptedConfig

type AcceptedConfig struct {
	ID         string
	Location   string
	RetryAfter time.Duration
}

AcceptedConfig configures WriteAccepted.

type Operation

type Operation[T any] struct {
	ID      string         `json:"id"`
	State   State          `json:"state"`
	Result  *T             `json:"result,omitempty"`
	Problem *httpx.Problem `json:"problem,omitempty"`
}

Operation is a pollable asynchronous operation resource.

func TransitionOperation

func TransitionOperation[T any](operation Operation[T], config TransitionConfig[T]) (Operation[T], error)

TransitionOperation applies a validated lifecycle transition.

type PollConfig

type PollConfig[T any] struct {
	Store       Store[T]
	OperationID func(*http.Request) string
	ErrorWriter func(http.ResponseWriter, int, httpx.Problem)
}

PollConfig configures PollHandler.

type Repository

type Repository[T any] interface {
	Store[T]
	Writer[T]
}

Repository loads and writes operation resources.

type State

type State string

State describes the lifecycle state of an asynchronous operation.

const (
	// StatePending means work has been accepted but not started.
	StatePending State = "pending"
	// StateRunning means work is in progress.
	StateRunning State = "running"
	// StateSucceeded means work completed successfully.
	StateSucceeded State = "succeeded"
	// StateFailed means work completed with a failure problem.
	StateFailed State = "failed"
	// StateCanceled means work was canceled before successful completion.
	StateCanceled State = "canceled"
)

type Store

type Store[T any] interface {
	GetOperation(ctx context.Context, id string) (Operation[T], bool, error)
}

Store loads operation resources for polling handlers.

type StoreFunc

type StoreFunc[T any] func(context.Context, string) (Operation[T], bool, error)

StoreFunc adapts a function to Store.

func (StoreFunc[T]) GetOperation

func (f StoreFunc[T]) GetOperation(ctx context.Context, id string) (Operation[T], bool, error)

GetOperation loads an operation resource.

type TransitionConfig

type TransitionConfig[T any] struct {
	To      State
	Result  *T
	Problem *httpx.Problem
}

TransitionConfig configures a lifecycle state transition.

type Writer

type Writer[T any] interface {
	CreateOperation(ctx context.Context, operation Operation[T]) error
	UpdateOperation(ctx context.Context, operation Operation[T]) error
}

Writer creates and updates operation resources.

type WriterFuncs

type WriterFuncs[T any] struct {
	Create func(context.Context, Operation[T]) error
	Update func(context.Context, Operation[T]) error
}

WriterFuncs adapts functions to Writer.

func (WriterFuncs[T]) CreateOperation

func (f WriterFuncs[T]) CreateOperation(ctx context.Context, operation Operation[T]) error

CreateOperation creates an operation resource.

func (WriterFuncs[T]) UpdateOperation

func (f WriterFuncs[T]) UpdateOperation(ctx context.Context, operation Operation[T]) error

UpdateOperation updates an operation resource.

Jump to

Keyboard shortcuts

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