smolder

package module
v0.0.0-...-8c88fb5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2019 License: AGPL-3.0 Imports: 7 Imported by: 60

README

smolder

smolder makes it easy to write restful Golang JSON APIs

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorResponseHandler

func ErrorResponseHandler(request *restful.Request, response *restful.Response, origin error, err *ErrorResponse)

ErrorResponseHandler is the default error response handler

func NewSmolderContainer

func NewSmolderContainer(config APIConfig, _shutdownGracefully *bool, _requestIncChan chan int) *restful.Container

NewSmolderContainer initializes a new Container with all the default filters

func Validate

func Validate(request *restful.Request, params []*restful.Parameter) (map[string][]string, error)

Validate is used to check input for required values

Types

type APIConfig

type APIConfig struct {
	BaseURL    string
	PathPrefix string
}

APIConfig contains all parameters required to set up a new smolder API

type APIContext

type APIContext interface {
	Authentication(request *restful.Request) (interface{}, error)
	SetAuth(auth interface{})
}

APIContext contains all the functions required to interact with the API user

type APIContextFactory

type APIContextFactory interface {
	NewAPIContext() APIContext
}

APIContextFactory allows you to retrieve a new APIContext

type APIError

type APIError struct {
	Code          int    `json:"statusCode"`
	InternalError bool   `json:"internalerror,omitempty"`
	Msg           string `json:"detail"`
	Source        struct {
		Pointer string `json:"pointer"`
	} `json:"source"`
	Context string `json:"context,omitempty"`
}

APIError describes an API error

type APIResource

type APIResource interface {
	Register(container *restful.Container, config APIConfig, context APIContextFactory)
}

APIResource contains all the functions required to register a new API resource

type DeleteSupported

type DeleteSupported interface {
	Delete(context APIContext, request *restful.Request, response *restful.Response)
	DeleteAuthRequired() bool
	DeleteDoc() string
	DeleteParams() []*restful.Parameter
}

DeleteSupported is the interface Resources need to fulfill to respond to generic DELETE requests

type ErrorResponse

type ErrorResponse struct {
	Err []APIError `json:"errors"`
}

ErrorResponse is the default error handling response

func NewErrorResponse

func NewErrorResponse(code int, err interface{}, context string) *ErrorResponse

NewErrorResponse creates a new ErrorResponse with the provided values

func (*ErrorResponse) Error

func (err *ErrorResponse) Error() string

type GetIDSupported

type GetIDSupported interface {
	GetByIDs(context APIContext, request *restful.Request, response *restful.Response, ids []string)
	GetByIDsAuthRequired() bool
	Returns() interface{}
}

GetIDSupported is the interface Resources need to fulfill to respond to GET-by-ID requests

type GetSupported

type GetSupported interface {
	Get(context APIContext, request *restful.Request, response *restful.Response, params map[string][]string)
	GetAuthRequired() bool
	GetDoc() string
	GetParams() []*restful.Parameter
	Returns() interface{}
}

GetSupported is the interface Resources need to fulfill to respond to generic GET requests

type PatchSupported

type PatchSupported interface {
	Patch(context APIContext, data interface{}, request *restful.Request, response *restful.Response)
	PatchAuthRequired() bool
	PatchDoc() string
	PatchParams() []*restful.Parameter
	Validate(context APIContext, data interface{}, request *restful.Request) error
	Reads() interface{}
	Returns() interface{}
}

PatchSupported is the interface Resources need to fulfill to respond to generic PATCH requests

type PostSupported

type PostSupported interface {
	Post(context APIContext, data interface{}, request *restful.Request, response *restful.Response)
	PostAuthRequired() bool
	PostDoc() string
	PostParams() []*restful.Parameter
	Validate(context APIContext, data interface{}, request *restful.Request) error
	Reads() interface{}
	Returns() interface{}
}

PostSupported is the interface Resources need to fulfill to respond to generic POST requests

type PutSupported

type PutSupported interface {
	Put(context APIContext, data interface{}, request *restful.Request, response *restful.Response)
	PutAuthRequired() bool
	PutDoc() string
	PutParams() []*restful.Parameter
	Validate(context APIContext, data interface{}, request *restful.Request) error
	Reads() interface{}
	Returns() interface{}
}

PutSupported is the interface Resources need to fulfill to respond to generic PUT requests

type Resource

type Resource struct {
	Name     string
	TypeName string
	Endpoint string
	Doc      string

	Config  APIConfig
	Context APIContextFactory

	Parent interface{}
}

Resource describes an API resource/endpoint

func (Resource) Delete

func (r Resource) Delete(request *restful.Request, response *restful.Response)

Delete responds to DELETE requests

func (Resource) Get

func (r Resource) Get(request *restful.Request, response *restful.Response)

Get responds to GET requests

func (Resource) GetByIDs

func (r Resource) GetByIDs(request *restful.Request, response *restful.Response)

GetByIDs handles GET requests which want to retrieve one or more IDs

func (Resource) Init

func (r Resource) Init(container *restful.Container, resource interface{})

Init registers a resource with the Container and sets up all the supported routes

func (Resource) NotFound

func (r Resource) NotFound(request *restful.Request, response *restful.Response)

NotFound is the default 404 response

func (Resource) Patch

func (r Resource) Patch(request *restful.Request, response *restful.Response)

Patch responds to PATCH requests

func (Resource) Post

func (r Resource) Post(request *restful.Request, response *restful.Response)

Post responds to POST requests

func (Resource) Put

func (r Resource) Put(request *restful.Request, response *restful.Response)

Put responds to PUT requests

type Response

type Response struct {
	Context APIContext  `json:"-"`
	Parent  interface{} `json:"-"`
}

Response provides convenience methods to respond to requests

func (*Response) Send

func (rc *Response) Send(response *restful.Response)

Send responds to a request with http.StatusOK

func (*Response) SendWithHeader

func (rc *Response) SendWithHeader(status int, response *restful.Response)

SendWithHeader responds to a request with a custom HTTP status code

type SupportsEmptyResponse

type SupportsEmptyResponse interface {
	EmptyResponse() interface{}
}

SupportsEmptyResponse is an interface to provide empty-result responses

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL