jsonapi

package
Version: v0.0.0-...-26839bd Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2015 License: MPL-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadRequest           = &Err{"bad_request", 400, "Bad request", "Request body is not well-formed. It must be JSON."}
	ErrUnauthorized         = &Err{"unauthorized", 401, "Unauthorized", "Access token is invalid."}
	ErrNotFound             = &Err{"not_found", 404, "Not found", "Route not found."}
	ErrNotAcceptable        = &Err{"not_acceptable", 406, "Not acceptable", "Accept HTTP header must be \"application/vnd.api+json\"."}
	ErrUnsupportedMediaType = &Err{"unsupported_media_type", 415, "Unsupported Media Type", "Content-Type header must be \"application/vnd.api+json\"."}
	ErrInternalServer       = &Err{"internal_server_error", 500, "Internal Server Error", "Something went wrong."}
)

Functions

func AcceptHandler

func AcceptHandler(next http.Handler) http.Handler

Returns an error if the Accept HTTP header is not "application/vnd.api+json".

func Body

func Body(r *http.Request) interface{}

Body(r *http.Request) is a function to get the decoded body from the request context

func BodyHandler

func BodyHandler(v interface{}) func(http.Handler) http.Handler

Decodes a request body into the struct passed to the middleware. If the request body is not JSON, it will return a 400 Bad Request error. Stores the decoded body into a context object.

func ContentTypeHandler

func ContentTypeHandler(next http.Handler) http.Handler

Returns an error if the Content-Type HTTP header is not "application/vnd.api+json".

func Error

func Error(w http.ResponseWriter, err *Err)

Encodes the error into JSON, sets the adequate Content-Type HTTP header and set the status code related to the error.

func LoggingHandler

func LoggingHandler(next http.Handler) http.Handler

Logs every request.

func New

func New() *mux.Mux

New() returns a new mux with handlers useful to create JSON APIs.

func RecoverHandler

func RecoverHandler(next http.Handler) http.Handler

If the code panics, it logs the error and returns a 500 Internal Server Error error.

func Write

func Write(w http.ResponseWriter, v interface{})

Encodes the response into JSON and sets the adequate Content-Type HTTP header.

Types

type Err

type Err struct {
	Id     string `json:"id"`
	Status int    `json:"status"`
	Title  string `json:"title"`
	Detail string `json:"detail"`
}

Err contains more information than standard go errors to create useful error messages for API consumers.

func (Err) Error

func (err Err) Error() string

Err implements the error interface.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL