Documentation
¶
Overview ¶
Package apihelper automate construction of http response
Index ¶
- Constants
- Variables
- func ConnectToDB(username string, dbName string, password string, host string) (DB *gorm.DB, err error)
- func DefaultCORSConfig() cors.Config
- func ExtractQueryParams(queryParams map[string]interface{}) (query string, args []interface{})
- func GenerateCleanString(originalString string) (string, error)
- func GenerateRandomString(strLen int) string
- func GetBoolQueryParam(c *gin.Context, value *bool, queryParam string, defaultValue bool) bool
- func GetDateQueryParam(c *gin.Context, queryParam string, dateLayout string, defaultValue string) (string, bool)
- func GetMandatoryIntQueryParam(c *gin.Context, queryParam string) (int, bool)
- func GetMandatoryUintQueryParam(c *gin.Context, queryParam string) (uint, bool)
- func GetStringQueryParam(c *gin.Context, queryParam string, acceptedValues []string, ...) (string, bool)
- func LoggingMiddleware(c *gin.Context)
- func ReadSecret(fileName string) (string, error)
- func SendLogToSlack(SlackAPILogWebHook string, message SlackMessage, context Context, ...)
- func WaitForServerToStart(url string) bool
- type ApiError
- type ApiErrors
- type Code
- type Context
- type Environment
- type Error
- type ErrorDescription
- type RequestHeader
- type SlackAdditional
- type SlackMessage
Constants ¶
const ( Processing Code = 102 NoContent = 204 BadRequest = 400 Forbidden = 403 NotFound = 404 AlreadyExist = 409 UnexpectedError = 500 NotImplemented = 501 )
Code describe the status that will be generated in the BuildResponseError for the http response
Variables ¶
var Logger = &logrus.Logger{ Out: os.Stdout, Formatter: &logrus.TextFormatter{ForceColors: true}, Hooks: make(logrus.LevelHooks), Level: logrus.DebugLevel, }
Functions ¶
func ConnectToDB ¶
func ConnectToDB(username string, dbName string, password string, host string) (DB *gorm.DB, err error)
connectToDB do the connection request to the database depending on provided parameters
func DefaultCORSConfig ¶
DefaultCORSConfig Generate CORS config for router
func ExtractQueryParams ¶
ExtractQueryParams format arguments for gorm from a map[string]interface{}
func GenerateCleanString ¶
GenerateCleanString take a string with symbols and extra spaces and return an alpha numerical string with no accents
func GenerateRandomString ¶
GenerateRandomString create a random string of the requested length using the hexadecimal symbols
func GetBoolQueryParam ¶
GetBoolQueryParam allow to retrieve a boolean query parameter. It takes the gin context as param to build error if queryParam is not formatted correctly and a default value to set value if the parameter is optional and not set.
func GetDateQueryParam ¶
func GetDateQueryParam(c *gin.Context, queryParam string, dateLayout string, defaultValue string) (string, bool)
GetDateQueryParam allow to retrieve a date (as a string) query parameter. It takes the gin context as param to build error if queryParam is not formatted correctly and a default value to set value if the parameter is optional and not set. The dateLayout will define if the date is valid or not
func GetMandatoryIntQueryParam ¶
GetStringQueryParam allow to retrieve a string query parameter. It takes the gin context as param to build error if queryParam is not formatted correctly and a default value to set value if the parameter is optional and not set. the accepted values list will define if the value is valid or not
func GetMandatoryUintQueryParam ¶
GetStringQueryParam allow to retrieve a string query parameter. It takes the gin context as param to build error if queryParam is not formatted correctly and a default value to set value if the parameter is optional and not set. the accepted values list will define if the value is valid or not
func GetStringQueryParam ¶
func GetStringQueryParam(c *gin.Context, queryParam string, acceptedValues []string, defaultValue string) (string, bool)
GetStringQueryParam allow to retrieve a string query parameter. It takes the gin context as param to build error if queryParam is not formatted correctly and a default value to set value if the parameter is optional and not set. the accepted values list will define if the value is valid or not
func LoggingMiddleware ¶
func ReadSecret ¶
func SendLogToSlack ¶
func SendLogToSlack(SlackAPILogWebHook string, message SlackMessage, context Context, args ...SlackAdditional)
func WaitForServerToStart ¶
WaitForServerToStart return true only when API is ready
Types ¶
type ApiErrors ¶
type ApiErrors struct {
Errors []ApiError `json:"errors"`
}
ApiErrors carry the list of errors returned by the API from a request
func BuildRequestError ¶
BuildRequestError build a usable JSON error object from an error string generated by the structure validator
func BuildResponseError ¶
func BuildResponseError(err *ErrorDescription) (status int, apiErrors ApiErrors)
BuildResponseError apply the right status to the http response and build the error JSON object
func HttpRequestHandlerForUnitTesting ¶
func HttpRequestHandlerForUnitTesting(t *testing.T, requestHeader RequestHeader, requestBody interface{}, responseDTO interface{}) (*http.Response, ApiErrors)
HttpRequestHandlerForUnitTesting is the same as HttpRequestHandler but add logging for tests
type Code ¶
type Code uint
Code describe the status that will be generated in the BuildResponseError for the http response
type Context ¶
type Context struct { AppEnvironment Environment AppName string AppDomain string ServiceProtocol string ServiceName string ServicePort string ServiceSubDomain string ServiceUrl string }
func (*Context) SetUpAsExternal ¶
func (*Context) SetUpAsInternal ¶
type Environment ¶
type Environment int
const ( Dev Environment = iota Test Prod )
func (Environment) String ¶
func (e Environment) String() string
type Error ¶
type Error struct { Param string `json:"param"` Detail string `json:"detail"` Message string `json:"message"` // contains filtered or unexported fields }
Error is the default error message structure the API returns
type ErrorDescription ¶
Error describe the error object returned from services that can be passed directly to the BuildResponseError method
func BuildErrorDescriptionFromApiError ¶
func BuildErrorDescriptionFromApiError(apiErrors ApiErrors, code int) *ErrorDescription
BuildErrorDescriptionFromApiError rebuild an ErrorDescription object out of an ApiError object
func HttpRequestHandler ¶
func HttpRequestHandler(requestHeader RequestHeader, requestBody interface{}, responseDTO interface{}) *ErrorDescription
HttpRequestHandler is a handler for easy http requests
type RequestHeader ¶
RequestHeader is the object to send to the HttpRequestHandlers
type SlackAdditional ¶
type SlackMessage ¶
type SlackMessage string