responses

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2020 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package responses handles common response logic and helpers for constructing and managing responses.

Index

Constants

View Source
const (
	TextPlainUTF8ContentType = "text/plain; charset=utf-8"
	JSONContentType          = "application/json; charset=utf-8"
)

Variables

View Source
var ContentTypeHeader = textproto.CanonicalMIMEHeaderKey("Content-Type")

ContentTypeHeader is the Content-Type header ran through the STD library formatter.

Functions

func DefaultContentTypeForEncoding

func DefaultContentTypeForEncoding(encodingType EncodingType) string

DefaultContentTypeForEncoding will return the actual encoding-type string for use in a Content-Type header.

Types

type AdditionalAttribute

type AdditionalAttribute func(*Builder) error

AdditionalAttribute is an attribute added to a builder either after creation or on creation.

func AddHeader

func AddHeader(key, value string, otherValues ...string) AdditionalAttribute

AddHeader adds a new value to the headers. If the header key exists it will add the provided header value(s) after the existing values.

func Body

func Body(data ...interface{}) AdditionalAttribute

Body sets the body to the value(s) provided. If multiple values are provided a top-level array is assumed.

func Encoding

func Encoding(encodingType EncodingType) AdditionalAttribute

func Headers

func Headers(headers map[string][]string) AdditionalAttribute

Headers will merge provided headers with the existing headers. Keys that exist both in the existing headers and provided headers will use the value provided by the new headers over the existing headers.

func OverrideContentType added in v0.3.3

func OverrideContentType(contentType string) AdditionalAttribute

func Status

func Status(statusCode int) AdditionalAttribute

Status will set the status code for the response.

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder is a helper struct for constructing a Data struct for a transaction response.

func NewBuilder

func NewBuilder(
	ctx context.Context,
	encoding EncodingType,
	additionals ...AdditionalAttribute,
) (*Builder, error)

NewBuilder generates a new builder to help generate a response Data struct.

func (*Builder) Abort

func (self *Builder) Abort(
	statusCode int,
	codedError neterr.CodedError,
	otherErrors ...neterr.CodedError,
) Data

Abort generates a failure response.

func (*Builder) AddHeader

func (self *Builder) AddHeader(key, value string, otherValues ...string)

AddHeader appends the value(s) provided to the header with the provided key. If no header with the provided key exists it is added with the provided values.

func (*Builder) AddHeaders

func (self *Builder) AddHeaders(headers map[string][]string)

AddHeaders will merge provided headers with the existing headers. Keys that exist both in the existing headers and provided headers will append the new values after the existing values.

func (*Builder) ChangeContext added in v0.2.2

func (self *Builder) ChangeContext(ctx context.Context)

ChangeContext changes the existing context on the builder to the provided one. Use this with caution.

func (*Builder) Finish

func (self *Builder) Finish(additionals ...AdditionalAttribute) Data

Finish finishes the builder generating a Data struct to return as a response.

func (*Builder) ReplaceHeaders

func (self *Builder) ReplaceHeaders(headers map[string][]string)

ReplaceHeaders replaces all the existing headers with the provided headers.

func (*Builder) SetBody

func (self *Builder) SetBody(data ...interface{})

SetBody sets the body to the provided data. If multiple values are provided, a top-level array is assumed.

func (*Builder) SetContentType

func (self *Builder) SetContentType(contentType string)

SetContentType sets the content type for the response.

func (*Builder) SetEncoding

func (self *Builder) SetEncoding(encoding EncodingType)

SetEncoding sets the encoding for the response.

func (*Builder) SetHeader

func (self *Builder) SetHeader(key, value string, otherValues ...string)

SetHeader will set the header with provided key to the provided value(s). If a header with the key provided already exists it will be overriden by the value(s) provided.

func (*Builder) SetHeaders

func (self *Builder) SetHeaders(headers map[string][]string)

SetHeaders will merge provided headers with the existing headers. Keys that exist both in the existing headers and provided headers will use the value provided by the new headers over the existing headers.

func (*Builder) SetStatus

func (self *Builder) SetStatus(statusCode int)

SetStatus sets the status for the response.

type ContentType

type ContentType string

ContentType is a representation of a content type header value.

type ContentTyper added in v0.3.0

type ContentTyper interface {
	ContentType(string) string
}

type Data

type Data struct {
	Headers    map[string][]string
	Body       []byte
	StatusCode int
	// contains filtered or unexported fields
}

Data is a representation of the route transaction's response.

func Abort

func Abort(
	ctx context.Context,
	encoding EncodingType,
	statusCode int,
	codedError neterr.CodedError,
	otherErrors ...neterr.CodedError,
) Data

Abort generates an abort Data struct for responding with an error.

func ErrorResponse

func ErrorResponse(err error) Data

ErrorResponse returns a response Data from an error. This is for fatal abortions, generally avoid this in favor of returning a neterr.CodedError with abort instead for better client experience.

func Respond

func Respond(
	ctx context.Context,
	encoding EncodingType,
	statusCode int,
	additionals ...AdditionalAttribute,
) Data

Respond is a shortcut for generating a response Data struct via a builder all at once.

func (Data) Error

func (self Data) Error() error

Error returns and unexpected errors that may have happened.

func (Data) Write

func (self Data) Write(w http.ResponseWriter) error

Write writes the data inside of a Data struct into a response writer.

type EncodingType

type EncodingType int

EncodingType is a way of encoding a response data.

const (
	UnsetEncoding EncodingType = iota
	JSONEncoding
	TextPlainEncoding
)

Jump to

Keyboard shortcuts

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