rest

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2021 License: MIT Imports: 10 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var HTTPBadRequest = &HTTPError{
	Code:    400,
	Type:    "https://httpstatus.es/400",
	Message: "Bad request",
}

HTTPBadRequest is a HTTP bad request 400

View Source
var HTTPConflict = &HTTPError{
	Code:    409,
	Type:    "https://httpstatus.es/409",
	Message: "Conflict",
}

HTTPConflict is a HTTP conflict 409

View Source
var HTTPForbidden = &HTTPError{
	Code:    403,
	Type:    "https://httpstatus.es/403",
	Message: "Forbidden",
}

HTTPForbidden is a HTTP forbidden 403

View Source
var HTTPMethodNotAllowed = &HTTPError{
	Code:    405,
	Type:    "https://httpstatus.es/405",
	Message: "Method not allowed",
}

HTTPMethodNotAllowed is a HTTP method not allowed 405

View Source
var HTTPNotFound = &HTTPError{
	Code:    404,
	Type:    "https://httpstatus.es/404",
	Message: "Not found",
}

HTTPNotFound is a HTTP not found 404

View Source
var HTTPUnauthorized = &HTTPError{
	Code:    401,
	Type:    "https://httpstatus.es/401",
	Message: "Unauthorized",
}

HTTPUnauthorized is a HTTP Unauthorized 401

View Source
var JsonResponseEncoder jsonResponseEncoder
View Source
var XmlResponseEncoder xmlResponseEncoder

Functions

func HTTPErrorMatcher

func HTTPErrorMatcher(httpError *HTTPError) routing.Matcher

HTTPErrorMatcher is a routing.Matcher that always response with a given HTTPError. Usually useful at the end of a routing chain as catch all for MethodNotAllowed or NotFound-

func ResourceMatcher

func ResourceMatcher(resource Resource) routing.Matcher

func ResourcesMatcher

func ResourcesMatcher(prefix string, collection Resources) routing.Matcher

Types

type HTTPError

type HTTPError struct {
	Code    int    `json:"code" xml:"code"`
	Type    string `json:"type" xml:"type"`
	Message string `json:"message" xml:"message"`
	Details string `json:"details,omitempty" xml:"details,omitempty"`
}

HTTPError is an error result of a HTTP/REST operation. Implements the Error interface.

func HTTPInternalServerError

func HTTPInternalServerError(err error) *HTTPError

HTTPInternalServerError wraps an arbitrary Error as HTTP internal server error 500

func WrapError

func WrapError(err error) *HTTPError

WrapError wrap a generic error as HTTPError. If err already is a HTTPError it will be left intact, otherwise the error will be mapped to InternalServerError

func (*HTTPError) Error

func (e *HTTPError) Error() string

func (*HTTPError) Send

func (e *HTTPError) Send(response http.ResponseWriter, encoder ResponseEncoder)

Send the HTTPError the a http.ResponseWriter

func (*HTTPError) WithDetails

func (e *HTTPError) WithDetails(details string) *HTTPError

WithDetails creates a new HTTPError with extra detail message

type LimitedResource

type LimitedResource struct {
	ResourceBase
	RequestSizeLimit int64
}

func (LimitedResource) BeforeFilter

func (r LimitedResource) BeforeFilter(resp http.ResponseWriter, req *http.Request) bool

type LimitedResources

type LimitedResources struct {
	ResourcesBase
	RequestSizeLimit int64
}

func (LimitedResources) BeforeFilter

func (r LimitedResources) BeforeFilter(resp http.ResponseWriter, req *http.Request) bool
type Link struct {
	Href   string `json:"href" xml:"href"`
	Method string `json:"method,omitempty" xml:"method,omitempty"`
}

Link is the minimal implementation of a HATOAS link

func SimpleLink(href string) Link

SimpleLink create a simple HATOAS link

type Resource

type Resource interface {
	BeforeFilter(resp http.ResponseWriter, req *http.Request) bool
	Self() Link
	Get(request *http.Request) (interface{}, error)
	Post(request *http.Request) (interface{}, error)
	Patch(request *http.Request) (interface{}, error)
	Update(request *http.Request) (interface{}, error)
	Delete(request *http.Request) (interface{}, error)

	SubResources() routing.Matcher
}

func NewStaticResource

func NewStaticResource(result *Result, self Link) Resource

func StaticContent

func StaticContent(content []byte, contentType string, selfHref string) Resource

type ResourceBase

type ResourceBase struct{}

func (ResourceBase) BeforeFilter

func (ResourceBase) BeforeFilter(resp http.ResponseWriter, req *http.Request) bool

func (ResourceBase) Delete

func (ResourceBase) Delete(request *http.Request) (interface{}, error)

func (ResourceBase) Get

func (ResourceBase) Get(request *http.Request) (interface{}, error)

func (ResourceBase) Patch

func (ResourceBase) Patch(request *http.Request) (interface{}, error)

func (ResourceBase) Post

func (ResourceBase) Post(request *http.Request) (interface{}, error)

func (ResourceBase) SubResources

func (ResourceBase) SubResources() routing.Matcher

func (ResourceBase) Update

func (ResourceBase) Update(request *http.Request) (interface{}, error)

type Resources

type Resources interface {
	BeforeFilter(resp http.ResponseWriter, req *http.Request) bool
	Self() Link
	Create(request *http.Request) (Resource, error)
	List(request *http.Request) (interface{}, error)

	FindById(id string) (interface{}, error)
}

type ResourcesBase

type ResourcesBase struct{}

func (ResourcesBase) BeforeFilter

func (ResourcesBase) BeforeFilter(resp http.ResponseWriter, req *http.Request) bool

func (ResourcesBase) Create

func (ResourcesBase) Create(*http.Request) (Resource, error)

func (ResourcesBase) FindById

func (ResourcesBase) FindById(id string) (interface{}, error)

func (ResourcesBase) List

func (ResourcesBase) List(*http.Request) (interface{}, error)

type ResponseEncoder

type ResponseEncoder interface {
	ContentType() string
	Encode(output io.Writer, data interface{}) error
}

func StdResponseEncoderChooser

func StdResponseEncoderChooser(request *http.Request) ResponseEncoder

type ResponseEncoderChooser

type ResponseEncoderChooser func(*http.Request) ResponseEncoder

type Result

type Result struct {
	Status int
	Header http.Header
	Body   interface{}
}

Result allows a better control of the response of a REST operation. This should be used if a REST handler needs to modify HTTP headers or has a specific status.

func Created

func Created() *Result

Created creates a CREATED (201) response.

func Ok

func Ok() *Result

Ok creates an OK (200) response.

func Status

func Status(status int) *Result

Status creates a response with a specific HTTP status

func (*Result) AddHeader

func (r *Result) AddHeader(key, value string) *Result

func (*Result) Send

func (r *Result) Send(resp http.ResponseWriter, encoder ResponseEncoder) error

func (*Result) WithBody

func (r *Result) WithBody(body interface{}) *Result

func (*Result) WithStatus

func (r *Result) WithStatus(status int) *Result

WithStatus modifies the HTTP status of a response

type StaticResource

type StaticResource struct {
	ResourceBase
	// contains filtered or unexported fields
}

func (StaticResource) Get

func (r StaticResource) Get(request *http.Request) (interface{}, error)

func (StaticResource) Self

func (r StaticResource) Self() Link

Jump to

Keyboard shortcuts

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