negotiator

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2017 License: MIT Imports: 4 Imported by: 9

README

negotiator

Build Status Coverage Status

An HTTP content negotiator for Go

Installation

go get -u github.com/go-http-utils/negotiator

Documentation

API documentation can be found here: https://godoc.org/github.com/go-http-utils/negotiator

Usage

import (
  "github.com/go-http-utils/negotiator"
)

negotiator := negotiator.New(req.Header)
Type
// Assume that the Accept header is "text/html, application/*;q=0.9, image/jpeg;q=0.8"

negotiator.Type()
// -> "text/html"

negotiator.Type("text/html", "application/json", "image/jpeg")
// -> "text/html"

negotiator.Type("application/json", "image/jpeg", "text/plain")
// -> "application/json"

negotiator.Type("text/plain")
// -> ""
Encoding
// Assume that the Accept-Encoding header is "gzip, compress;q=0.2, identity;q=0.5"

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

negotiator.Encoding("identity", "gzip")
// -> "gzip"

negotiator.Encoding("compress", "identity")
// -> "identity"
Language
// Assume that the Accept-Language header is "en;q=0.8, es, pt"

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

negotiator.Language("en", "es", "fr")
// -> "es"

negotiator.Language("es", "pt")
// -> "es"
Charset
// Assume that the Accept-Charset header is "utf-8, iso-8859-1;q=0.8, utf-7;q=0.2"

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

negotiator.Charset("utf-8", "iso-8859-1", "iso-8859-5")
// -> "utf-8"

negotiator.Charset("iso-8859-5")
// -> ""

Documentation

Index

Examples

Constants

View Source
const Version = "1.0.0"

Version is this package's version

Variables

This section is empty.

Functions

This section is empty.

Types

type Negotiator

type Negotiator struct {
	// contains filtered or unexported fields
}

Negotiator repensents the HTTP negotiator.

func New

func New(header http.Header) *Negotiator

New creates an instance of Negotiator.

func (*Negotiator) Charset

func (n *Negotiator) Charset(offers ...string) (bestOffer string)

Charset returns the most preferred charset from the HTTP Accept-Charset header. If nothing accepted, then empty string is returned.

Example
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/go-http-utils/negotiator"
)

func main() {
	req := httptest.NewRequest(http.MethodGet, "/", nil)
	req.Header.Set("Accept-Language", "utf-8, iso-8859-1;q=0.8, utf-7;q=0.2")
	negotiator := negotiator.New(req.Header)

	fmt.Println(negotiator.Charset("UTF-8", "ISO-8859-1", "ISO-8859-5"))
	// -> "UTF-8"

	fmt.Println(negotiator.Charset("ISO-8859-5"))
	// -> ""
}
Output:

func (*Negotiator) Encoding

func (n *Negotiator) Encoding(offers ...string) (bestOffer string)

Encoding returns the most preferred encoding from the HTTP Accept-Encoding header. If nothing accepted, then empty string is returned.

Example
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/go-http-utils/negotiator"
)

func main() {
	req := httptest.NewRequest(http.MethodGet, "/", nil)
	req.Header.Set("Accept-Encoding", "gzip, compress;q=0.2, identity;q=0.5")
	negotiator := negotiator.New(req.Header)

	fmt.Println(negotiator.Encoding("identity", "gzip"))
	// -> "gzip"

	fmt.Println(negotiator.Encoding("compress", "identity"))
	// -> "identity"
}
Output:

func (*Negotiator) Language

func (n *Negotiator) Language(offers ...string) (bestOffer string)

Language returns the most preferred language from the HTTP Accept-Language header. If nothing accepted, then empty string is returned.

Example
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/go-http-utils/negotiator"
)

func main() {
	req := httptest.NewRequest(http.MethodGet, "/", nil)
	req.Header.Set("Accept-Language", "en;q=0.8, es, pt")
	negotiator := negotiator.New(req.Header)

	fmt.Println(negotiator.Language("en", "es", "fr"))
	// -> "es"

	fmt.Println(negotiator.Language("es", "pt"))
	// -> "es"
}
Output:

func (*Negotiator) Type

func (n *Negotiator) Type(offers ...string) (bestOffer string)

Type returns the most preferred content type from the HTTP Accept header. If nothing accepted, then empty string is returned.

Jump to

Keyboard shortcuts

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