helpers

package
v0.0.0-...-fb9541f Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2020 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// InternalServerError is an error response
	InternalServerError = &ErrorResponse{Code: "internal_server_error", Message: "Internal Server Error, please try again.", Status: http.StatusInternalServerError}
	// InvalidParameterError is an error response
	InvalidParameterError = &ErrorResponse{Code: "invalid_parameter", Message: "Invalid parameter.", Status: http.StatusBadRequest}
)

Functions

func FromRequest

func FromRequest(r *http.Request, destination interface{}) error

FromRequest will populate destination fields using http.Request post values.

func ToValues

func ToValues(source interface{}) url.Values

ToValues will create url.Values from source.

func Validate

func Validate(request Request, params ...interface{}) error

func Write

func Write(w http.ResponseWriter, response Response) error

Write writes a response to the given http.ResponseWriter

Types

type ErrorResponse

type ErrorResponse struct {
	// HTTP status code
	Status int `json:"-"`
	// Error status code
	Code string `json:"code"`
	// Error message that will be returned to API consumer
	Message string `json:"message"`
	// Additional information returned to API consumer
	MoreInfo string `json:"more_info,omitempty"`
	// Error data that will be returned to API consumer
	Data map[string]interface{} `json:"data,omitempty"`
}

ErrorResponse represents error response and implements server.Response and error interfaces

func NewInvalidParameterError

func NewInvalidParameterError(name, moreInfo string) *ErrorResponse

NewInvalidParameterError creates and returns a new InvalidParameterError

func NewMissingParameter

func NewMissingParameter(name string) *ErrorResponse

NewMissingParameter creates and returns a new missingParameterError

func (*ErrorResponse) Error

func (error *ErrorResponse) Error() string

Error returns Message

func (*ErrorResponse) HTTPStatus

func (error *ErrorResponse) HTTPStatus() int

HTTPStatus returns ErrorResponse.Status

func (*ErrorResponse) Marshal

func (error *ErrorResponse) Marshal() ([]byte, error)

Marshal marshals ErrorResponse

type Request

type Request interface {
	Validate(params ...interface{}) error
}

Response represents request. Params are additional parameters required to validate the request.

type Response

type Response interface {
	HTTPStatus() int
	Marshal() ([]byte, error)
}

Response represents response that can be returned by a server

type SpecialValuesConvertable

type SpecialValuesConvertable interface {
	FromRequestSpecial(r *http.Request, destination interface{}) error
	ToValuesSpecial(values url.Values)
}

SpecialValuesConvertable allows converting special values (not easily convertable): * from struct to url.Values * from http.Request to struct

type SuccessResponse

type SuccessResponse struct{}

SuccessResponse can be embedded in success responses

func (*SuccessResponse) HTTPStatus

func (r *SuccessResponse) HTTPStatus() int

Jump to

Keyboard shortcuts

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