httpconv

package
v0.0.0-...-0f8ae5d Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2019 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

package httpconv provides methods for mapping TypedValues to and from HTTP requests and responses.

Any interaction with HTTP requests or responses should be handled by the high-level implementations in this package, such as ParseResponse, FormatResponse, ParseRequest, or FormatRequest.

Although you get most reliable results by properly formatting your requests (especially the Content-Type header) this package aims to be lenient by trying to infer content-types, and so on.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Common media types
	MediaTypeBytes    = mediatype.MustParse("application/octet-stream")
	MediaTypeJSON     = mediatype.MustParse("application/json")
	MediaTypeProtobuf = mediatype.MustParse("application/protobuf")
	MediaTypeText     = mediatype.MustParse("text/plain")

	ErrMessageTypeNotFound = errors.New("media type did not contain message type parameter")
)
View Source
var DefaultHTTPMapper = &HTTPMapper{
	DefaultHTTPMethod: http.MethodPost,
	ValueTypeResolver: func(tv *typedvalues.TypedValue) *mediatype.MediaType {

		if tv == nil {
			return MediaTypeBytes
		}

		if ct, ok := tv.GetMetadataValue(headerContentType); ok {
			mt, err := mediatype.Parse(ct)
			if err == nil {
				return mt
			}
		}

		switch tv.ValueType() {
		case typedvalues.TypeBytes:
			return MediaTypeBytes
		case typedvalues.TypeNil:
			return MediaTypeBytes
		case typedvalues.TypeString:
			return MediaTypeText
		}

		return MediaTypeJSON
	},
	MediaTypeResolver: func(mt *mediatype.MediaType) ParserFormatter {
		if mt == nil {
			return bytesMapper
		}

		switch mt.Identifier() {
		case MediaTypeJSON.String(), "text/json":
			return jsonMapper
		case MediaTypeText.String():
			return textMapper
		case MediaTypeBytes.String():
			return bytesMapper
		case MediaTypeProtobuf.String(), "application/vnd.google.protobuf", "application/x.google.protobuf",
			"application/x.protobuf":
			return protobufMapper
		}

		if mt.Type == "text" {
			return textMapper
		}

		if mt.Type == "application" {
			return bytesMapper
		}

		return bytesMapper
	},
}

Functions

func FormatRequest

func FormatRequest(source map[string]*typedvalues.TypedValue, target *http.Request) error

func FormatResponse

func FormatResponse(w http.ResponseWriter, output *typedvalues.TypedValue, outputHeaders *typedvalues.TypedValue, outputErr *types.Error)

func ParseConsentId

func ParseConsentId(req *http.Request) (string, error)

A temporary hack to extract the 'x-consent header from the request headers as required by the data-flow consent validation service

func ParseRequest

func ParseRequest(req *http.Request) (map[string]*typedvalues.TypedValue, error)

func ParseResponse

func ParseResponse(resp *http.Response) (*typedvalues.TypedValue, error)

func ParseResponseHeaders

func ParseResponseHeaders(resp *http.Response) *typedvalues.TypedValue

Types

type BytesMapper

type BytesMapper struct {
}

func (*BytesMapper) Format

func (*BytesMapper) Parse

func (p *BytesMapper) Parse(mt *mediatype.MediaType, reader io.Reader) (*typedvalues.TypedValue, error)

type Formatter

type Formatter interface {
	Format(w http.ResponseWriter, body *typedvalues.TypedValue) error
}

type HTTPMapper

type HTTPMapper struct {
	DefaultHTTPMethod string
	ValueTypeResolver func(tv *typedvalues.TypedValue) *mediatype.MediaType
	DefaultMediaType  *mediatype.MediaType
	MediaTypeResolver func(mediaType *mediatype.MediaType) ParserFormatter
}

func (*HTTPMapper) Clone

func (h *HTTPMapper) Clone() *HTTPMapper

func (*HTTPMapper) FormatRequest

func (h *HTTPMapper) FormatRequest(source map[string]*typedvalues.TypedValue, target *http.Request) error

FormatRequest maps a map of typed values to an HTTP request

func (*HTTPMapper) FormatResponse

func (h *HTTPMapper) FormatResponse(w http.ResponseWriter, output *typedvalues.TypedValue, outputHeaders *typedvalues.TypedValue, outputErr *types.Error)

FormatResponse maps an TypedValue to an HTTP response

func (*HTTPMapper) ParseRequest

func (h *HTTPMapper) ParseRequest(req *http.Request) (map[string]*typedvalues.TypedValue, error)

ParseRequest maps a HTTP request to a target map of typedvalues.

func (*HTTPMapper) ParseResponse

func (h *HTTPMapper) ParseResponse(resp *http.Response) (*typedvalues.TypedValue, error)

func (*HTTPMapper) ParseResponseHeaders

func (h *HTTPMapper) ParseResponseHeaders(resp *http.Response) *typedvalues.TypedValue

type JSONMapper

type JSONMapper struct {
}

func (*JSONMapper) Format

func (*JSONMapper) Parse

func (m *JSONMapper) Parse(mt *mediatype.MediaType, reader io.Reader) (*typedvalues.TypedValue, error)

type Parser

type Parser interface {
	Parse(mt *mediatype.MediaType, reader io.Reader) (*typedvalues.TypedValue, error)
}

type ParserFormatter

type ParserFormatter interface {
	Formatter
	Parser
}

type ProtobufMapper

type ProtobufMapper struct{}

func (*ProtobufMapper) Format

func (*ProtobufMapper) Parse

type TextMapper

type TextMapper struct{}

func (*TextMapper) Format

func (*TextMapper) Parse

func (m *TextMapper) Parse(mt *mediatype.MediaType, reader io.Reader) (*typedvalues.TypedValue, error)

Jump to

Keyboard shortcuts

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