httpencoder

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2023 License: Apache-2.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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.

Types

type BoolResultResponse

type BoolResultResponse struct {
	Result bool `json:"result"`
}

BoolResultResponse struct

func BoolResult

func BoolResult(result bool) BoolResultResponse

BoolResult response helper

type ErrorResponse

type ErrorResponse struct {
	Code      int         `json:"code"`
	Err       string      `json:"error"`
	Message   string      `json:"message,omitempty"`
	Details   interface{} `json:"details,omitempty"`
	RequestID string      `json:"request_id,omitempty"`
}

Error represents an error response

func (ErrorResponse) Error

func (e ErrorResponse) Error() string

Error implements the error interface

type ListMeta

type ListMeta struct {
	TotalItems   int64 `json:"total_items,omitempty"`
	ItemsPerPage int32 `json:"items_per_page,omitempty"`
	Page         int32 `json:"page,omitempty"`
	Limit        int32 `json:"limit,omitempty"`
	Offset       int32 `json:"offset,omitempty"`
}

type ListResponse

type ListResponse struct {
	Data interface{} `json:"data,omitempty"`
	Meta *ListMeta   `json:"meta,omitempty"`
}

ListResponse struct

type Response

type Response struct {
	Data interface{}            `json:"data,omitempty"`
	Meta map[string]interface{} `json:"meta,omitempty"`
}

Response struct

Jump to

Keyboard shortcuts

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