apiutil

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2019 License: MIT Imports: 3 Imported by: 2

README

apiutil

apiutil joyfully breaks the "never write a package called util" pattern. It's a tiny library that provides common behaviour for user-facing apis. Think of it like ioutil, but not that, and instead for making JSON HTTP APIs

Documentation

Index

Constants

View Source
const DefaultPageSize = 100

DefaultPageSize is the

Variables

This section is empty.

Functions

func EmptyOkHandler

func EmptyOkHandler(w http.ResponseWriter, r *http.Request)

EmptyOkHandler is an empty 200 response, often used for OPTIONS requests that responds with headers set in addCorsHeaders

func HealthCheckHandler

func HealthCheckHandler(w http.ResponseWriter, r *http.Request)

HealthCheckHandler is a basic "hey I'm fine" for load balancers & co TODO - add Database connection & proper configuration checks here for more accurate health reporting

func NotFoundHandler

func NotFoundHandler(w http.ResponseWriter, r *http.Request)

NotFoundHandler is a JSON 404 response

func ReqParamBool

func ReqParamBool(key string, r *http.Request) (bool, error)

ReqParamBool pulls a boolean parameter from a request form value TODO - consider providing a default param & removing the error

func ReqParamInt

func ReqParamInt(key string, r *http.Request) (int, error)

ReqParamInt extracts an integer parameter from a request form value TODO - consider providing a default param & removing the error

func WriteErrResponse

func WriteErrResponse(w http.ResponseWriter, code int, err error) error

WriteErrResponse writes a JSON error response message & HTTP status

func WriteMessageResponse

func WriteMessageResponse(w http.ResponseWriter, message string, data interface{}) error

WriteMessageResponse includes a message with a data response

func WritePageResponse

func WritePageResponse(w http.ResponseWriter, data interface{}, r *http.Request, p Page) error

WritePageResponse wraps response data and pagination information in an envelope and writes it

func WriteResponse

func WriteResponse(w http.ResponseWriter, data interface{}) error

WriteResponse wraps response data in an envelope & writes it

Types

type Page

type Page struct {
	Number int `json:"page"`
	Size   int `json:"pageSize"`
}

Page represents pagination information

func NewPage

func NewPage(number, size int) Page

NewPage is a conveince page constructor

func NewPageFromOffsetAndLimit

func NewPageFromOffsetAndLimit(offset, limit int) Page

NewPageFromOffsetAndLimit converts a offset and Limit to a Page struct

func PageFromRequest

func PageFromRequest(r *http.Request) Page

PageFromRequest extracts pagination params from an http request

func (Page) Limit

func (p Page) Limit() int

Limit is a convenience accessor for page size

func (Page) Offset

func (p Page) Offset() int

Offset calculates the starting index for pagination based on page size & number

Jump to

Keyboard shortcuts

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