Documentation
¶
Index ¶
- Variables
- func CreateResponseMessage(message string, code string, contentType string) ([]byte, error)
- func Error(w http.ResponseWriter, r *http.Request, errType ErrEnum, cfg config.Config, ...)
- func MarshalContent(doc interface{}, contentType string, prefix string, indent string) ([]byte, error)
- func ParseAcceptHeader(r *http.Request) (string, error)
- func PrepAppRoutes(s *mux.Router, confHandler *ConfHandler, routes []AppRoutes) *mux.Router
- func ResetCache(w http.ResponseWriter, r *http.Request, cfg config.Config) []byte
- func ValidateDateOnly(dateStr string) error
- func WrapAuthenticate(hfn http.Handler, cfg config.Config, routeName string) http.HandlerFunc
- func WrapAuthorize(hfn http.Handler, cfg config.Config, routeName string) http.HandlerFunc
- func WrapValidate(hfn http.Handler, cfg config.Config, routeName string) http.HandlerFunc
- type AppRoutes
- type ConfHandler
- type ErrEnum
- type ErrorResponse
- type ResponseMessage
- type SelfLinks
- type SelfReference
- type StatusResponse
Constants ¶
This section is empty.
Variables ¶
var BadRequestBadJSON = ResponseMessage{ Status: StatusResponse{ Message: "Bad Request", Code: "400", Details: "Request Body contains malformed JSON, thus rendering the Request Bad", }}
BadRequestBadJson is used to inform the user about malformed json body
var BadRequestInvalidJSON = ResponseMessage{ Status: StatusResponse{ Message: "Bad Request", Code: "400", }, Errors: []StatusResponse{ {Message: "Bad Request", Code: "400", Details: "Request Body contains malformed JSON, thus rendering the Request Bad"}, }, }
var BadRequestSimple = ResponseMessage{ Status: StatusResponse{ Message: "Bad Request", Code: "400", }}
BadRequest is used to inform the user about malformed json body
var ErrBadRequestDetails = func(details string) ResponseMessage { return ResponseMessage{ Status: StatusResponse{ Message: "Bad Request", Code: "400", }, Errors: []StatusResponse{ {Message: "Bad Request", Code: "400", Details: details}, }, } }
ErrBadRequestDetails provides an error messsage with specific details
var ErrConflict = func(details string) ResponseMessage { return ResponseMessage{ Status: StatusResponse{ Message: "Conflict", Code: "409", }, Errors: []StatusResponse{ {Message: "Conflict", Code: "409", Details: details}, }, } }
var ErrNotAcceptableContentType = func() ResponseMessage { return ResponseMessage{ Status: StatusResponse{ Message: "Not Acceptable Content Type", Code: "406", }, Errors: []StatusResponse{ {Message: "Conflict", Code: "406", Details: "Accept header provided did not contain any valid content types. Acceptable content types are 'application/xml' and 'application/json'"}, }, } }
var ErrNotFound = ResponseMessage{ Status: StatusResponse{ Message: "Not Found", Code: "404", }, Errors: []StatusResponse{ {Message: "Not Found", Code: "404", Details: "item with the specific ID was not found on the server"}, }, }
NotFound is used to inform the user about not found item
var ErrNotFoundQuery = ResponseMessage{ Status: StatusResponse{ Message: "Not Found", Code: "404", }, Errors: []StatusResponse{ {Message: "Not Found", Code: "404", Details: "Specific query returned no items"}, }, }
var ErrUnprocessableEntity = func(details string) ResponseMessage { return ResponseMessage{ Status: StatusResponse{ Message: "Unprocessable Entity", Code: "422", }, Errors: []StatusResponse{ {Message: "Unprocessable Entity", Code: "422", Details: details}, }, } }
var Forbidden = ResponseMessage{ Status: StatusResponse{ Code: "403", Message: "Access to the resource is Forbidden", }, }
Forbidden is used to marshal a response
var InternalServerErrorMessage = ResponseMessage{ Status: StatusResponse{ Code: "500", Message: "Internal Server Error", }, }
InternalServerErrorMessage is used to marshal a response
var MalformedJSONInput = ResponseMessage{ Status: StatusResponse{ Code: "400", Message: "Malformated json input data", Details: "Check that your json input is valid", }, }
MalformedJSONInput is used to marshal a response when user json input is malformed
var NotAcceptableContentType = ResponseMessage{ Status: StatusResponse{ Message: "Not Acceptable Content Type", Code: "406", Details: "Accept header provided did not contain any valid content types. Acceptable content types are 'application/xml' and 'application/json'", }}
NotAcceptableContentType is used to inform the user about incorrect Accept header and can be marshaled to xml and json
var NotFound = ResponseMessage{ Status: StatusResponse{ Message: "Not Found", Code: "404", Details: "item with the specific ID was not found on the server", }, }
NotFound is used to inform the user about not found item
Status: StatusResponse{ Message: "Unauthorized", Code: "401", Details: "You need to provide a correct authentication token using the header 'x-api-key'", }}
UnauthorizedMessage is used to inform the user about incorrect api key and can be marshaled to xml and json
var UnprocessableEntity = ResponseMessage{ Status: StatusResponse{ Code: "422", Message: "Unprocessable Entity", }, }
UnprocessableEntity is used to marshal a response
Functions ¶
func CreateResponseMessage ¶
CreateResponseMessage creates an output using the parameters given and the correct marshaller according to the contetnType
func Error ¶
func Error(w http.ResponseWriter, r *http.Request, errType ErrEnum, cfg config.Config, errs []ErrorResponse)
Error responds immediately when errors arise in handler chain
func MarshalContent ¶
func MarshalContent(doc interface{}, contentType string, prefix string, indent string) ([]byte, error)
MarshalContent marshals content using the marshaler that corresponds to the contentType parameter
func ParseAcceptHeader ¶
ParseAcceptHeader parses the accept header to determine the content type
func PrepAppRoutes ¶
PrepAppRoutes is used in apps to prepare app's routes
func ResetCache ¶
ResetCache resets the cache if it is set
func ValidateDateOnly ¶
ValidateDateOnly validates an input date if its in the "YYYY-MM-DD" format
func WrapAuthenticate ¶
WrapAuthenticate handle wrapper to apply authentication
func WrapAuthorize ¶
WrapAuthorize handle wrapper to apply authorization
func WrapValidate ¶
WrapValidate handle wrapper to apply validation
Types ¶
type AppRoutes ¶
type AppRoutes struct {
Name string
Verb string
Path string
SubrouterHandler func(r *http.Request, cfg config.Config) (int, http.Header, []byte, error)
}
AppRoutes holds
type ConfHandler ¶
ConfHandler Keeps all the configuration/variables required by all the requests
type ErrorResponse ¶
type ErrorResponse struct {
Message string `xml:"message,omitempty" json:"message,omitempty"`
Code string `xml:"code,omitempty" json:"code,omitempty"`
Details string `xml:"details,omitempty" json:"details,omitempty"`
}
ErrorResponse holds a list of error objects
func ValidateAcceptHeader ¶
func ValidateAcceptHeader(accept string) ErrorResponse
ValidateAcceptHeader parses the accept header to determine the content type
func ValidateMetricParams ¶
func ValidateMetricParams(queries url.Values) []ErrorResponse
ValidateMetricParams validates the parameters required for the metric call
func ValidateResultsParams ¶
func ValidateResultsParams(queries url.Values) []ErrorResponse
ValidateResultsParams validates the input parameters required for the result related calls
func ValidateStatusParams ¶
func ValidateStatusParams(queries url.Values) []ErrorResponse
ValidateStatusParams validates the input parameters required for the status related calls
type ResponseMessage ¶
type ResponseMessage struct {
XMLName xml.Name `xml:"root" json:"-"`
Status StatusResponse `xml:"status,omitempty" json:"status,omitempty"`
Data interface{} `xml:"data>result,omitempty" json:"data,omitempty"`
Errors interface{} `xml:"errors>error,omitempty" json:"errors,omitempty"`
}
ResponseMessage is used to construct and marshal correctly response messages
func CreateFailureResponseMessage ¶
func CreateFailureResponseMessage(message string, code string, errs []ErrorResponse) ResponseMessage
CreateFailureResponseMessage creates a response message struct intance to represent all the errors that occured during the request
func (ResponseMessage) MarshalTo ¶
func (resp ResponseMessage) MarshalTo(contentType string) []byte
MarshalTo takes a reponse message and uses the requested marshaller to render it into the desired content type
type SelfLinks ¶
type SelfLinks struct {
Self string `xml:"self" json:"self"`
}
SelfLinks struct to acoomodate link inside the SelfReference
type SelfReference ¶
type SelfReference struct {
ID string `xml:"id" json:"id" bson:"id,omitempty"`
Links SelfLinks `xml:"links" json:"links"`
}
SelfReference struct for self referencing resource after they are created
type StatusResponse ¶
type StatusResponse struct {
Message string `xml:"message,omitempty" json:"message,omitempty"`
Code string `xml:"code,omitempty" json:"code,omitempty"`
Details string `xml:"details,omitempty" json:"details,omitempty"`
}
StatusResponse accompanies the ResponseMessage struct to construct a response