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 ¶
- func PollHandler[T any](config PollConfig[T]) http.Handler
- func WriteAccepted(w http.ResponseWriter, config AcceptedConfig)
- func WriteOperation[T any](w http.ResponseWriter, status int, operation Operation[T])
- type Accepted
- type AcceptedConfig
- type Operation
- type PollConfig
- type State
- type Store
- type StoreFunc
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 ¶
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.