Documentation ¶
Index ¶
- Constants
- func CodeAndMessageFrom(err error) (int, interface{})
- func EncodeError(l logger, codeAndMessageFrom func(err error) (int, interface{})) httptransport.ErrorEncoder
- func EncodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error
- func EncodeResponseAsIs(_ context.Context, w http.ResponseWriter, response interface{}) error
- func JSON(w http.ResponseWriter, response Responser) error
- func SetDefaultResponder(responder Responder)
- type Error
- type List
- type Meta
- type Pagination
- type Responder
- type Response
- type Responser
Constants ¶
const ( ContentTypeHeader = "Content-Type" ContentType = "application/json; charset=utf-8" )
Predefined http encoder content type
Variables ¶
This section is empty.
Functions ¶
func CodeAndMessageFrom ¶
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 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 (*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
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.
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 NewCreated ¶
NewCreated creates new created response
func (*Response) GetPayload ¶
func (r *Response) GetPayload() interface{}
GetPayload returns payload of response