Documentation ¶
Overview ¶
Package represent provides a plug-in mechanism for serialization formats and matches against the Accept and Content-Type headers of http.Requests to make it easy to support a variety of representations of HTTP resources.
Index ¶
- Variables
- func DecodeRequest(container interface{}, r *http.Request) (int, error)
- func EncodeResponse(data interface{}, r *http.Request, w http.ResponseWriter) (int, error)
- func Register(p Protocol)
- func SetDefault(contentType string)
- type Protocol
- type Registry
- func (reg *Registry) DecodeRequest(container interface{}, r *http.Request) (int, error)
- func (reg *Registry) EncodeResponse(data interface{}, r *http.Request, w http.ResponseWriter) (int, error)
- func (reg *Registry) Match(header string) (Protocol, error)
- func (reg *Registry) Register(p Protocol)
- func (reg *Registry) SetDefault(contentType string)
Constants ¶
This section is empty.
Variables ¶
var ErrNoMatch = errors.New("no protocol matched the request")
ErrNoMatch is for when no suitable protocol is found
Functions ¶
func DecodeRequest ¶
DecodeRequest uses an appropriate protocol to decode an http request body into a container. It returns a suggested HTTP response code and an error (if the error is nil the response code will be StatusOK, 200).
func EncodeResponse ¶
EncodeResponse will serialize data with a format acceptable by the HTTP client (using the Accept header of the request). If there is an error it will NOT send the response but will instead return a suggested HTTP response code and the error. If it returns (200, nil) then the data has already been written to the response.
func Register ¶
func Register(p Protocol)
Register sets a Protocol implementation as the handler for its content type.
func SetDefault ¶
func SetDefault(contentType string)
SetDefault sets the content type to prefer in the event of match ties (especially because the Accept header contained */*).
Types ¶
type Protocol ¶
type Protocol interface { // ContentType returns the content-type this Protocol handles ContentType() string // Decode reads content from a Reader and deserializes it into a container Decode(interface{}, io.Reader) error // Encode serializes an object to a Writer Encode(interface{}, io.Writer) error }
Protocol is the plug-in interface for a specific serialization format implementation. These can be created and registered with the global registry to make them eligible for selection based on request Accept headers.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry is a container for Protocols
func (*Registry) DecodeRequest ¶
DecodeRequest as a Registry method performs the same job as the global function, but using the protocols registered with a specific registry.
func (*Registry) EncodeResponse ¶
func (reg *Registry) EncodeResponse(data interface{}, r *http.Request, w http.ResponseWriter) (int, error)
EncodeResponse performs the same job as the global function, but matches against the set of Protocols registered with the specific registry.
func (*Registry) Match ¶
Match on a registry performs the same operation as the Match function, just matches against the set of Protocols registered on the specific registry.
func (*Registry) Register ¶
Register stores a Protocol as a content-type handler on a registry instance.
func (*Registry) SetDefault ¶
SetDefault sets the default content type for a specific registry.
Directories ¶
Path | Synopsis |
---|---|
Package json implements the registry.Protocol interface, and registers it with the default registry.
|
Package json implements the registry.Protocol interface, and registers it with the default registry. |
Package msgpack implements the registry.Protocol interface, and registers it with the default registry.
|
Package msgpack implements the registry.Protocol interface, and registers it with the default registry. |
Package xml implements the registry.Protocol interface, and registers it with the default registry.
|
Package xml implements the registry.Protocol interface, and registers it with the default registry. |
Package yaml implements the registry.Protocol interface, and registers it with the default registry.
|
Package yaml implements the registry.Protocol interface, and registers it with the default registry. |