Documentation
¶
Overview ¶
Package apicontract describes a query operation once, for both the OpenAPI document a client or an assistant reads and the validation the handler applies: the required inputs, the supported filters, paging and the response schema. A request naming a filter the operation does not list is refused with a problem document before the handler runs, so the surface the model is told about is the surface it gets.
var api = apicontract.New("Inventory", "/api/v1/inventory",
apicontract.Operation{Path: "/nodes/summary", ID: "summarizeNodes", Summary: "Count nodes per cluster",
Parameters: append([]apicontract.Parameter{apicontract.Query("cluster_id", "One cluster", false)}, apicontract.Paging()...),
ResponseSchema: apicontract.Schema[NodeSummary]()})
router.HandleFunc("/api/v1/inventory/openapi.json", auth.Bearer(api.ServeHTTP)).Methods("GET")
router.HandleFunc("/api/v1/inventory/nodes/summary", auth.Bearer(api.Wrap("GET", "/nodes/summary", summarizeNodes))).Methods("GET")
The document is also what instance.RouteTools overlays on the routes it walks (RouteToolsOptions.Contracts), so the generated tools carry the declared parameters instead of inferred ones.
Index ¶
Constants ¶
const ( // DefaultPageLimit is the page size when a request names none. DefaultPageLimit = 100 // MaxPageLimit is the largest page a request may ask for. MaxPageLimit = 200 )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Contract ¶
type Contract struct {
// contains filtered or unexported fields
}
Contract is a set of operations and their OpenAPI document.
func New ¶
New builds the contract and its document. base is the path every operation's Path is relative to. It panics on an operation without an ID or summary, as a missing contract would otherwise surface only to the model.
func (*Contract) ServeHTTP ¶
func (c *Contract) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP serves the document.
func (*Contract) Wrap ¶
func (c *Contract) Wrap(method, path string, next http.HandlerFunc) http.HandlerFunc
Wrap validates a request against the operation registered for method and path (relative to base) and answers a problem document for unknown filters, missing required inputs and values outside a parameter's schema; valid requests reach next. It panics when the operation is not in the contract. The accountID and userID query parameters are always accepted, because the platform's ID middleware reads them.
type Operation ¶
type Operation struct {
Method string `json:"-"`
Path string `json:"-"`
ID string `json:"operationId"`
Summary string `json:"summary"`
Description string `json:"description"`
Parameters []Parameter `json:"parameters"`
Responses map[string]any `json:"responses"`
ResponseSchema map[string]any `json:"-"`
}
Operation is one documented operation. Method defaults to GET; ID and Summary are required. Responses is generated from ResponseSchema when empty.
type Page ¶
type Page struct {
Total int `json:"total"`
Limit int `json:"limit"`
Offset int `json:"offset"`
HasMore bool `json:"has_more"`
NextOffset *int `json:"next_offset,omitempty"`
}
Page describes one page of a result: totals precede paging, so a reader knows the size of the whole filtered result before walking it.
func Pagination ¶
Pagination reads limit and offset from the request (see Paging) and places them against total.
type Parameter ¶
type Parameter struct {
Name string `json:"name"`
In string `json:"in"`
Description string `json:"description"`
Required bool `json:"required,omitempty"`
Schema map[string]any `json:"schema"`
}
Parameter is one query parameter of an operation.