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 ¶
- Variables
- func FormatRequest(source map[string]*typedvalues.TypedValue, target *http.Request) error
- func FormatResponse(w http.ResponseWriter, output *typedvalues.TypedValue, ...)
- func ParseConsentId(req *http.Request) (string, error)
- func ParseRequest(req *http.Request) (map[string]*typedvalues.TypedValue, error)
- func ParseResponse(resp *http.Response) (*typedvalues.TypedValue, error)
- func ParseResponseHeaders(resp *http.Response) *typedvalues.TypedValue
- type BytesMapper
- type Formatter
- type HTTPMapper
- func (h *HTTPMapper) Clone() *HTTPMapper
- func (h *HTTPMapper) FormatRequest(source map[string]*typedvalues.TypedValue, target *http.Request) error
- func (h *HTTPMapper) FormatResponse(w http.ResponseWriter, output *typedvalues.TypedValue, ...)
- func (h *HTTPMapper) ParseRequest(req *http.Request) (map[string]*typedvalues.TypedValue, error)
- func (h *HTTPMapper) ParseResponse(resp *http.Response) (*typedvalues.TypedValue, error)
- func (h *HTTPMapper) ParseResponseHeaders(resp *http.Response) *typedvalues.TypedValue
- type JSONMapper
- type Parser
- type ParserFormatter
- type ProtobufMapper
- type TextMapper
Constants ¶
This section is empty.
Variables ¶
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") )
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 ¶
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 (p *BytesMapper) Format(w http.ResponseWriter, body *typedvalues.TypedValue) error
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 (m *JSONMapper) Format(w http.ResponseWriter, body *typedvalues.TypedValue) error
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 ProtobufMapper ¶
type ProtobufMapper struct{}
func (*ProtobufMapper) Format ¶
func (p *ProtobufMapper) Format(w http.ResponseWriter, body *typedvalues.TypedValue) error
func (*ProtobufMapper) Parse ¶
func (p *ProtobufMapper) Parse(mt *mediatype.MediaType, reader io.Reader) (*typedvalues.TypedValue, error)
type TextMapper ¶
type TextMapper struct{}
func (*TextMapper) Format ¶
func (m *TextMapper) Format(w http.ResponseWriter, body *typedvalues.TypedValue) error
func (*TextMapper) Parse ¶
func (m *TextMapper) Parse(mt *mediatype.MediaType, reader io.Reader) (*typedvalues.TypedValue, error)