2021-12-10 17:15:33 +00:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package apigen
|
|
|
|
|
2022-02-17 13:49:07 +00:00
|
|
|
import (
|
2023-09-23 18:37:11 +01:00
|
|
|
"fmt"
|
2022-02-17 13:49:07 +00:00
|
|
|
"net/http"
|
|
|
|
"reflect"
|
2023-09-23 18:37:11 +01:00
|
|
|
"strings"
|
2022-02-17 13:49:07 +00:00
|
|
|
)
|
2021-12-10 17:15:33 +00:00
|
|
|
|
|
|
|
// Endpoint represents endpoint's configuration.
|
|
|
|
type Endpoint struct {
|
2023-09-23 17:40:30 +01:00
|
|
|
// Name is a free text used to name the endpoint for documentation purpose.
|
|
|
|
// It cannot be empty.
|
|
|
|
Name string
|
|
|
|
// Description is a free text to describe the endpoint for documentation purpose.
|
|
|
|
Description string
|
|
|
|
// MethodName is the name of method of the service interface which handles the business logic of
|
|
|
|
// this endpoint.
|
|
|
|
// It must fulfill the Go language specification for method names
|
|
|
|
// (https://go.dev/ref/spec#MethodName)
|
|
|
|
// TODO: Should we rename this field to be something like ServiceMethodName?
|
|
|
|
MethodName string
|
|
|
|
// RequestName is the name of the method used to name the method in the client side code. When not
|
|
|
|
// set, MethodName is used.
|
|
|
|
// TODO: Should we delete this field in favor of always using MethodName?
|
2022-08-09 22:38:01 +01:00
|
|
|
RequestName string
|
2021-12-10 17:15:33 +00:00
|
|
|
NoCookieAuth bool
|
|
|
|
NoAPIAuth bool
|
2023-09-23 17:40:30 +01:00
|
|
|
// Request is the type that defines the format of the request body.
|
|
|
|
Request interface{}
|
|
|
|
// Response is the type that defines the format of the response body.
|
|
|
|
Response interface{}
|
|
|
|
// QueryParams is the list of query parameters that the endpoint accepts.
|
|
|
|
QueryParams []Param
|
|
|
|
// PathParams is the list of path parameters that appear in the path associated with this
|
|
|
|
// endpoint.
|
|
|
|
PathParams []Param
|
2021-12-10 17:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CookieAuth returns endpoint's cookie auth status.
|
|
|
|
func (e *Endpoint) CookieAuth() bool {
|
|
|
|
return !e.NoCookieAuth
|
|
|
|
}
|
|
|
|
|
|
|
|
// APIAuth returns endpoint's API auth status.
|
|
|
|
func (e *Endpoint) APIAuth() bool {
|
|
|
|
return !e.NoAPIAuth
|
|
|
|
}
|
|
|
|
|
2022-06-09 16:23:08 +01:00
|
|
|
// fullEndpoint represents endpoint with path and method.
|
|
|
|
type fullEndpoint struct {
|
|
|
|
Endpoint
|
2021-12-10 17:15:33 +00:00
|
|
|
Path string
|
|
|
|
Method string
|
|
|
|
}
|
|
|
|
|
|
|
|
// EndpointGroup represents endpoints group.
|
2023-09-23 18:37:11 +01:00
|
|
|
// You should always create a group using API.Group because it validates the field values to
|
|
|
|
// guarantee correct code generation.
|
2021-12-10 17:15:33 +00:00
|
|
|
type EndpointGroup struct {
|
|
|
|
Name string
|
|
|
|
Prefix string
|
2022-06-09 16:23:08 +01:00
|
|
|
endpoints []*fullEndpoint
|
2021-12-10 17:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get adds new GET endpoint to endpoints group.
|
2023-09-23 18:37:11 +01:00
|
|
|
// It panics if path doesn't begin with '/'.
|
2021-12-10 17:15:33 +00:00
|
|
|
func (eg *EndpointGroup) Get(path string, endpoint *Endpoint) {
|
|
|
|
eg.addEndpoint(path, http.MethodGet, endpoint)
|
|
|
|
}
|
|
|
|
|
2022-04-07 09:05:28 +01:00
|
|
|
// Patch adds new PATCH endpoint to endpoints group.
|
2023-09-23 18:37:11 +01:00
|
|
|
// It panics if path doesn't begin with '/'.
|
2022-04-07 09:05:28 +01:00
|
|
|
func (eg *EndpointGroup) Patch(path string, endpoint *Endpoint) {
|
|
|
|
eg.addEndpoint(path, http.MethodPatch, endpoint)
|
|
|
|
}
|
|
|
|
|
2022-04-27 12:52:02 +01:00
|
|
|
// Post adds new POST endpoint to endpoints group.
|
2023-09-23 18:37:11 +01:00
|
|
|
// It panics if path doesn't begin with '/'.
|
2022-04-27 12:52:02 +01:00
|
|
|
func (eg *EndpointGroup) Post(path string, endpoint *Endpoint) {
|
|
|
|
eg.addEndpoint(path, http.MethodPost, endpoint)
|
2022-03-21 12:15:33 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 16:59:55 +01:00
|
|
|
// Delete adds new DELETE endpoint to endpoints group.
|
2023-09-23 18:37:11 +01:00
|
|
|
// It panics if path doesn't begin with '/'.
|
2022-04-28 16:59:55 +01:00
|
|
|
func (eg *EndpointGroup) Delete(path string, endpoint *Endpoint) {
|
|
|
|
eg.addEndpoint(path, http.MethodDelete, endpoint)
|
|
|
|
}
|
|
|
|
|
2021-12-10 17:15:33 +00:00
|
|
|
// addEndpoint adds new endpoint to endpoints list.
|
2023-09-23 18:37:11 +01:00
|
|
|
// It panics if path doesn't begin with '/'.
|
2021-12-10 17:15:33 +00:00
|
|
|
func (eg *EndpointGroup) addEndpoint(path, method string, endpoint *Endpoint) {
|
2023-09-23 18:37:11 +01:00
|
|
|
if !strings.HasPrefix(path, "/") {
|
|
|
|
panic(
|
|
|
|
fmt.Sprintf(
|
|
|
|
"invalid path for method %q of EndpointGroup %q. path must start with slash, got %q",
|
|
|
|
method,
|
|
|
|
eg.Name,
|
|
|
|
path,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-09 16:23:08 +01:00
|
|
|
ep := &fullEndpoint{*endpoint, path, method}
|
|
|
|
for i, e := range eg.endpoints {
|
|
|
|
if e.Path == path && e.Method == method {
|
|
|
|
eg.endpoints[i] = ep
|
|
|
|
return
|
|
|
|
}
|
2021-12-10 17:15:33 +00:00
|
|
|
}
|
2022-06-09 16:23:08 +01:00
|
|
|
eg.endpoints = append(eg.endpoints, ep)
|
2021-12-10 17:15:33 +00:00
|
|
|
}
|
2022-02-17 13:49:07 +00:00
|
|
|
|
|
|
|
// Param represents string interpretation of param's name and type.
|
|
|
|
type Param struct {
|
|
|
|
Name string
|
|
|
|
Type reflect.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewParam constructor which creates new Param entity by given name and type.
|
|
|
|
func NewParam(name string, instance interface{}) Param {
|
|
|
|
return Param{
|
|
|
|
Name: name,
|
|
|
|
Type: reflect.TypeOf(instance),
|
|
|
|
}
|
|
|
|
}
|