linkheader

package module
v0.0.0-...-02ca582 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2018 License: MIT Imports: 2 Imported by: 291

README

# Golang Link Header Parser

Library for parsing HTTP Link headers. Requires Go 1.6 or higher.

Docs can be found on [the GoDoc page](https://godoc.org/github.com/tomnomnom/linkheader).

[![Build Status](https://travis-ci.org/tomnomnom/linkheader.svg)](https://travis-ci.org/tomnomnom/linkheader)

## Basic Example

```go
package main

import (
	"fmt"

	"github.com/tomnomnom/linkheader"
)

func main() {
	header := "<https://api.github.com/user/58276/repos?page=2>; rel=\"next\"," +
		"<https://api.github.com/user/58276/repos?page=2>; rel=\"last\""
	links := linkheader.Parse(header)

	for _, link := range links {
		fmt.Printf("URL: %s; Rel: %s\n", link.URL, link.Rel)
	}
}

// Output:
// URL: https://api.github.com/user/58276/repos?page=2; Rel: next
// URL: https://api.github.com/user/58276/repos?page=2; Rel: last
```


Documentation

Overview

Package linkheader provides functions for parsing HTTP Link headers

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Link struct {
	URL    string
	Rel    string
	Params map[string]string
}

A Link is a single URL and related parameters

func (Link) HasParam

func (l Link) HasParam(key string) bool

HasParam returns if a Link has a particular parameter or not

func (Link) Param

func (l Link) Param(key string) string

Param returns the value of a parameter if it exists

func (Link) String

func (l Link) String() string

String returns the string representation of a link

Example
package main

import (
	"fmt"

	"github.com/tomnomnom/linkheader"
)

func main() {
	link := linkheader.Link{
		URL: "http://example.com/page/2",
		Rel: "next",
	}

	fmt.Printf("Link: %s\n", link.String())

}
Output:

Link: <http://example.com/page/2>; rel="next"
type Links []Link

Links is a slice of Link structs

func Parse

func Parse(raw string) Links

Parse parses a raw Link header in the form:

<url>; rel="foo", <url>; rel="bar"; wat="dis"

returning a slice of Link structs

Example
package main

import (
	"fmt"

	"github.com/tomnomnom/linkheader"
)

func main() {
	header := "<https://api.github.com/user/58276/repos?page=2>; rel=\"next\"," +
		"<https://api.github.com/user/58276/repos?page=2>; rel=\"last\""
	links := linkheader.Parse(header)

	for _, link := range links {
		fmt.Printf("URL: %s; Rel: %s\n", link.URL, link.Rel)
	}

}
Output:

URL: https://api.github.com/user/58276/repos?page=2; Rel: next
URL: https://api.github.com/user/58276/repos?page=2; Rel: last

func ParseMultiple

func ParseMultiple(headers []string) Links

ParseMultiple is like Parse, but accepts a slice of headers rather than just one header string

Example
package main

import (
	"fmt"

	"github.com/tomnomnom/linkheader"
)

func main() {
	headers := []string{
		"<https://api.github.com/user/58276/repos?page=2>; rel=\"next\"",
		"<https://api.github.com/user/58276/repos?page=2>; rel=\"last\"",
	}
	links := linkheader.ParseMultiple(headers)

	for _, link := range links {
		fmt.Printf("URL: %s; Rel: %s\n", link.URL, link.Rel)
	}

}
Output:

URL: https://api.github.com/user/58276/repos?page=2; Rel: next
URL: https://api.github.com/user/58276/repos?page=2; Rel: last

func (Links) FilterByRel

func (l Links) FilterByRel(r string) Links

FilterByRel filters a group of Links by the provided Rel attribute

Example
package main

import (
	"fmt"

	"github.com/tomnomnom/linkheader"
)

func main() {
	header := "<https://api.github.com/user/58276/repos?page=2>; rel=\"next\"," +
		"<https://api.github.com/user/58276/repos?page=2>; rel=\"last\""
	links := linkheader.Parse(header)

	for _, link := range links.FilterByRel("last") {
		fmt.Printf("URL: %s; Rel: %s\n", link.URL, link.Rel)
	}

}
Output:

URL: https://api.github.com/user/58276/repos?page=2; Rel: last

func (Links) String

func (l Links) String() string

String returns the string representation of multiple Links for use in HTTP responses etc

Example
package main

import (
	"fmt"

	"github.com/tomnomnom/linkheader"
)

func main() {

	links := linkheader.Links{
		{URL: "http://example.com/page/3", Rel: "next"},
		{URL: "http://example.com/page/1", Rel: "last"},
	}

	fmt.Printf("Link: %s\n", links.String())

}
Output:

Link: <http://example.com/page/3>; rel="next", <http://example.com/page/1>; rel="last"

Jump to

Keyboard shortcuts

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