endpoints

package
v0.0.0-...-d28c369 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2022 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidBody   = errors.New("invalid body")
	ErrInvalidJSON   = errors.New("body contains invalid JSON")
	ErrInternal      = errors.New("internal error")
	ErrResponseWrite = errors.New("failed to write response")
)

Functions

func NewAPI

func NewAPI(s service.Service, udps udp.Server) httpapi.HTTPAPI

Types

type ContextResponseEncoder

type ContextResponseEncoder string

ContextResponseEncoder is a type used to identify encoders in Contexts

const ResponseEncoderKey ContextResponseEncoder = "encoder"

ResponseEncoderKey is the common key used by this package to store an EncodeDecoder in a Context

type HTTPWritter

type HTTPWritter interface {
	// WriteHTTP writes the contents of the object to the http.ResponseWriter `w`
	WriteHTTP(ctx context.Context, w http.ResponseWriter)
}

HTTPWriter writes an object to a http.ResponseWriter as a HTTP response

type HttpResponse

type HttpResponse[T any] struct {
	Success bool   `json:"success,omitempty"`
	Message string `json:"message,omitempty"`
	Error   string `json:"error,omitempty"`
	Data    *T     `json:"data,omitempty"`
	Status  int    `json:"-"`
}

httpResponse is a generic type for HTTP responses, both OK and not-OK

func NewResponse

func NewResponse[T any](status int, message string, err error, data *T) HttpResponse[T]

newResponse builds a httpResponse object for type T derived from the input data (*T)

It takes in an int `status`, a string `message` and either an error or data.

  • if the error is nil, it is assumed that it is an OK response and the provided *T `data` is stored in the response, and the error ignored
  • if the error is not nil, it is assumed that the response is not-OK, so the *T `data` is ignored, and the error stored in the response

func (HttpResponse[T]) WriteHTTP

func (r HttpResponse[T]) WriteHTTP(ctx context.Context, w http.ResponseWriter)

WriteHTTP writes the contents of the object to the http.ResponseWriter `w`

Jump to

Keyboard shortcuts

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