link

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2022 License: MIT Imports: 3 Imported by: 97

README

Build status Go Report Card GoDoc License MIT

Parses Link headers used for pagination, as defined in RFC 5988.

This package was originally based on https://github.com/swhite24/link, but Parse takes a string instead of *http.Request in this version. It also has the convenience functions ParseHeader, ParseRequest and ParseResponse.

Installation

go get -u github.com/peterhellberg/link

Exported functions

Usage

package main

import (
	"fmt"
	"net/http"

	"github.com/peterhellberg/link"
)

func main() {
	for _, l := range link.Parse(`<https://example.com/?page=2>; rel="next"; foo="bar"`) {
		fmt.Printf("URI: %q, Rel: %q, Extra: %+v\n", l.URI, l.Rel, l.Extra)
		// URI: "https://example.com/?page=2", Rel: "next", Extra: map[foo:bar]
	}

	if resp, err := http.Get("https://api.github.com/search/code?q=Println+user:golang"); err == nil {
		for _, l := range link.ParseResponse(resp) {
			fmt.Printf("URI: %q, Rel: %q, Extra: %+v\n", l.URI, l.Rel, l.Extra)
			// URI: "https://api.github.com/search/code?q=Println+user%3Agolang&page=2", Rel: "next", Extra: map[]
			// URI: "https://api.github.com/search/code?q=Println+user%3Agolang&page=34", Rel: "last", Extra: map[]
		}
	}
}

Not supported

Alternatives to this package

License (MIT)

Copyright (c) 2015-2022 Peter Hellberg

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package link parses Link headers used for pagination, as defined in RFC 5988

Installation

Just go get the package:

go get -u github.com/peterhellberg/link

Usage

A small usage example

package main

import (
	"fmt"
	"net/http"

	"github.com/peterhellberg/link"
)

func main() {
	for _, l := range link.Parse(`<https://example.com/?page=2>; rel="next"; foo="bar"`) {
		fmt.Printf("URI: %q, Rel: %q, Extra: %+v\n", l.URI, l.Rel, l.Extra)
		// URI: "https://example.com/?page=2", Rel: "next", Extra: map[foo:bar]
	}

	if resp, err := http.Get("https://api.github.com/search/code?q=Println+user:golang"); err == nil {
		for _, l := range link.ParseResponse(resp) {
			fmt.Printf("URI: %q, Rel: %q, Extra: %+v\n", l.URI, l.Rel, l.Extra)
			// URI: "https://api.github.com/search/code?q=Println+user%3Agolang&page=2", Rel: "next", Extra: map[]
			// URI: "https://api.github.com/search/code?q=Println+user%3Agolang&page=34", Rel: "last", Extra: map[]
		}
	}
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Group

type Group map[string]*Link

Group returned by Parse, contains multiple links indexed by "rel".

func Parse

func Parse(s string) Group

Parse parses the provided string into a Group.

Example
l := Parse(`<https://example.com/?page=2>; rel="next"; title="foo"`)["next"]

fmt.Printf("URI: %q, Rel: %q, Extra: %+v\n", l.URI, l.Rel, l.Extra)
Output:

URI: "https://example.com/?page=2", Rel: "next", Extra: map[title:foo]

func ParseHeader

func ParseHeader(h http.Header) Group

ParseHeader retrieves the Link header from the provided http.Header and parses it into a Group.

func ParseRequest

func ParseRequest(req *http.Request) Group

ParseRequest parses the provided *http.Request into a Group.

func ParseResponse

func ParseResponse(resp *http.Response) Group

ParseResponse parses the provided *http.Response into a Group.

type Link struct {
	URI   string
	Rel   string
	Extra map[string]string
}

Link contains a Link item with URI, Rel, and other non-URI components in Extra.

func (*Link) String

func (l *Link) String() string

String returns the URI.

Jump to

Keyboard shortcuts

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