openapi

package
v0.0.0-...-af5c341 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ApiInfo

type ApiInfo struct {
	OpenAPICli `json:"open_api_json"`
	Enable     `json:"enable"`
	// contains filtered or unexported fields
}

func NewOpenApiCli

func NewOpenApiCli(fileName string) (*ApiInfo, error)

func NewOpenApiCliEx

func NewOpenApiCliEx(apiJson, enableJson []byte) (*ApiInfo, error)

func (*ApiInfo) GetEnable

func (api *ApiInfo) GetEnable() *Enable

func (*ApiInfo) InitApis

func (api *ApiInfo) InitApis(filename string) error

func (*ApiInfo) InitEnable

func (api *ApiInfo) InitEnable(filePath string) error

func (*ApiInfo) SendGETMethod

func (api *ApiInfo) SendGETMethod(path string, parameters []Parameter) ([]byte, error)

func (*ApiInfo) SendPOSTMethod

func (api *ApiInfo) SendPOSTMethod(path string, body RequestBody) ([]byte, error)

func (*ApiInfo) SendPOSTMethodEx

func (api *ApiInfo) SendPOSTMethodEx(path, ct string, body []byte) ([]byte, error)

type Enable

type Enable struct {
	Get  map[string][]Parameter  `json:"get"`
	Post map[string]*RequestBody `json:"post"`
	Slot map[int]string          `json:"slot"` // slot_id:method-path
}

func (*Enable) SlotGetEnable

func (e *Enable) SlotGetEnable(slot int) (method, path string)

type EnableParams

type EnableParams struct {
	// 不需要考虑同时设置get和post参数的情况
	GetParams  map[string]string `json:"get_params"` // key is request param-name and value is param-value
	PostParams *RequestBody      `json:"post_params"`
	Slot       int               `json:"slot"`
}

type Info

type Info struct {
	Title   string `json:"title"`
	Version string `json:"version"`
}

type MediaType

type MediaType struct {
	Schema interface{} `json:"schema"` // 使用interface{}来表示未知的内容
}

MediaType 定义了请求内容的结构

func (*MediaType) GetSchema

func (api *MediaType) GetSchema() interface{}

func (*MediaType) GetSchemaContent

func (api *MediaType) GetSchemaContent() ([]byte, error)

type OpenAPICli

type OpenAPICli struct {
	OpenAPI string              `json:"openapi"`
	Info    Info                `json:"info"`
	Servers []Server            `json:"servers,omitempty"`
	Paths   map[string]PathItem `json:"paths"`
}

定义OpenAPI文档的结构体

func (*OpenAPICli) GetApiInfo

func (api *OpenAPICli) GetApiInfo() Info

func (*OpenAPICli) GetPaths

func (api *OpenAPICli) GetPaths() map[string]PathItem

type OpenApi

type OpenApi interface {
	SendGETMethod(path string, parameters []Parameter) ([]byte, error)
	SendPOSTMethod(path string, body RequestBody) ([]byte, error)
	SendPOSTMethodEx(path, ct string, body []byte) ([]byte, error)

	GetApiInfo() Info
	GetPaths() map[string]PathItem
	GetEnable() *Enable
}

func DisableApi

func DisableApi(doc OpenApi, slot int) (OpenApi, error)

func EnableApi

func EnableApi(doc OpenApi, params *EnableParams, path string) (OpenApi, error)

EnableApi 启用api中指定的方法,并设置参数,返回处理后的api

如果不需要添加参数,params 仍然应该不为 nil,但里面的参数全为空即可

func UpdateApi

func UpdateApi(doc OpenApi, params *EnableParams, path string) (OpenApi, error)

UpdateApi 更新api中指定的方法,并设置参数,返回处理后的api

与EnableApi类似但只能更新已启用的api和slot,, 如果slot = -1,api会自动寻找对应的slot,否则为覆盖 slot

type Operation

type Operation struct {
	Summary     string              `json:"summary"`
	RequestBody *RequestBody        `json:"requestBody,omitempty"` // POST
	Parameters  []Parameter         `json:"parameters,omitempty"`  // GET
	Responses   map[string]Response `json:"responses"`             // 200, 400等
}

Operation 定义了操作的详细信息,包括请求和响应

func (*Operation) GetParameters

func (api *Operation) GetParameters() []Parameter

func (*Operation) GetRequestBody

func (api *Operation) GetRequestBody() *RequestBody

func (*Operation) GetResponses

func (api *Operation) GetResponses() map[string]Response

func (*Operation) GetSummary

func (api *Operation) GetSummary() string

type Parameter

type Parameter struct {
	Name        string `json:"name"`
	In          string `json:"in"` // 通常是"query"、"header"等
	Description string `json:"description,omitempty"`
	Required    bool   `json:"required,omitempty"`
	Schema      Schema `json:"schema"`
}

Parameter 定义了GET请求的参数

func (*Parameter) Set

func (p *Parameter) Set(k, v string)

type PathItem

type PathItem struct {
	Get  *Operation `json:"get,omitempty"`
	Post *Operation `json:"post,omitempty"`
}

PathItem 包含多个HTTP方法的操作

func (*PathItem) GetGet

func (api *PathItem) GetGet() *Operation

func (*PathItem) GetPost

func (api *PathItem) GetPost() *Operation

type Property

type Property struct {
	Type string `json:"type"`
}

Property 定义了body中字段的类型

type RequestBody

type RequestBody struct {
	Description string               `json:"description"`
	Content     map[string]MediaType `json:"content"`
}

RequestBody 定义了POST请求的body结构

func (*RequestBody) GetContent

func (api *RequestBody) GetContent() map[string]MediaType

func (*RequestBody) GetDescription

func (api *RequestBody) GetDescription() string

func (*RequestBody) SetBodyWithJson

func (api *RequestBody) SetBodyWithJson(kv map[string]string)

type Response

type Response struct {
	Description string               `json:"description"`
	Content     map[string]MediaType `json:"content"` //key: such as application/json
}

Response 定义了接口的响应

func (*Response) GetContent

func (api *Response) GetContent() map[string]MediaType

type Schema

type Schema struct {
	Type       string              `json:"type"`
	Properties map[string]Property `json:"properties,omitempty"`
}

Schema 定义了JSON请求body的schema

func (*Schema) GetProperties

func (api *Schema) GetProperties() map[string]Property

type Server

type Server struct {
	URL         string `json:"url"`
	Description string `json:"description,omitempty"`
}

Server 结构体定义了服务器的URL和描述

Jump to

Keyboard shortcuts

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