vc

package
v0.0.0-...-795d1f5 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2017 License: MIT Imports: 15 Imported by: 1

README

VC

VC provides an opinionated View Controller system for Go apis.

It prescribes a structure for registering handlers for specific entity and REST method combinations.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EmptyResponse = &Response{}

EmptyResponse is used to determine if a response should be empty.

View Source
var SideloadQueryKey = "include"

SideloadQueryKey is the name of the query parameter that determines which entities should be sideloaded.

View Source
var StateQueryKey = "state"

StateQueryKey is the name of the query paramtere that determines which entity states should be returned.

Functions

func ActorID

func ActorID(actor Actor) string

ActorID returns an identifier for the supplied actor.

func ContextParams

func ContextParams(context *ctx.Context) httprouter.Params

ContextParams returns the httprouter.Params for the context.

func MustSchema

func MustSchema(schemaString string) *schema.Schema

func MustSchemaFromFile

func MustSchemaFromFile(file string) *schema.Schema

func RegisterCriteriaTransformer

func RegisterCriteriaTransformer(transformer func(*ctx.Context, *Criteria))

func RespondWithData

func RespondWithData(w http.ResponseWriter, r *http.Request, data interface{}, code int)

RespondWithData will use the default renderer to render the response.

func RespondWithError

func RespondWithError(w http.ResponseWriter, r *http.Request, err error)

RespondWithError will return an error response with the appropriate message, and status codes set.

func RespondWithRenderedData

func RespondWithRenderedData(renderer Renderer, w http.ResponseWriter, r *http.Request, data interface{}, code int)

RespondWithRenderedData will render the supplied data as a response.

func RespondWithStatusCode

func RespondWithStatusCode(w http.ResponseWriter, r *http.Request, code int)

RespondWithStatusCode returns an empty response.

func SetContextCriteria

func SetContextCriteria(context *ctx.Context, criteria *Criteria)

SetContextCriteria sets the criteria against a context.

func SetContextParams

func SetContextParams(context *ctx.Context, params httprouter.Params)

SetContextParams sets the supplied httprouter.Params on the context.

func UnmarshalAndValidateRequestSchema

func UnmarshalAndValidateRequestSchema(context *ctx.Context, s *schema.Schema, obj interface{}) error

UnmarshalAndValidateRequestSchema attempts to validate the body on the suppled context against the supplied schema, then unmarshal it into the supplied obj.

func UnmarshalAndValidateSchema

func UnmarshalAndValidateSchema(body []byte, s *schema.Schema, obj interface{}) error

UnmarshalAndValidateSchema attempts to validate the body against the supplied schema, then unmarshal it into the supplied obj.

Types

type APIError

type APIError struct {
	Code        int               `json:"code,omitempty"`
	Description string            `json:"description,omitempty"`
	Error       string            `json:"error"`
	Fields      map[string]string `json:"fields,omitempty"`
}

APIError represents an API error response. This will be passed to a renderer error method for conversion into an appropriate response.

type ActionHandler

type ActionHandler interface {
	HandleAction(*ctx.Context) (interface{}, int, error)
}

ActionHandler implementers are responsible for returning payload data for a request, along with a status code or error.

type ActionHandlerFunc

type ActionHandlerFunc struct {
	Handler func(*ctx.Context) (interface{}, int, error)
}

ActionHandlerFunc wraps a function with the HandleAction signature to a full ActionHandler interface.

func (ActionHandlerFunc) HandleAction

func (fn ActionHandlerFunc) HandleAction(context *ctx.Context) (interface{}, int, error)

HandleAction implements the ActionHander interface and simply calls the underlying function.

type ActionProcessor

type ActionProcessor struct {
	SideloadEnabled bool
	MetricsRegistry metrics.Registry
}

ActionProcessor handles an entire action lifecycle, from data retrieval through to unlocking, and responding.

func NewActionProcessor

func NewActionProcessor() *ActionProcessor

func (*ActionProcessor) HTTPHandler

func (p *ActionProcessor) HTTPHandler(typ, action string, handler ActionHandler) httprouter.Handle

HTTPHandler takes an ActionHandler and returns a http.Handler instance that can be used. The type and action are used to determine the context in several areas, such as transformers and metrics.

func (*ActionProcessor) HandleActionFunc

func (p *ActionProcessor) HandleActionFunc(typ, action string, fn func(*ctx.Context) (interface{}, int, error)) httprouter.Handle

HandleActionFunc returns an http.Handler for the suppled action function. A type and action name are used in metrics and logging functions.

type Actor

type Actor interface {
	// ActorInfo returns both the identifier and type of the actor.
	ActorInfo() (actorID string, actorType string)
}

Actor represents a request actor, and is generally either an oauth client or user.

type AnnotatedError

type AnnotatedError interface {
	ErrorFields() map[string]string
}

AnnotatedError defines an interface for additional metadata for an error. This is where you want to put all of your error data when it's more than just a single error case condition, for example all of the invalid fields in a bad update or create operation.

type Criteria

type Criteria struct {
	// Sideload is a slice of related entities to sideload.
	Sideload []string
	// OrderColumn defines the order that collections should be ordered by.
	OrderColumn    *string
	OrderAscending bool
	// From represents a time that entities should have been updated after.
	From *time.Time
	// Limit is the number of results in a collection
	Limit int
	// State is an array of item states to filter by
	State []string
}

Criteria defines how results should be modified, or filtered.

func ContextCriteria

func ContextCriteria(context *ctx.Context) *Criteria

ContextCriteria returns the criteria for the supplied context.

func RequestCriteria

func RequestCriteria(r *http.Request) *Criteria

RequestCriteria generates a criteria instance from the request.

func (Criteria) ShouldSideload

func (criteria Criteria) ShouldSideload(name string) bool

ShouldSideload returns true if the supplied name is in the sideload list.

type DescriptiveError

type DescriptiveError interface {
	ErrorDescription() string
}

DescriptiveError defines an interface for returning a human readable error description. It’s ok to use humour here, as these descriptions will be read by developers, or at least real people, and can often be read in a time of frustration. Be authentic with your descriptions, and good karma may come your way.

type Renderer

type Renderer interface {
	Render(interface{}) ([]byte, error)
	RenderError(APIError) []byte
}
var (
	// DefaultRenderer is the renderer used by default. This can be changed at
	// runtime to a renderer that suits.
	// DefaultRenderer = render.JSONRenderer{}
	DefaultRenderer Renderer
)

type Response

type Response struct {
	Payload interface{} `json:"payload"`
	// A pointer is used here to allow empty maps to be returned.
	Sideload *map[string]map[string]interface{} `json:"related,omitempty"`
}

Response represents a non error display response.

type StatusError

type StatusError interface {
	StatusCode() int
}

StatusError defines an interface for an error that also includes a custom http status code. Use this liberally to differentiate service errors from things like authentication, authorization or validation errors.

type StructuredLogsError

type StructuredLogsError interface {
	LogFields() map[string]string
}

StructuredLogsError defines an interface for additional metadata for an error. This metadata will be logged, but not output to the public error response.

Jump to

Keyboard shortcuts

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