commonhttp

package
v1.0.0-beta.183 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultPageSize is the default page size for pagination.
	DefaultPageSize = 100
	// MaxPageSize is the maximum page size for pagination.
	MaxPageSize = 1000
	// DefaultPage is the default page number for pagination.
	DefaultPage = 1
)

Variables

This section is empty.

Functions

func EmptyResponseEncoder

func EmptyResponseEncoder[Response any](statusCode int) httptransport.ResponseEncoder[Response]

func ErrorEncoder

func ErrorEncoder(ctx context.Context, _ error, w http.ResponseWriter) bool

ErrorEncoder encodes an error as HTTP 500 Internal Server Error.

func GetSortOrder

func GetSortOrder[TInput comparable](asc TInput, inp *TInput) sortx.Order

func HandleErrorIfTypeMatches

func HandleErrorIfTypeMatches[T error](ctx context.Context, statusCode int, err error, w http.ResponseWriter, extendedProblemFunc ...func(T) (string, string)) bool

HandleErrorIfTypeMatches checks if the error is of the given type and encodes it as an HTTP error. Using the generic feature we can mandate that the error implements the error interface. This is a must, as the errors.As would panic if the error does not implement the error interface.

func JSONRequestBodyDecoder

func JSONRequestBodyDecoder(r *http.Request, out any) error

func JSONResponseEncoder

func JSONResponseEncoder[Response any](_ context.Context, w http.ResponseWriter, response Response) error

JSONResponseEncoder encodes a response as JSON.

func JSONResponseEncoderWithStatus

func JSONResponseEncoderWithStatus[Response any](statusCode int) httptransport.ResponseEncoder[Response]

func PlainTextResponseEncoder

func PlainTextResponseEncoder[Response string](_ context.Context, w http.ResponseWriter, response Response) error

PlainTextResponseEncoder encodes a response as PlainText.

Types

type ErrorWithHTTPStatusCode

type ErrorWithHTTPStatusCode struct {
	StatusCode int
	Extensions []ExtendProblemFunc
	// contains filtered or unexported fields
}

func NewHTTPError

func NewHTTPError(statusCode int, err error, extensions ...ExtendProblemFunc) ErrorWithHTTPStatusCode

func (ErrorWithHTTPStatusCode) EncodeError

type ExtendProblemFunc

type ExtendProblemFunc func() (name string, details string)

func ExtendProblem

func ExtendProblem(name, details string) ExtendProblemFunc

type Union

type Union[Primary any, Secondary any] struct {
	Option1 *Primary
	Option2 *Secondary
}

func (Union[Primary, Secondary]) MarshalJSON

func (u Union[Primary, Secondary]) MarshalJSON() ([]byte, error)

Implements json.Marshaler with primary having precedence.

Jump to

Keyboard shortcuts

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