operations

package
v2.1.0 Latest Latest
Warning

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

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

Documentation

Overview

Package operations provides HTTP contracts for asynchronous API operations.

It standardizes 202 Accepted responses and pollable operation resources while leaving queues, workers, persistence, and retry policy to application code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PollHandler

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

PollHandler returns an HTTP handler for polling operation 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.

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 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.

Jump to

Keyboard shortcuts

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