package
0.0.0-20200628105730-7179885c4902
Repository: https://github.com/exlinc/golang-utils.git
Documentation: pkg.go.dev
# README
Utils for JSON APIs over HTTP
A suite of standardized functions for building REST APIs over HTTP
Usage
Request Payloads
The library is flexible here. They are simply passed into the JSONDecodeAndCatchForAPI
function for parsing. For 'checkable' payloads, that struct must fulfil the CheckableRequest
interface.
Response Payloads
Response payloads are always of the APIResponse
type:
// APIResponse contains the attributes found in an API response
type APIResponse struct {
Message string `json:"message"`
Success bool `json:"success"`
Data interface{} `json:"data"`
Debug string `json:"debug,omitempty"`
}
Using the response payload utils
Use the JSON*
functions to send your messages, data, and debug through helpers that will package up APIResponse
objects, marshal them to JSON, and then send them on your HTTP writer
# Functions
JSONInternalError returns a bad request error APIResponse on the http response with the provided parameters.
JSONDecodeAndCatchForAPI is the primary function for decoding checkable (and non-checkable) payloads into structs.
JSONDecodeNoCatch decodes checkable (and non-checkable) payloads into structs.
JSONDetailed returns the provided APIResponse on the http response with the provided HTTP status code.
JSONError returns an APIResponse on the http response with the provided parameters and status code.
JSONForbiddenError returns a forbidden error APIResponse on the http response with the provided parameters.
JSONInternalError returns an internal server error APIResponse on the http response with the provided parameters.
JSONNotFoundError returns a not found error APIResponse on the http response with the provided parameters.
JSONSuccess returns a successful APIResponse on the http response with the provided parameters.
JSONWriter provides a wrapper function to marshal an interface{} type to JSON and then send the bytes back over an http.ResponseWriter.
# Structs
APIResponse contains the attributes found in an API response.
# Interfaces
CheckableRequest defines an interface for request payloads that can be checked with the jsonhttp checker.