apicore

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2020 License: MIT Imports: 21 Imported by: 0

README

apicore

Package with set of helper functions to create a rest api.

Documentation

Index

Constants

View Source
const (
	APIKEY          string = "api-key"
	REQUEST_ID_KEY  string = "x-request-id"
	REMOTE_ADDR_KEY string = "remote-address"
	SESSIONID       string = "session"
)

Variables

View Source
var (
	// ErrBadPayload the error to be returned when a request is malformed
	ErrBadPayload = errors.New("Malformed json request payload")
)

Functions

func DecodeBody

func DecodeBody(ctx context.Context, data io.ReadCloser, model interface{}) (err error)

DecodeBody decodes a json request body into a given struct

func DecodeParams

func DecodeParams(ctx context.Context, dst interface{}, src map[string][]string) (errors schema.MultiError)

func Respond

func Respond(ctx context.Context,
	w http.ResponseWriter, data interface{}, httpStatusCode int, err error)

Respond sends standard json response

func RespondError

func RespondError(
	ctx context.Context, w http.ResponseWriter, httpStatusCode int, err error)

RespondError sends a json error response

func RespondSuccess

func RespondSuccess(
	ctx context.Context, w http.ResponseWriter, data interface{})

RespondSuccess sends a json success response

func ValidationRules

func ValidationRules() *validator.Validate

Types

type API

type API struct {
	Router *mux.Router
	// contains filtered or unexported fields
}

API holds all

func New

func New(
	ctx context.Context,
	conf *apisettings.Service,
	handlers []Handler) *API

New constructs and returns a new API struct

type BaseHandler

type BaseHandler struct {
}

BaseHandler is the handler for base api routes

func (*BaseHandler) InitRoutes

func (h *BaseHandler) InitRoutes(ctx context.Context, r *mux.Router)

InitRoutes initializes base (non-versioned) api routes

type ConsumerErrors

type ConsumerErrors struct {
	Errors []ResponseError `json:"errors"`
}

type Handler

type Handler interface {
	InitRoutes(context.Context, *mux.Router)
}

Handler is an interface that each api version's handler should implement

type ResponseErr

type ResponseErr interface {
	Error() string
}

type ResponseError

type ResponseError struct {
	Message    string                  `json:"message"`
	Code       string                  `json:"code"`
	Properties []ResponsePropertyError `json:"properties"`
}

func (ResponseError) Error

func (re ResponseError) Error() string

type ResponsePropertyError

type ResponsePropertyError struct {
	Property    string   `json:"property"`
	Constraints []string `json:"constraints"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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