api

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package api provides standard API request/response handling.

Index

Constants

View Source
const HeaderAPIVersion = `X-Api-Version`

HeaderAPIVersion is the canonical name of the HTTP header key that reports the API version.

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.

func Deprecated

func Deprecated(next http.Handler) http.Handler

Deprecated adds a header to indicate the particular endpoint is deprecated.

Types

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 ErrorResponse

type ErrorResponse struct {
	Error struct {
		Message   string `json:"message"`
		RequestID string `json:"request_id,omitempty"`
	} `json:"error"`
}

ErrorResponse is the body of an Error served up by Err

type OK

type OK interface {
	OK() error
}

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

type Option

type Option func(w http.ResponseWriter)

Option functions provide a means for manipulating things like adding additional headers to responses.

func Header(key, value string) Option

Header adds a Header to the response.

type Responder

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

Responder writes API responses. nil Responder is safe to use.

func NewResponder

func NewResponder(logger *logger.L, version string) *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) OK

func (r *Responder) OK(w http.ResponseWriter, req *http.Request, opts ...Option)

OK responds with status code OK and JSON response indicating the operation succeeded

func (*Responder) With

func (r *Responder) With(w http.ResponseWriter, req *http.Request, status int, data interface{}, opts ...Option)

With responds with the specified data.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳