debian-forge-composer/internal/worker/api/api.gen.go
Lars Karlitski ad11ceecf4 worker: use openapi spec and generated code
Write an openapi spec for the worker API and use `deepmap/oapi-codegen`
to generate scaffolding for the server-side using the `labstack/echo`
server.

Incidentally, echo by default returns the errors in the same format that
worker API always has:

    { "message": "..." }

The API itself is unchanged to make this change easier to understand. It
will be changed to better suit our needs in future commits.
2020-09-06 18:42:23 +01:00

154 lines
5.6 KiB
Go

// Package api provides primitives to interact the openapi HTTP API.
//
// Code generated by github.com/deepmap/oapi-codegen DO NOT EDIT.
package api
import (
"fmt"
"github.com/deepmap/oapi-codegen/pkg/runtime"
"github.com/labstack/echo/v4"
"net/http"
)
// PostJobQueueV1JobsJSONBody defines parameters for PostJobQueueV1Jobs.
type PostJobQueueV1JobsJSONBody map[string]interface{}
// PatchJobQueueV1JobsJobIdJSONBody defines parameters for PatchJobQueueV1JobsJobId.
type PatchJobQueueV1JobsJobIdJSONBody struct {
Result interface{} `json:"result"`
Status string `json:"status"`
}
// PostJobQueueV1JobsRequestBody defines body for PostJobQueueV1Jobs for application/json ContentType.
type PostJobQueueV1JobsJSONRequestBody PostJobQueueV1JobsJSONBody
// PatchJobQueueV1JobsJobIdRequestBody defines body for PatchJobQueueV1JobsJobId for application/json ContentType.
type PatchJobQueueV1JobsJobIdJSONRequestBody PatchJobQueueV1JobsJobIdJSONBody
// ServerInterface represents all server handlers.
type ServerInterface interface {
// create-job
// (POST /job-queue/v1/jobs)
PostJobQueueV1Jobs(ctx echo.Context) error
// get-job
// (GET /job-queue/v1/jobs/{job_id})
GetJobQueueV1JobsJobId(ctx echo.Context, jobId string) error
// update-job
// (PATCH /job-queue/v1/jobs/{job_id})
PatchJobQueueV1JobsJobId(ctx echo.Context, jobId string) error
// add-image
// (POST /job-queue/v1/jobs/{job_id}/artifacts/{name})
PostJobQueueV1JobsJobIdArtifactsName(ctx echo.Context, jobId string, name string) error
// status
// (GET /status)
GetStatus(ctx echo.Context) error
}
// ServerInterfaceWrapper converts echo contexts to parameters.
type ServerInterfaceWrapper struct {
Handler ServerInterface
}
// PostJobQueueV1Jobs converts echo context to params.
func (w *ServerInterfaceWrapper) PostJobQueueV1Jobs(ctx echo.Context) error {
var err error
// Invoke the callback with all the unmarshalled arguments
err = w.Handler.PostJobQueueV1Jobs(ctx)
return err
}
// GetJobQueueV1JobsJobId converts echo context to params.
func (w *ServerInterfaceWrapper) GetJobQueueV1JobsJobId(ctx echo.Context) error {
var err error
// ------------- Path parameter "job_id" -------------
var jobId string
err = runtime.BindStyledParameter("simple", false, "job_id", ctx.Param("job_id"), &jobId)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter job_id: %s", err))
}
// Invoke the callback with all the unmarshalled arguments
err = w.Handler.GetJobQueueV1JobsJobId(ctx, jobId)
return err
}
// PatchJobQueueV1JobsJobId converts echo context to params.
func (w *ServerInterfaceWrapper) PatchJobQueueV1JobsJobId(ctx echo.Context) error {
var err error
// ------------- Path parameter "job_id" -------------
var jobId string
err = runtime.BindStyledParameter("simple", false, "job_id", ctx.Param("job_id"), &jobId)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter job_id: %s", err))
}
// Invoke the callback with all the unmarshalled arguments
err = w.Handler.PatchJobQueueV1JobsJobId(ctx, jobId)
return err
}
// PostJobQueueV1JobsJobIdArtifactsName converts echo context to params.
func (w *ServerInterfaceWrapper) PostJobQueueV1JobsJobIdArtifactsName(ctx echo.Context) error {
var err error
// ------------- Path parameter "job_id" -------------
var jobId string
err = runtime.BindStyledParameter("simple", false, "job_id", ctx.Param("job_id"), &jobId)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter job_id: %s", err))
}
// ------------- Path parameter "name" -------------
var name string
err = runtime.BindStyledParameter("simple", false, "name", ctx.Param("name"), &name)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter name: %s", err))
}
// Invoke the callback with all the unmarshalled arguments
err = w.Handler.PostJobQueueV1JobsJobIdArtifactsName(ctx, jobId, name)
return err
}
// GetStatus converts echo context to params.
func (w *ServerInterfaceWrapper) GetStatus(ctx echo.Context) error {
var err error
// Invoke the callback with all the unmarshalled arguments
err = w.Handler.GetStatus(ctx)
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) {
wrapper := ServerInterfaceWrapper{
Handler: si,
}
router.POST("/job-queue/v1/jobs", wrapper.PostJobQueueV1Jobs)
router.GET("/job-queue/v1/jobs/:job_id", wrapper.GetJobQueueV1JobsJobId)
router.PATCH("/job-queue/v1/jobs/:job_id", wrapper.PatchJobQueueV1JobsJobId)
router.POST("/job-queue/v1/jobs/:job_id/artifacts/:name", wrapper.PostJobQueueV1JobsJobIdArtifactsName)
router.GET("/status", wrapper.GetStatus)
}