1009 lines
33 KiB
Go
1009 lines
33 KiB
Go
// Package server provides primitives to interact with the openapi HTTP API.
|
|
//
|
|
// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.3.0 DO NOT EDIT.
|
|
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"compress/gzip"
|
|
"context"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/getkin/kin-openapi/openapi3"
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/oapi-codegen/runtime"
|
|
strictnethttp "github.com/oapi-codegen/runtime/strictmiddleware/nethttp"
|
|
"gitlab.mareshq.com/hq/backoffice/backoffice-api/internal/currency"
|
|
"gitlab.mareshq.com/hq/backoffice/backoffice-api/internal/training"
|
|
)
|
|
|
|
// Defines values for TrainingPriceType.
|
|
const (
|
|
CORPORATE TrainingPriceType = "CORPORATE"
|
|
OPEN TrainingPriceType = "OPEN"
|
|
)
|
|
|
|
// NewTraining defines model for NewTraining.
|
|
type NewTraining struct {
|
|
Days int8 `json:"days"`
|
|
Name string `json:"name"`
|
|
Pricing []TrainingPrice `json:"pricing"`
|
|
}
|
|
|
|
// Price defines model for Price.
|
|
type Price struct {
|
|
Amount string `json:"amount"`
|
|
Currency currency.Currency `json:"currency"`
|
|
}
|
|
|
|
// ProblemDetails Schema that carries the details of an error in an HTTP response. See https://datatracker.ietf.org/doc/html/rfc7807 for more information.
|
|
type ProblemDetails struct {
|
|
// Detail A human-readable explanation specific to this occurrence of the problem.
|
|
Detail string `json:"detail"`
|
|
|
|
// Instance A URI reference that identifies the specific occurrence of the problem.
|
|
Instance string `json:"instance"`
|
|
|
|
// Status The HTTP status code generated by the origin server for this occurrence of the problem.
|
|
Status int `json:"status"`
|
|
|
|
// Title A human-readable summary of the problem type.
|
|
Title string `json:"title"`
|
|
|
|
// Type A URI reference that identifies the problem type.
|
|
Type string `json:"type"`
|
|
}
|
|
|
|
// Training defines model for Training.
|
|
type Training struct {
|
|
Days int8 `json:"days"`
|
|
Id TrainingID `json:"id"`
|
|
Name string `json:"name"`
|
|
Pricing []TrainingPrice `json:"pricing"`
|
|
Published bool `json:"published"`
|
|
Retired bool `json:"retired"`
|
|
}
|
|
|
|
// TrainingID defines model for TrainingID.
|
|
type TrainingID = training.ID
|
|
|
|
// TrainingPrice defines model for TrainingPrice.
|
|
type TrainingPrice struct {
|
|
Amount string `json:"amount"`
|
|
Currency currency.Currency `json:"currency"`
|
|
Type TrainingPriceType `json:"type"`
|
|
}
|
|
|
|
// TrainingPriceType defines model for TrainingPrice.Type.
|
|
type TrainingPriceType string
|
|
|
|
// CreateTrainingResponse defines model for CreateTrainingResponse.
|
|
type CreateTrainingResponse = Training
|
|
|
|
// GetTrainingResponse defines model for GetTrainingResponse.
|
|
type GetTrainingResponse = Training
|
|
|
|
// InternalError Schema that carries the details of an error in an HTTP response. See https://datatracker.ietf.org/doc/html/rfc7807 for more information.
|
|
type InternalError = ProblemDetails
|
|
|
|
// InvalidInputError Schema that carries the details of an error in an HTTP response. See https://datatracker.ietf.org/doc/html/rfc7807 for more information.
|
|
type InvalidInputError = ProblemDetails
|
|
|
|
// ListTrainingsResponse defines model for ListTrainingsResponse.
|
|
type ListTrainingsResponse struct {
|
|
// Trainings List of trainings
|
|
Trainings *[]Training `json:"trainings,omitempty"`
|
|
}
|
|
|
|
// NotFoundError Schema that carries the details of an error in an HTTP response. See https://datatracker.ietf.org/doc/html/rfc7807 for more information.
|
|
type NotFoundError = ProblemDetails
|
|
|
|
// CreateTrainingRequest defines model for CreateTrainingRequest.
|
|
type CreateTrainingRequest struct {
|
|
Training *NewTraining `json:"training,omitempty"`
|
|
}
|
|
|
|
// CreateTrainingJSONBody defines parameters for CreateTraining.
|
|
type CreateTrainingJSONBody struct {
|
|
Training *NewTraining `json:"training,omitempty"`
|
|
}
|
|
|
|
// CreateTrainingJSONRequestBody defines body for CreateTraining for application/json ContentType.
|
|
type CreateTrainingJSONRequestBody CreateTrainingJSONBody
|
|
|
|
// ServerInterface represents all server handlers.
|
|
type ServerInterface interface {
|
|
// List all trainings
|
|
// (GET /v0/trainings)
|
|
ListTrainings(w http.ResponseWriter, r *http.Request)
|
|
// Create a new training
|
|
// (POST /v0/trainings)
|
|
CreateTraining(w http.ResponseWriter, r *http.Request)
|
|
// Get a training by ID
|
|
// (GET /v0/trainings/{trainingID})
|
|
GetTrainingByID(w http.ResponseWriter, r *http.Request, trainingID TrainingID)
|
|
// Publish a training
|
|
// (PUT /v0/trainings/{trainingID}/publish)
|
|
PublishTraining(w http.ResponseWriter, r *http.Request, trainingID TrainingID)
|
|
// Retire a training
|
|
// (PUT /v0/trainings/{trainingID}/retire)
|
|
RetireTraining(w http.ResponseWriter, r *http.Request, trainingID TrainingID)
|
|
// Unpublish a training
|
|
// (PUT /v0/trainings/{trainingID}/unpublish)
|
|
UnpublishTraining(w http.ResponseWriter, r *http.Request, trainingID TrainingID)
|
|
}
|
|
|
|
// Unimplemented server implementation that returns http.StatusNotImplemented for each endpoint.
|
|
|
|
type Unimplemented struct{}
|
|
|
|
// List all trainings
|
|
// (GET /v0/trainings)
|
|
func (_ Unimplemented) ListTrainings(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusNotImplemented)
|
|
}
|
|
|
|
// Create a new training
|
|
// (POST /v0/trainings)
|
|
func (_ Unimplemented) CreateTraining(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusNotImplemented)
|
|
}
|
|
|
|
// Get a training by ID
|
|
// (GET /v0/trainings/{trainingID})
|
|
func (_ Unimplemented) GetTrainingByID(w http.ResponseWriter, r *http.Request, trainingID TrainingID) {
|
|
w.WriteHeader(http.StatusNotImplemented)
|
|
}
|
|
|
|
// Publish a training
|
|
// (PUT /v0/trainings/{trainingID}/publish)
|
|
func (_ Unimplemented) PublishTraining(w http.ResponseWriter, r *http.Request, trainingID TrainingID) {
|
|
w.WriteHeader(http.StatusNotImplemented)
|
|
}
|
|
|
|
// Retire a training
|
|
// (PUT /v0/trainings/{trainingID}/retire)
|
|
func (_ Unimplemented) RetireTraining(w http.ResponseWriter, r *http.Request, trainingID TrainingID) {
|
|
w.WriteHeader(http.StatusNotImplemented)
|
|
}
|
|
|
|
// Unpublish a training
|
|
// (PUT /v0/trainings/{trainingID}/unpublish)
|
|
func (_ Unimplemented) UnpublishTraining(w http.ResponseWriter, r *http.Request, trainingID TrainingID) {
|
|
w.WriteHeader(http.StatusNotImplemented)
|
|
}
|
|
|
|
// ServerInterfaceWrapper converts contexts to parameters.
|
|
type ServerInterfaceWrapper struct {
|
|
Handler ServerInterface
|
|
HandlerMiddlewares []MiddlewareFunc
|
|
ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)
|
|
}
|
|
|
|
type MiddlewareFunc func(http.Handler) http.Handler
|
|
|
|
// ListTrainings operation middleware
|
|
func (siw *ServerInterfaceWrapper) ListTrainings(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
|
|
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
siw.Handler.ListTrainings(w, r)
|
|
}))
|
|
|
|
for _, middleware := range siw.HandlerMiddlewares {
|
|
handler = middleware(handler)
|
|
}
|
|
|
|
handler.ServeHTTP(w, r.WithContext(ctx))
|
|
}
|
|
|
|
// CreateTraining operation middleware
|
|
func (siw *ServerInterfaceWrapper) CreateTraining(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
|
|
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
siw.Handler.CreateTraining(w, r)
|
|
}))
|
|
|
|
for _, middleware := range siw.HandlerMiddlewares {
|
|
handler = middleware(handler)
|
|
}
|
|
|
|
handler.ServeHTTP(w, r.WithContext(ctx))
|
|
}
|
|
|
|
// GetTrainingByID operation middleware
|
|
func (siw *ServerInterfaceWrapper) GetTrainingByID(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
// ------------- Path parameter "trainingID" -------------
|
|
var trainingID TrainingID
|
|
|
|
err = runtime.BindStyledParameterWithOptions("simple", "trainingID", chi.URLParam(r, "trainingID"), &trainingID, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
|
|
if err != nil {
|
|
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "trainingID", Err: err})
|
|
return
|
|
}
|
|
|
|
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
siw.Handler.GetTrainingByID(w, r, trainingID)
|
|
}))
|
|
|
|
for _, middleware := range siw.HandlerMiddlewares {
|
|
handler = middleware(handler)
|
|
}
|
|
|
|
handler.ServeHTTP(w, r.WithContext(ctx))
|
|
}
|
|
|
|
// PublishTraining operation middleware
|
|
func (siw *ServerInterfaceWrapper) PublishTraining(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
// ------------- Path parameter "trainingID" -------------
|
|
var trainingID TrainingID
|
|
|
|
err = runtime.BindStyledParameterWithOptions("simple", "trainingID", chi.URLParam(r, "trainingID"), &trainingID, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
|
|
if err != nil {
|
|
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "trainingID", Err: err})
|
|
return
|
|
}
|
|
|
|
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
siw.Handler.PublishTraining(w, r, trainingID)
|
|
}))
|
|
|
|
for _, middleware := range siw.HandlerMiddlewares {
|
|
handler = middleware(handler)
|
|
}
|
|
|
|
handler.ServeHTTP(w, r.WithContext(ctx))
|
|
}
|
|
|
|
// RetireTraining operation middleware
|
|
func (siw *ServerInterfaceWrapper) RetireTraining(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
// ------------- Path parameter "trainingID" -------------
|
|
var trainingID TrainingID
|
|
|
|
err = runtime.BindStyledParameterWithOptions("simple", "trainingID", chi.URLParam(r, "trainingID"), &trainingID, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
|
|
if err != nil {
|
|
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "trainingID", Err: err})
|
|
return
|
|
}
|
|
|
|
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
siw.Handler.RetireTraining(w, r, trainingID)
|
|
}))
|
|
|
|
for _, middleware := range siw.HandlerMiddlewares {
|
|
handler = middleware(handler)
|
|
}
|
|
|
|
handler.ServeHTTP(w, r.WithContext(ctx))
|
|
}
|
|
|
|
// UnpublishTraining operation middleware
|
|
func (siw *ServerInterfaceWrapper) UnpublishTraining(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
// ------------- Path parameter "trainingID" -------------
|
|
var trainingID TrainingID
|
|
|
|
err = runtime.BindStyledParameterWithOptions("simple", "trainingID", chi.URLParam(r, "trainingID"), &trainingID, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
|
|
if err != nil {
|
|
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "trainingID", Err: err})
|
|
return
|
|
}
|
|
|
|
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
siw.Handler.UnpublishTraining(w, r, trainingID)
|
|
}))
|
|
|
|
for _, middleware := range siw.HandlerMiddlewares {
|
|
handler = middleware(handler)
|
|
}
|
|
|
|
handler.ServeHTTP(w, r.WithContext(ctx))
|
|
}
|
|
|
|
type UnescapedCookieParamError struct {
|
|
ParamName string
|
|
Err error
|
|
}
|
|
|
|
func (e *UnescapedCookieParamError) Error() string {
|
|
return fmt.Sprintf("error unescaping cookie parameter '%s'", e.ParamName)
|
|
}
|
|
|
|
func (e *UnescapedCookieParamError) Unwrap() error {
|
|
return e.Err
|
|
}
|
|
|
|
type UnmarshalingParamError struct {
|
|
ParamName string
|
|
Err error
|
|
}
|
|
|
|
func (e *UnmarshalingParamError) Error() string {
|
|
return fmt.Sprintf("Error unmarshaling parameter %s as JSON: %s", e.ParamName, e.Err.Error())
|
|
}
|
|
|
|
func (e *UnmarshalingParamError) Unwrap() error {
|
|
return e.Err
|
|
}
|
|
|
|
type RequiredParamError struct {
|
|
ParamName string
|
|
}
|
|
|
|
func (e *RequiredParamError) Error() string {
|
|
return fmt.Sprintf("Query argument %s is required, but not found", e.ParamName)
|
|
}
|
|
|
|
type RequiredHeaderError struct {
|
|
ParamName string
|
|
Err error
|
|
}
|
|
|
|
func (e *RequiredHeaderError) Error() string {
|
|
return fmt.Sprintf("Header parameter %s is required, but not found", e.ParamName)
|
|
}
|
|
|
|
func (e *RequiredHeaderError) Unwrap() error {
|
|
return e.Err
|
|
}
|
|
|
|
type InvalidParamFormatError struct {
|
|
ParamName string
|
|
Err error
|
|
}
|
|
|
|
func (e *InvalidParamFormatError) Error() string {
|
|
return fmt.Sprintf("Invalid format for parameter %s: %s", e.ParamName, e.Err.Error())
|
|
}
|
|
|
|
func (e *InvalidParamFormatError) Unwrap() error {
|
|
return e.Err
|
|
}
|
|
|
|
type TooManyValuesForParamError struct {
|
|
ParamName string
|
|
Count int
|
|
}
|
|
|
|
func (e *TooManyValuesForParamError) Error() string {
|
|
return fmt.Sprintf("Expected one value for %s, got %d", e.ParamName, e.Count)
|
|
}
|
|
|
|
// Handler creates http.Handler with routing matching OpenAPI spec.
|
|
func Handler(si ServerInterface) http.Handler {
|
|
return HandlerWithOptions(si, ChiServerOptions{})
|
|
}
|
|
|
|
type ChiServerOptions struct {
|
|
BaseURL string
|
|
BaseRouter chi.Router
|
|
Middlewares []MiddlewareFunc
|
|
ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)
|
|
}
|
|
|
|
// HandlerFromMux creates http.Handler with routing matching OpenAPI spec based on the provided mux.
|
|
func HandlerFromMux(si ServerInterface, r chi.Router) http.Handler {
|
|
return HandlerWithOptions(si, ChiServerOptions{
|
|
BaseRouter: r,
|
|
})
|
|
}
|
|
|
|
func HandlerFromMuxWithBaseURL(si ServerInterface, r chi.Router, baseURL string) http.Handler {
|
|
return HandlerWithOptions(si, ChiServerOptions{
|
|
BaseURL: baseURL,
|
|
BaseRouter: r,
|
|
})
|
|
}
|
|
|
|
// HandlerWithOptions creates http.Handler with additional options
|
|
func HandlerWithOptions(si ServerInterface, options ChiServerOptions) http.Handler {
|
|
r := options.BaseRouter
|
|
|
|
if r == nil {
|
|
r = chi.NewRouter()
|
|
}
|
|
if options.ErrorHandlerFunc == nil {
|
|
options.ErrorHandlerFunc = func(w http.ResponseWriter, r *http.Request, err error) {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
}
|
|
}
|
|
wrapper := ServerInterfaceWrapper{
|
|
Handler: si,
|
|
HandlerMiddlewares: options.Middlewares,
|
|
ErrorHandlerFunc: options.ErrorHandlerFunc,
|
|
}
|
|
|
|
r.Group(func(r chi.Router) {
|
|
r.Get(options.BaseURL+"/v0/trainings", wrapper.ListTrainings)
|
|
})
|
|
r.Group(func(r chi.Router) {
|
|
r.Post(options.BaseURL+"/v0/trainings", wrapper.CreateTraining)
|
|
})
|
|
r.Group(func(r chi.Router) {
|
|
r.Get(options.BaseURL+"/v0/trainings/{trainingID}", wrapper.GetTrainingByID)
|
|
})
|
|
r.Group(func(r chi.Router) {
|
|
r.Put(options.BaseURL+"/v0/trainings/{trainingID}/publish", wrapper.PublishTraining)
|
|
})
|
|
r.Group(func(r chi.Router) {
|
|
r.Put(options.BaseURL+"/v0/trainings/{trainingID}/retire", wrapper.RetireTraining)
|
|
})
|
|
r.Group(func(r chi.Router) {
|
|
r.Put(options.BaseURL+"/v0/trainings/{trainingID}/unpublish", wrapper.UnpublishTraining)
|
|
})
|
|
|
|
return r
|
|
}
|
|
|
|
type CreateTrainingResponseJSONResponse Training
|
|
|
|
type GetTrainingResponseJSONResponse Training
|
|
|
|
type InternalErrorApplicationProblemPlusJSONResponse ProblemDetails
|
|
|
|
type InvalidInputErrorApplicationProblemPlusJSONResponse ProblemDetails
|
|
|
|
type ListTrainingsResponseJSONResponse struct {
|
|
// Trainings List of trainings
|
|
Trainings *[]Training `json:"trainings,omitempty"`
|
|
}
|
|
|
|
type NotFoundErrorApplicationProblemPlusJSONResponse ProblemDetails
|
|
|
|
type ListTrainingsRequestObject struct {
|
|
}
|
|
|
|
type ListTrainingsResponseObject interface {
|
|
VisitListTrainingsResponse(w http.ResponseWriter) error
|
|
}
|
|
|
|
type ListTrainings200JSONResponse struct {
|
|
ListTrainingsResponseJSONResponse
|
|
}
|
|
|
|
func (response ListTrainings200JSONResponse) VisitListTrainingsResponse(w http.ResponseWriter) error {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(200)
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
type ListTrainings500ApplicationProblemPlusJSONResponse struct {
|
|
InternalErrorApplicationProblemPlusJSONResponse
|
|
}
|
|
|
|
func (response ListTrainings500ApplicationProblemPlusJSONResponse) VisitListTrainingsResponse(w http.ResponseWriter) error {
|
|
w.Header().Set("Content-Type", "application/problem+json")
|
|
w.WriteHeader(500)
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
type CreateTrainingRequestObject struct {
|
|
Body *CreateTrainingJSONRequestBody
|
|
}
|
|
|
|
type CreateTrainingResponseObject interface {
|
|
VisitCreateTrainingResponse(w http.ResponseWriter) error
|
|
}
|
|
|
|
type CreateTraining201JSONResponse struct {
|
|
CreateTrainingResponseJSONResponse
|
|
}
|
|
|
|
func (response CreateTraining201JSONResponse) VisitCreateTrainingResponse(w http.ResponseWriter) error {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(201)
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
type CreateTraining400ApplicationProblemPlusJSONResponse struct {
|
|
InvalidInputErrorApplicationProblemPlusJSONResponse
|
|
}
|
|
|
|
func (response CreateTraining400ApplicationProblemPlusJSONResponse) VisitCreateTrainingResponse(w http.ResponseWriter) error {
|
|
w.Header().Set("Content-Type", "application/problem+json")
|
|
w.WriteHeader(400)
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
type CreateTraining500ApplicationProblemPlusJSONResponse struct {
|
|
InternalErrorApplicationProblemPlusJSONResponse
|
|
}
|
|
|
|
func (response CreateTraining500ApplicationProblemPlusJSONResponse) VisitCreateTrainingResponse(w http.ResponseWriter) error {
|
|
w.Header().Set("Content-Type", "application/problem+json")
|
|
w.WriteHeader(500)
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
type GetTrainingByIDRequestObject struct {
|
|
TrainingID TrainingID `json:"trainingID"`
|
|
}
|
|
|
|
type GetTrainingByIDResponseObject interface {
|
|
VisitGetTrainingByIDResponse(w http.ResponseWriter) error
|
|
}
|
|
|
|
type GetTrainingByID200JSONResponse struct {
|
|
GetTrainingResponseJSONResponse
|
|
}
|
|
|
|
func (response GetTrainingByID200JSONResponse) VisitGetTrainingByIDResponse(w http.ResponseWriter) error {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(200)
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
type GetTrainingByID404ApplicationProblemPlusJSONResponse struct {
|
|
NotFoundErrorApplicationProblemPlusJSONResponse
|
|
}
|
|
|
|
func (response GetTrainingByID404ApplicationProblemPlusJSONResponse) VisitGetTrainingByIDResponse(w http.ResponseWriter) error {
|
|
w.Header().Set("Content-Type", "application/problem+json")
|
|
w.WriteHeader(404)
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
type GetTrainingByID500ApplicationProblemPlusJSONResponse struct {
|
|
InternalErrorApplicationProblemPlusJSONResponse
|
|
}
|
|
|
|
func (response GetTrainingByID500ApplicationProblemPlusJSONResponse) VisitGetTrainingByIDResponse(w http.ResponseWriter) error {
|
|
w.Header().Set("Content-Type", "application/problem+json")
|
|
w.WriteHeader(500)
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
type PublishTrainingRequestObject struct {
|
|
TrainingID TrainingID `json:"trainingID"`
|
|
}
|
|
|
|
type PublishTrainingResponseObject interface {
|
|
VisitPublishTrainingResponse(w http.ResponseWriter) error
|
|
}
|
|
|
|
type PublishTraining204Response struct {
|
|
}
|
|
|
|
func (response PublishTraining204Response) VisitPublishTrainingResponse(w http.ResponseWriter) error {
|
|
w.WriteHeader(204)
|
|
return nil
|
|
}
|
|
|
|
type PublishTraining404ApplicationProblemPlusJSONResponse struct {
|
|
NotFoundErrorApplicationProblemPlusJSONResponse
|
|
}
|
|
|
|
func (response PublishTraining404ApplicationProblemPlusJSONResponse) VisitPublishTrainingResponse(w http.ResponseWriter) error {
|
|
w.Header().Set("Content-Type", "application/problem+json")
|
|
w.WriteHeader(404)
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
type PublishTraining500ApplicationProblemPlusJSONResponse struct {
|
|
InternalErrorApplicationProblemPlusJSONResponse
|
|
}
|
|
|
|
func (response PublishTraining500ApplicationProblemPlusJSONResponse) VisitPublishTrainingResponse(w http.ResponseWriter) error {
|
|
w.Header().Set("Content-Type", "application/problem+json")
|
|
w.WriteHeader(500)
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
type RetireTrainingRequestObject struct {
|
|
TrainingID TrainingID `json:"trainingID"`
|
|
}
|
|
|
|
type RetireTrainingResponseObject interface {
|
|
VisitRetireTrainingResponse(w http.ResponseWriter) error
|
|
}
|
|
|
|
type RetireTraining204Response struct {
|
|
}
|
|
|
|
func (response RetireTraining204Response) VisitRetireTrainingResponse(w http.ResponseWriter) error {
|
|
w.WriteHeader(204)
|
|
return nil
|
|
}
|
|
|
|
type RetireTraining404ApplicationProblemPlusJSONResponse struct {
|
|
NotFoundErrorApplicationProblemPlusJSONResponse
|
|
}
|
|
|
|
func (response RetireTraining404ApplicationProblemPlusJSONResponse) VisitRetireTrainingResponse(w http.ResponseWriter) error {
|
|
w.Header().Set("Content-Type", "application/problem+json")
|
|
w.WriteHeader(404)
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
type RetireTraining500ApplicationProblemPlusJSONResponse struct {
|
|
InternalErrorApplicationProblemPlusJSONResponse
|
|
}
|
|
|
|
func (response RetireTraining500ApplicationProblemPlusJSONResponse) VisitRetireTrainingResponse(w http.ResponseWriter) error {
|
|
w.Header().Set("Content-Type", "application/problem+json")
|
|
w.WriteHeader(500)
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
type UnpublishTrainingRequestObject struct {
|
|
TrainingID TrainingID `json:"trainingID"`
|
|
}
|
|
|
|
type UnpublishTrainingResponseObject interface {
|
|
VisitUnpublishTrainingResponse(w http.ResponseWriter) error
|
|
}
|
|
|
|
type UnpublishTraining204Response struct {
|
|
}
|
|
|
|
func (response UnpublishTraining204Response) VisitUnpublishTrainingResponse(w http.ResponseWriter) error {
|
|
w.WriteHeader(204)
|
|
return nil
|
|
}
|
|
|
|
type UnpublishTraining404ApplicationProblemPlusJSONResponse struct {
|
|
NotFoundErrorApplicationProblemPlusJSONResponse
|
|
}
|
|
|
|
func (response UnpublishTraining404ApplicationProblemPlusJSONResponse) VisitUnpublishTrainingResponse(w http.ResponseWriter) error {
|
|
w.Header().Set("Content-Type", "application/problem+json")
|
|
w.WriteHeader(404)
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
type UnpublishTraining500ApplicationProblemPlusJSONResponse struct {
|
|
InternalErrorApplicationProblemPlusJSONResponse
|
|
}
|
|
|
|
func (response UnpublishTraining500ApplicationProblemPlusJSONResponse) VisitUnpublishTrainingResponse(w http.ResponseWriter) error {
|
|
w.Header().Set("Content-Type", "application/problem+json")
|
|
w.WriteHeader(500)
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
// StrictServerInterface represents all server handlers.
|
|
type StrictServerInterface interface {
|
|
// List all trainings
|
|
// (GET /v0/trainings)
|
|
ListTrainings(ctx context.Context, request ListTrainingsRequestObject) (ListTrainingsResponseObject, error)
|
|
// Create a new training
|
|
// (POST /v0/trainings)
|
|
CreateTraining(ctx context.Context, request CreateTrainingRequestObject) (CreateTrainingResponseObject, error)
|
|
// Get a training by ID
|
|
// (GET /v0/trainings/{trainingID})
|
|
GetTrainingByID(ctx context.Context, request GetTrainingByIDRequestObject) (GetTrainingByIDResponseObject, error)
|
|
// Publish a training
|
|
// (PUT /v0/trainings/{trainingID}/publish)
|
|
PublishTraining(ctx context.Context, request PublishTrainingRequestObject) (PublishTrainingResponseObject, error)
|
|
// Retire a training
|
|
// (PUT /v0/trainings/{trainingID}/retire)
|
|
RetireTraining(ctx context.Context, request RetireTrainingRequestObject) (RetireTrainingResponseObject, error)
|
|
// Unpublish a training
|
|
// (PUT /v0/trainings/{trainingID}/unpublish)
|
|
UnpublishTraining(ctx context.Context, request UnpublishTrainingRequestObject) (UnpublishTrainingResponseObject, error)
|
|
}
|
|
|
|
type StrictHandlerFunc = strictnethttp.StrictHTTPHandlerFunc
|
|
type StrictMiddlewareFunc = strictnethttp.StrictHTTPMiddlewareFunc
|
|
|
|
type StrictHTTPServerOptions struct {
|
|
RequestErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)
|
|
ResponseErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)
|
|
}
|
|
|
|
func NewStrictHandler(ssi StrictServerInterface, middlewares []StrictMiddlewareFunc) ServerInterface {
|
|
return &strictHandler{ssi: ssi, middlewares: middlewares, options: StrictHTTPServerOptions{
|
|
RequestErrorHandlerFunc: func(w http.ResponseWriter, r *http.Request, err error) {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
},
|
|
ResponseErrorHandlerFunc: func(w http.ResponseWriter, r *http.Request, err error) {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
},
|
|
}}
|
|
}
|
|
|
|
func NewStrictHandlerWithOptions(ssi StrictServerInterface, middlewares []StrictMiddlewareFunc, options StrictHTTPServerOptions) ServerInterface {
|
|
return &strictHandler{ssi: ssi, middlewares: middlewares, options: options}
|
|
}
|
|
|
|
type strictHandler struct {
|
|
ssi StrictServerInterface
|
|
middlewares []StrictMiddlewareFunc
|
|
options StrictHTTPServerOptions
|
|
}
|
|
|
|
// ListTrainings operation middleware
|
|
func (sh *strictHandler) ListTrainings(w http.ResponseWriter, r *http.Request) {
|
|
var request ListTrainingsRequestObject
|
|
|
|
handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, request interface{}) (interface{}, error) {
|
|
return sh.ssi.ListTrainings(ctx, request.(ListTrainingsRequestObject))
|
|
}
|
|
for _, middleware := range sh.middlewares {
|
|
handler = middleware(handler, "ListTrainings")
|
|
}
|
|
|
|
response, err := handler(r.Context(), w, r, request)
|
|
|
|
if err != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, err)
|
|
} else if validResponse, ok := response.(ListTrainingsResponseObject); ok {
|
|
if err := validResponse.VisitListTrainingsResponse(w); err != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, err)
|
|
}
|
|
} else if response != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, fmt.Errorf("unexpected response type: %T", response))
|
|
}
|
|
}
|
|
|
|
// CreateTraining operation middleware
|
|
func (sh *strictHandler) CreateTraining(w http.ResponseWriter, r *http.Request) {
|
|
var request CreateTrainingRequestObject
|
|
|
|
var body CreateTrainingJSONRequestBody
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
sh.options.RequestErrorHandlerFunc(w, r, fmt.Errorf("can't decode JSON body: %w", err))
|
|
return
|
|
}
|
|
request.Body = &body
|
|
|
|
handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, request interface{}) (interface{}, error) {
|
|
return sh.ssi.CreateTraining(ctx, request.(CreateTrainingRequestObject))
|
|
}
|
|
for _, middleware := range sh.middlewares {
|
|
handler = middleware(handler, "CreateTraining")
|
|
}
|
|
|
|
response, err := handler(r.Context(), w, r, request)
|
|
|
|
if err != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, err)
|
|
} else if validResponse, ok := response.(CreateTrainingResponseObject); ok {
|
|
if err := validResponse.VisitCreateTrainingResponse(w); err != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, err)
|
|
}
|
|
} else if response != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, fmt.Errorf("unexpected response type: %T", response))
|
|
}
|
|
}
|
|
|
|
// GetTrainingByID operation middleware
|
|
func (sh *strictHandler) GetTrainingByID(w http.ResponseWriter, r *http.Request, trainingID TrainingID) {
|
|
var request GetTrainingByIDRequestObject
|
|
|
|
request.TrainingID = trainingID
|
|
|
|
handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, request interface{}) (interface{}, error) {
|
|
return sh.ssi.GetTrainingByID(ctx, request.(GetTrainingByIDRequestObject))
|
|
}
|
|
for _, middleware := range sh.middlewares {
|
|
handler = middleware(handler, "GetTrainingByID")
|
|
}
|
|
|
|
response, err := handler(r.Context(), w, r, request)
|
|
|
|
if err != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, err)
|
|
} else if validResponse, ok := response.(GetTrainingByIDResponseObject); ok {
|
|
if err := validResponse.VisitGetTrainingByIDResponse(w); err != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, err)
|
|
}
|
|
} else if response != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, fmt.Errorf("unexpected response type: %T", response))
|
|
}
|
|
}
|
|
|
|
// PublishTraining operation middleware
|
|
func (sh *strictHandler) PublishTraining(w http.ResponseWriter, r *http.Request, trainingID TrainingID) {
|
|
var request PublishTrainingRequestObject
|
|
|
|
request.TrainingID = trainingID
|
|
|
|
handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, request interface{}) (interface{}, error) {
|
|
return sh.ssi.PublishTraining(ctx, request.(PublishTrainingRequestObject))
|
|
}
|
|
for _, middleware := range sh.middlewares {
|
|
handler = middleware(handler, "PublishTraining")
|
|
}
|
|
|
|
response, err := handler(r.Context(), w, r, request)
|
|
|
|
if err != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, err)
|
|
} else if validResponse, ok := response.(PublishTrainingResponseObject); ok {
|
|
if err := validResponse.VisitPublishTrainingResponse(w); err != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, err)
|
|
}
|
|
} else if response != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, fmt.Errorf("unexpected response type: %T", response))
|
|
}
|
|
}
|
|
|
|
// RetireTraining operation middleware
|
|
func (sh *strictHandler) RetireTraining(w http.ResponseWriter, r *http.Request, trainingID TrainingID) {
|
|
var request RetireTrainingRequestObject
|
|
|
|
request.TrainingID = trainingID
|
|
|
|
handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, request interface{}) (interface{}, error) {
|
|
return sh.ssi.RetireTraining(ctx, request.(RetireTrainingRequestObject))
|
|
}
|
|
for _, middleware := range sh.middlewares {
|
|
handler = middleware(handler, "RetireTraining")
|
|
}
|
|
|
|
response, err := handler(r.Context(), w, r, request)
|
|
|
|
if err != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, err)
|
|
} else if validResponse, ok := response.(RetireTrainingResponseObject); ok {
|
|
if err := validResponse.VisitRetireTrainingResponse(w); err != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, err)
|
|
}
|
|
} else if response != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, fmt.Errorf("unexpected response type: %T", response))
|
|
}
|
|
}
|
|
|
|
// UnpublishTraining operation middleware
|
|
func (sh *strictHandler) UnpublishTraining(w http.ResponseWriter, r *http.Request, trainingID TrainingID) {
|
|
var request UnpublishTrainingRequestObject
|
|
|
|
request.TrainingID = trainingID
|
|
|
|
handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, request interface{}) (interface{}, error) {
|
|
return sh.ssi.UnpublishTraining(ctx, request.(UnpublishTrainingRequestObject))
|
|
}
|
|
for _, middleware := range sh.middlewares {
|
|
handler = middleware(handler, "UnpublishTraining")
|
|
}
|
|
|
|
response, err := handler(r.Context(), w, r, request)
|
|
|
|
if err != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, err)
|
|
} else if validResponse, ok := response.(UnpublishTrainingResponseObject); ok {
|
|
if err := validResponse.VisitUnpublishTrainingResponse(w); err != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, err)
|
|
}
|
|
} else if response != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, fmt.Errorf("unexpected response type: %T", response))
|
|
}
|
|
}
|
|
|
|
// Base64 encoded, gzipped, json marshaled Swagger object
|
|
var swaggerSpec = []string{
|
|
|
|
"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",
|
|
}
|
|
|
|
// 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
|
|
}
|