httputil

package
v0.0.0-...-3faa8e7 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HeaderAccept      = "Accept"
	HeaderContentType = "Content-Type"

	MediaTypeJson = "application/json"
	MediaTypeYaml = "application/x-yaml"

	ContentTypeYaml = MediaTypeYaml + "; charset=utf-8"
	ContentTypeJson = MediaTypeJson + "; charset=utf-8"
)

Variables

View Source
var (
	DefaultMaxLimit   = 300
	DefaultMinLimit   = 30
	DefaultPageNumber = 1
)

Functions

func NewRequestInterceptor

func NewRequestInterceptor(h http.Handler) http.Handler

creates a http.Handler that wraps the handler passed in and sets the original http protocol and host requested by the client

func SetProtocolAndHostIn

func SetProtocolAndHostIn(r *http.Request)

set the (possibly forwarded) http protocol and host in the request

func WriteResponse

func WriteResponse(w http.ResponseWriter, r *http.Request, v interface{})

Types

type Builder

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

func UriBuilder

func UriBuilder(r *http.Request) *Builder

works out the request uri, including scheme, and port from the request passed in the full path is built up from this base

func (*Builder) Build

func (b *Builder) Build() string

func (*Builder) Parent

func (b *Builder) Parent() *Builder

func (*Builder) Path

func (b *Builder) Path(path ...string) *Builder

the path/s to be added to the base uri, in the order passed in

func (*Builder) Replace

func (b *Builder) Replace(pathParam, paramValue string) *Builder

replaces a path parameter in the currently built path with the value passed in. will also remove a 'slash' suffix if one exists.

type Link struct {
	Href string `json:"href"`
	Rel  string `json:"rel,omitempty"`
}

type Page

type Page struct {
	// defaults to 1, even if page does not contain any items
	PageNumber int
	// defaults to 30, max limit is 300
	PerPage int
	// it the page does not contain any items, this will be set to 0
	TotalPages int
	// (PageNumber - 1) * PerPage
	StartIndex int
	// contains filtered or unexported fields
}

func NewPage

func NewPage(r *http.Request, totalItems int) *Page

called in the handler, page and per_page are retrieved and set from the http request query

func (*Page) PageLinksFor

func (p *Page) PageLinksFor(uri string, defaultLinks []Link) []Link

return 'default' links, plus first, prev, next and last links (if applicable)

Jump to

Keyboard shortcuts

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