Documentation ¶
Overview ¶
Package wrphttp integrates go-kit's transport/http package with the patterns used by WebPA/XMiDT servers. Primarily, this package concerns itself with translating between HTTP requests and responses and WRP requests and responses.
Index ¶
- Constants
- Variables
- func AddMessageHeaders(h http.Header, m *wrp.Message)
- func DecodeRequest(r *http.Request, _ any) (*http.Request, error)
- func DetermineFormat(defaultFormat wrp.Format, h http.Header, names ...string) (wrp.Format, error)
- func NewHTTPHandler(h Handler, options ...Option) http.Handler
- func NewMessageFromHeaders(h http.Header, p io.Reader) (message *wrp.Message, err error)
- func ReadPayload(h http.Header, p io.Reader, m *wrp.Message) (int, error)
- func SetMessageFromHeaders(h http.Header, m *wrp.Message) (err error)
- func WritePayload(h http.Header, p io.Writer, m *wrp.Message) (int, error)
- type Decoder
- type Entity
- type Handler
- type HandlerFunc
- type MessageFunc
- type MockHandler
- type Option
- type Request
- type ResponseWriter
- type ResponseWriterFunc
Constants ¶
const ( MessageTypeHeader = "X-Xmidt-Message-Type" TransactionUuidHeader = "X-Xmidt-Transaction-Uuid" // nolint:gosec StatusHeader = "X-Xmidt-Status" RequestDeliveryResponseHeader = "X-Xmidt-Request-Delivery-Response" IncludeSpansHeader = "X-Xmidt-Include-Spans" SpanHeader = "X-Xmidt-Span" PathHeader = "X-Xmidt-Path" SourceHeader = "X-Xmidt-Source" DestinationHeader = "X-Webpa-Device-Name" AcceptHeader = "X-Xmidt-Accept" MetadataHeader = "X-Xmidt-Metadata" PartnerIdHeader = "X-Xmidt-Partner-Id" SessionIdHeader = "X-Xmidt-Session-Id" HeadersHeader = "X-Xmidt-Headers" ServiceNameHeader = "X-Xmidt-Service-Name" URLHeader = "X-Xmidt-Url" )
Constant HTTP header strings representing WRP fields
Variables ¶
var ( ErrEmptyWRPBytes = errors.New("Encoded WRP bytes were empty.") ErrContentNegotiationMismatch = errors.New("Given format violates content negotiation") )
Functions ¶
func AddMessageHeaders ¶
AddMessageHeaders adds the HTTP header representation of a given WRP message. This function does not handle the payload, to allow further headers to be written by calling code.
func DecodeRequest ¶ added in v3.1.5
DecodeRequest is a Decoder that provides lower-level way of decoding an *http.Request Can work for servers that don't use a wrp.Handler
func DetermineFormat ¶
DetermineFormat examines zero or more headers to determine which WRP format is to be used, either for decoding or encoding. The headers are tried in order, and the first non-empty value that maps to a WRP format is returned. Any non-empty header that is invalid results in an error. If none of the headers are present, this function returns the defaultFormat.
This function can be used with a single header, e.g. DetermineFormat(wrp.Msgpack, header, "Content-Type"). It can also be used for simple content negotiation, e.g. DetermineFormat(wrp.Msgpack, header, "Accept", "Content-Type").
func NewHTTPHandler ¶
NewHTTPHandler creates an http.Handler that forwards WRP requests to the supplied WRP handler.
func NewMessageFromHeaders ¶
NewMessageFromHeaders extracts a WRP message from a set of HTTP headers. If supplied, the given io.Reader is assumed to contain the payload of the WRP message.
func ReadPayload ¶
ReadPayload extracts the payload from a reader, setting the appropriate fields on the given message.
func SetMessageFromHeaders ¶
SetMessageFromHeaders transfers header fields onto the given WRP message. The payload is not handled by this method.
func WritePayload ¶
WritePayload writes the WRP payload to the given io.Writer. If the message has no payload, this function does nothing.
The http.Header is optional. If supplied, the header's Content-Type and Content-Length will be set appropriately.
Types ¶
type Decoder ¶
Decoder turns an HTTP request into a WRP entity.
func DecodeEntity ¶
func DecodeEntity(defaultFormat wrp.Format) Decoder
func DecodeEntityFromSources ¶
func DefaultDecoder ¶
func DefaultDecoder() Decoder
type Entity ¶
type Entity struct { // Message holds the WRP message. Message wrp.Message // Format is the format of the WRP message encoded into the Bytes // field. // (Optional) Format wrp.Format // Bytes is the Format-encoded version of the WRP Message. // It serves as an optimization strategy for WRP handlers // to avoid unnecessary re-encodings. // (Optional) Bytes []byte }
Entity represents a WRP message in transit. Useful for request/response WRP handler exchanges.
type Handler ¶
type Handler interface {
ServeWRP(ResponseWriter, *Request)
}
Handler is a WRP handler for messages over HTTP. This is the analog of http.Handler.
type HandlerFunc ¶
type HandlerFunc func(ResponseWriter, *Request)
func (HandlerFunc) ServeWRP ¶
func (hf HandlerFunc) ServeWRP(w ResponseWriter, r *Request)
type MessageFunc ¶
MessageFunc is a strategy for post-processing a WRP message, adding things to the context or performing other processing on the message itself.
type MockHandler ¶
func (*MockHandler) ServeWRP ¶
func (m *MockHandler) ServeWRP(response ResponseWriter, request *Request)
type Option ¶
type Option func(*wrpHandler)
Option is a configurable option for an HTTP handler that works with WRP
func WithAfter ¶ added in v3.1.4
func WithAfter(funcs ...MessageFunc) Option
func WithBefore ¶
func WithBefore(funcs ...MessageFunc) Option
func WithDecoder ¶
WithDecoder sets a go-kit DecodeRequestFunc strategy that turns an http.Request into a WRP request. By default, DefaultDecoder() is used. If the supplied strategy is nil, it reverts to the default.
func WithErrorEncoder ¶
func WithErrorEncoder(ee gokithttp.ErrorEncoder) Option
WithErrorEncoder establishes a go-kit ErrorEncoder for the given handler. By default, DefaultErrorEncoder is used. If the supplied ErrorEncoder is nil, it reverts to the default.
func WithNewResponseWriter ¶
func WithNewResponseWriter(rwf ResponseWriterFunc) Option
WithNewResponseWriter establishes a factory function for ResponseWriter objects. By default, DefaultResponseWriterFunc() is used. If the supplied strategy function is nil, it reverts to the default.
type Request ¶
type Request struct { // Original is the HTTP request which corresponds to this WRP request. The request body will have // already been read to produce the entity. Original *http.Request // Entity is the decoded WRP message Entity *Entity // contains filtered or unexported fields }
Request wraps an original http.Request and contains WRP message information. Context handling mimics http.Request.
func (*Request) Context ¶
Context returns the context associated with this WRP Request, which is not necessarily the same as the context returned by r.Original.Context(). Use this method instead of the original request.
func (*Request) WithContext ¶
WithContext returns a shallow copy of this WRP Request using the supplied context. The semantics of this method are the same as http.Request.WithContext. Note that the original request's context is not updated via this method.
type ResponseWriter ¶
type ResponseWriter interface { http.ResponseWriter // WriteWRP writes a WRP message to the underlying response. The format used is determined // by the configuration of the underlying implementation. This method is idempotent, and returns // an error if called multiple times for the same instance. WriteWRP(e *Entity) (int, error) // WriteWRPBytes writes a WRP message to the underlying response. The byte array input is assumed // to be the WRP message in the given format. This method is idempotent, and behaves // similarly as WriteWRP. WriteWRPBytes(wrp.Format, []byte) (int, error) // WRPFormat returns the format in which the WRP message response will be written. It is a convenience // function to verify the right format is called with WriteWRPBytes WRPFormat() wrp.Format }
ResponseWriter extends http.ResponseWriter with some WRP behavior.
type ResponseWriterFunc ¶
type ResponseWriterFunc func(http.ResponseWriter, *Request) (ResponseWriter, error)
func DefaultResponseWriterFunc ¶
func DefaultResponseWriterFunc() ResponseWriterFunc
func NewEntityResponseWriter ¶
func NewEntityResponseWriter(defaultFormat wrp.Format) ResponseWriterFunc
NewEntityResponseWriter creates a ResponseWriterFunc that returns an entity-based ResponseWriter. The returned ResponseWriter writes WRP messages to the response body, using content negotiation with a fallback to the supplied default format.