api

package
v0.4.0-rc1 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2023 License: Apache-2.0 Imports: 22 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.

Types

type API

type API struct {
	Version    string      `json:"version"`
	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.

func LoadOpenAPI

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

LoadOpenAPI load OpenAPI schema from response body and generate API.

func (*API) GenerateCommand

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

GenerateCommand generate command from api and add to root command.

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]interface{}.

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"`
}

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"`
}

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.

Jump to

Keyboard shortcuts

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