lockserver/openapi/openapi.go

455 lines
15 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"`
}
// UserCode defines model for UserCode.
type UserCode struct {
Code *string `json:"code,omitempty"`
// Ends when the code expires
Ends *string `json:"ends,omitempty"`
// Starts when the code becomes active
Starts *string `json:"starts,omitempty"`
User *string `json:"user,omitempty"`
}
// AddUserCodeJSONRequestBody defines body for AddUserCode for application/json ContentType.
type AddUserCodeJSONRequestBody = UserCode
2024-04-09 23:18:43 +00:00
// PutLockCodeSlotJSONRequestBody defines body for PutLockCodeSlot for application/json ContentType.
type PutLockCodeSlotJSONRequestBody = LockCodeSlot
// ServerInterface represents all server handlers.
type ServerInterface interface {
// (GET /admin/user-codes)
GetAllUserCodes(ctx echo.Context) error
// (POST /admin/user-codes)
AddUserCode(ctx echo.Context) error
2024-04-09 23:18:43 +00:00
// (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
}
// GetAllUserCodes converts echo context to params.
func (w *ServerInterfaceWrapper) GetAllUserCodes(ctx echo.Context) error {
var err error
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.GetAllUserCodes(ctx)
return err
}
// AddUserCode converts echo context to params.
func (w *ServerInterfaceWrapper) AddUserCode(ctx echo.Context) error {
var err error
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.AddUserCode(ctx)
return err
}
2024-04-09 23:18:43 +00:00
// 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+"/admin/user-codes", wrapper.GetAllUserCodes)
router.POST(baseURL+"/admin/user-codes", wrapper.AddUserCode)
2024-04-09 23:18:43 +00:00
router.GET(baseURL+"/locks/:lock/slots/:slot", wrapper.GetLockCodeSlot)
router.PUT(baseURL+"/locks/:lock/slots/:slot", wrapper.PutLockCodeSlot)
}
type GetAllUserCodesRequestObject struct {
}
type GetAllUserCodesResponseObject interface {
VisitGetAllUserCodesResponse(w http.ResponseWriter) error
}
type GetAllUserCodes200JSONResponse []UserCode
func (response GetAllUserCodes200JSONResponse) VisitGetAllUserCodesResponse(w http.ResponseWriter) error {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
return json.NewEncoder(w).Encode(response)
}
type AddUserCodeRequestObject struct {
Body *AddUserCodeJSONRequestBody
}
type AddUserCodeResponseObject interface {
VisitAddUserCodeResponse(w http.ResponseWriter) error
}
type AddUserCode201JSONResponse UserCode
func (response AddUserCode201JSONResponse) VisitAddUserCodeResponse(w http.ResponseWriter) error {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(201)
return json.NewEncoder(w).Encode(response)
}
2024-04-09 23:18:43 +00:00
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 /admin/user-codes)
GetAllUserCodes(ctx context.Context, request GetAllUserCodesRequestObject) (GetAllUserCodesResponseObject, error)
// (POST /admin/user-codes)
AddUserCode(ctx context.Context, request AddUserCodeRequestObject) (AddUserCodeResponseObject, error)
2024-04-09 23:18:43 +00:00
// (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
}
// GetAllUserCodes operation middleware
func (sh *strictHandler) GetAllUserCodes(ctx echo.Context) error {
var request GetAllUserCodesRequestObject
handler := func(ctx echo.Context, request interface{}) (interface{}, error) {
return sh.ssi.GetAllUserCodes(ctx.Request().Context(), request.(GetAllUserCodesRequestObject))
}
for _, middleware := range sh.middlewares {
handler = middleware(handler, "GetAllUserCodes")
}
response, err := handler(ctx, request)
if err != nil {
return err
} else if validResponse, ok := response.(GetAllUserCodesResponseObject); ok {
return validResponse.VisitGetAllUserCodesResponse(ctx.Response())
} else if response != nil {
return fmt.Errorf("unexpected response type: %T", response)
}
return nil
}
// AddUserCode operation middleware
func (sh *strictHandler) AddUserCode(ctx echo.Context) error {
var request AddUserCodeRequestObject
var body AddUserCodeJSONRequestBody
if err := ctx.Bind(&body); err != nil {
return err
}
request.Body = &body
handler := func(ctx echo.Context, request interface{}) (interface{}, error) {
return sh.ssi.AddUserCode(ctx.Request().Context(), request.(AddUserCodeRequestObject))
}
for _, middleware := range sh.middlewares {
handler = middleware(handler, "AddUserCode")
}
response, err := handler(ctx, request)
if err != nil {
return err
} else if validResponse, ok := response.(AddUserCodeResponseObject); ok {
return validResponse.VisitAddUserCodeResponse(ctx.Response())
} else if response != nil {
return fmt.Errorf("unexpected response type: %T", response)
}
return nil
}
2024-04-09 23:18:43 +00:00
// 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/+RVO2/bMBD+K8S1o2w6bSZtjgsUAToUCDoUQQZaOttMKZI9npwahv57cZQcx4mQB9pO",
"nSiRvMf3OGkPVWhi8Og5QbmHVG2wMfnxS6h+LEKNVy6wvEcKEYkt5tMq1Cgr7yJCCYnJ+jV0BaA3S4f1",
"g7NlCA6Nh64rgPBna0mOr/sUx4Cb4hAQlrdYsST7lpAWQ6VX16/zhRpTRTayDR5KuNugV7xBJXEKf0VL",
"mKB4Gp7YEL+YYIlVaDApU7Hd4lieNiGN9Nc9ASlb1q+CXHa2Qp8yLm8aubVYTC6+T67mk/PpDApoyUEJ",
"G+aYSq0rQiP1q9A0wadpoLUeUiS93Onz6UxLL2zZSTJRNCFtkaCALVLqoc2mZ9OZ3AsRvYkWSviYtwqI",
"hjeZDG3qxnotqCbCQN5cYzaGyGKEpssaSviMPHfuIJxwTJhikJ7k8ofZrJfPM/ocbmJ0tsoJ9G2Sjg42",
"lCfL2OTA94QrKOGdPhpWD27V9zY50muIzK5n91RI45yqWiL0rIyv1arlllAJMtUjy0ExpBFw87q+r9Wb",
"GRNfhHr3Jkyvg9L183LC3dk/qfOYonsu1J1JKrsM66Eh7cREei9Lp5MLnPRelu45R5x8S8RWZBpkpATl",
"9eNJu/ykwkpJfpDJgDK7EIrDSAwnxy8JU4vFA9iDA6xnXCPJzI2XyAhT39FIneHkDXVu/tDrz+l1wuCI",
"ZvPM2ANMvYvbET2+tv+THn9/RF+SQv4SHu/U1rgW1SpQ/m+Mtn864b51TqbsdwAAAP//L1q9IJUHAAA=",
2024-04-09 23:18:43 +00:00
}
// 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
}