Documentation
¶
Index ¶
- Constants
- Variables
- func PrettyPrintJSON() func(*JSONRender)
- func PrettyPrintXML() func(*XMLRenderer)
- type APIRenderer
- type ByteRenderer
- type ByteResponder
- type ClientErrRenderer
- type HTTPError
- type JSONRender
- func (j *JSONRender) BadRequest(w http.ResponseWriter, err error)
- func (j *JSONRender) Created(w http.ResponseWriter, v interface{})
- func (j *JSONRender) InternalServerError(w http.ResponseWriter, err error)
- func (j *JSONRender) MethodNotAllowed(w http.ResponseWriter, err error)
- func (j *JSONRender) NoContent(w http.ResponseWriter)
- func (j *JSONRender) NotFound(w http.ResponseWriter, err error)
- func (j *JSONRender) Response(w http.ResponseWriter, code int, v interface{})
- func (j *JSONRender) Send(w http.ResponseWriter, v interface{})
- type OKRenderer
- type Renderer
- type ServerErrRenderer
- type StringRenderer
- type StringResponder
- type XMLRenderer
- func (x *XMLRenderer) BadRequest(w http.ResponseWriter, err error)
- func (x *XMLRenderer) Created(w http.ResponseWriter, v interface{})
- func (x *XMLRenderer) InternalServerError(w http.ResponseWriter, err error)
- func (x *XMLRenderer) MethodNotAllowed(w http.ResponseWriter, err error)
- func (x *XMLRenderer) NoContent(w http.ResponseWriter)
- func (x *XMLRenderer) NotFound(w http.ResponseWriter, err error)
- func (x *XMLRenderer) Response(w http.ResponseWriter, code int, v interface{})
- func (x *XMLRenderer) Send(w http.ResponseWriter, v interface{})
Constants ¶
const ( // DefaultFindHeaderIndex defines the maximum number of characters // to go through to find a generic XML header. DefaultFindHeaderIndex = 100 // DefaultPrettyPrintXMLindent defines the number of spaces to pretty print a xml DefaultPrettyPrintXMLindent = " " // DefaultPrettyPrintXMLPrefix defines the number of spaces to pretty print a xml DefaultPrettyPrintXMLPrefix = " " )
const DefaultPrettyPrintJSONIndent = " "
DefaultPrettyPrintJSONIndent defines the default number of spaces to pretty print a json.
Variables ¶
var JSON = NewJSON()
JSON is the default JSON renderer
var XML = NewXML()
XML is the default XML renderer
Functions ¶
func PrettyPrintJSON ¶
func PrettyPrintJSON() func(*JSONRender)
PrettyPrintJSON set JSONRender encoding indent to DefaultPrettyPrintJSONIndent
func PrettyPrintXML ¶
func PrettyPrintXML() func(*XMLRenderer)
PrettyPrintXML set XML encoding indent to DefaultPrettyPrintJSONIdent
Types ¶
type APIRenderer ¶
type APIRenderer interface { Renderer OKRenderer ClientErrRenderer ServerErrRenderer }
APIRenderer interface for managing API response payloads.
type ByteRenderer ¶
type ByteRenderer interface { // Response encoded []byte into ResponseWriter with the HTTP status code. Response(w http.ResponseWriter, code int, response []byte) }
ByteRenderer interface manage []byte responses.
type ByteResponder ¶
type ByteResponder string
ByteResponder response []byte with application/octet-stream Content-Type
const ( // Data returns a ByteResponder who's Response writes a string into a ResponseWriter // with the Content-Type as application/octet-stream. Data ByteResponder = "application/octet-stream" )
func (ByteResponder) Response ¶
func (s ByteResponder) Response(w http.ResponseWriter, code int, response []byte)
Response encoded []byte into ResponseWriter with the HTTP status code.
type ClientErrRenderer ¶
type ClientErrRenderer interface { BadRequest(w http.ResponseWriter, err error) NotFound(w http.ResponseWriter, err error) MethodNotAllowed(w http.ResponseWriter, err error) }
ClientErrRenderer interface for managing API responses when client error.
type HTTPError ¶
type HTTPError struct { Message string `json:"message,omitempty" xml:"message,attr,omitempty"` Error string `json:"error,omitempty" xml:"error,attr,omitempty"` Status int `json:"status,omitempty" xml:"status,attr,omitempty"` }
HTTPError represents an error that occurred while handling a request.
func NewHTTPError ¶
NewHTTPError returns a new HTTPError instance.
type JSONRender ¶
type JSONRender struct {
// contains filtered or unexported fields
}
JSONRender encode the response as "application/json" content type It implements the Renderer and APIRenderer interface.
func NewJSON ¶
func NewJSON(opts ...func(*JSONRender)) *JSONRender
NewJSON returns a new JSONRender responder instance.
func (*JSONRender) BadRequest ¶
func (j *JSONRender) BadRequest(w http.ResponseWriter, err error)
BadRequest sends a JSONRender-encoded error response in the body of a request with the 400 status code. The response will contains the status 400 and error "Bad Request".
func (*JSONRender) Created ¶
func (j *JSONRender) Created(w http.ResponseWriter, v interface{})
Created sends a JSONRender-encoded v in the body of a request with the 201 status code.
func (*JSONRender) InternalServerError ¶
func (j *JSONRender) InternalServerError(w http.ResponseWriter, err error)
InternalServerError sends a JSONRender-encoded error response in the body of a request with the 500 status code. The response will contains the status 500 and error "Internal Server Error".
func (*JSONRender) MethodNotAllowed ¶
func (j *JSONRender) MethodNotAllowed(w http.ResponseWriter, err error)
MethodNotAllowed sends a JSONRender-encoded error response in the body of a request with the 405 status code. The response will contains the status 405 and error "Method Not Allowed".
func (*JSONRender) NoContent ¶
func (j *JSONRender) NoContent(w http.ResponseWriter)
NoContent sends a v without no content with the 204 status code.
func (*JSONRender) NotFound ¶
func (j *JSONRender) NotFound(w http.ResponseWriter, err error)
NotFound sends a JSONRender-encoded error response in the body of a request with the 404 status code. The response will contains the status 404 and error "Not Found".
func (*JSONRender) Response ¶
func (j *JSONRender) Response(w http.ResponseWriter, code int, v interface{})
Response sends a JSONRender-encoded v in the body of a request with the HTTP status code.
func (*JSONRender) Send ¶
func (j *JSONRender) Send(w http.ResponseWriter, v interface{})
Send sends a JSONRender-encoded v in the body of a request with the 200 status code.
type OKRenderer ¶
type OKRenderer interface { Send(w http.ResponseWriter, response interface{}) Created(w http.ResponseWriter, response interface{}) NoContent(w http.ResponseWriter) }
OKRenderer interface for managing success API response payloads.
type Renderer ¶
type Renderer interface { // Response encoded responses in the ResponseWriter with the HTTP status code. Response(w http.ResponseWriter, code int, response interface{}) }
Renderer describes the interface which needs to be implemented for managing response payloads.
type ServerErrRenderer ¶
type ServerErrRenderer interface {
InternalServerError(w http.ResponseWriter, err error)
}
ServerErrRenderer interface for managing API responses when server error.
type StringRenderer ¶
type StringRenderer interface { // Response encoded string into ResponseWriter with the HTTP status code. Response(w http.ResponseWriter, code int, response string) }
StringRenderer interface manage string responses.
type StringResponder ¶
type StringResponder string
StringResponder response strings
const ( // Text returns a StringResponder who's Response writes a string into a ResponseWriter // with the Content-Type as text/plain. Text StringResponder = "text/plain; charset=utf-8" // HTML returns a StringResponder who's Response writes a string into a ResponseWriter // with the Content-Type as text/html. HTML StringResponder = "text/html; charset=utf-8" )
func (StringResponder) Response ¶
func (s StringResponder) Response(w http.ResponseWriter, code int, response string)
Response encoded responses in the ResponseWriter with the HTTP status code.
type XMLRenderer ¶
type XMLRenderer struct {
// contains filtered or unexported fields
}
XMLRenderer encode the response as "application/xml" content type and implement the Renderer and APIRenderer interface.
func NewXML ¶
func NewXML(opts ...func(*XMLRenderer)) *XMLRenderer
NewXML returns a new XML responder instance.
func (*XMLRenderer) BadRequest ¶
func (x *XMLRenderer) BadRequest(w http.ResponseWriter, err error)
BadRequest sends a XML-encoded error response in the body of a request with the 400 status code. The response will contains the status 400 and error "Bad Request".
func (*XMLRenderer) Created ¶
func (x *XMLRenderer) Created(w http.ResponseWriter, v interface{})
Created sends a XML-encoded v in the body of a request with the 201 status code.
func (*XMLRenderer) InternalServerError ¶
func (x *XMLRenderer) InternalServerError(w http.ResponseWriter, err error)
InternalServerError sends a XML-encoded error response in the body of a request with the 500 status code. The response will contains the status 500 and error "Internal Server Error".
func (*XMLRenderer) MethodNotAllowed ¶
func (x *XMLRenderer) MethodNotAllowed(w http.ResponseWriter, err error)
MethodNotAllowed sends a XML-encoded error response in the body of a request with the 405 status code. The response will contains the status 405 and error "Method Not Allowed".
func (*XMLRenderer) NoContent ¶
func (x *XMLRenderer) NoContent(w http.ResponseWriter)
NoContent sends a v without no content with the 204 status code.
func (*XMLRenderer) NotFound ¶
func (x *XMLRenderer) NotFound(w http.ResponseWriter, err error)
NotFound sends a XML-encoded error response in the body of a request with the 404 status code. The response will contains the status 404 and error "Not Found".
func (*XMLRenderer) Response ¶
func (x *XMLRenderer) Response(w http.ResponseWriter, code int, v interface{})
Response marshals 'v' to XML, setting the Content-Type as application/xml. It will automatically prepend a generic XML header (see encoding/xml.Header) if one is not found in the first 100 bytes of 'v'.
func (*XMLRenderer) Send ¶
func (x *XMLRenderer) Send(w http.ResponseWriter, v interface{})
Send sends a XML-encoded v in the body of a request with the 200 status code.