api

package
v0.5.1-rc2 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2024 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ValueTypeObjectID        = "objectID"
	ValueTypeArrayObject     = "array[object]"
	ValueTypeArrayString     = "array[string]"
	ValueTypeArrayNumber     = "array[number]"
	ValueTypeArrayInt        = "array[integer]"
	ValueTypeArrayBoolean    = "array[boolean]"
	ValueTypeMapStringInt64  = "map[string]int64"
	ValueTypeMapStringInt32  = "map[string]int32"
	ValueTypeMapStringInt    = "map[string]int"
	ValueTypeMapStringString = "map[string]string"
)

Value types extend the OpenAPI specification to support more types.

View Source
const (
	// JsonMediaType is support request body media type.
	JsonMediaType = "application/json"
)

Variables

This section is empty.

Functions

func AddFlag

func AddFlag(name, schemaType, description string, value any, flags *pflag.FlagSet) any

AddFlag create flag with name, type, description, default value, and add it to flagSet.

func InitOpenAPI added in v0.5.0

func InitOpenAPI(sc *config.Config, skipCache bool) error

InitOpenAPI load from cache or remote and setup command.

func InitOpenAPIFromSchema added in v0.5.0

func InitOpenAPIFromSchema(t openapi3.T) error

InitOpenAPIFromSchema load from schema and setup command.

Types

type API

type API struct {
	Version    `json:",inline"`
	Short      string      `json:"short"`
	Long       string      `json:"long,omitempty"`
	Operations []Operation `json:"operations,omitempty"`
}

API represents an abstracted API description, include details used to build CLI commands.

var OpenAPI *API

func LoadOpenAPIFromResp added in v0.5.0

func LoadOpenAPIFromResp(resp *http.Response) (*API, error)

LoadOpenAPIFromResp load OpenAPI schema from response body and generate API.

func LoadOpenAPIFromSchema added in v0.5.0

func LoadOpenAPIFromSchema(t openapi3.T) (*API, error)

LoadOpenAPIFromSchema load the OpenAPI from schema.

func (*API) GenerateCommand

func (api *API) GenerateCommand(sc *config.Config, root *cobra.Command)

GenerateCommand generate command from api and add to root command.

func (*API) GetOperation added in v0.5.0

func (api *API) GetOperation(group, name string) *Operation

type ArrayObjectFlag

type ArrayObjectFlag []any

ArrayObjectFlag creates a custom flag for []interface.

func (*ArrayObjectFlag) Set

func (i *ArrayObjectFlag) Set(value string) error

Set a new value on the flag.

func (*ArrayObjectFlag) String

func (i *ArrayObjectFlag) String() string

String returns a string represent of this flag.

func (*ArrayObjectFlag) Type

func (i *ArrayObjectFlag) Type() string

Type returns the type of this custom flag, which will be displayed in `--help` output.

type BodyParam

type BodyParam struct {
	Name        string `json:"name,omitempty"`
	Type        string `json:"type,omitempty"`
	Description string `json:"description,omitempty"`
	Default     any    `json:"default,omitempty"`
}

BodyParam represents each field in body.

func (BodyParam) AddFlag

func (b BodyParam) AddFlag(flags *pflag.FlagSet) any

AddFlag adds a new option flag to a command's flag set for this body param.

func (BodyParam) OptionName

func (b BodyParam) OptionName() string

OptionName returns the commandline option name for this parameter.

type BodyParams

type BodyParams struct {
	Type   string       `json:"type,omitempty"`
	Params []*BodyParam `json:"params,omitempty"`
}

BodyParams represent request body and params type.

type DataFrom

type DataFrom string
const (
	DataFromContextAndArg DataFrom = "contextAndArg"
	DataFromArg           DataFrom = "arg"
	DataFromFlag          DataFrom = "flag"
)

type ObjectFlag

type ObjectFlag map[string]any

ObjectFlag creates a custom flag for map[string]any.

func (*ObjectFlag) Set

func (i *ObjectFlag) Set(value string) error

Set a new value on the flag.

func (*ObjectFlag) String

func (i *ObjectFlag) String() string

String returns a string represent of this flag.

func (*ObjectFlag) Type

func (i *ObjectFlag) Type() string

Type returns the type of this custom flag, which will be displayed in `--help` output.

type ObjectIDFlag

type ObjectIDFlag map[string]string

ObjectIDFlag creates a custom flag for map[string]string{"id": "xxx"}.

func (ObjectIDFlag) Set

func (i ObjectIDFlag) Set(value string) error

Set a new value on the flag.

func (ObjectIDFlag) String

func (i ObjectIDFlag) String() string

String returns a string represent of this flag.

func (ObjectIDFlag) Type

func (i ObjectIDFlag) Type() string

Type returns the type of this custom flag, which will be displayed in `--help` output.

type Operation

type Operation struct {
	Name          string      `json:"name"`
	Group         string      `json:"group,omitempty"`
	Short         string      `json:"short,omitempty"`
	Long          string      `json:"long,omitempty"`
	Method        string      `json:"method,omitempty"`
	URITemplate   string      `json:"uriTemplate"`
	URIParams     []string    `json:"uriParams"`
	PathParams    []*Param    `json:"pathParams,omitempty"`
	QueryParams   []*Param    `json:"queryParams,omitempty"`
	HeaderParams  []*Param    `json:"headerParams,omitempty"`
	BodyParams    *BodyParams `json:"bodyParams,omitempty"`
	BodyMediaType string      `json:"bodyMediaType,omitempty"`
	Hidden        bool        `json:"hidden,omitempty"`
	Deprecated    string      `json:"deprecated,omitempty"`
	Formats       []string    `json:"formats,omitempty"`
	TableColumns  []string    `json:"tableColumns,omitempty"`

	// CmdIgnore is used to ignore the operation when generating CLI commands.
	CmdIgnore bool `json:"cmdIgnore,omitempty"`
}

Operation represents an API action, e.g. list-things or create-user.

func (Operation) Command

func (o Operation) Command(sc *config.Config) *cobra.Command

Command returns a Cobra command instance for this operation.

func (Operation) Request

func (o Operation) Request(
	cmd *cobra.Command,
	args []string,
	flags map[string]any,
	body any,
	sc config.ServerContext,
) (*http.Request, error)

Request generate http request base on the operation.

type Param

type Param struct {
	Name        string   `json:"name"`
	Type        string   `json:"type"`
	Description string   `json:"description,omitempty"`
	Style       string   `json:"style,omitempty"`
	Explode     bool     `json:"explode,omitempty"`
	Default     any      `json:"default,omitempty"`
	DataFrom    DataFrom `json:"dataFrom,omitempty"`

	// CmdIgnore is used to ignore the operation when generating CLI commands.
	CmdIgnore bool `json:"cmdIgnore,omitempty"`
}

Param represents an API operation input parameter.

func (Param) AddFlag

func (p Param) AddFlag(flags *pflag.FlagSet) any

AddFlag adds a new option flag to a command's flag set for this parameter.

func (Param) OptionName

func (p Param) OptionName() string

OptionName returns the formatted commandline option name for this parameter.

func (Param) Serialize

func (p Param) Serialize(value any) []string

Serialize the parameter based on the type/style/explode.

type Version added in v0.5.0

type Version struct {
	Version      string `json:"version" yaml:"version"`
	GitCommit    string `json:"gitCommit" yaml:"gitCommit"`
	IsDevVersion bool   `json:"isDevVersion" yaml:"isDevVersion"`
}

Version include the version and commit.

func GetAPIVersionFromCache added in v0.5.0

func GetAPIVersionFromCache() *Version

GetAPIVersionFromCache load api version from cache.

type VersionInfo added in v0.5.0

type VersionInfo struct {
	ClientVersion  Version `json:"clientVersion,omitempty" yaml:"clientVersion,omitempty"`
	ServerVersion  Version `json:"serverVersion,omitempty" yaml:"serverVersion,omitempty"`
	OpenAPIVersion Version `json:"openAPIVersion,omitempty" yaml:"-"`
}

VersionInfo include the client, server and openapi version.

func GetVersion added in v0.5.0

func GetVersion(sc *config.Config) (*VersionInfo, error)

GetVersion get client, server and openapi version.

Jump to

Keyboard shortcuts

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