api

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2023 License: MIT Imports: 13 Imported by: 2

README

api

API developer helpful library

Documentation

Index

Constants

View Source
const MethodGetKey = "get"

MethodGetKey GET http method to validate in NewRequestBodyMiddleware

View Source
const MethodPatchKey = "patch"

MethodPatchKey http method to validate in NewRequestBodyMiddleware

View Source
const MethodPostKey = "post"

MethodPostKey POST http method to validate in NewRequestBodyMiddleware

View Source
const MethodPutKey = "put"

MethodPutKey PUT http method to validate in NewRequestBodyMiddleware

View Source
const PPMethodsKey = "pp"

PPMethodsKey POST and PUT http methods to validate in NewRequestBodyMiddleware

View Source
const PPPGMethodsKey = "pppg"

PPPGMethodsKey POST, PUT, PATCH, and GET http methods to validate in NewRequestBodyMiddleware

View Source
const PPPMethodsKey = "ppp"

PPPMethodsKey POST, PUT and PATCH http methods to validate in NewRequestBodyMiddleware

Variables

View Source
var (
	// DefaultInvalidAuthHeaderMsg doc ..
	DefaultInvalidAuthHeaderMsg = "Invalid Authorization header!"
	// DefaultUnauthorizedTitle doc ...
	DefaultUnauthorizedTitle = "Unauthorized!"
	// DefaultBasicUnauthorizedMsg doc ..
	DefaultBasicUnauthorizedMsg = "Invalid basic token"
	// DefaultBearerUnauthorizedMsg doc ...
	DefaultBearerUnauthorizedMsg = "Invalid bearer token"
	// CustomTokenPrefix doc...
	CustomTokenPrefix = "Bearer"
	// DefaultCustomUnauthorizedMsg doc ...
	DefaultCustomUnauthorizedMsg = fmt.Sprintf("Invalid %v token", CustomTokenPrefix)
)
View Source
var (
	// DefaultErrorTitle doc ...
	DefaultErrorTitle = "Error response!"
	// DefaultErrorMessage doc ..
	DefaultErrorMessage = "The service has not completed the operation!"
	// ErrorType error response type the value is "error"
	ErrorType core.ResponseType = "error"
)
View Source
var (
	// DefaultInfoTitle doc ...
	DefaultInfoTitle = "Information!"
	// DefaultInfoMessage doc ..
	DefaultInfoMessage = "The request has been successful!"
	// InformativeType info response type the value is "info"
	InformativeType core.ResponseType = "info"
)
View Source
var (
	// DefaultSuccessTitle doc ...
	DefaultSuccessTitle = "Successful!"
	// DefaultSuccessMessage doc ..
	DefaultSuccessMessage = "The request has been successful!"
	// SuccessType success response type the value is "success"
	SuccessType core.ResponseType = "success"
)
View Source
var (
	// DefaultWarningTitle doc ...
	DefaultWarningTitle = "Alert!"
	// DefaultWarningMessage doc ..
	DefaultWarningMessage = "The application has been successful but with potential problems!"
	// WarningType warning response type the value is "warning"
	WarningType core.ResponseType = "warning"
)
View Source
var (
	// Username basic authentication
	// Default: admin
	Username = "admin"
	// Password basic authentication
	// Default: admin
	Password = "admin"
)
View Source
var CustomTokenValidatorFunc core.CustomTokenValidator

CustomTokenValidatorFunc define custom function to validate custom token

View Source
var EventIDHeaderKey = "EventID"

EventIDHeaderKey doc ..

View Source
var Fatal func(...interface{}) = log.Fatal

Fatal defines a function to print the fatal errors that occurred in this package, this function can be redefined. By default, this package implement the log.Fatal() function.

View Source
var GetRouteVar func(string, *http.Request) string = func(string, *http.Request) string {
	PrintError("Define a GetRouteVar function in this package")
	return ""
}

GetRouteVar returns the route variables for the current request, if any define it as: api.GetRouteVar = myCustomGetRouteVarFunc

View Source
var MiddlewaresChain = core.MiddlewaresChain

MiddlewaresChain provides syntactic sugar to create a new middleware which will be the result of chaining the ones received as parameters

View Source
var PrintError func(...interface{}) = log.Print

PrintError defines a function to print the errors that occurred in this package, this function can be redefined. By default, this package implement the log.Print() function.

View Source
var Printf func(string, ...interface{}) = log.Printf

Printf defines a function to print the messages that occurred in this package, this function can be redefined. By default, this package implement the log.Printf() function.

RequestBody doc ...

View Source
var SessionIDHeaderKey = "SessionID"

SessionIDHeaderKey doc ...

View Source
var UserIDHeaderKey = "UserID"

UserIDHeaderKey doc ...

Functions

func AddNewMapMethod

func AddNewMapMethod(key string, methods []string)

AddNewMapMethod add a new methods map to validate in a custom implementation of API-core package

func BasicToken

func BasicToken(next http.HandlerFunc) http.HandlerFunc

BasicToken validate basic authentication token middleware.

func ContentExtractor

func ContentExtractor(next http.HandlerFunc) http.HandlerFunc

ContentExtractor doc

func CustomToken

func CustomToken(next http.HandlerFunc) http.HandlerFunc

CustomToken middleware ...

func LogRequest

func LogRequest(method, uri, eventID, form string, headers http.Header, rawBody []byte)

LogRequest doc ...

func LogResponse

func LogResponse(res *httptest.ResponseRecorder)

LogResponse doc ...

func NewRequestBodyMiddleware

func NewRequestBodyMiddleware(keyListMethods string) core.Middleware

NewRequestBodyMiddleware doc ...

func RegisterNewAPIRequestValidator

func RegisterNewAPIRequestValidator(v core.APIRequestValidator)

RegisterNewAPIRequestValidator register a new request validator to this implementation API-core package

func RegisterNewAPIResponseFormatter

func RegisterNewAPIResponseFormatter(f core.APIResponseFormatter)

RegisterNewAPIResponseFormatter register a new custom API response formatter to this implementation of API-core package

func RegisterNewAPIResponseWriter

func RegisterNewAPIResponseWriter(f core.APIResponseWriter)

RegisterNewAPIResponseWriter register a new custom API response writer to this implementation of API-core package

func RequestHeaderJSON

func RequestHeaderJSON(next http.HandlerFunc) http.HandlerFunc

RequestHeaderJSON validate header Content-Type, is required and equal to application/json

func RequestHeaderSession

func RequestHeaderSession(next http.HandlerFunc) http.HandlerFunc

RequestHeaderSession doc ...

func ValidateBasicToken

func ValidateBasicToken(token string) (client, secret string, valid bool)

ValidateBasicToken doc ...

func ValidateCustomToken

func ValidateCustomToken(token string) (json.RawMessage, bool)

ValidateCustomToken doc ...

Types

type Error

type Error core.ResponseData

Error error response type the value is "error"

func (Error) Write

func (err Error) Write(w http.ResponseWriter)

Write ...

type Header struct {
	DeviceUUID      string `json:"uuid,omitempty"`
	DeviceType      string `json:"device_type,omitempty"`
	DeviceBrand     string `json:"device_brand,omitempty"`
	DeviceModel     string `json:"device_model,omitempty"`
	OS              string `json:"os,omitempty"`
	OSVersion       string `json:"os_version,omitempty"`
	Lang            string `json:"lang,omitempty"`
	Timezone        string `json:"timezone,omitempty"`
	AppVersion      string `json:"app_version,omitempty"`
	AppBuildVersion string `json:"app_build_version,omitempty"`
	AppName         string `json:"app_name,omitempty"`
	Token           string `json:"token,omitempty"`
}

Header request info section fields for encrypted requests

type Informative

type Informative core.ResponseData

Informative info response type the value is "info"

func (Informative) Write

func (info Informative) Write(w http.ResponseWriter)

Write ...

type JSONRequest

type JSONRequest struct {
	Header  Header          `json:"header,omitempty"`
	Content json.RawMessage `json:"content"`
}

JSONRequest struct used to parse the request content section

type JSONResponse

type JSONResponse struct {
	Header  JSONResponseInfo `json:"info"`
	Content interface{}      `json:"content,omitempty"`
}

JSONResponse response body structure contains the info section, with the response type and the messages for users and the content section, with the required data for the request

type JSONResponseInfo

type JSONResponseInfo struct {
	Type           core.ResponseType `json:"type"`
	Title          string            `json:"title,omitempty"`
	Message        string            `json:"message,omitempty"`
	Action         string            `json:"action,omitempty"`
	Token          string            `json:"session_id,omitempty"`
	Code           string            `json:"code,omitempty"`
	EventID        string            `json:"event_id,omitempty"`
	AdditionalInfo map[string]string `json:"additional_info,omitempty"`
}

JSONResponseInfo response body info section

type RequestBasic

type RequestBasic struct {
	JSONStruct interface{}
	SessionID  string
	UserID     string
	EventID    string
	HTTPReq    *http.Request
}

RequestBasic doc ...

func (*RequestBasic) GetRequestBasicInfo

func (request *RequestBasic) GetRequestBasicInfo() Response

GetRequestBasicInfo ..

func (*RequestBasic) GetRequestFullInfo

func (request *RequestBasic) GetRequestFullInfo() Response

GetRequestFullInfo ..

func (*RequestBasic) GetSessionID

func (request *RequestBasic) GetSessionID() Response

GetSessionID get session from user

func (*RequestBasic) GetTraceID

func (request *RequestBasic) GetTraceID() Response

GetTraceID doc

func (*RequestBasic) GetUserID

func (request *RequestBasic) GetUserID() Response

GetUserID get id user session

func (*RequestBasic) UnmarshalBody

func (request *RequestBasic) UnmarshalBody() Response

UnmarshalBody doc ...

type RequestValidator

type RequestValidator struct {
}

RequestValidator implementation

func (RequestValidator) GetRouteVar

func (v RequestValidator) GetRouteVar(key string, r *http.Request) string

GetRouteVar returns the route variables for the current request, if any

func (RequestValidator) ValidateRequest

func (v RequestValidator) ValidateRequest(r *http.Request) (*core.RequestData, error)

ValidateRequest doc

type Response

type Response interface {
	Write(w http.ResponseWriter)
}

Response interface

func GetHeaderValueBool

func GetHeaderValueBool(key string, r *http.Request) (bool, Response)

GetHeaderValueBool doc ...

func GetHeaderValueFloat64

func GetHeaderValueFloat64(key string, r *http.Request) (float64, Response)

GetHeaderValueFloat64 doc ...

func GetHeaderValueInt

func GetHeaderValueInt(key string, r *http.Request) (int, Response)

GetHeaderValueInt doc ...

func GetHeaderValueInt64

func GetHeaderValueInt64(key string, r *http.Request) (int64, Response)

GetHeaderValueInt64 doc ...

func GetHeaderValueString

func GetHeaderValueString(key string, r *http.Request) (string, Response)

GetHeaderValueString doc ...

func GetQueryParamValueBool

func GetQueryParamValueBool(queryParamName string, r *http.Request) (bool, Response)

GetQueryParamValueBool ...

func GetQueryParamValueFloat64

func GetQueryParamValueFloat64(queryParamName string, r *http.Request) (float64, Response)

GetQueryParamValueFloat64 ...

func GetQueryParamValueInt

func GetQueryParamValueInt(queryParamName string, r *http.Request) (int, Response)

GetQueryParamValueInt ...

func GetQueryParamValueInt64

func GetQueryParamValueInt64(queryParamName string, r *http.Request) (int64, Response)

GetQueryParamValueInt64 ...

func GetQueryParamValueString

func GetQueryParamValueString(queryParamName string, r *http.Request) (string, Response)

GetQueryParamValueString ...

func GetRouteVarValueBool

func GetRouteVarValueBool(urlVarName string, r *http.Request) (bool, Response)

GetRouteVarValueBool ...

func GetRouteVarValueFloat64

func GetRouteVarValueFloat64(urlVarName string, r *http.Request) (float64, Response)

GetRouteVarValueFloat64 ...

func GetRouteVarValueInt

func GetRouteVarValueInt(urlVarName string, r *http.Request) (int, Response)

GetRouteVarValueInt ...

func GetRouteVarValueInt64

func GetRouteVarValueInt64(urlVarName string, r *http.Request) (int64, Response)

GetRouteVarValueInt64 ...

func GetRouteVarValueString

func GetRouteVarValueString(urlVarName string, r *http.Request) (string, Response)

GetRouteVarValueString ...

func UnmarshalBody

func UnmarshalBody(v interface{}, r *http.Request) Response

UnmarshalBody doc ...

type ResponseFormatter

type ResponseFormatter struct{}

ResponseFormatter doc ...

func (ResponseFormatter) Format

Format doc ...

type ResponseWriter

type ResponseWriter struct{}

ResponseWriter doc ...

func (ResponseWriter) Write

Write doc ...

type Security

type Security struct{}

Security core interface implement

func (*Security) ValidateBasicToken

func (s *Security) ValidateBasicToken(token string) (client, secret string, valid bool)

ValidateBasicToken doc ...

func (*Security) ValidateCustomToken

func (s *Security) ValidateCustomToken(token string, validator core.CustomTokenValidator) (json.RawMessage, bool)

ValidateCustomToken doc ...

type Success

type Success core.ResponseData

Success success response type the value is "success"

func (Success) Write

func (success Success) Write(w http.ResponseWriter)

Write ...

type ValidateCredentials

type ValidateCredentials func(string, string) bool

ValidateCredentials func doc ...

var ValidateBasicAuthCredentialsFunc ValidateCredentials = validateCredentials

ValidateBasicAuthCredentialsFunc define custom function for validate basic authentication credential

type Warning

type Warning core.ResponseData

Warning warning response type the value is "warning"

func (Warning) Write

func (warning Warning) Write(w http.ResponseWriter)

Write ...

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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