api

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

README

go-api

Go Reference

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(r *http.Request, v interface{}) error

Decode decodes the body of a request into the specified object. If the object implements the OK interface, that method is called to validate the object.

Types

type ClientIPFunc

type ClientIPFunc func(context.Context) string

ClientIPFunc is a function that can be used to retrieve the client IP from a context.

type Conflicter

type Conflicter interface {
	Conflict() bool
}

Conflicter determines if an error exhibits the behavior of a conflict err. This corresponds to times when you receive unexpected errors or an error of high severity.

type Counter added in v0.0.5

type Counter interface {
	Inc()
}

Counter is a metric for counting the number of occurances of an event.

type ErrJSON

type ErrJSON struct {
	// contains filtered or unexported fields
}

ErrJSON error is returned by Decode when the incoming JSON unmarshaling fails.

func (*ErrJSON) Error

func (e *ErrJSON) Error() string

Error returns the error that unmarshal failed.

func (*ErrJSON) StatusCode

func (e *ErrJSON) StatusCode() int

StatusCode returns the http status code that would refer to json payload that fails to decode error.

type ErrValidationFailed

type ErrValidationFailed struct {
	// contains filtered or unexported fields
}

ErrValidationFailed is returned by Decode if the object is not valid.

func (*ErrValidationFailed) Error

func (e *ErrValidationFailed) Error() string

Error reports the error string.

func (*ErrValidationFailed) StatusCode

func (e *ErrValidationFailed) StatusCode() int

StatusCode returns the http status code that would refer to a validation error.

type ErrorData

type ErrorData struct {
	Message string `json:"message"`
}

type ErrorResponse

type ErrorResponse struct {
	RequestID string    `json:"request_id"`
	Error     ErrorData `json:"error"`
}

ErrorResponse is the body of an Error served up by Err

type Exister

type Exister interface {
	Exists() bool
}

Exister determines if an error exhibits the behavior of a resource already exists err.

type HTTP

type HTTP interface {
	StatusCode() int
}

HTTP determines if an error exhibits the behavior of an error that provides an HTTP status code and the error message itself is safe for returning to a client.

type HTTPErr

type HTTPErr struct {
	Code int
	// contains filtered or unexported fields
}

HTTPErr is a error that provides a status code.

func NewHTTPErr

func NewHTTPErr(err error, code int) *HTTPErr

NewHTTPErr generates a new HTTPErr.

func (*HTTPErr) StatusCode

func (h *HTTPErr) StatusCode() int

StatusCode provides the status code associated with the error message.

func (*HTTPErr) Unwrap

func (h *HTTPErr) Unwrap() error

Unwrap supports the Go 1.13 error semantics.

type NotFounder

type NotFounder interface {
	NotFound() bool
}

NotFounder determines if an error exhibits the behavior of a resource not found err.

type OK

type OK interface {
	OK() error
}

OK checks to see if an object is valid or not.

type RequestIDFunc

type RequestIDFunc func(context.Context) string

RequestIDFunc is a function that can be used to retrieve the request ID from a context.

type Responder

type Responder struct {
	// contains filtered or unexported fields
}

Responder writes API responses.

func NewResponder

func NewResponder(logger *logger.L, opts ...ResponderOption) *Responder

NewResponder makes a new Responder object.

func (*Responder) Err

func (r *Responder) Err(w http.ResponseWriter, req *http.Request, err error)

Err responds with an error that corresponds to the behavior the type is illustrating.

func (*Responder) Recoverer added in v0.0.4

func (r *Responder) Recoverer(next http.Handler) http.Handler

func (*Responder) With

func (r *Responder) With(w http.ResponseWriter, req *http.Request, status int, data any)

With responds with the specified data.

type ResponderOption

type ResponderOption func(*options)

ResponderOption is used to customize the API responder.

func WithClientIPFunc

func WithClientIPFunc(fn ClientIPFunc) ResponderOption

WithClientIPFunc sets the function to retrieve the client IP.

func WithCounterPanics added in v0.0.5

func WithCounterPanics(c Counter) ResponderOption

WithCounterPanics will use the Counter to track any recovered panics.

func WithLogResponseBodies

func WithLogResponseBodies(enabled bool) ResponderOption

WithLogResponseBodies enables the logging of response bodies.

func WithRequestIDFunc

func WithRequestIDFunc(fn RequestIDFunc) ResponderOption

WithRequestIDFunc sets the function to retrieve the request ID.

type UserMessager added in v0.0.3

type UserMessager interface {
	UserMessage() string
}

UserMessager can be used to present a specific message to the user.

Jump to

Keyboard shortcuts

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