response

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContentTypeHeader = "Content-Type"
	ContentType       = "application/json; charset=utf-8"
)

Predefined http encoder content type

Variables

This section is empty.

Functions

func CodeAndMessageFrom

func CodeAndMessageFrom(err error) (int, interface{})

CodeAndMessageFrom helper

func EncodeError

func EncodeError(l logger, codeAndMessageFrom func(err error) (int, interface{})) httptransport.ErrorEncoder

EncodeError ...

func EncodeResponse

func EncodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error

EncodeResponse is the common method to encode all response types to the client. I chose to do it this way because, since we're using JSON, there's no reason to provide anything more specific. It's certainly possible to specialize on a per-response (per-method) basis.

func EncodeResponseAsIs

func EncodeResponseAsIs(_ context.Context, w http.ResponseWriter, response interface{}) error

EncodeResponseAsIs is almost the same as EncodeResponse, but it doesn't wrap the response in a Response struct. This is useful for endpoints that return a single value, like a string or a number.

func JSON

func JSON(w http.ResponseWriter, response Responser) error

JSON HTTP response

func SetDefaultResponder

func SetDefaultResponder(responder Responder)

SetDefaultResponder sets default responder.

Types

type Error

type Error struct {
	// Code is a code of error
	Code int `json:"code" example:"400"`
	// Error is a string error representation
	Error string `json:"error" example:"Bad Request"`
	// Message is a message of error
	Message string `json:"message" example:"Validation error"`
	// Validation is a validation of error
	Validation map[string][]string `json:"validation,omitempty" example:"{'email': ['Email is required']}"`
	// Meta is a meta of response
	Meta *Meta `json:"meta,omitempty"`
}

Error is a struct for error response

func NewError

func NewError(code int, err error, message string, validation map[string][]string) *Error

NewError creates new error response

func (*Error) GetCode

func (e *Error) GetCode() int

GetCode returns HTTP code of response

func (*Error) GetPayload

func (e *Error) GetPayload() interface{}

GetPayload returns payload of response

type List

type List struct {
	// Items is a list of items
	Items interface{} `json:"items" example:"[]"`
	// Total is a total count of items
	Total int `json:"total" example:"0"`
	// Pagination is a pagination of list
	Pagination *Pagination `json:"pagination,omitempty"`
}

List represents a list of response data

func NewList

func NewList(items interface{}, total int, pagination *Pagination) *List

NewList creates new list response

type Meta

type Meta struct {
	// Title is a title of response
	Title string `json:"title" example:"Title"`
	// Description is a description of response
	Description string `json:"description" example:"Description"`
	// Version is a version of response
	Version string `json:"version" example:"1.0.0"`
	// RequestID is a request ID of response
	RequestID string `json:"request_id"`
}

Meta represents a meta of response

func NewMeta

func NewMeta(title, description, version string) *Meta

NewMeta creates new meta

type Pagination

type Pagination struct {
	// Limit is a limit of items per page
	Limit int `json:"limit"`
	// Offset is a offset of items per page
	Offset int `json:"offset"`
	// Page is a current page
	Page int `json:"page"`
	// Pages is a total count of pages
	Pages int `json:"pages"`
}

Pagination represents a pagination of list

func NewPagination

func NewPagination(limit, offset, page, pages int) *Pagination

NewPagination creates new pagination

func NewPaginationFromInterface

func NewPaginationFromInterface(source paginator) *Pagination

NewPaginationFromInterface creates new pagination from interface

type Responder

type Responder interface {
	// JSON writes JSON response to http.ResponseWriter.
	// headersKV is a list of headers key-value pairs.
	// E.g. "Content-Type", "application/json", "X-Request-ID", "123"
	JSON(w http.ResponseWriter, response Responser, headersKV ...string) error
}

Responder is an interface for response. Respond writes response to http.ResponseWriter.

func NewDefaultResponder

func NewDefaultResponder() Responder

NewDefaultResponder creates new default responder.

func NewResponder

func NewResponder() Responder

NewResponder creates new Responder.

type Response

type Response struct {
	// Code is a code of response
	Code int `json:"code" example:"200"`
	// Message is a message of response
	Message string `json:"message" example:"OK"`
	// Data is a data of response
	Data interface{} `json:"data" example:"{}"`
	// Meta is a meta of response
	Meta *Meta `json:"meta,omitempty"`
}

Response is a struct for response

func New

func New(code int, message string, data interface{}, meta *Meta) *Response

New creates new response

func NewCreated

func NewCreated(message string, data interface{}) *Response

NewCreated creates new created response

func NewNoContent

func NewNoContent() *Response

NewNoContent creates new no content response

func NewOk

func NewOk(message string, data interface{}) *Response

NewOk creates new ok response

func (*Response) GetCode

func (r *Response) GetCode() int

GetCode returns HTTP code of response

func (*Response) GetPayload

func (r *Response) GetPayload() interface{}

GetPayload returns payload of response

type Responser

type Responser interface {
	// GetCode returns HTTP code of response
	GetCode() int
	// GetPayload returns payload of response
	GetPayload() interface{}
}

Response interface

Jump to

Keyboard shortcuts

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