response

package
v0.0.0-...-3215105 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2018 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DefaultBody

type DefaultBody struct {
	Content map[string]interface{} `json:"content,omitempty"`
	Errors  []*resterrors.Error    `json:"errors,omitempty"`
}

DefaultBody is a structure that defines default response body It implements Responser interface

func (*DefaultBody) AddContent

func (d *DefaultBody) AddContent(content ...interface{})

AddContent adds the given 'content' to the default body content The 'content' is saved with key - lowercased model struct name pluralized if slice provided. I.e. Providing model type Foo struct{} - would be saved as 'foo'

But slice []Foo or []*Foo would result in 'foos'

With this method DefaultBody implements ContentAdder interface

func (*DefaultBody) AddErrors

func (d *DefaultBody) AddErrors(errors ...*resterrors.Error)

AddErrors adds the given 'errors' to the default body content. With this method DefaultBody implements ErrorAdder interface

func (*DefaultBody) New

func (d *DefaultBody) New() Responser

New creates and returns new *DefaultBody of given type Implements Responser New() method

func (*DefaultBody) NewErrored

func (d *DefaultBody) NewErrored() Responser

NewErrored implements Responser NewErrored() method In this implementation there is no difference between New and NewErrored method.

func (*DefaultBody) WithContent

func (d *DefaultBody) WithContent(content ...interface{}) Responser

WithContent adds the given 'content' to the DefaultBody Content field The content is added with the same rules as with AddContent() method The difference is that this method may be used as callback - after processing it returns itself.

func (*DefaultBody) WithErrors

func (d *DefaultBody) WithErrors(errors ...*resterrors.Error) Responser

WithErrors adds the given 'errors' to the default body content. This method acts like AddErrors() method, but in addition it may be used as a callback function - returning *DefaultBody itself

type DetailedBody

type DetailedBody struct {
	// Status is an easy to check variable with only two possible values:
	// - 'ok'
	// - 'error'
	// If the status is 'ok' the response recipient can proceed to result variable
	Status Status `json:"status"`

	// HttpCode is a http status code applicable to this problem
	// While using multiple ResponseErrors with http status
	// it is a good practice to set this value as a leading Http Status
	// i.e.:
	//		- 200 - correct status
	//		- 400 - multiple client error - 4xx
	//		- 500 - multiple API server error - 5xx
	HttpStatus int `json:"httpCode,omitempty"`

	// Errors - list of errors that occurred
	// The server MAY choose to stop processing as soon as a problem is encountered, or it
	// MAY continue processing and encounter multiple problems.
	Errors []*resterrors.Error `json:"errors,omitempty"`

	// Content contains all response results
	// Composed as a map[string]interface{}
	// Every result should have it's own tag
	// i.e. "user" : User{1} - user object
	// 		"users" : []User{1,2} - list (plural)
	Content map[string]interface{} `json:"result,omitempty"`
}

DetailedBody - basic REST API response structure Created on purpose of easily managable and It implements StatusResponser interface

func (*DetailedBody) AddContent

func (d *DetailedBody) AddContent(content ...interface{})

AddContent adds a content to the Detailed body Content The key for the Content is set as provided 'content' struct Name - lowercased I.e. type Model struct would use key 'model' But Slice of models []Model or []*Model would use pluralized name - 'models' For basic types like 'int' or 'string' use wrapper struct so that the name would be as proided i.e.: having some Limit variable of type int, by wrapping it as type Limit int and insert content as Limit(limitValue) would result storing the Limit content with key 'limit' Implements ContentAdder interface

func (*DetailedBody) AddErrors

func (d *DetailedBody) AddErrors(errors ...*resterrors.Error)

AddErrors adds errors to the Errors field within the *DetailedBody

func (*DetailedBody) New

func (d *DetailedBody) New() Responser

New creates new response DetailedBody with positive status. The function initialize the DetailedBody with:

  • Status: StatusOK
  • HttpStatus: 200
  • empty inited 'Content'

func (*DetailedBody) NewErrored

func (d *DetailedBody) NewErrored() Responser

NewErrored prepares DetailedBody with Status: 'StatusError' By default HttpStatus is set to 500

func (*DetailedBody) WithContent

func (d *DetailedBody) WithContent(content ...interface{}) Responser

WithContent adds provided content to the DetailedBody.Content field The rules are the same as with AddContent() method. In addition the method may be used as callback function returning itself after processing

func (*DetailedBody) WithErrors

func (d *DetailedBody) WithErrors(errors ...*resterrors.Error) Responser

WithErrors adds errors to the *DetailedBody. Acts like AddErrors() but in addition the method may be used as a callback that returns itself after processing

func (*DetailedBody) WithStatus

func (d *DetailedBody) WithStatus(status interface{}) StatusResponser

WithStatus if the provided status is of type int, the method sets the httpStatus with the provided in the argument and returns itself as a callback function

type Responser

type Responser interface {
	// AddContent and WithContent adds the content to the Responser implementation
	// Both methods should do it with the same rules, but WithContent() acts like
	// callback function that after processing returns itself
	AddContent(content ...interface{})
	WithContent(content ...interface{}) Responser

	// AddErrors and WithErrors adds the errors to the given Responser
	// Both method should do it with the same rules, but WithErrors() should act like a
	//callback function that returns itself after processing
	AddErrors(errors ...*resterrors.Error)
	WithErrors(errors ...*resterrors.Error) Responser

	// New() creates a new Responser entity
	// the status argument may be not used in implementations
	New() Responser

	// NewErrored() creates new Resposner that is defined for errored response.
	// This enables some implementations to differ when some error occured
	NewErrored() Responser
}

Responser is an interface that is used for response bodies. It implements both ContentAdder and ErrorAdder as well as defines two additional methods:

  • New() that creates new Responser
  • NewErrored(status interface{}) - creates new Responser that is specified for errored response. I.e. implementation differs when an error occured In addition status is provided as an argument, where some implementations might used it.

type Status

type Status int

Status is a basic status for API Response A developer can easily manage the response just by knowing the short status value. The status is a binary value - either the request where operated correctly or there exists error

const (
	Unknown Status = iota
	StatusOk
	StatusError
)

func (*Status) MarshalJSON

func (s *Status) MarshalJSON() ([]byte, error)

MarshalJSON - implements json marshaller interface

func (Status) String

func (s Status) String() string

String - implements the Stringer interface

func (*Status) UnmarshalJSON

func (s *Status) UnmarshalJSON(b []byte) error

UnmarshalJSON - implements json Unmarshaler interface

type StatusResponser

type StatusResponser interface {
	Responser
	//WithStatus is a callback function that sets the status for given Responser
	WithStatus(status interface{}) StatusResponser
}

StatusResponser is an interface that inherit Responser interface In addition it contains WithStatus method that sets the status for given StatusResponser

Jump to

Keyboard shortcuts

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