apitool

package module
v0.0.0-...-e0de5b9 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2018 License: MIT Imports: 29 Imported by: 0

README

apitool

A set of tools for APIs

Documentation

Overview

Package apihelper automate construction of http response

Index

Constants

View Source
const (
	Processing      Code = 102
	NoContent            = 204
	BadRequest           = 400
	Unauthorized         = 401
	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

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

func DefaultCORSConfig() cors.Config

DefaultCORSConfig Generate CORS config for router

func ExtractQueryParams

func ExtractQueryParams(queryParams map[string]interface{}) (query string, args []interface{})

ExtractQueryParams format arguments for gorm from a map[string]interface{}

func GenerateCleanString

func GenerateCleanString(originalString string) (string, error)

GenerateCleanString take a string with symbols and extra spaces and return an alpha numerical string with no accents

func GenerateRandomString

func GenerateRandomString(strLen int) string

GenerateRandomString create a random string of the requested length using the hexadecimal symbols

func GetBoolQueryParam

func GetBoolQueryParam(c *gin.Context, value *bool, queryParam string, defaultValue bool) bool

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

func GetMandatoryIntQueryParam(c *gin.Context, queryParam string) (int, 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 GetMandatoryUintQueryParam

func GetMandatoryUintQueryParam(c *gin.Context, queryParam string) (uint, 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 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 LoggingMiddleware(c *gin.Context)

func ReadSecret

func ReadSecret(fileName string) (string, error)

func SendLogToSlack

func SendLogToSlack(SlackAPILogWebHook string, message SlackMessage, context Context, args ...SlackAdditional)

func WaitForServerToStart

func WaitForServerToStart(url string) bool

WaitForServerToStart return true only when API is ready

Types

type ApiError

type ApiError interface {
}

ApiError interface for all API error messages

type ApiErrors

type ApiErrors struct {
	Errors []ApiError `json:"errors"`
}

ApiErrors carry the list of errors returned by the API from a request

func BuildRequestError

func BuildRequestError(err error) (int, ApiErrors)

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 (ctx *Context) SetUpAsExternal() error

func (*Context) SetUpAsInternal

func (ctx *Context) SetUpAsInternal() error

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

type ErrorDescription struct {
	Param   string
	Detail  error
	Message string
	Code    Code
}

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

type RequestHeader struct {
	URL           string
	Method        string
	ContentType   string
	Authorization string
}

RequestHeader is the object to send to the HttpRequestHandlers

type SlackAdditional

type SlackAdditional struct {
	Name  string
	Value string
}

type SlackMessage

type SlackMessage string

Jump to

Keyboard shortcuts

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