Documentation ¶
Overview ¶
Package responses handles common response logic and helpers for constructing and managing responses.
Index ¶
- Constants
- Variables
- func DefaultContentTypeForEncoding(encodingType EncodingType) string
- type AdditionalAttribute
- func AddHeader(key, value string, otherValues ...string) AdditionalAttribute
- func Body(data ...interface{}) AdditionalAttribute
- func Encoding(encodingType EncodingType) AdditionalAttribute
- func Headers(headers map[string][]string) AdditionalAttribute
- func OverrideContentType(contentType string) AdditionalAttribute
- func Status(statusCode int) AdditionalAttribute
- type Builder
- func (self *Builder) Abort(statusCode int, codedError neterr.CodedError, otherErrors ...neterr.CodedError) Data
- func (self *Builder) AddHeader(key, value string, otherValues ...string)
- func (self *Builder) AddHeaders(headers map[string][]string)
- func (self *Builder) ChangeContext(ctx context.Context)
- func (self *Builder) Finish(additionals ...AdditionalAttribute) Data
- func (self *Builder) ReplaceHeaders(headers map[string][]string)
- func (self *Builder) SetBody(data ...interface{})
- func (self *Builder) SetContentType(contentType string)
- func (self *Builder) SetEncoding(encoding EncodingType)
- func (self *Builder) SetHeader(key, value string, otherValues ...string)
- func (self *Builder) SetHeaders(headers map[string][]string)
- func (self *Builder) SetStatus(statusCode int)
- type ContentType
- type ContentTyper
- type Data
- type EncodingType
Constants ¶
const ( TextPlainUTF8ContentType = "text/plain; charset=utf-8" JSONContentType = "application/json; charset=utf-8" )
Variables ¶
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 ¶
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 ¶
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 ¶
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
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 ¶
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 ¶
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 ¶
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 ¶
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.
type ContentType ¶
type ContentType string
ContentType is a representation of a content type header value.
type ContentTyper ¶ added in v0.3.0
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 ¶
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.
type EncodingType ¶
type EncodingType int
EncodingType is a way of encoding a response data.
const ( UnsetEncoding EncodingType = iota JSONEncoding TextPlainEncoding )