responders

package
v0.0.0-...-86643de Latest Latest
Warning

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

Go to latest
Published: May 10, 2019 License: BSD-2-Clause Imports: 6 Imported by: 2

Documentation

Overview

The responders package provides objects capable of making different kinds of responses to requests.

The HTTPResponder allows projects to make normal low-level HTTP responses, such as redirection or 200 OK responses. Where the APIResponder allows you to easily and quickly build data services that can be consumed by your apps.

Advanced users can build their own APIResponder if they want more control over how Goweb builds data responses.

Index

Constants

View Source
const (
	// DefaultStandardFieldDataKey is the default response object field for the data.
	DefaultStandardFieldDataKey string = "d"
	// DefaultStandardFieldStatusKey is the default response object field for the status.
	DefaultStandardFieldStatusKey string = "s"
	// DefaultStandardFieldErrorsKey is the default response object field for the errors.
	DefaultStandardFieldErrorsKey string = "e"
)
View Source
const (
	// DefaultAlways200ParamName is the default parameter name that tells the GowebHTTPResponder to always
	// return with a 200 status code.  By default, this will be "always200".
	DefaultAlways200ParamName string = "always200"
)
View Source
const (
	DefaultCallbackParameter string = "callback"
)

Variables

View Source
var (
	// Always200ParamName is the parameter name that tells the GowebHTTPResponder to always
	// return with a 200 status code.  By default, this will be "always200" or DefaultAlways200ParamName.
	Always200ParamName string = DefaultAlways200ParamName
)
View Source
var (
	CallbackParameter string = DefaultCallbackParameter
)

Functions

This section is empty.

Types

type APIResponder

type APIResponder interface {

	// SetCodecService sets the codec service to use.
	SetCodecService(codecsservices.CodecService)

	// GetCodecService gets the codec service that will be used by this object.
	GetCodecService() codecsservices.CodecService

	// TransformStandardResponseObject transforms the standard response object before it is written to the response if a
	// transformer func has been set via SetStandardResponseObjectTransformer.  Otherwise, it just returns the same
	// object that is passed in.
	TransformStandardResponseObject(ctx context.Context, object interface{}) (interface{}, error)

	// SetStandardResponseObjectTransformer sets the function to use to transform the standard response object before it is
	// written to the response.
	//
	// You should use this function to control what kind of response your API makes.
	SetStandardResponseObjectTransformer(transformer func(ctx context.Context, object interface{}) (interface{}, error))

	// Responds to the Context with the specified status, data and errors.
	Respond(ctx context.Context, status int, data interface{}, errors []string) error

	// WriteResponseObject writes the status code and response object to the HttpResponseWriter in
	// the specified context, in the format best suited based on the
	// request.  In certain cases, some data may be added to the
	// passed in context.Context value's CodecOptions() value.
	//
	// Goweb uses the WebCodecService to decide which codec to use when responding
	// see http://godoc.org/github.com/stretchr/codecs/services#WebCodecService for more information.
	//
	// This method should be used when the Goweb Standard Response Object does not satisfy the needs of
	// the API, but other Respond* methods are recommended.
	WriteResponseObject(ctx context.Context, status int, responseObject interface{}) error

	// RespondWithData responds with the specified data, no errors and a 200 StatusOK response.
	RespondWithData(ctx context.Context, data interface{}) error

	// RespondWithError responds with the specified error message and status code.
	RespondWithError(ctx context.Context, status int, err string) error
}

APIResponder represents objects capable of provide API responses. Note that when one of the response methods is called, it may (depending on the request) add data to the map returned by context.Context.CodecOptions() before passing it off to the chosen codec's Marshal method.

type GowebAPIResponder

type GowebAPIResponder struct {

	// StandardFieldDataKey is the response object field name for the data.
	StandardFieldDataKey string

	// StandardFieldStatusKey is the response object field name for the status.
	StandardFieldStatusKey string

	// StandardFieldErrorsKey is the response object field name for the errors.
	StandardFieldErrorsKey string

	// AlwaysEnvelopeResponse tells Goweb whether to envelope the response or not
	AlwaysEnvelopResponse bool
	// contains filtered or unexported fields
}

func NewGowebAPIResponder

func NewGowebAPIResponder(codecService codecsservices.CodecService, httpResponder HTTPResponder) *GowebAPIResponder

func (*GowebAPIResponder) GetCodecService

func (a *GowebAPIResponder) GetCodecService() codecsservices.CodecService

GetCodecService gets the codec service that will be used by this object.

func (*GowebAPIResponder) Respond

func (a *GowebAPIResponder) Respond(ctx context.Context, status int, data interface{}, errors []string) error

Responds to the Context with the specified status, data and errors.

func (*GowebAPIResponder) RespondWithData

func (a *GowebAPIResponder) RespondWithData(ctx context.Context, data interface{}) error

RespondWithData responds with the specified data, no errors and a 200 StatusOK response.

func (*GowebAPIResponder) RespondWithError

func (a *GowebAPIResponder) RespondWithError(ctx context.Context, status int, err string) error

RespondWithError responds with the specified error and status code.

func (*GowebAPIResponder) SetCodecService

func (a *GowebAPIResponder) SetCodecService(service codecsservices.CodecService)

SetCodecService sets the codec service to use.

func (*GowebAPIResponder) SetStandardResponseObjectTransformer

func (a *GowebAPIResponder) SetStandardResponseObjectTransformer(transformer func(ctx context.Context, object interface{}) (interface{}, error))

SetStandardResponseObjectTransformer sets the function to use to transform the standard response object beore it is written to the response.

func (*GowebAPIResponder) TransformStandardResponseObject

func (a *GowebAPIResponder) TransformStandardResponseObject(ctx context.Context, object interface{}) (interface{}, error)

TransformStandardResponseObject transforms the standard response object before it is written to the response if a transformer func has been set via SetStandardResponseObjectTransformer.

func (*GowebAPIResponder) WriteResponseObject

func (a *GowebAPIResponder) WriteResponseObject(ctx context.Context, status int, responseObject interface{}) error

WriteResponseObject writes the status code and response object to the HttpResponseWriter in the specified context, in the format best suited based on the request.

Goweb uses the WebCodecService to decide which codec to use when responding see http://godoc.org/github.com/stretchr/codecs/services#WebCodecService for more information.

This method should be used when the Goweb Standard Response Object does not satisfy the needs of the API, but other Respond* methods are recommended.

type GowebHTTPResponder

type GowebHTTPResponder struct {
}

GowebHTTPResponder is the default HTTPResponder used to make responses.

func (*GowebHTTPResponder) With

func (r *GowebHTTPResponder) With(ctx context.Context, httpStatus int, body []byte) error

With writes a response to the request in the specified context.

func (*GowebHTTPResponder) WithOK

func (r *GowebHTTPResponder) WithOK(ctx context.Context) error

WithOK responds with a 200 OK status code, and no body.

func (*GowebHTTPResponder) WithPermanentRedirect

func (r *GowebHTTPResponder) WithPermanentRedirect(ctx context.Context, pathOrURLSegments ...interface{}) error

WithPermanentRedirect responds with a redirection to the specific path or URL with the http.StatusMovedPermanently status.

func (*GowebHTTPResponder) WithRedirect

func (r *GowebHTTPResponder) WithRedirect(ctx context.Context, pathOrURLSegments ...interface{}) error

WithRedirect responds with a Found redirection to the specific path or URL.

func (*GowebHTTPResponder) WithStatus

func (r *GowebHTTPResponder) WithStatus(ctx context.Context, httpStatus int) error

WithStatus writes the specified HTTP Status Code to the Context's ResponseWriter.

If the Always200ParamName parameter is present, it will ignore the httpStatus argument, and always write net/http.StatusOK (200).

func (*GowebHTTPResponder) WithStatusText

func (r *GowebHTTPResponder) WithStatusText(ctx context.Context, httpStatus int) error

WithStatusText writes the specified HTTP Status Code to the Context's ResponseWriter and includes a body with the default status text.

func (*GowebHTTPResponder) WithTemporaryRedirect

func (r *GowebHTTPResponder) WithTemporaryRedirect(ctx context.Context, pathOrURLSegments ...interface{}) error

WithTemporaryRedirect responds with a TemporaryRedirect redirection to the specific path or URL.

type HTTPResponder

type HTTPResponder interface {

	// WithStatus writes the specified HTTP Status Code to the Context's ResponseWriter.
	WithStatus(ctx context.Context, httpStatus int) error

	// WithStatusText writes the specified HTTP Status Code to the Context's ResponseWriter and
	// includes a body with the default status text.
	WithStatusText(ctx context.Context, httpStatus int) error

	// WithOK responds with a 200 OK status code, and no body.
	WithOK(ctx context.Context) error

	// With writes a response to the request in the specified context.
	With(ctx context.Context, httpStatus int, body []byte) error

	// WithRedirect responds with a redirection to the specific path or URL with the
	// http.StatusTemporaryRedirect status.
	WithRedirect(ctx context.Context, pathOrURLSegments ...interface{}) error

	// WithPermanentRedirect responds with a redirection to the specific path or URL with the
	// http.StatusMovedPermanently status.
	WithPermanentRedirect(ctx context.Context, pathOrURLSegments ...interface{}) error
}

Jump to

Keyboard shortcuts

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