Documentation
¶
Overview ¶
Package api is the CLI layer over the platform's own OpenAPI documents. It backs `dtctl get apis`, `dtctl describe api`, and the request classification `dtctl exec api` gates on.
It delegates all HTTP and parsing to sdk/api/apispec and adds the parts that are dtctl's rather than the platform's: which APIs already have a native command, how a specification-declared scope maps onto a safety operation, and what the projections look like on screen.
Index ¶
- Constants
- func CanonicalRequestPath(requestPath string) (string, error)
- func CoveredBasePaths() map[string]Coverage
- func DestructivePatternCount() int
- func ExampleInvocation(url string, op Operation) string
- func IsAbsoluteURL(s string) bool
- func NativeResourceFor(basePath string) string
- func QuoteArg(s string) string
- func RawSpecEncoding(e Entry) string
- type APIDescription
- type APIInfo
- type BlockedError
- type Classification
- type Coverage
- type Entry
- type Handler
- func (h *Handler) Describe(e Entry) (*APIDescription, error)
- func (h *Handler) DescribeOperation(e Entry, operationID string) (*OperationDetail, error)
- func (h *Handler) List(opts ListOptions) ([]APIInfo, error)
- func (h *Handler) RawSpec(e Entry) ([]byte, error)
- func (h *Handler) Registry() (*Registry, error)
- func (h *Handler) Resolve(query string) (*Entry, error)
- func (h *Handler) ResolveForRequest(method, requestPath string) (apiName string, op *Operation)
- func (h *Handler) Spec(e Entry) (*Spec, error)
- type ListOptions
- type MediaType
- type Operation
- type OperationDetail
- type OperationSummary
- type Parameter
- type Registry
- type RegistryUnavailableError
- type RequestBody
- type Response
- type SchemaField
- type Spec
- type SpecUnavailableError
Constants ¶
const SwaggerUIPath = "/platform/swagger-ui/index.html"
SwaggerUIPath is the human-facing API explorer every environment serves. It is the documented entry point, and therefore the fallback to name whenever the machine-readable index is unavailable.
Variables ¶
This section is empty.
Functions ¶
func CanonicalRequestPath ¶
CanonicalRequestPath returns the spelling of a request path that resolution and classification must run on, refusing spellings that change meaning under the normalization a server-side router performs before matching a route.
The gate reasons about paths textually — the destructive table is a set of patterns, specification templates are segment patterns — while the platform router decodes and normalizes before routing. Any spelling a router reduces to a *different* path is therefore a way to make the text the gate sees disagree with the endpoint the server executes. Those spellings are refused outright: no legitimate caller writes `x/../y`, `a//b`, or an encoded `/`, and gating them "correctly" would mean guessing which router semantics the environment uses.
Benign percent-encoding is different: `%20` in an identifier is the only way to spell a space, so it is accepted — and the *decoded* form is returned, because that is the path the server routes on. `%3A` and `:` must classify identically, or encoding a colon would spell `:truncate` past the destructive table.
func CoveredBasePaths ¶
CoveredBasePaths returns every base path with native coverage. Used by the drift test that keeps the map honest.
func DestructivePatternCount ¶
func DestructivePatternCount() int
DestructivePatternCount reports how many curated escalations exist. The test that pins the table uses it to notice a silently emptied list.
func ExampleInvocation ¶
ExampleInvocation renders a ready-to-run `dtctl exec api` command for an operation, with path placeholders left in place so the caller sees exactly what it has to substitute.
func IsAbsoluteURL ¶
IsAbsoluteURL reports whether a target is an absolute URL rather than a path on the current environment. `exec api` refuses the former: the context's credentials belong to the environment it names.
func NativeResourceFor ¶
NativeResourceFor returns the dtctl resource covering an API base path, or "" when the API has no native command.
func QuoteArg ¶
QuoteArg single-quotes a value that a shell would otherwise split, so a suggested command can be pasted and run. API names contain spaces, and a suggestion the caller has to repair is a suggestion that gets retyped wrong.
func RawSpecEncoding ¶
RawSpecEncoding reports the on-disk encoding of a raw specification document, derived from the document's own path rather than sniffed from its bytes: the same artifact is served under four different content types, so the filename is the only reliable signal.
Types ¶
type APIDescription ¶
type APIDescription struct {
Name string `json:"name" yaml:"name"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Version string `json:"version,omitempty" yaml:"version,omitempty"`
Category string `json:"category,omitempty" yaml:"category,omitempty"`
Summary string `json:"summary,omitempty" yaml:"summary,omitempty"`
BasePath string `json:"base_path" yaml:"base_path"`
Dtctl string `json:"dtctl,omitempty" yaml:"dtctl,omitempty"`
Operations []OperationSummary `json:"operations" yaml:"operations"`
}
APIDescription is the operation index for one API.
type APIInfo ¶
type APIInfo struct {
Name string `json:"name" yaml:"name" table:"NAME"`
BasePath string `json:"base_path" yaml:"base_path" table:"BASE PATH"`
// Category comes from the specification, so it is known only when the
// specification was fetched (--ops-count). It is a wide column rather than a
// default one for exactly that reason.
Category string `json:"category,omitempty" yaml:"category,omitempty" table:"CATEGORY,wide"`
// Operations is the operation count, populated only when specifications were
// fetched (--ops-count). A nil pointer means "not counted", which renders as
// a blank cell rather than a misleading 0.
Operations *int `json:"operations,omitempty" yaml:"operations,omitempty" table:"OPS"`
// Dtctl names the native dtctl resource covering this API, or is empty.
Dtctl string `json:"dtctl,omitempty" yaml:"dtctl,omitempty" table:"DTCTL"`
// External marks an entry documented at an absolute URL on another host.
// dtctl lists it (the environment published it) but cannot fetch it, and it
// has no gateway base path — which is why the column is worth showing.
External bool `json:"external,omitempty" yaml:"external,omitempty" table:"EXTERNAL,wide"`
// SpecError records why this row could not be enriched, when a specification
// fetch failed. One bad document degrades its own row and never fails the
// listing.
SpecError string `json:"spec_error,omitempty" yaml:"spec_error,omitempty" table:"-"`
}
APIInfo is one row of `dtctl get apis`.
The DTCTL column is the coverage report: it names the native resource that wraps the API, or is blank. That makes the gap between what the platform publishes and what dtctl wraps visible and, via --uncovered, actionable.
type BlockedError ¶
type BlockedError struct {
Method string
RequestPath string
Class Classification
// NativeCommand is the dtctl command covering this path as a caller would type
// it, or "" when none does.
NativeCommand string
// Cause is the underlying safety refusal.
Cause error
}
BlockedError explains a refusal in terms a caller can act on, naming the native command when one covers the path.
"Denied" with no next step is what makes an agent start guessing, so the message always offers both routes: the native command (preferred) and the context change that would permit the request.
func (*BlockedError) Error ¶
func (e *BlockedError) Error() string
func (*BlockedError) Headline ¶
func (e *BlockedError) Headline() string
Headline is the refusal in one line, without the appended explanations. The envelope carries those as suggestions instead.
func (*BlockedError) Suggestions ¶
func (e *BlockedError) Suggestions() []string
Suggestions are the routes out of the refusal: the native command when one covers the path, the operation's own documentation when the specification matched, and whatever the safety checker advised (usually the context change that would permit it).
func (*BlockedError) Unwrap ¶
func (e *BlockedError) Unwrap() error
Unwrap exposes the safety refusal, so errors.As still finds it. The agent envelope keeps its own case ahead of the generic safety one, because the classification — why this request counts as a delete — is the part that tells a caller what to do differently.
type Classification ¶
type Classification struct {
// SafetyOp is the operation the request is gated as.
SafetyOp safety.Operation
// Resolved reports whether a specification determined the access level. When
// false, the gate is the strict fallback rather than a derived answer, and
// the caller should say so.
Resolved bool
// Access is the specification-derived access level, or AccessUnknown.
Access apispec.Access
// Escalated reports that the verdict came from the curated destructive table
// rather than from the method/specification floors. It is a *deliberate*
// classification even when Resolved is false, so an error message must not
// apologise for being unable to classify the request.
Escalated bool
// Scopes are the scopes the specification declares for the matched operation.
// Empty means the specification declares none — which is common and is not
// the same as "none required".
Scopes []string
// APIName and OperationID identify what the request path matched, when it
// matched anything. They are what makes an error message actionable.
APIName string
OperationID string
// Reason explains the verdict in one sentence, for the error message and for
// --dry-run.
Reason string
}
Classification is dtctl's verdict on what a generic request may do, and why.
Nothing here is asserted by the caller. There is deliberately no flag that lets a caller declare the operation: a caller-asserted classification is a bypass, and the place the gate matters most is exactly where an agent is most likely to reach for the flag that makes the error go away.
func Classify ¶
func Classify(method, requestPath string, apiName string, op *Operation) Classification
Classify determines how a generic request must be gated.
The verdict is the stricter of two independent lower bounds — what the method guarantees and what the specification declares — then escalated for paths whose destructiveness a URL cannot convey (see [escalateDestructive]). Taking the stricter of two floors, rather than reading one table, means neither source can weaken the other: a specification cannot turn a DELETE into a read, and a method cannot turn a declared delete into a create.
spec may be nil, and op may be nil when no operation matched. Both are ordinary outcomes: an environment may not publish the specification, the path may belong to no listed API, or the API may declare nothing per operation. In every such case classification still returns a usable verdict rather than refusing to run.
type Coverage ¶
type Coverage struct {
// Resource is the short label shown in the DTCTL column of `dtctl get apis`.
Resource string
// Command is the command a caller should prefer, spelled as they would type
// it. It is deliberately not derived from Resource: not every wrapped API is
// reached through `get <resource>` — some have a verb of their own
// (`dtctl query`), some live under a nested exec subcommand, and the plural
// is not always the resource name. A suggestion that does not run is worse
// than none, because it teaches a caller that dtctl's advice is unreliable.
Command string
}
Coverage records what dtctl already offers for an API base path.
func NativeCoverageForPath ¶
NativeCoverageForPath returns the coverage for a concrete request path, matching on the longest base path that prefixes it. It is what `exec api` uses to notice that a caller is bypassing a native command.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler resolves and projects the specifications one environment publishes.
func NewHandler ¶
NewHandler creates an API-specification handler.
func (*Handler) Describe ¶
func (h *Handler) Describe(e Entry) (*APIDescription, error)
Describe projects a specification to its operation index.
func (*Handler) DescribeOperation ¶
func (h *Handler) DescribeOperation(e Entry, operationID string) (*OperationDetail, error)
DescribeOperation resolves one operation of a specification in full. The operation is addressed as "METHOD /path" (its [Operation.ID]).
func (*Handler) List ¶
func (h *Handler) List(opts ListOptions) ([]APIInfo, error)
List projects the environment's registry into displayable rows.
dtctl mirrors the registry: it adds nothing and filters nothing. Whatever this returns, the environment's own Swagger UI already shows, because both read the same index. Any client-side filtering would have to hard-code knowledge of which APIs to conceal — and since dtctl is open source, that list would itself be the disclosure. Deciding what to publish belongs to whoever configures the environment.
func (*Handler) RawSpec ¶
RawSpec returns the specification document exactly as the environment served it, for `describe api --raw`.
func (*Handler) Resolve ¶
Resolve maps a user-supplied name or base path to a registry entry.
Resolution consults only what the registry returned. A miss is a miss: dtctl never synthesizes a candidate path from a name and retries it, which would turn a name lookup into an existence oracle for APIs an environment deliberately omits from its own index.
An explicit, fully-qualified base path is accepted as given — the caller already knows it, so echoing it back discloses nothing.
func (*Handler) ResolveForRequest ¶
ResolveForRequest identifies the API and the operation a concrete request path belongs to.
Every failure is soft and reported as "not resolved": the environment may publish no index, the path may belong to no listed API, the document may be unreadable, and the specification may declare nothing for that path+method. All four are ordinary, and none of them may stop a request — the classifier gates an unresolved request strictly instead (see Classify).
type ListOptions ¶
type ListOptions struct {
// Uncovered filters to APIs with no native dtctl command.
Uncovered bool
// OpsCount fetches every specification to fill in the operation count. It
// fans out one request per API, so the default listing leaves it off and
// costs exactly one request.
OpsCount bool
}
ListOptions shapes `dtctl get apis`.
type OperationDetail ¶
type OperationDetail struct {
API string `json:"api" yaml:"api"`
BasePath string `json:"base_path" yaml:"base_path"`
Operation string `json:"operation" yaml:"operation"`
OperationID string `json:"operation_id,omitempty" yaml:"operation_id,omitempty"`
Summary string `json:"summary,omitempty" yaml:"summary,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Deprecated bool `json:"deprecated,omitempty" yaml:"deprecated,omitempty"`
// URL is the full request path to call, base path plus operation path, so the
// caller does not have to concatenate them (and cannot get it wrong).
URL string `json:"url" yaml:"url"`
// RequiredScopes is what the document declares. Empty means it declares
// nothing — see [Classification] for what dtctl does in that case.
RequiredScopes []string `json:"required_scopes,omitempty" yaml:"required_scopes,omitempty"`
Parameters []apispec.Parameter `json:"parameters,omitempty" yaml:"parameters,omitempty"`
RequestBody *apispec.RequestBody `json:"request_body,omitempty" yaml:"request_body,omitempty"`
Responses []apispec.Response `json:"responses,omitempty" yaml:"responses,omitempty"`
// Example is a ready-to-run `dtctl exec api` invocation for this operation.
Example string `json:"example" yaml:"example"`
}
OperationDetail is one operation in full — everything needed to compose the call without the rest of the document.
The bar here is deliberately higher than "a summary of the operation": composing a real request needs every parameter with its type and required-ness, the request body schema, and the response shapes. This is the prerequisite that makes `exec api` usable at all, which is why the operation index and this view are two grains of the same feature rather than alternatives.
type OperationSummary ¶
type OperationSummary struct {
Operation string `json:"operation" yaml:"operation" table:"OPERATION"`
Summary string `json:"summary,omitempty" yaml:"summary,omitempty" table:"SUMMARY"`
// Scope is the specification-declared scope, or empty when the document
// declares none. Empty is meaningful and common: it is why `exec api` cannot
// treat a declared scope as guaranteed.
Scope string `json:"scope,omitempty" yaml:"scope,omitempty" table:"SCOPE"`
Deprecated bool `json:"deprecated,omitempty" yaml:"deprecated,omitempty" table:"DEPRECATED,wide"`
}
OperationSummary is one line of an operation index — the compact projection `dtctl describe api <name>` emits.
A raw specification runs from 10k to 47k tokens, which is unusable for an agent and unreadable for a human. Projecting to this shape costs roughly 300 tokens for a 38-operation API. It is a complete view at a coarser grain, not a truncation: every operation appears, and the full detail of any one of them is one drill-down away.
type RegistryUnavailableError ¶
type RegistryUnavailableError = apispec.RegistryUnavailableError
RegistryUnavailableError reports an environment with no usable API index.
type RequestBody ¶
type RequestBody = apispec.RequestBody
RequestBody is an operation's request body across its media types.
type SchemaField ¶
type SchemaField struct {
Name string `json:"name" yaml:"name"`
Type string `json:"type,omitempty" yaml:"type,omitempty"`
Required bool `json:"required,omitempty" yaml:"required,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
// Nested marks a field whose own shape is not shown here, so a caller knows
// to look at the full schema rather than assuming a scalar.
Nested bool `json:"nested,omitempty" yaml:"nested,omitempty"`
}
SchemaField is one property of a schema, projected one level deep.
A request-body schema is the part of an operation a caller most needs and the part that is least readable raw: nested objects, allOf compositions and unresolved `$ref`s. One level is enough to answer "what do I put in the body", and the full schema is always one `-o yaml` away — the same complete-at-a-coarser-grain trade the operation index makes.
func TopLevelFields ¶
func TopLevelFields(schema any) ([]SchemaField, bool)
TopLevelFields projects an object schema's own properties. The bool reports whether the schema was introspectable at all: a schema that is a bare `$ref`, an `allOf` composition or an untyped free-form object yields false, which a caller should render as "not introspectable" rather than "no fields".
type SpecUnavailableError ¶
type SpecUnavailableError = apispec.SpecUnavailableError
SpecUnavailableError reports a listed specification that could not be read.