lockserver/openapi/openapi.go

321 lines
11 KiB
Go
Raw Normal View History

2024-04-09 23:18:43 +00:00
// Package openapi provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/deepmap/oapi-codegen/v2 version v2.1.0 DO NOT EDIT.
package openapi
import (
"bytes"
"compress/gzip"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/url"
"path"
"strings"
"github.com/getkin/kin-openapi/openapi3"
"github.com/labstack/echo/v4"
"github.com/oapi-codegen/runtime"
strictecho "github.com/oapi-codegen/runtime/strictmiddleware/echo"
)
// LockCodeSlot defines model for LockCodeSlot.
type LockCodeSlot struct {
Code string `json:"code"`
Enabled bool `json:"enabled"`
}
// PutLockCodeSlotJSONRequestBody defines body for PutLockCodeSlot for application/json ContentType.
type PutLockCodeSlotJSONRequestBody = LockCodeSlot
// ServerInterface represents all server handlers.
type ServerInterface interface {
// (GET /locks/{lock}/slots/{slot})
GetLockCodeSlot(ctx echo.Context, lock int, slot int) error
// (PUT /locks/{lock}/slots/{slot})
PutLockCodeSlot(ctx echo.Context, lock int, slot int) error
}
// ServerInterfaceWrapper converts echo contexts to parameters.
type ServerInterfaceWrapper struct {
Handler ServerInterface
}
// GetLockCodeSlot converts echo context to params.
func (w *ServerInterfaceWrapper) GetLockCodeSlot(ctx echo.Context) error {
var err error
// ------------- Path parameter "lock" -------------
var lock int
err = runtime.BindStyledParameterWithOptions("simple", "lock", ctx.Param("lock"), &lock, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter lock: %s", err))
}
// ------------- Path parameter "slot" -------------
var slot int
err = runtime.BindStyledParameterWithOptions("simple", "slot", ctx.Param("slot"), &slot, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter slot: %s", err))
}
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.GetLockCodeSlot(ctx, lock, slot)
return err
}
// PutLockCodeSlot converts echo context to params.
func (w *ServerInterfaceWrapper) PutLockCodeSlot(ctx echo.Context) error {
var err error
// ------------- Path parameter "lock" -------------
var lock int
err = runtime.BindStyledParameterWithOptions("simple", "lock", ctx.Param("lock"), &lock, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter lock: %s", err))
}
// ------------- Path parameter "slot" -------------
var slot int
err = runtime.BindStyledParameterWithOptions("simple", "slot", ctx.Param("slot"), &slot, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter slot: %s", err))
}
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.PutLockCodeSlot(ctx, lock, slot)
return err
}
// This is a simple interface which specifies echo.Route addition functions which
// are present on both echo.Echo and echo.Group, since we want to allow using
// either of them for path registration
type EchoRouter interface {
CONNECT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
DELETE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
GET(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
HEAD(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
OPTIONS(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
PATCH(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
POST(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
PUT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
TRACE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
}
// RegisterHandlers adds each server route to the EchoRouter.
func RegisterHandlers(router EchoRouter, si ServerInterface) {
RegisterHandlersWithBaseURL(router, si, "")
}
// Registers handlers, and prepends BaseURL to the paths, so that the paths
// can be served under a prefix.
func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL string) {
wrapper := ServerInterfaceWrapper{
Handler: si,
}
router.GET(baseURL+"/locks/:lock/slots/:slot", wrapper.GetLockCodeSlot)
router.PUT(baseURL+"/locks/:lock/slots/:slot", wrapper.PutLockCodeSlot)
}
type GetLockCodeSlotRequestObject struct {
Lock int `json:"lock"`
Slot int `json:"slot"`
}
type GetLockCodeSlotResponseObject interface {
VisitGetLockCodeSlotResponse(w http.ResponseWriter) error
}
type GetLockCodeSlot200JSONResponse LockCodeSlot
func (response GetLockCodeSlot200JSONResponse) VisitGetLockCodeSlotResponse(w http.ResponseWriter) error {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
return json.NewEncoder(w).Encode(response)
}
type PutLockCodeSlotRequestObject struct {
Lock int `json:"lock"`
Slot int `json:"slot"`
Body *PutLockCodeSlotJSONRequestBody
}
type PutLockCodeSlotResponseObject interface {
VisitPutLockCodeSlotResponse(w http.ResponseWriter) error
}
// StrictServerInterface represents all server handlers.
type StrictServerInterface interface {
// (GET /locks/{lock}/slots/{slot})
GetLockCodeSlot(ctx context.Context, request GetLockCodeSlotRequestObject) (GetLockCodeSlotResponseObject, error)
// (PUT /locks/{lock}/slots/{slot})
PutLockCodeSlot(ctx context.Context, request PutLockCodeSlotRequestObject) (PutLockCodeSlotResponseObject, error)
}
type StrictHandlerFunc = strictecho.StrictEchoHandlerFunc
type StrictMiddlewareFunc = strictecho.StrictEchoMiddlewareFunc
func NewStrictHandler(ssi StrictServerInterface, middlewares []StrictMiddlewareFunc) ServerInterface {
return &strictHandler{ssi: ssi, middlewares: middlewares}
}
type strictHandler struct {
ssi StrictServerInterface
middlewares []StrictMiddlewareFunc
}
// GetLockCodeSlot operation middleware
func (sh *strictHandler) GetLockCodeSlot(ctx echo.Context, lock int, slot int) error {
var request GetLockCodeSlotRequestObject
request.Lock = lock
request.Slot = slot
handler := func(ctx echo.Context, request interface{}) (interface{}, error) {
return sh.ssi.GetLockCodeSlot(ctx.Request().Context(), request.(GetLockCodeSlotRequestObject))
}
for _, middleware := range sh.middlewares {
handler = middleware(handler, "GetLockCodeSlot")
}
response, err := handler(ctx, request)
if err != nil {
return err
} else if validResponse, ok := response.(GetLockCodeSlotResponseObject); ok {
return validResponse.VisitGetLockCodeSlotResponse(ctx.Response())
} else if response != nil {
return fmt.Errorf("unexpected response type: %T", response)
}
return nil
}
// PutLockCodeSlot operation middleware
func (sh *strictHandler) PutLockCodeSlot(ctx echo.Context, lock int, slot int) error {
var request PutLockCodeSlotRequestObject
request.Lock = lock
request.Slot = slot
var body PutLockCodeSlotJSONRequestBody
if err := ctx.Bind(&body); err != nil {
return err
}
request.Body = &body
handler := func(ctx echo.Context, request interface{}) (interface{}, error) {
return sh.ssi.PutLockCodeSlot(ctx.Request().Context(), request.(PutLockCodeSlotRequestObject))
}
for _, middleware := range sh.middlewares {
handler = middleware(handler, "PutLockCodeSlot")
}
response, err := handler(ctx, request)
if err != nil {
return err
} else if validResponse, ok := response.(PutLockCodeSlotResponseObject); ok {
return validResponse.VisitPutLockCodeSlotResponse(ctx.Response())
} else if response != nil {
return fmt.Errorf("unexpected response type: %T", response)
}
return nil
}
// Base64 encoded, gzipped, json marshaled Swagger object
var swaggerSpec = []string{
"H4sIAAAAAAAC/+SSsY7bMAyGX8VgOzqW296k7S4FigM6FLipONygyEyiqyzqJDpFYOjdCyo+JAGydOnS",
"SbZI/qS+nzNYGiMFDJxBz5DtHkdTP7+T/bWmAZ88sfzHRBETO6xRSwPKyceIoCFzcmEHpQUMZuNxuIht",
"iDyaAKW0kPBtcknCzyeJc8FL+15Am1e0DEUKXNiSaHlnMeTaMphRstbr1cPP1dP96q7roYUpedCwZ45Z",
"K2UTGnYHtDSOFHJHaacWiaw2R3XX9UqmZcdexOSxGdMBE7RwwJQdBdDQd5+6XvIoYjDRgYYv9aqFaHhf",
"SSgvtWqWo6jsibOa5SgS3WGFJ+gMOwqPA2j4hnxFV9SSGZExZdDPMwyYbXKRT1M8fm1o24g+CBDQtTm0",
"7ySWyJktpwnbxcsLI1xg3GESI263EEeafJroRp8l8hd9XiQ7RxLskvC570/LExhDBWNi9M5WNOo1yyzz",
"heDHhFvQ8EGdt1QtK6quCNZduX7SfSV28aaaE6cbfvyY/ic/3ibM/EDD8Z9ZwXtsAv5uDsZP2GwpNXJz",
"c/xytTFh8r6UUv4EAAD///FFWy6nBAAA",
}
// GetSwagger returns the content of the embedded swagger specification file
// or error if failed to decode
func decodeSpec() ([]byte, error) {
zipped, err := base64.StdEncoding.DecodeString(strings.Join(swaggerSpec, ""))
if err != nil {
return nil, fmt.Errorf("error base64 decoding spec: %w", err)
}
zr, err := gzip.NewReader(bytes.NewReader(zipped))
if err != nil {
return nil, fmt.Errorf("error decompressing spec: %w", err)
}
var buf bytes.Buffer
_, err = buf.ReadFrom(zr)
if err != nil {
return nil, fmt.Errorf("error decompressing spec: %w", err)
}
return buf.Bytes(), nil
}
var rawSpec = decodeSpecCached()
// a naive cached of a decoded swagger spec
func decodeSpecCached() func() ([]byte, error) {
data, err := decodeSpec()
return func() ([]byte, error) {
return data, err
}
}
// Constructs a synthetic filesystem for resolving external references when loading openapi specifications.
func PathToRawSpec(pathToFile string) map[string]func() ([]byte, error) {
res := make(map[string]func() ([]byte, error))
if len(pathToFile) > 0 {
res[pathToFile] = rawSpec
}
return res
}
// GetSwagger returns the Swagger specification corresponding to the generated code
// in this file. The external references of Swagger specification are resolved.
// The logic of resolving external references is tightly connected to "import-mapping" feature.
// Externally referenced files must be embedded in the corresponding golang packages.
// Urls can be supported but this task was out of the scope.
func GetSwagger() (swagger *openapi3.T, err error) {
resolvePath := PathToRawSpec("")
loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = true
loader.ReadFromURIFunc = func(loader *openapi3.Loader, url *url.URL) ([]byte, error) {
pathToFile := url.String()
pathToFile = path.Clean(pathToFile)
getSpec, ok := resolvePath[pathToFile]
if !ok {
err1 := fmt.Errorf("path not found: %s", pathToFile)
return nil, err1
}
return getSpec()
}
var specData []byte
specData, err = rawSpec()
if err != nil {
return
}
swagger, err = loader.LoadFromData(specData)
if err != nil {
return
}
return
}