rest

package
v0.0.0-...-4426a52 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2015 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package rest provides RESTful support for applications.

type MyResource struct {
}

func (*MyResource) Path() string {
	return "/my/path"
}

func (*MyResource) GET(c context.Context) (interface{}, error) {
	return &myModel{}, nil
}

func (*MyResource) POST(c context.Context) (interface{}, error) {
	return &myModel{}, nil
}

func (*MyResource) DELETE(c context.Context) (interface{}, error) {
	return &myModel{}, nil
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EntityFromContext

func EntityFromContext(c context.Context, v interface{}) error

EntityFromContext returns marshalled http.Request.Body.

func ParamsFromContext

func ParamsFromContext(c context.Context) map[string]string

ParamsFromContext returns path params.

func RequestFromContext

func RequestFromContext(c context.Context) *http.Request

RequestFromContext returns http.Request. Panic if http.Request is not in the given context.

func ResponseWriterFromContext

func ResponseWriterFromContext(c context.Context) http.ResponseWriter

ResponseWriterFromContext returns http.ResponseWriter. Panic if http.ResponseWriter is not in the given context.

func ValidEntityFromContext

func ValidEntityFromContext(c context.Context, v interface{}) error

ValidEntityFromContext is similar to EntityFromContext but also validate the entity.

Types

type Bundle

type Bundle struct {
}

Bundle adds support for RESTful application.

func (*Bundle) Initialize

func (bundle *Bundle) Initialize(bootstrap *core.Bootstrap)

func (*Bundle) Run

func (bundle *Bundle) Run(conf interface{}, env *core.Environment) error

Run registers the RESTful handler and set JSONProvider as default. To support other providers (like XML), use core.Server.Register(), e.g:

environment.Server.Register(&rest.XMLProvider{})

type Consumes

type Consumes interface {
	Consumes() []string
}

Consumes defines the MIME Types that a resource can accept.

type DELETE

type DELETE interface {
	Path() string
	DELETE(context.Context) (interface{}, error)
}

type ErrorMapper

type ErrorMapper interface {
	MapError(error, http.ResponseWriter, *http.Request)
}

ErrorMapper maps error to http error.

type GET

type GET interface {
	Path() string
	GET(context.Context) (interface{}, error)
}
type HEAD interface {
	Path() string
	HEAD(context.Context) (interface{}, error)
}

type HTTPError

type HTTPError struct {
	Message string
	Code    int
}

func NewHTTPError

func NewHTTPError(msg string, code int) *HTTPError

func (*HTTPError) Error

func (e *HTTPError) Error() string

type JSONProvider

type JSONProvider struct {
}

JSONProvider reads JSON request and responds JSON.

func (*JSONProvider) ContentTypes

func (p *JSONProvider) ContentTypes() []string

func (*JSONProvider) IsReadable

func (p *JSONProvider) IsReadable(r *http.Request, v interface{}) bool

func (*JSONProvider) IsWriteable

func (p *JSONProvider) IsWriteable(r *http.Request, v interface{}, w http.ResponseWriter) bool

func (*JSONProvider) Read

func (p *JSONProvider) Read(r *http.Request, v interface{}) error

func (*JSONProvider) Write

func (p *JSONProvider) Write(r *http.Request, v interface{}, w http.ResponseWriter) error

type Metrics

type Metrics interface {
	Metrics() string
}

Metrics adds metrics of timing for the endpoint.

type POST

type POST interface {
	Path() string
	POST(context.Context) (interface{}, error)
}

type PUT

type PUT interface {
	Path() string
	PUT(context.Context) (interface{}, error)
}

type Produces

type Produces interface {
	Produces() []string
}

Produces defines the MIME Types that a resource can produce.

type Provider

type Provider interface {
	// ContentTypes returns list of MIME types associated with this provider.
	ContentTypes() []string
	RequestReader
	ResponseWriter
}

Provider define reader and writer for particular MIME types.

type RequestReader

type RequestReader interface {
	IsReadable(*http.Request, interface{}) bool
	Read(*http.Request, interface{}) error
}

RequestReader reads entity from message body.

type ResourceHandler

type ResourceHandler struct {
	// contains filtered or unexported fields
}

ResourceHandler implements core.ResourceHandler

func NewResourceHandler

func NewResourceHandler(env *core.Environment) *ResourceHandler

func (*ResourceHandler) AddProvider

func (h *ResourceHandler) AddProvider(provider Provider)

AddProvider adds the given provider to the resource handler.

func (*ResourceHandler) HandleResource

func (h *ResourceHandler) HandleResource(v interface{})

Handle must only be called after all providers are added.

type ResponseWriter

type ResponseWriter interface {
	IsWriteable(*http.Request, interface{}, http.ResponseWriter) bool
	Write(*http.Request, interface{}, http.ResponseWriter) error
}

ResponseWriter writes entity to message body.

type XMLProvider

type XMLProvider struct {
}

XMLProvider reads XML request and responds XML.

func (*XMLProvider) ContentTypes

func (p *XMLProvider) ContentTypes() []string

func (*XMLProvider) IsReadable

func (p *XMLProvider) IsReadable(r *http.Request, v interface{}) bool

func (*XMLProvider) IsWriteable

func (p *XMLProvider) IsWriteable(r *http.Request, v interface{}, w http.ResponseWriter) bool

func (*XMLProvider) Read

func (p *XMLProvider) Read(r *http.Request, v interface{}) error

func (*XMLProvider) Write

func (p *XMLProvider) Write(r *http.Request, v interface{}, w http.ResponseWriter) error

Jump to

Keyboard shortcuts

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