negotiation

package module
v0.0.0-...-5f2c7e6 Latest Latest
Warning

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

Go to latest
Published: May 29, 2016 License: MIT Imports: 4 Imported by: 13

README

negotiation Build Status

negotiation is a standalone library that allows you to implement content negotiation in your application, whatever framework you use. This provides specific functions to negotiate Accept and Accept-Language headers, although any kind of header can be parsed.

Status

This project is DEPRECATED and should NOT be used.

If someone magically appears and wants to maintain this project, I'll gladly give access to this repository.

Usage

Language negotiation
package main

import (
  "fmt"
  "github.com/K-Phoen/negotiation"
)

func main() {
  language, err := negotiation.NegotiateLanguage("da, en-gb;q=0.8, en;q=0.7", []string{"es", "fr", "en"})

  if err != nil {
    fmt.Println("Unable to negotiate the language")
  }

  fmt.Println("Negotiated language: ", language.Value) // outputs: "Negotiated language: en"
}
Format negotiation
package main

import (
  "fmt"
  "github.com/K-Phoen/negotiation"
)

func main() {
  negotiation.RegisterFormat("html", []string{"text/html", "application/xhtml+xml"})
  format, err := negotiation.NegotiateAccept("application/rdf+xml;q=0.5,text/html;q=.3", []string{"html", "application/xml"})

  if err != nil {
    fmt.Println("Unable to negotiate the format")
  }

  fmt.Println("Negotiated format: " + format.Name + " (" + format.Value + ")") // outputs: "Negotiated format: html (text/html)"
}

Tests

$ go test

Credits

License

This library is released under the MIT License. See the bundled LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultQualityEvaluator

func DefaultQualityEvaluator(alternative *Alternative)

func DefaultTokenizer

func DefaultTokenizer(text string) (string, string, error)

func LanguageQualityEvaluator

func LanguageQualityEvaluator(alternative *Alternative)

func LanguageTokenizer

func LanguageTokenizer(text string) (string, string, error)

func RegisterFormat

func RegisterFormat(name string, mimeTypes []string)

Types

type Alternative

type Alternative struct {
	Name    string
	Value   string
	Quality float64
	Params  map[string]string
	// contains filtered or unexported fields
}

func Negotiate

func Negotiate(header string, acceptedAlternatives []string, mediaTokenizer MediaTokenizer, qualityEvaluator QualityEvaluator) (*Alternative, error)

func NegotiateAccept

func NegotiateAccept(header string, alternatives []string) (*Alternative, error)

func NegotiateLanguage

func NegotiateLanguage(header string, alternatives []string) (*Alternative, error)

type MediaTokenizer

type MediaTokenizer func(string) (string, string, error)

Split Media tokens in type and sub-type parts

type QualityEvaluator

type QualityEvaluator func(*Alternative)

Evaluate default quality for catch-all values

Jump to

Keyboard shortcuts

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