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
- func AllMediaTypes() []string
- func DecodeFromParts(headers http.Header, body io.ReadCloser, validators ...wrp.Processor) ([]wrp.Union, error)
- func DecodeRequest(req *http.Request, validators ...wrp.Processor) ([]wrp.Union, error)
- func DecodeResponse(resp *http.Response, validators ...wrp.Processor) ([]wrp.Union, error)
- func NegotiateMediaType(r *http.Request) (string, error)
- type Encoder
- type Option
- func AsJSON() Option
- func AsJSONL() Option
- func AsMediaType(s string) Option
- func AsMsgpack() Option
- func AsMsgpackL() Option
- func AsNegotiated(r *http.Request) Option
- func AsOctetStream(style ...string) Option
- func CompatibilityMode(enabled ...bool) Option
- func EncodeDeflate(level ...int) Option
- func EncodeGzip(level ...int) Option
- func EncodeNoCompression() Option
- func EncodeValidators(v ...wrp.Processor) Option
- func EncodeZlib(level ...int) Option
- func WithMaxItemsPerChunk(maxItems int) Option
Constants ¶
const ( MEDIA_TYPE_JSON = "application/json" MEDIA_TYPE_MSGPACK = "application/msgpack" MEDIA_TYPE_OCTET_STREAM = "application/octet-stream" MEDIA_TYPE_JSONL = "application/jsonl" MEDIA_TYPE_MSGPACKL = "application/msgpackl" // These are the styles that are supported for octet-stream MEDIA_TYPE_OCTET_STREAM_X_XMIDT_STYLE = "application/octet-stream; style=x-xmidt" MEDIA_TYPE_OCTET_STREAM_X_MIDT_STYLE = "application/octet-stream; style=x-midt" MEDIA_TYPE_OCTET_STREAM_XMIDT_STYLE = "application/octet-stream; style=xmidt" MEDIA_TYPE_OCTET_STREAM_WEBPA_STYLE = "application/octet-stream; style=x-webpa" )
Variables ¶
This section is empty.
Functions ¶
func AllMediaTypes ¶ added in v0.0.2
func AllMediaTypes() []string
AllMediaTypes returns a list of all the media types supported by the encoder. This allows new formats to be added in the future without breaking existing code. The list is not guaranteed to be in any particular order.
func DecodeFromParts ¶
func DecodeFromParts(headers http.Header, body io.ReadCloser, validators ...wrp.Processor) ([]wrp.Union, error)
DecodeFromParts converts an http.Header and io.ReadCloser into the provided wrp messages if applicable. This will handle any of the valid forms the encoder can produce.
func DecodeRequest ¶
DecodeRequest converts an http.Request into the provided wrp messages if applicable. This will handle any of the valid forms the encoder can produce.
func DecodeResponse ¶
DecodeResponse converts an http.Response into the provided wrp messages if applicable. This will handle any of the valid forms the encoder can produce.
Types ¶
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder contains the options used for encoding new http.Request and http.Response objects. The Encoder is not safe for concurrent use.
func NewEncoder ¶
NewEncoder creates a new Encoder with the provided options. The options are applied in the order they are provided. If no options are provided, the default options are used. The Encoder is not safe for concurrent use.
The default options are: - AsMsgpack() - EncodeNoCompression() - WithMaxItemsPerChunk(0)
func (*Encoder) NewRequest ¶
NewRequest creates a new http.Request with the provided method, URL, and messages. The messages are encoded using the Encoder's media type and compression. The request is not sent and the body is not closed.
func (*Encoder) NewRequestWithContext ¶
func (e *Encoder) NewRequestWithContext(ctx context.Context, method, url string, msgs ...wrp.Union) (*http.Request, error)
NewRequestWithContext creates a new http.Request with the provided context in addition to the method, URL, and messages. The messages are encoded using the Encoder's media type and compression. The request is not sent and the body is not closed.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option is a functional option for configuring the Encoder. The options are applied in the order they are provided.
func AsJSON ¶
func AsJSON() Option
AsJSON sets the encoder to use JSON encoding for WRP messages. A single message is encoded with the body as the wrp.Message encoded as JSON with the Content-Type set to "application/wrp+json".
If multiple messages are provided, a multipart message is created with each message as a separate part. The Content-Type of the message is set to "application/wrp+json".
func AsJSONL ¶
func AsJSONL() Option
AsJSONL sets the encoder to use JSONL encoding for WRP messages. All provided messages are encoded as a single JSONL document up until the MaxItemsPerChunk() limit is reached. If the limit is reached, a multipart message is created with each array of messages as a separate part. The Content-Type of each part is set to "application/wrp+jsonl".
func AsMediaType ¶ added in v0.1.0
AsMediaType sets the encoder to use the specified media type. The media type
func AsMsgpack ¶
func AsMsgpack() Option
AsMsgpack sets the encoder to use Msgpack encoding for WRP messages. A single message is encoded with the body as the wrp.Message encoded as Msgpack with the Content-Type set to "application/wrp+msgpack".
If multiple messages are provided, a multipart message is created with each message as a separate part. The Content-Type of the message is set to "application/wrp+msgpack".
func AsMsgpackL ¶
func AsMsgpackL() Option
AsMsgpackL sets the encoder to use MsgpackL encoding for WRP messages. All provided messages are encoded as a single MsgpackL document up until the MaxItemsPerChunk() limit is reached. If the limit is reached, a multipart message is created with each array of messages as a separate part. The Content-Type of each part is set to "application/wrp+msgpackl".
func AsNegotiated ¶ added in v0.1.0
AsNegotiated sets the encoder to use the negotiated media type from the request. This is useful for ensuring that the encoder is compatible with the negotiated media type from the request.
func AsOctetStream ¶
AsOctetStream sets the encoder to use octet-stream encoding for WRP messages. This is useful for raw payloads without any encoding and the additional wrp fields as headers. The optional style parameter can be used to specify the style of the header.
The valid styles are:
- "X-Xmidt"
- "X-Midt"
- "Xmidt"
- "X-Webpa" default & best for backward compatibility
The Content-Type of the message is set to "application/wrp+octet-stream". If multiple messages are provided, a multipart message is created with each message as a separate part. The Content-Type of the message is set to "application/wrp+octet-stream".
func CompatibilityMode ¶ added in v0.1.0
CompatibilityMode sets the encoder to use compatibility mode. This is useful for ensuring that the encoder is compatible with older versions of the WRP protocol that may not support content-type values with the style parameters. The default value is false.
func EncodeDeflate ¶
EncodeDeflate uses the deflate compressor with the specified compression level.
func EncodeGzip ¶
EncodeGzip uses the gzip compressor with the specified compression level.
func EncodeNoCompression ¶
func EncodeNoCompression() Option
EncodeNoCompression sets the encoder to not use any compression. This is the default behavior.
func EncodeValidators ¶
EncodeValidators sets the validators for the encoder.
func EncodeZlib ¶
EncodeZlib uses the zlib compressor with the specified compression level.
func WithMaxItemsPerChunk ¶
WithMaxItemsPerChunk sets the maximum number of items per chunk for the encoder. This is useful for controlling the size of the chunks when encoding large payloads. The default value is 1000. If set to less than 0, the encoder will not chunk the payload if possible.