link

package module
v0.0.0-...-b46beb5 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2023 License: MIT Imports: 6 Imported by: 0

README

Go Reference

Easily parse and create Link headers for pagination.

Easily create link headers for responses to your paginated endpoints.

func handler(w http.ResponseWriter, req http.Request) {
    // ...
    linkHeader := link.NewHeader(map[string]*url.URL{
        "first": firstURL,
        "last": lastURL,
        // if these are nil then they will not be included in the header
        "next": nextURL,
        "prev": prevURL,
    })
    w.WriteHeader("link", linkHeader.String())
    // ...
}

Easily parse link headers from external services to paginate through results.

func loadUsers(path string) {
    resp, err := http.DefaultClient.Get(path)
    linkHeader, err := link.Parse(resp)
    if next := linkHeader.Next(); next != nil {
        loadUsers(next.URL.String())
    }
}

func main() {
    loadUsers("http://service.com/users")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Header struct {
	Links []*Link
}

Header contains all of the links in the header with ways of accessing them

func NewHeader

func NewHeader(links map[string]*url.URL) *Header

NewHeader will format a new link header with a map of rel = link

func Parse

func Parse(req *http.Response) (*Header, error)

Parse will retrieve the link header from a response and parse it.

func ParseString

func ParseString(header string) (*Header, error)

ParseString will parse a string from the link header and find all appropriate infomation from link headers

func (Header) Find

func (header Header) Find(rel ...string) *Link

Find will search for a link with the rel matching one of the values passed. If it does not exist then nil will be returned.

func (Header) First

func (header Header) First() *Link

First will find the link for the link to the first page with rel="first" if it exists. It will return nil if it does not exist.

func (Header) Last

func (header Header) Last() *Link

Last will find the link for the link to the last page with rel="last" if it exists. It will return nil if it does not exist.

func (Header) Next

func (header Header) Next() *Link

Next will find the link for the link to the next page with rel="next" if it exists. It will return nil if it does not exist.

func (Header) Prev

func (header Header) Prev() *Link

Prev will find the link for the link to the previous with rel="prev" or rel="previous"" if it exists. It will return nil if it does not exist.

func (Header) String

func (header Header) String() string

type HeaderConfig

type HeaderConfig struct {
	First string
	Last  string
	Next  string
	Prev  string
}
type Link struct {
	URL    *url.URL
	Params map[string]string
}

Link contains a single link with it's associated data.

func (Link) String

func (link Link) String() string

Jump to

Keyboard shortcuts

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