282 lines
7.9 KiB
Go
282 lines
7.9 KiB
Go
//go:build go1.22
|
|
|
|
// Package api provides primitives to interact with the openapi HTTP API.
|
|
//
|
|
// Code generated by github.com/deepmap/oapi-codegen/v2 version v2.2.0 DO NOT EDIT.
|
|
package api
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/oapi-codegen/runtime"
|
|
)
|
|
|
|
// CreateElectionRequest defines model for CreateElectionRequest.
|
|
type CreateElectionRequest struct {
|
|
AreVotersKnown bool `json:"areVotersKnown"`
|
|
Choices []string `json:"choices"`
|
|
ExpiresAt time.Time `json:"expiresAt"`
|
|
|
|
// MaxVoters Must be greater than 0 when voters are known; 0 = no limit
|
|
MaxVoters int `json:"maxVoters"`
|
|
Name string `json:"name"`
|
|
Tokens int `json:"tokens"`
|
|
}
|
|
|
|
// CreateElectionResponse defines model for CreateElectionResponse.
|
|
type CreateElectionResponse struct {
|
|
VoterIdentities *[]string `json:"voterIdentities,omitempty"`
|
|
}
|
|
|
|
// CreateVotesRequest defines model for CreateVotesRequest.
|
|
type CreateVotesRequest struct {
|
|
Choices []struct {
|
|
ChoiceText string `json:"choiceText"`
|
|
Tokens int `json:"tokens"`
|
|
} `json:"choices"`
|
|
|
|
// VoterIdentity Must be filled if election has known voters
|
|
VoterIdentity *string `json:"voterIdentity,omitempty"`
|
|
}
|
|
|
|
// ElectionResultsResponse defines model for ElectionResultsResponse.
|
|
type ElectionResultsResponse struct {
|
|
Results *[]VotesForChoice `json:"results,omitempty"`
|
|
}
|
|
|
|
// ErrorResponse defines model for ErrorResponse.
|
|
type ErrorResponse struct {
|
|
// Code Machine-readable error code
|
|
Code int `json:"code"`
|
|
|
|
// Details Additional error details when available
|
|
Details *map[string]interface{} `json:"details,omitempty"`
|
|
|
|
// Message Human-readable error message
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
// VotesForChoice defines model for VotesForChoice.
|
|
type VotesForChoice struct {
|
|
Choice string `json:"choice"`
|
|
Votes int `json:"votes"`
|
|
}
|
|
|
|
// CreateElectionJSONRequestBody defines body for CreateElection for application/json ContentType.
|
|
type CreateElectionJSONRequestBody = CreateElectionRequest
|
|
|
|
// CreateVotesJSONRequestBody defines body for CreateVotes for application/json ContentType.
|
|
type CreateVotesJSONRequestBody = CreateVotesRequest
|
|
|
|
// ServerInterface represents all server handlers.
|
|
type ServerInterface interface {
|
|
// Create a new election
|
|
// (POST /election)
|
|
CreateElection(w http.ResponseWriter, r *http.Request)
|
|
// Get the results of an election
|
|
// (GET /election/{id}/results)
|
|
GetElectionResults(w http.ResponseWriter, r *http.Request, id int)
|
|
// Cast your votes for an election
|
|
// (POST /election/{id}/votes)
|
|
CreateVotes(w http.ResponseWriter, r *http.Request, id int)
|
|
}
|
|
|
|
// 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
|
|
|
|
// CreateElection operation middleware
|
|
func (siw *ServerInterfaceWrapper) CreateElection(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
|
|
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
siw.Handler.CreateElection(w, r)
|
|
}))
|
|
|
|
for _, middleware := range siw.HandlerMiddlewares {
|
|
handler = middleware(handler)
|
|
}
|
|
|
|
handler.ServeHTTP(w, r.WithContext(ctx))
|
|
}
|
|
|
|
// GetElectionResults operation middleware
|
|
func (siw *ServerInterfaceWrapper) GetElectionResults(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
// ------------- Path parameter "id" -------------
|
|
var id int
|
|
|
|
err = runtime.BindStyledParameterWithOptions("simple", "id", r.PathValue("id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
|
|
if err != nil {
|
|
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "id", Err: err})
|
|
return
|
|
}
|
|
|
|
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
siw.Handler.GetElectionResults(w, r, id)
|
|
}))
|
|
|
|
for _, middleware := range siw.HandlerMiddlewares {
|
|
handler = middleware(handler)
|
|
}
|
|
|
|
handler.ServeHTTP(w, r.WithContext(ctx))
|
|
}
|
|
|
|
// CreateVotes operation middleware
|
|
func (siw *ServerInterfaceWrapper) CreateVotes(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
// ------------- Path parameter "id" -------------
|
|
var id int
|
|
|
|
err = runtime.BindStyledParameterWithOptions("simple", "id", r.PathValue("id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
|
|
if err != nil {
|
|
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "id", Err: err})
|
|
return
|
|
}
|
|
|
|
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
siw.Handler.CreateVotes(w, r, id)
|
|
}))
|
|
|
|
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, StdHTTPServerOptions{})
|
|
}
|
|
|
|
type StdHTTPServerOptions struct {
|
|
BaseURL string
|
|
BaseRouter *http.ServeMux
|
|
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, m *http.ServeMux) http.Handler {
|
|
return HandlerWithOptions(si, StdHTTPServerOptions{
|
|
BaseRouter: m,
|
|
})
|
|
}
|
|
|
|
func HandlerFromMuxWithBaseURL(si ServerInterface, m *http.ServeMux, baseURL string) http.Handler {
|
|
return HandlerWithOptions(si, StdHTTPServerOptions{
|
|
BaseURL: baseURL,
|
|
BaseRouter: m,
|
|
})
|
|
}
|
|
|
|
// HandlerWithOptions creates http.Handler with additional options
|
|
func HandlerWithOptions(si ServerInterface, options StdHTTPServerOptions) http.Handler {
|
|
m := options.BaseRouter
|
|
|
|
if m == nil {
|
|
m = http.NewServeMux()
|
|
}
|
|
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,
|
|
}
|
|
|
|
m.HandleFunc("POST "+options.BaseURL+"/election", wrapper.CreateElection)
|
|
m.HandleFunc("GET "+options.BaseURL+"/election/{id}/results", wrapper.GetElectionResults)
|
|
m.HandleFunc("POST "+options.BaseURL+"/election/{id}/votes", wrapper.CreateVotes)
|
|
|
|
return m
|
|
}
|