Register an API
openapi_cli reads the spec and returns a CLI; install it with
register_cli, passing the same config:
GitHub publishes its REST API as an OpenAPI spec; this installs the
licenses and meta parts, which need no token:
auth, select the parts you need, such as tags: [pulls].
The whole GitHub spec has more than 1024 operations and is refused; see
What a spec must hold.
To build from a spec already in memory, pass it as
document:
await openapi_cli(config, document=spec).
How an API becomes a CLI
One spec is one CLI, and each operation in it one subcommand. GitHub’s spec describesGET /licenses/{license} as:
<name>__<in>
(id__path); a body that is not an object of fixed properties (one open
to more keys, nullable, or composed with allOf, oneOf or anyOf) goes
under body. Accept,
Content-Type and Authorization header parameters are left to the
client. A schema longer than 16,384 characters is cut in --help, and the
heading says where.
Coverage
An API whose spec uses something outside this list still installs when
tags or operations leave those operations out.
What a spec must hold
An agent learns what a command does from its description, so every selected operation needs one; a summary alone is not enough. Twilio’s main spec has 47 operations without one, and installing all of it is refused, naming each:operations: [ListCall, CreateCall, FetchCall] gives list-call,
create-call and fetch-call.
The whole spec is refused, with OpenAPISpecError naming every operation
at fault, when any selected operation:
- has no description;
- sends a body other than JSON or
application/x-www-form-urlencoded, or takes a cookie parameter; - has a path that does not start with
/; - is a
TRACE, or sends a body onGETorHEAD, which fetch cannot send; - names servers of its own, other than the spec’s;
- gives a form field an encoding other than the default while the form
goes under
bodywhole; - has a parameter without a name, or one not in the path, query, header or cookie.
openapi_cli also raises OpenAPISpecError when the spec cannot be read,
is not OpenAPI 3 (convert a Swagger 2.0 spec first), names a tag or an
operation it doesn’t have, selects no operation or more than 1024, has a
path item or operation that is not a mapping, has a $ref outside the
document or to nothing, or has two operations that become one command. Narrow the selection with tags
or operations to leave an operation out.
Authentication
Credentials go only to
base_url: a call whose URL would leave its origin
is refused, and a redirect from the API is never followed. The spec is
fetched without them, following redirects unless it carries headers.
base_url must be an http(s) URL with a host and no query, fragment or
credentials, and https whenever auth or headers are set, unless the
host is local (localhost, 127.0.0.1, ::1). A basic username cannot
hold :. The token is redacted in snapshots.
headers adds headers to every call, for an API that wants a User-Agent
naming the caller, a version header or a tenant ID. The spec fetch carries
them too when the spec is on base_url’s origin. Auth wins over a header of
the same name; values are checked like the token and redacted in snapshots.
Register from YAML
An API is aclis: entry with cli: openapi:
workspace.yaml
mirage workspace create workspace.yaml builds it on the server. A
program that loads the file itself passes the built CLIs to
to_workspace_kwargs, which raises ValueError without them:
network: also holds fetching the spec.
Call an operation
A path value is percent-encoded whole, a list repeats its query parameter,
a
null query or header value is left out, and a null body property is
sent as null, so a PATCH can clear a field; {"body": null} sends a
null body, while leaving body out sends none. A form field follows its
encoding: by default a list repeats its key (tags=a&tags=b) and an
object spreads its keys; deepObject writes brackets
(metadata[plan]=pro, as Stripe declares).
Flags instead of a JSON string
Withflags: true in its config, each subcommand takes one flag per
argument instead of a single JSON string, and sends the same request:
--help lists each option with its description’s first line and default,
and ends with the schema of the JSON options. A long option is never
abbreviated. An option left out is not sent, and a plain option cannot send
null. An option the CLI refuses (unknown, missing its value, a required
one left out, a value outside its choices or not a number) and any operand
exit 2 with nothing sent. A snapshot keeps the setting.
Profiles
A profile names an operation the way it names any CLI subcommand.GET,
HEAD, OPTIONS and TRACE operations run as reads; every other counts
as a write.
Snapshots
A snapshot keeps the selected operations, so loading one rebuilds the CLI without the spec. The token is redacted, so pass the config again:Example
examples/python/cli/openapi/ installs GitHub’s REST API from its
published spec, runs reads that need no token, shows the whole spec
refused, and writes the same CLI out as a module with
openapi_to_mirage.py: