apiutil

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2020 License: MIT Imports: 6 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

This section is empty.

Variables

View Source
var (
	// OrderASC defines the ascending order keyword
	OrderASC = "asc"
	// OrderDESC defines the descending order keyword
	OrderDESC = "desc"
)
View Source
var (
	// DefaultPageSize is the number NewPage
	DefaultPageSize = 50
	// DefaultMaxPageSize is the max size a page can be by default, set to -1 to
	// ignore maximum sizes
	DefaultMaxPageSize = 100
)

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(r *http.Request, key string, def bool) bool

ReqParamBool pulls a boolean parameter from a request form value

func ReqParamInt

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

ReqParamInt extracts an integer parameter from a request form value

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 Order added in v0.2.0

type Order struct {
	Key       string
	Direction string
}

Order represents ordering information for a single key

func NewOrder added in v0.2.0

func NewOrder(key, orderDirection string) Order

NewOrder constructs a basic order struct

func (Order) String added in v0.2.0

func (o Order) String() string

String implements the stringer interface for Order

type OrderBy added in v0.2.0

type OrderBy []Order

OrderBy represents ordering information

func NewOrderByFromString added in v0.2.0

func NewOrderByFromString(orderBy string, validKeys []string) OrderBy

NewOrderByFromString converts a commaa delimited string to an OrderBy struct

func OrderByFromRequest added in v0.2.0

func OrderByFromRequest(r *http.Request) OrderBy

OrderByFromRequest extracts orderBy params from an http request

func OrderByFromRequestWithKeys added in v0.2.0

func OrderByFromRequestWithKeys(r *http.Request, validKeys []string) OrderBy

OrderByFromRequestWithKeys extracts orderBy params from an http request and only takes the specified keys

func (OrderBy) SetQueryParams added in v0.2.0

func (o OrderBy) SetQueryParams(u *url.URL) *url.URL

SetQueryParams adds order by info to a url as query parameters

func (OrderBy) String added in v0.2.0

func (o OrderBy) String() string

String implements the stringer interface for OrderBy

type Page

type Page struct {
	Number      int    `json:"page,omitempty"`
	Size        int    `json:"pageSize,omitempty"`
	ResultCount int    `json:"resultCount,omitempty"`
	NextURL     string `json:"nextUrl"`
	PrevURL     string `json:"prevUrl"`
}

Page represents pagination information

func NewPage

func NewPage(number, size int) Page

NewPage constructs a basic page struct, setting sensible defaults

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) Next added in v0.2.0

func (p Page) Next() Page

Next returns a page with the number advanced by one

func (Page) NextPageExists added in v0.2.0

func (p Page) NextPageExists() bool

NextPageExists returns false if the next page.ResultCount is a postive number and the starting offset of the next page exceeds page.ResultCount

func (Page) Offset

func (p Page) Offset() int

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

func (Page) Prev added in v0.2.0

func (p Page) Prev() Page

Prev returns a page with the number decremented by 1

func (Page) PrevPageExists added in v0.2.0

func (p Page) PrevPageExists() bool

PrevPageExists returns false if the page number is 1

func (Page) SetQueryParams added in v0.2.0

func (p Page) SetQueryParams(u *url.URL) *url.URL

SetQueryParams adds pagination info to a url as query parameters

Jump to

Keyboard shortcuts

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