Documentation
¶
Overview ¶
Package pagination provides support for pagination requests and responses.
Index ¶
- Variables
- func Decode(r *http.Request, fn RequestURLParam, data interface{}) error
- func QueryParam[T ParamTypes, K FromConstraint](from K, param string, validators ...validator.ValidatorFunc[T]) (T, error)
- func QueryParamOrDefault[T ParamTypes, K FromConstraint](from K, param string, defValue T, validators ...validator.ValidatorFunc[T]) T
- type Chain
- type Client
- func (c *Client) Delete(ctx context.Context, rawurl string, options ...RequestOption) error
- func (c *Client) Get(ctx context.Context, rawurl string, out any, options ...RequestOption) error
- func (c *Client) Patch(ctx context.Context, rawurl string, in, out any, options ...RequestOption) error
- func (c *Client) Post(ctx context.Context, rawurl string, in, out any, options ...RequestOption) error
- func (c *Client) SetClient(client *http.Client)
- func (c *Client) SetDebug(debug bool)
- type ClientOption
- type ClientOptionFunc
- type Constructor
- type ErrorResponse
- type FromConstraint
- type Pages
- func NewPages[T any](page, perPage, total int) *Pages[T]
- func NewPagesWithItems[T any](page, perPage, total int, items []T) *Pages[T]
- func PagesFromReqAndData[T any](req *http.Request, fn func(limit, offset int) ([]T, int64, error)) (*Pages[T], error)
- func PagesFromRequest[T any](req *http.Request, count int) *Pages[T]
- type ParamTypes
- type RequestOption
- type RequestOptionFunc
- type RequestURLParam
- type URLParam
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultPageSize specifies the default page size DefaultPageSize = 100 // MaxPageSize specifies the maximum page size MaxPageSize = 1000 // PageVar specifies the query parameter name for page number PageVar = "page" // PageSizeVar specifies the query parameter name for page size PageSizeVar = "per_page" )
Functions ¶
func Decode ¶ added in v0.4.0
func Decode(r *http.Request, fn RequestURLParam, data interface{}) error
Decode an HTTP request into the provided struct
func QueryParam ¶
func QueryParam[T ParamTypes, K FromConstraint](from K, param string, validators ...validator.ValidatorFunc[T]) (T, error)
func QueryParamOrDefault ¶
func QueryParamOrDefault[T ParamTypes, K FromConstraint](from K, param string, defValue T, validators ...validator.ValidatorFunc[T]) T
Types ¶
type Chain ¶
type Chain struct {
// contains filtered or unexported fields
}
Chain acts as a list of http.Handler constructors. Chain is effectively immutable: once created, it will always hold the same set of constructors in the same order.
func NewChain ¶
func NewChain(constructors ...Constructor) Chain
NewChain creates a new chain, memorizing the given list of middleware constructors. New serves no other function, constructors are only called upon a call to Then().
func (Chain) Append ¶
func (c Chain) Append(constructors ...Constructor) Chain
Append extends a chain, adding the specified constructors as the last ones in the request flow.
Append returns a new chain, leaving the original one untouched.
stdChain := httputil.NewChain(m1, m2) extChain := stdChain.Append(m3, m4) // requests in stdChain go m1 -> m2 // requests in extChain go m1 -> m2 -> m3 -> m4
func (Chain) Extend ¶
Extend extends a chain by adding the specified chain as the last one in the request flow.
Extend returns a new chain, leaving the original one untouched.
stdChain := httputil.NewChain(m1, m2) ext1Chain := httputil.NewChain(m3, m4) ext2Chain := stdChain.Extend(ext1Chain) // requests in stdChain go m1 -> m2 // requests in ext1Chain go m3 -> m4 // requests in ext2Chain go m1 -> m2 -> m3 -> m4
Another example:
aHtmlAfterNosurf := httputil.NewChain(m2)
aHtml := httputil.NewChain(m1, func(h http.Handler) http.Handler {
csrf := nosurf.New(h)
csrf.SetFailureHandler(aHtmlAfterNosurf.ThenFunc(csrfFail))
return csrf
}).Extend(aHtmlAfterNosurf)
// requests to aHtml hitting nosurfs success handler go m1 -> nosurf -> m2 -> target-handler
// requests to aHtml hitting nosurfs failure handler go m1 -> nosurf -> m2 -> csrfFail
func (Chain) Then ¶
Then chains the middleware and returns the final http.Handler.
New(m1, m2, m3).Then(h)
is equivalent to:
m1(m2(m3(h)))
When the request comes in, it will be passed to m1, then m2, then m3 and finally, the given handler (assuming every middleware calls the following one).
A chain can be safely reused by calling Then() several times.
stdStack := httputil.NewChain(ratelimitHandler, csrfHandler) indexPipe = stdStack.Then(indexHandler) authPipe = stdStack.Then(authHandler)
Note that constructors are called on every call to Then() and thus several instances of the same middleware will be created when a chain is reused in this way. For proper middleware, this should cause no problems.
Then() treats nil as http.DefaultServeMux.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func NewClient ¶
func NewClient(uri string, options ...ClientOption) *Client
func (*Client) Patch ¶
func (c *Client) Patch(ctx context.Context, rawurl string, in, out any, options ...RequestOption) error
helper function for making an http PATCH request.
func (*Client) Post ¶
func (c *Client) Post(ctx context.Context, rawurl string, in, out any, options ...RequestOption) error
helper function for making an http POST request.
type ClientOption ¶
type ClientOption interface {
Apply(r *Client)
}
type ClientOptionFunc ¶
type ClientOptionFunc func(c *Client)
func WithHTTPTimeout ¶
func WithHTTPTimeout(timeout time.Duration) ClientOptionFunc
WithHTTPTimeout sets timeout
func (ClientOptionFunc) Apply ¶
func (f ClientOptionFunc) Apply(c *Client)
type Constructor ¶
A constructor for a piece of middleware.
type ErrorResponse ¶
func (ErrorResponse) Error ¶
func (r ErrorResponse) Error() string
type Pages ¶
type Pages[T any] struct { Page int `json:"page"` PerPage int `json:"per_page"` PageCount int `json:"page_count"` TotalCount int `json:"total_count"` Items []T `json:"items"` }
Pages represents a paginated list of data items.
func NewPages ¶
NewPages creates a new Pages instance. The page parameter is 1-based and refers to the current page index/number. The perPage parameter refers to the number of items on each page. And the total parameter specifies the total number of data items. If total is less than 0, it means total is unknown.
func NewPagesWithItems ¶ added in v0.3.1
func PagesFromReqAndData ¶ added in v0.3.1
func PagesFromReqAndData[T any](req *http.Request, fn func(limit, offset int) ([]T, int64, error)) (*Pages[T], error)
PagesFromRequest creates a Pages object using the query parameters found in the given HTTP request. count stands for the total number of items. Use -1 if this is unknown.
func PagesFromRequest ¶
PagesFromRequest creates a Pages object using the query parameters found in the given HTTP request. count stands for the total number of items. Use -1 if this is unknown.
func (*Pages[T]) BuildLinkHeader ¶
BuildLinkHeader returns an HTTP header containing the links about the pagination.
func (*Pages[T]) BuildLinks ¶
BuildLinks returns the first, prev, next, and last links corresponding to the pagination. A link could be an empty string if it is not needed. For example, if the pagination is at the first page, then both first and prev links will be empty.
type ParamTypes ¶
type RequestOption ¶
type RequestOptionFunc ¶
func WithAcceptVersion ¶
func WithAcceptVersion(value string) RequestOptionFunc
func WithAuthHeader ¶
func WithAuthHeader(value string) RequestOptionFunc
func (RequestOptionFunc) Apply ¶
func (f RequestOptionFunc) Apply(r *http.Request)