respond

package
v1.14.1 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2025 License: Apache-2.0, Apache-2.0 Imports: 17 Imported by: 44

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

View Source
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"},
	},
}
View Source
var BadRequestSimple = ResponseMessage{
	Status: StatusResponse{
		Message: "Bad Request",
		Code:    "400",
	}}

BadRequest is used to inform the user about malformed json body

View Source
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

View Source
var ErrConflict = func(details string) ResponseMessage {
	return ResponseMessage{
		Status: StatusResponse{
			Message: "Conflict",
			Code:    "409",
		},
		Errors: []StatusResponse{
			{Message: "Conflict", Code: "409", Details: details},
		},
	}
}
View Source
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'"},
		},
	}
}
View Source
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

View Source
var ErrNotFoundQuery = ResponseMessage{
	Status: StatusResponse{
		Message: "Not Found",
		Code:    "404",
	},
	Errors: []StatusResponse{
		{Message: "Not Found", Code: "404", Details: "Specific query returned no items"},
	},
}
View Source
var ErrUnprocessableEntity = func(details string) ResponseMessage {
	return ResponseMessage{
		Status: StatusResponse{
			Message: "Unprocessable Entity",
			Code:    "422",
		},
		Errors: []StatusResponse{
			{Message: "Unprocessable Entity", Code: "422", Details: details},
		},
	}
}
View Source
var Forbidden = ResponseMessage{
	Status: StatusResponse{
		Code:    "403",
		Message: "Access to the resource is Forbidden",
	},
}

Forbidden is used to marshal a response

View Source
var InternalServerErrorMessage = ResponseMessage{
	Status: StatusResponse{
		Code:    "500",
		Message: "Internal Server Error",
	},
}

InternalServerErrorMessage is used to marshal a response

View Source
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

View Source
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

View Source
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

View Source
var UnauthorizedMessage = ResponseMessage{
	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

View Source
var UnprocessableEntity = ResponseMessage{
	Status: StatusResponse{
		Code:    "422",
		Message: "Unprocessable Entity",
	},
}

UnprocessableEntity is used to marshal a response

Functions

func CreateResponseMessage

func CreateResponseMessage(message string, code string, contentType string) ([]byte, error)

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

func ParseAcceptHeader(r *http.Request) (string, error)

ParseAcceptHeader parses the accept header to determine the content type

func PrepAppRoutes

func PrepAppRoutes(s *mux.Router, confHandler *ConfHandler, routes []AppRoutes) *mux.Router

PrepAppRoutes is used in apps to prepare app's routes

func ResetCache

func ResetCache(w http.ResponseWriter, r *http.Request, cfg config.Config) []byte

ResetCache resets the cache if it is set

func ValidateDateOnly

func ValidateDateOnly(dateStr string) error

ValidateDateOnly validates an input date if its in the "YYYY-MM-DD" format

func WrapAuthenticate

func WrapAuthenticate(hfn http.Handler, cfg config.Config, routeName string) http.HandlerFunc

WrapAuthenticate handle wrapper to apply authentication

func WrapAuthorize

func WrapAuthorize(hfn http.Handler, cfg config.Config, routeName string) http.HandlerFunc

WrapAuthorize handle wrapper to apply authorization

func WrapValidate

func WrapValidate(hfn http.Handler, cfg config.Config, routeName string) http.HandlerFunc

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

type ConfHandler struct {
	Config config.Config
}

ConfHandler Keeps all the configuration/variables required by all the requests

func (*ConfHandler) Respond

func (confhandler *ConfHandler) Respond(fn func(r *http.Request, cfg config.Config) (int, http.Header, []byte, error)) http.HandlerFunc

Respond will be called to answer to http requests to the PI

type ErrEnum

type ErrEnum int

ErrEnum used as type for enumerations of errors

const (
	//ErrAuthen is Error during authentication
	ErrAuthen ErrEnum = iota
	//ErrAuthor is Error during authorization
	ErrAuthor ErrEnum = iota
	//ErrValidHead is Error during validation
	ErrValidHead ErrEnum = iota
	//ErrValidQuery is Error during validation
	ErrValidQuery ErrEnum = iota
)

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 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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL