response

package
v3.5.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2018 License: Apache-2.0 Imports: 6 Imported by: 2

Documentation

Overview

Package response defines the how the default microservice response must look and behave like.

Index

Examples

Constants

View Source
const (
	StatusOk   = "ok"
	StatusFail = "fail"
)

Standard response statuses.

Variables

This section is empty.

Functions

This section is empty.

Types

type Data

type Data struct {
	Type    string
	Content interface{}
}

Data represents the collection data the the response will return to the consumer. Type ends up being the name of the key containing the collection of Content

func (*Data) Map

func (d *Data) Map() map[string]interface{}

Map returns a version of the data as a map

func (*Data) MarshalJSON

func (d *Data) MarshalJSON() ([]byte, error)

MarshalJSON implements the Marshaler interface and is there to ensure the output is correct when we return data to the consumer

func (*Data) UnmarshalJSON

func (d *Data) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the Unmarshaler interface this implementation will fill the type in the case we're been provided a valid single collection and set the content to the contents of said collection. for every other options, it behaves like normal. Despite the fact that we are not supposed to marshal without a type set, this is purposefully left open to unmarshal without a collection name set, in case you may want to set it later, and for interop with other systems which may not send the collection properly.

func (*Data) Valid

func (d *Data) Valid() bool

Valid ensures the Data passed to the response is correct (it must contain a Type along with the data).

type PaginatedResponse

type PaginatedResponse struct {
	Status     string               `json:"status"`         // Can be 'ok' or 'fail'
	Code       int                  `json:"code"`           // Any valid HTTP response code
	Message    string               `json:"message"`        // Any relevant message (optional)
	Data       *Data                `json:"data,omitempty"` // Data to pass along to the response (optional)
	Pagination *pagination.Response `json:"pagination"`     // Pagination data
}

PaginatedResponse - A paginated response format for a microservice.

func NewPaginated

func NewPaginated(paginator *pagination.Paginator, code int, message string, data *Data) *PaginatedResponse

NewPaginated returns a new PaginatedResponse for a microservice endpoint

func (*PaginatedResponse) ExtractData

func (p *PaginatedResponse) ExtractData(srcKey string, dst interface{}) error

ExtractData returns a particular item of data from the response.

func (*PaginatedResponse) GetCode

func (p *PaginatedResponse) GetCode() int

GetCode returns the response code.

func (*PaginatedResponse) WriteTo

WriteTo - pick a response writer to write the default json response to.

type Responder

type Responder interface {
	// ExtractData returns a particular item of data from the response.
	ExtractData(srcKey string, dst interface{}) error

	// GetCode returns the response code.
	GetCode() int

	// WriteTo writes back to the network connection.
	WriteTo(w http.ResponseWriter) error
}

Responder - Responder for microservice responses.

type Response

type Response struct {
	Status  string `json:"status"`         // Can be 'ok' or 'fail'
	Code    int    `json:"code"`           // Any valid HTTP response code
	Message string `json:"message"`        // Any relevant message (optional)
	Data    *Data  `json:"data,omitempty"` // Data to pass along to the response (optional)
}

Response - A standardised response format for a microservice.

func ConflictErr

func ConflictErr(msg string) *Response

ConflictErr returns a prepared 409 Conflict response, including the message passed by the user in the message field of the response object.

func DBError

func DBError(err error) *Response

DBError returns a prepared 500 Internal Server Error response.

func DBErrorf

func DBErrorf(format string, err error) *Response

DBErrorf returns a prepared 500 Internal Server Error response, using the user provided formatted message.

func InternalError

func InternalError(err error) *Response

InternalError returns a prepared 500 Internal Server Error, including the error message in the message field of the response object

func JSONError

func JSONError(err error) *Response

JSONError returns a prepared 422 Unprocessable Entity response if the JSON is found to contain syntax errors, or invalid values for types.

func New

func New(code int, message string, data *Data) *Response

New returns a new Response for a microservice endpoint This ensures that all API endpoints return data in a standardised format:

{
   "status": "ok or fail",
   "code": any HTTP response code,
   "message": "any relevant message (optional)",
   "data": {[
      ...
   ]}
}
Example

ExampleNew - Example usage for the New function.

data := &Data{
	Type: "things",
	Content: map[string]interface{}{
		"thing_one": "a thing",
		"thing_two": "another thing",
	},
}

resp := New(http.StatusOK, "test message", data)
fmt.Printf("%+v", resp)
Output:

func NotFoundErr

func NotFoundErr(msg string) *Response

NotFoundErr returns a prepared 404 Not Found response, including the message passed by the user in the message field of the response object.

func ParamError

func ParamError(name string) *Response

ParamError returns a prepared 422 Unprocessable Entity response, including the name of the failing parameter in the message field of the response object.

func SQLError deprecated

func SQLError(err error) *Response

SQLError - currently only wraps DBError

Deprecated: This function has been made redundant by the more generic DBError

func SQLErrorf deprecated

func SQLErrorf(format string, err error) *Response

SQLErrorf - currently only wraps DBErrorf

Deprecated: This function has been made redundant by the more generic DBErrorf

func ValidationError

func ValidationError(err error, name string) *Response

ValidationError returns a prepared 422 Unprocessable Entity response, including the name of the failing validation/validator in the message field of the response object.

func (*Response) ExtractData

func (r *Response) ExtractData(srcKey string, dst interface{}) error

ExtractData returns a particular item of data from the response.

func (*Response) GetCode

func (r *Response) GetCode() int

GetCode returns the response code.

func (*Response) WriteTo

func (r *Response) WriteTo(w http.ResponseWriter) error

WriteTo - pick a response writer to write the default json response to.

Jump to

Keyboard shortcuts

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