hypermedia

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package hypermedia provides parsers that extract typed links from HTTP responses in various hypermedia formats (Link headers, HAL, TSJ, JSON:API, Siren). All URIs are resolved to absolute form before being returned.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(baseURL *url.URL, header http.Header, body any, parsers []Parser) map[string]string

Parse runs all parsers against the response and returns a rel→uri map. When multiple parsers produce the same rel, the last value wins. Returns nil if no links are found.

Types

type HALParser

type HALParser struct{}

HALParser extracts links from the `_links` object.

func (HALParser) ParseLinks(baseURL *url.URL, _ http.Header, body any) []Link

type JSONAPIParser

type JSONAPIParser struct{}

JSONAPIParser extracts links from the top-level `links` object. Requires either a `jsonapi` object or one of the JSON:API document members.

func (JSONAPIParser) ParseLinks(baseURL *url.URL, _ http.Header, body any) []Link
type Link struct {
	Rel string // relation type, e.g. "next", "self"
	URI string // always an absolute URI
}

Link is a normalized hypermedia link.

func LinkHeaderLinks(baseURL *url.URL, header http.Header) []Link

LinkHeaderLinks extracts all rel/URI pairs from HTTP Link headers.

type LinkHeaderParser

type LinkHeaderParser struct{}

LinkHeaderParser extracts links from HTTP Link headers. Example: Link: <https://api.example.com/page2>; rel="next"

func (LinkHeaderParser) ParseLinks(baseURL *url.URL, header http.Header, _ any) []Link

type Parser

type Parser interface {
	// ParseLinks returns all links it can find. Implementations return nil
	// when they cannot recognise the format.
	ParseLinks(baseURL *url.URL, header http.Header, body any) []Link
}

Parser extracts links from a response.

func DefaultParsers

func DefaultParsers() []Parser

DefaultParsers returns the built-in set of hypermedia parsers in priority order (Link header first, then body-based formats).

type SirenParser

type SirenParser struct{}

SirenParser extracts links from the `links` array. Siren link objects have `rel` (array of strings) and `href`.

func (SirenParser) ParseLinks(baseURL *url.URL, _ http.Header, body any) []Link

type TSJParser

type TSJParser struct{}

TSJParser extracts a self link from the `@id` field (JSON-LD / TSJ).

func (TSJParser) ParseLinks(baseURL *url.URL, _ http.Header, body any) []Link

Jump to

Keyboard shortcuts

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