Documentation ¶
Index ¶
- Constants
- func PrettyPrintJSON() func(*JSON)
- func PrettyPrintXML() func(*XML)
- type APIRenderer
- type ByteRenderer
- type ByteResponder
- type ClientErrRenderer
- type HTTPError
- type JSON
- func (j *JSON) BadRequest(w http.ResponseWriter, err error)
- func (j *JSON) Created(w http.ResponseWriter, v interface{})
- func (j *JSON) InternalServerError(w http.ResponseWriter, err error)
- func (j *JSON) MethodNotAllowed(w http.ResponseWriter, err error)
- func (j *JSON) NoContent(w http.ResponseWriter)
- func (j *JSON) NotFound(w http.ResponseWriter, err error)
- func (j *JSON) Response(w http.ResponseWriter, code int, v interface{})
- func (j *JSON) Send(w http.ResponseWriter, v interface{})
- type OKRenderer
- type Renderer
- type ServerErrRenderer
- type StringRenderer
- type StringResponder
- type XML
- func (x *XML) BadRequest(w http.ResponseWriter, err error)
- func (x *XML) Created(w http.ResponseWriter, v interface{})
- func (x *XML) InternalServerError(w http.ResponseWriter, err error)
- func (x *XML) MethodNotAllowed(w http.ResponseWriter, err error)
- func (x *XML) NoContent(w http.ResponseWriter)
- func (x *XML) NotFound(w http.ResponseWriter, err error)
- func (x *XML) Response(w http.ResponseWriter, code int, v interface{})
- func (x *XML) 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 ¶
This section is empty.
Functions ¶
func PrettyPrintJSON ¶
func PrettyPrintJSON() func(*JSON)
PrettyPrintJSON set JSON encoding indent to DefaultPrettyPrintJSONIndent
func PrettyPrintXML ¶
func PrettyPrintXML() func(*XML)
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 JSON ¶
type JSON struct {
// contains filtered or unexported fields
}
JSON encode the response as "application/json" content type and implement the Renderer and APIRenderer interface.
func (*JSON) BadRequest ¶
func (j *JSON) BadRequest(w http.ResponseWriter, err error)
BadRequest sends a JSON-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 (*JSON) Created ¶
func (j *JSON) Created(w http.ResponseWriter, v interface{})
Created sends a JSON-encoded v in the body of a request with the 201 status code.
func (*JSON) InternalServerError ¶
func (j *JSON) InternalServerError(w http.ResponseWriter, err error)
InternalServerError sends a JSON-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 (*JSON) MethodNotAllowed ¶
func (j *JSON) MethodNotAllowed(w http.ResponseWriter, err error)
MethodNotAllowed sends a JSON-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 (*JSON) NoContent ¶
func (j *JSON) NoContent(w http.ResponseWriter)
NoContent sends a v without no content with the 204 status code.
func (*JSON) NotFound ¶
func (j *JSON) NotFound(w http.ResponseWriter, err error)
NotFound sends a JSON-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 (*JSON) Response ¶
func (j *JSON) Response(w http.ResponseWriter, code int, v interface{})
Response sends a JSON-encoded v in the body of a request with the HTTP status code.
func (*JSON) Send ¶
func (j *JSON) Send(w http.ResponseWriter, v interface{})
Send sends a JSON-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 interface 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 XML ¶
type XML struct {
// contains filtered or unexported fields
}
XML encode the response as "application/xml" content type and implement the Renderer and APIRenderer interface.
func (*XML) BadRequest ¶
func (x *XML) 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 (*XML) Created ¶
func (x *XML) Created(w http.ResponseWriter, v interface{})
Created sends a XML-encoded v in the body of a request with the 201 status code.
func (*XML) InternalServerError ¶
func (x *XML) 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 (*XML) MethodNotAllowed ¶
func (x *XML) 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 (*XML) NoContent ¶
func (x *XML) NoContent(w http.ResponseWriter)
NoContent sends a v without no content with the 204 status code.
func (*XML) NotFound ¶
func (x *XML) 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 (*XML) Response ¶
func (x *XML) 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 (*XML) Send ¶
func (x *XML) Send(w http.ResponseWriter, v interface{})
Send sends a XML-encoded v in the body of a request with the 200 status code.