wrphttp

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2025 License: Apache-2.0 Imports: 19 Imported by: 0

README

wrphttp

wrphttp provides a library implementing helper utilities for working with WRP messages over HTTP.

Build Status codecov Go Report Card Apache V2 License GitHub Release GoDoc

Table of Contents

Code of Conduct

This project and everyone participating in it are governed by the XMiDT Code Of Conduct. By participating, you agree to this Code.

Examples

To use the wrphttp library, it first should be added as an import in the file you plan to use it. Examples can be found at the top of the GoDoc.

Contributing

Refer to CONTRIBUTING.md.

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

View Source
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

func DecodeRequest(req *http.Request, validators ...wrp.Processor) ([]wrp.Union, error)

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

func DecodeResponse(resp *http.Response, validators ...wrp.Processor) ([]wrp.Union, error)

DecodeResponse converts an http.Response into the provided wrp messages if applicable. This will handle any of the valid forms the encoder can produce.

func NegotiateMediaType added in v0.1.0

func NegotiateMediaType(r *http.Request) (string, error)

NegotiateMediaType examines the headers of the request and returns the media type and style the request wants in response.

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

func NewEncoder(opts ...Option) (*Encoder, error)

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

func (e *Encoder) NewRequest(method, url string, msgs ...wrp.Union) (*http.Request, error)

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.

func (*Encoder) ToParts

func (e *Encoder) ToParts(msgs ...wrp.Union) (http.Header, io.Reader, error)

ToParts encodes the provided messages into a mixed response body that can be used in a http.Response. The messages are encoded using the Encoder's media type and compression.

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

func AsMediaType(s string) Option

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

func AsNegotiated(r *http.Request) Option

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

func AsOctetStream(style ...string) Option

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

func CompatibilityMode(enabled ...bool) Option

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

func EncodeDeflate(level ...int) Option

EncodeDeflate uses the deflate compressor with the specified compression level.

func EncodeGzip

func EncodeGzip(level ...int) Option

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

func EncodeValidators(v ...wrp.Processor) Option

EncodeValidators sets the validators for the encoder.

func EncodeZlib

func EncodeZlib(level ...int) Option

EncodeZlib uses the zlib compressor with the specified compression level.

func WithMaxItemsPerChunk

func WithMaxItemsPerChunk(maxItems int) Option

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.

Jump to

Keyboard shortcuts

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