Documentation
¶
Index ¶
- func Validate(value any, paramType string) error
- type Body
- type Endpoint
- func (e *Endpoint) BodySchema(prefixes ...string) string
- func (e *Endpoint) HasRequestBody() bool
- func (e *Endpoint) MethodName() string
- func (e *Endpoint) MustMakeUrl(params map[string][]any) string
- func (e *Endpoint) ParamNames() []string
- func (e *Endpoint) String() string
- func (e *Endpoint) Validate(params map[string][]any, headers map[string][]string, body any) error
- type OpenApiSpec
- func (o *OpenApiSpec) GetOperationName(methodName string) string
- func (o *OpenApiSpec) MethodNames() []string
- func (o *OpenApiSpec) New() (err error)
- func (o *OpenApiSpec) NewFromBytes(source []byte) (*OpenApiSpec, error)
- func (o *OpenApiSpec) NewFromSource(source string) (*OpenApiSpec, error)
- func (o *OpenApiSpec) OperationNames() []string
- func (o *OpenApiSpec) ReadSource() ([]byte, error)
- type Param
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Body ¶
type Body struct {
// MapSchema is the JSON schema for the request body in map format.
MapSchema map[string]any
// JsonSchema is the JSON schema for the request body in as a parsed jsonschema validator.
JsonSchema *jsonschema.Schema
// Yaml is the YAML representation of the request body schema.
Yaml string
// Json is the JSON representation of the request body schema.
Json string
}
Body represents the request body of an API endpoint.
type Endpoint ¶
type Endpoint struct {
// Name is the name of the endpoint.
Name string
// Summary is a brief summary of the endpoint.
Summary string
// Description is a detailed description of the endpoint.
Description string
// Method is the HTTP method of the endpoint.
Method string
// Path is the URL path of the endpoint.
Path string
// Params is a map of parameters that the endpoint accepts. The key is the name
// of the parameter, and the value is the parameter itself.
Params map[string]*Param
// Body is the request body of the endpoint, if any.
Body *Body
// contains filtered or unexported fields
}
Endpoint represents an API endpoint.
func MakeEndpoint ¶
MakeEndpoint creates an endpoint from an OpenAPI operation.
func (*Endpoint) BodySchema ¶
BodySchema returns the JSON schema for the endpoint's request body.
func (*Endpoint) HasRequestBody ¶
HasRequestBody returns true if the endpoint has a request body.
func (*Endpoint) MethodName ¶
MethodName returns the CamelCase method name for the endpoint.
This converts _, -, and camelCase/PascalCase boundaries into words that are joined as CamelCase. For example, "accounts_verify-email" and "accountsVerifyEmail" both become "AccountsVerifyEmail".
func (*Endpoint) ParamNames ¶
func (*Endpoint) Validate ¶
Validate checks the supplied params, headers, and body against the endpoint's declared schema.
Spec-declared header parameters MUST be supplied via headers (i.e. request.Header(...)), not via params (request.Param(...)). Passing a spec-declared header parameter through params returns an error pointing the caller at the right helper. Headers that are not declared in the spec are passed through untouched (e.g. ad-hoc Authorization, Content-Type).
type OpenApiSpec ¶
type OpenApiSpec struct {
// Source is the OpenAPI specification schema. It can be raw JSON or YAML, a
// file:// URL, or an HTTP URL
Source string
// RawSchema is the raw bytes of the OpenAPI schema
RawSchema []byte
Endpoints map[string]*Endpoint
// contains filtered or unexported fields
}
func (*OpenApiSpec) GetOperationName ¶
func (o *OpenApiSpec) GetOperationName(methodName string) string
func (*OpenApiSpec) MethodNames ¶
func (o *OpenApiSpec) MethodNames() []string
MethodNames the CamelCase method names for each endpoint.
func (*OpenApiSpec) New ¶
func (o *OpenApiSpec) New() (err error)
New builds a new OpenApiSpec from the provided source. [OpenApiSpec.Source] must be set on the object before calling this method.
func (*OpenApiSpec) NewFromBytes ¶
func (o *OpenApiSpec) NewFromBytes(source []byte) (*OpenApiSpec, error)
NewFromBytes builds a new OpenApiSpec from the provided schema encoded as bytes. This method does not provide caching.
func (*OpenApiSpec) NewFromSource ¶
func (o *OpenApiSpec) NewFromSource(source string) (*OpenApiSpec, error)
NewFromSource builds a new OpenApiSpec from the provided source. The source can be a file:// URL or an HTTP URL or raw bytes. This method will cache the resulting OpenApiSpec, and return the same instance for the exact same source.
func (*OpenApiSpec) OperationNames ¶
func (o *OpenApiSpec) OperationNames() []string
OperationNames snake_case operation names for each endpoint.
func (*OpenApiSpec) ReadSource ¶
func (o *OpenApiSpec) ReadSource() ([]byte, error)
ReadSource reads the source file specified by the OpenAPI struct's Source field. It handles file:// URLs and will eventually support other protocols.
If the Source field does not contain a protocol (e.g., "file://"), it is assumed to be a local file path.
If the Source field contains a protocol but it's not "file://", an error is returned.
The function returns the contents of the file as a byte slice and any encountered errors.
Example:
openapi := &OpenAPI{Source: "file:///path/to/source.yaml"}
data, err := openapi.ReadSource()
if err != nil {
log.Fatal(err)
}
type Param ¶
type Param struct {
// Name is the name of the parameter.
Name string
// In is the location of the parameter. It can be "query", "path", "header",
// or "cookie".
In string
// Type is the data type of the parameter.
Type string
// Description is a brief description of the parameter.
Description string
// Required is a boolean indicating whether the parameter is required.
Required bool
}
Param represents a parameter of an API endpoint.