httpext

package
v0.0.0-...-313f127 Latest Latest
Warning

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

Go to latest
Published: May 6, 2016 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InvokeHandler

func InvokeHandler(h Handler, method, path string,
	w http.ResponseWriter, r *http.Request,
	tx *sqlx.Tx, renderer ResponseRenderer) error

InvokeHandler invokes the handler given. This test should be used in unit tests only.

func NewHttpHandler

func NewHttpHandler(db *sqlx.DB, renderer ResponseRenderer, h Handler) http.Handler

Types

type ChainHandler

type ChainHandler interface {
	Handler
	// Then creates a new handler adds all handlers from this
	// chain and the handler provided.
	Then(h Handler) ChainHandler
}

ChainHandler extends Handler and is used by Chain() to allow for incrementally building chained handlers.

func Chain

func Chain(handlers ...Handler) ChainHandler

Chain creates a handler that executes the handlers in sequence. If a handler returns an error or writes any bytes to the http.ResponseWriter, the chain is broken and no more handlers are processed.

type Handler

type Handler interface {
	// Handle handles a HTTP request and returns an error if the operation fails. The implementor
	// is responsible for writing the error to the response.
	Handle(tx *sqlx.Tx, renderer ResponseRenderer, w http.ResponseWriter, r *http.Request) error
}

type HttpError

type HttpError interface {
	StatusCode() int
	Error() string
	InnerError() error
}

HttpError is an interface for errors that are transferable over HTTP as JSON.

func EncapulateIfError

func EncapulateIfError(err error, statusCode int) HttpError

EncapulateIfError encapsulates the error provided as a HttpError if not nil. If the error provided is nil, this function returns nil.

func NewHttpError

func NewHttpError(err error, statusCode int) HttpError

NewHttpError returns an encapsulated error suitable for JSON serialization. The error also has a HTTP status code for usability.

func ReadInt64ID

func ReadInt64ID(vars map[string]string, fieldName string) (int64, HttpError)

ReadInt64ID returns an int64 ID read from the mux vars (URL vars). vars is generated from a request by using 'mux.Vars(r)'. If the operation fails a HttpError with status code http.StatusBadRequest is returned.

type MockResponseRenderer

type MockResponseRenderer struct {
	mock.Mock
}

func (*MockResponseRenderer) WriteEmpty

func (_m *MockResponseRenderer) WriteEmpty(w http.ResponseWriter, statusCode int)

WriteEmpty provides a mock function with given fields: w, statusCode

func (*MockResponseRenderer) WriteError

func (_m *MockResponseRenderer) WriteError(w http.ResponseWriter, err error)

WriteError provides a mock function with given fields: w, err

func (*MockResponseRenderer) WriteObject

func (_m *MockResponseRenderer) WriteObject(w http.ResponseWriter, statusCode int, val interface{})

WriteObject provides a mock function with given fields: w, statusCode, val

type ResponseRenderer

type ResponseRenderer interface {
	WriteEmpty(w http.ResponseWriter, statusCode int)
	WriteObject(w http.ResponseWriter, statusCode int, val interface{})
	WriteError(w http.ResponseWriter, err error)
}

func NewJSONResponseRenderer

func NewJSONResponseRenderer() ResponseRenderer

NewJSONResponseRenderer creates a renderer that outputs JSON.

Jump to

Keyboard shortcuts

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