negotiator

package module
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: May 21, 2020 License: MIT Imports: 7 Imported by: 1

README

negotiator

Build Status codecov Go Report Card GoDoc License

An HTTP content negotiator for Go

Thanks to negotiator which is the original version written in javascript.

Installation

To install negotiator, you need to install Go and set your Go workspace first.

The first need Go installed (version 1.11+ is required), then you can use the below Go command to install negotiator.

$ go get -u github.com/soongo/negotiator

Usage

import "github.com/soongo/negotiator"

// The negotiator constructor receives a http.Header object
negotiator := negotiator.New(header)
Accept Negotiation
availableMediaTypes := []string{"text/html", "text/plain", "application/json"}

// Let's say Accept header is 'text/html, application/*;q=0.2, image/jpeg;q=0.8'

negotiator.MediaTypes()
// -> ["text/html", "image/jpeg", "application/*"]

negotiator.MediaTypes(availableMediaTypes)
// -> ["text/html", "application/json"]

negotiator.MediaType(availableMediaTypes)
// -> "text/html"
Methods
MediaType()

Returns the most preferred media type from the client.

MediaType(availableMediaType)

Returns the most preferred media type from a list of available media types.

MediaTypes()

Returns an array of preferred media types ordered by the client preference.

MediaTypes(availableMediaTypes)

Returns an array of preferred media types ordered by priority from a list of available media types.

Accept-Language Negotiation
availableLanguages := []string{"en", "es", "fr"}

// Let's say Accept-Language header is 'en;q=0.8, es, pt'

negotiator.Languages()
// -> ["es", "pt", "en"]

negotiator.Languages(availableLanguages)
// -> ["es", "en"]

negotiator.Language(availableLanguages)
// -> "es"
Methods
Language()

Returns the most preferred language from the client.

Language(availableLanguages)

Returns the most preferred language from a list of available languages.

Languages()

Returns an array of preferred languages ordered by the client preference.

Languages(availableLanguages)

Returns an array of preferred languages ordered by priority from a list of available languages.

Accept-Charset Negotiation
availableCharsets := []string{"utf-8", "iso-8859-1", "iso-8859-5"}

// Let's say Accept-Charset header is 'utf-8, iso-8859-1;q=0.8, utf-7;q=0.2'

negotiator.Charsets()
// -> ["utf-8", "iso-8859-1", "utf-7"]

negotiator.Charsets(availableCharsets...)
// -> ["utf-8", "iso-8859-1"]

negotiator.Charset(availableCharsets...)
// -> "utf-8"
Methods
Charset()

Returns the most preferred charset from the client.

Charset(availableCharsets...)

Returns the most preferred charset from a list of available charsets.

Charsets()

Returns an array of preferred charsets ordered by the client preference.

Charsets(availableCharsets...)

Returns an array of preferred charsets ordered by priority from a list of available charsets.

Accept-Encoding Negotiation
availableEncodings := []string{"identity", "gzip"}

// Let's say Accept-Encoding header is 'gzip, compress;q=0.2, identity;q=0.5'

negotiator.Encodings()
// -> ["gzip", "identity", "compress"]

negotiator.Encodings(availableEncodings...)
// -> ["gzip", "identity"]

negotiator.Encoding(availableEncodings...)
// -> "gzip"
Methods
Encoding()

Returns the most preferred encoding from the client.

Encoding(availableEncodings...)

Returns the most preferred encoding from a list of available encodings.

Encodings()

Returns an array of preferred encodings ordered by the client preference.

Encodings(availableEncodings...)

Returns an array of preferred encodings ordered by priority from a list of available encodings.

Documentation

Index

Constants

View Source
const Version = "v0.6.3"

Version is the current version of negotiator.

Variables

View Source
var HeaderAccept = textproto.CanonicalMIMEHeaderKey("Accept")

HeaderAccept is `Accept`

View Source
var HeaderAcceptCharset = textproto.CanonicalMIMEHeaderKey("Accept-Charset")

HeaderAcceptCharset is `Accept-Charset`

View Source
var HeaderAcceptEncoding = textproto.CanonicalMIMEHeaderKey("Accept-Encoding")

HeaderAcceptEncoding is `Accept-Encoding`

View Source
var HeaderAcceptLanguage = textproto.CanonicalMIMEHeaderKey("Accept-Language")

HeaderAcceptLanguage is `Accept-Language`

Functions

func PreferredCharsets

func PreferredCharsets(accept string, provided ...string) []string

PreferredCharsets gets the preferred charsets from an Accept-Charset header. RFC 2616 sec 14.2: no header = *, so you should pass * if no Accept-Charset field in header.

func PreferredEncodings

func PreferredEncodings(accept string, provided ...string) []string

PreferredEncodings gets the preferred encodings from an Accept-Encoding header.

func PreferredLanguages

func PreferredLanguages(accept string, provided ...string) []string

PreferredLanguages gets the preferred languages from an Accept-Language header. RFC 2616 sec 14.2: no header = *, so you should pass * if no Accept-Language field in header.

func PreferredMediaTypes

func PreferredMediaTypes(accept string, provided ...string) []string

PreferredMediaTypes gets the preferred media types from an Accept header. RFC 2616 sec 14.2: no header = */*, so you should pass */* if no Accept field in header.

Types

type Negotiator

type Negotiator struct {
	Header http.Header
}

Negotiator gets the negotiation info from http header

func New

func New(header http.Header) *Negotiator

New creates a Negotiator instance from a header object.

func (*Negotiator) Charset

func (n *Negotiator) Charset(available ...string) string

Charset gets the most preferred charset from a list of available charsets.

func (*Negotiator) Charsets

func (n *Negotiator) Charsets(available ...string) []string

Charsets gets an array of preferred charsets ordered by priority from a list of available charsets.

func (*Negotiator) Encoding

func (n *Negotiator) Encoding(available ...string) string

Encoding gets the most preferred encoding from a list of available encodings.

func (*Negotiator) Encodings

func (n *Negotiator) Encodings(available ...string) []string

Encodings gets an array of preferred encodings ordered by priority from a list of available encodings.

func (*Negotiator) Language

func (n *Negotiator) Language(available ...string) string

Language gets the most preferred language from a list of available languages.

func (*Negotiator) Languages

func (n *Negotiator) Languages(available ...string) []string

Languages gets an array of preferred languages ordered by priority from a list of available languages.

func (*Negotiator) MediaType

func (n *Negotiator) MediaType(available ...string) string

MediaType gets the most preferred media type from a list of available media types.

func (*Negotiator) MediaTypes

func (n *Negotiator) MediaTypes(available ...string) []string

MediaTypes gets an array of preferred mediaTypes ordered by priority from a list of available media types.

Jump to

Keyboard shortcuts

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