core

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type API

type API struct {

	// MapMethods contain a array of HTTP methos for API validations.
	MapMethods *MapMethods
	// contains filtered or unexported fields
}

API core developer toolkit.

func New

func New(receiver APIRequestReceiver, formater APIResponseFormatter, writer APIResponseWriter, guarantor APISecurityGuarantor, mapMethods *MapMethods) *API

New API core developer toolkit.

func (*API) AddMapMethod

func (api *API) AddMapMethod(key string, methods []string)

AddMapMethod add a new method in a map of methods.

func (API) GetHeaderValueBool

func (api API) GetHeaderValueBool(key string, r *http.Request) (bool, error)

GetHeaderValueBool gets header values as bool.

func (API) GetHeaderValueFloat64

func (api API) GetHeaderValueFloat64(key string, r *http.Request) (float64, error)

GetHeaderValueFloat64 gets header value as float 64.

func (API) GetHeaderValueInt

func (api API) GetHeaderValueInt(key string, r *http.Request) (int, error)

GetHeaderValueInt gets header value as integer.

func (API) GetHeaderValueInt64

func (api API) GetHeaderValueInt64(key string, r *http.Request) (int64, error)

GetHeaderValueInt64 gets header value as integer 64.

func (API) GetHeaderValueString

func (api API) GetHeaderValueString(key string, r *http.Request) (string, error)

GetHeaderValueString gets header value as string.

func (API) GetQueryParamValueBool

func (api API) GetQueryParamValueBool(queryParamName string, r *http.Request) (bool, error)

GetQueryParamValueBool gets param value as bool.

func (API) GetQueryParamValueFloat64

func (api API) GetQueryParamValueFloat64(queryParamName string, r *http.Request) (float64, error)

GetQueryParamValueFloat64 gets query param value as float 64.

func (API) GetQueryParamValueInt

func (api API) GetQueryParamValueInt(queryParamName string, r *http.Request) (int, error)

GetQueryParamValueInt gets query param value as integer.

func (API) GetQueryParamValueInt64

func (api API) GetQueryParamValueInt64(queryParamName string, r *http.Request) (int64, error)

GetQueryParamValueInt64 gets query param value as integer 64.

func (API) GetQueryParamValueString

func (api API) GetQueryParamValueString(queryParamName string, r *http.Request) (string, error)

GetQueryParamValueString gets query param value as string.

func (API) GetRouteVarValueBool

func (api API) GetRouteVarValueBool(urlVarName string, r *http.Request) (bool, error)

GetRouteVarValueBool gets route variable value as bool.

func (API) GetRouteVarValueFloat64

func (api API) GetRouteVarValueFloat64(urlVarName string, r *http.Request) (float64, error)

GetRouteVarValueFloat64 gets route variable value as float 64.

func (API) GetRouteVarValueInt

func (api API) GetRouteVarValueInt(urlVarName string, r *http.Request) (int, error)

GetRouteVarValueInt gets route variable value as integer.

func (API) GetRouteVarValueInt64

func (api API) GetRouteVarValueInt64(urlVarName string, r *http.Request) (int64, error)

GetRouteVarValueInt64 gets route variable value as integer 64.

func (API) GetRouteVarValueString

func (api API) GetRouteVarValueString(urlVarName string, r *http.Request) (string, error)

GetRouteVarValueString gets route variable value as string.

func (*API) ProcessBody added in v0.1.1

func (api *API) ProcessBody(r *http.Request) (*RequestData, error)

ProcessBody API request. You can to define request JSON format and how to validate it implemented the APIRequestReciver interface.

func (*API) ProcessEncryptedBody added in v0.1.1

func (api *API) ProcessEncryptedBody(r *http.Request) (*RequestEncryptedData, error)

ProcessEncryptedBody API request. You can to define request body encription and how to validate it implemented the APIRequestReciver interface.

func (*API) RegisterNewAPIRequestReceiver added in v0.1.1

func (api *API) RegisterNewAPIRequestReceiver(receiver APIRequestReceiver)

RegisterNewAPIRequestReceiver inject a new implementation in the APIRequestReceiver interface.

func (*API) RegisterNewAPIResponseFormatter

func (api *API) RegisterNewAPIResponseFormatter(formatter APIResponseFormatter)

RegisterNewAPIResponseFormatter inject a new implementation in the APIResponseFormatter interface.

func (*API) RegisterNewAPIResponseWriter

func (api *API) RegisterNewAPIResponseWriter(writer APIResponseWriter)

RegisterNewAPIResponseWriter inject a new implementation in the APIResponseWriter interface.

func (*API) RegisterNewAPISecurityGuarantor added in v0.1.1

func (api *API) RegisterNewAPISecurityGuarantor(guarantor APISecurityGuarantor)

RegisterNewAPISecurityGuarantor inject a new implementation in the APISecurityGuarantor interface

func (API) UnmarshalBody

func (api API) UnmarshalBody(v interface{}, r *http.Request) error

UnmarshalBody parses request body to a struct.

func (*API) ValidateBasicToken

func (api *API) ValidateBasicToken(token string) (client, secret string, valid bool)

ValidateBasicToken validate token with a basic auth token validation method.

func (*API) ValidateCustomToken

func (api *API) ValidateCustomToken(token string, customValidator CustomTokenValidator) (json.RawMessage, bool)

ValidateCustomToken validate token with a custom method.

func (*API) ValidateMethods

func (api *API) ValidateMethods(keyMapMethod, method string) bool

ValidateMethods validates if a method exist in a methods map.

func (*API) Write

func (api *API) Write(data ResponseData, w http.ResponseWriter)

Write API response in JSON format in screen. You can to define response JSON format implemented the APIResponseFormatter interface.

type APIRequestReceiver added in v0.1.1

type APIRequestReceiver interface {
	// ProcessBody API request body information
	ProcessBody(*http.Request) (*RequestData, error)
	// ProcessEncryptedBody API request url encode data
	ProcessEncryptedBody(*http.Request) (*RequestEncryptedData, error)
	// GetRouteVar returns the route var for the current request, if any.
	GetRouteVar(string, *http.Request) string
}

APIRequestReceiver implemnt this interface to process request body information.

type APIResponseFormatter

type APIResponseFormatter interface {
	// Format the response body information.
	Format(ResponseData) *ResponseFormatted
}

APIResponseFormatter implement this interface to format the API response information to JSON.

type APIResponseWriter

type APIResponseWriter interface {
	// Write the API response in screen.
	Write(*ResponseFormatted, http.ResponseWriter)
}

APIResponseWriter implement this interface to write API response information in screen.

type APISecurityGuarantor added in v0.1.1

type APISecurityGuarantor interface {
	// ValidateBasicToken validate a token with a basic authentication method.
	ValidateBasicToken(token string) (client, secret string, valid bool)
	// ValidateBasicToken validate a token with a custmo authentication method.
	ValidateCustomToken(token string, validator CustomTokenValidator) (json.RawMessage, bool)
}

APISecurityGuarantor you can to implement this interface to implement authentication methos.

type CustomTokenValidator

type CustomTokenValidator func(string) (json.RawMessage, bool)

CustomTokenValidator validator custom token function type. Implement this type to creat a custom token validation method as bearer authentication method or specific company methods.

type JSONContent added in v0.1.1

type JSONContent []byte

JSONContent use to set a json request body. Use to parse json request body to a structure.

func (JSONContent) Decode added in v0.1.1

func (content JSONContent) Decode(v interface{}) error

Decode func decodes json content to an any structure.

func (JSONContent) MarshalJSON added in v0.1.1

func (content JSONContent) MarshalJSON() ([]byte, error)

MarshalJSON returns m as the JSON encoding of m.

func (*JSONContent) UnmarshalJSON added in v0.1.1

func (content *JSONContent) UnmarshalJSON(data []byte) error

UnmarshalJSON sets *m to a copy of data.

type MapMethods

type MapMethods map[string][]string

MapMethods you can to use this map to define your methods that allow or block in your API module.

type Middleware

type Middleware func(next http.HandlerFunc) http.HandlerFunc

Middleware provides a convenient mechanism for filtering HTTP requests entering the application. It returns a new handler which may perform various operations and should finish by calling the next HTTP handler.

func MiddlewaresChain

func MiddlewaresChain(mw ...Middleware) Middleware

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

type RequestData

type RequestData struct {
	// Client device UUID
	UUID string

	// Client device type
	DeviceType string

	// Client device brand
	DeviceBrand string

	// Client device model
	DeviceModel string

	// Client device operating system
	DeviceOS string

	// Client device operating system version
	OSVersion string

	// Client device operating system timezone
	OSTimezone string

	// Client App language config
	AppLanguage string

	// Client App version
	AppVersion string

	// Client App build information
	AppBuildInfo string

	// Client App name
	AppName string

	// Client security token
	SecurityToken string

	// DeviceSerial device serial number
	DeviceSerial string

	// DeviceId device unique id
	DeviceId string

	// Latitude device latitude
	Latitude string

	// Longitude device longitude
	Longitude string

	// Request event ID
	EventID string

	// HTTP request headers
	Headers map[string]string

	// You can to use this map to extend request information
	Info info.Info

	// You can use this property to add the body content for your
	// API request in json format.
	Content JSONContent

	// RawBody content
	RawBody []byte

	// Data contains more data
	Data interface{}
}

RequestData contains all information to process the API request.

func (*RequestData) AddHeader

func (data *RequestData) AddHeader(key, value string)

AddHeader adds new header to Headers map.

func (*RequestData) AddInfo added in v0.1.1

func (data *RequestData) AddInfo(key, value string)

AddInfo adds new item to AditionalInfo map.

func (*RequestData) DecodeContent added in v0.1.1

func (data *RequestData) DecodeContent(v interface{}) error

DecodeContent decodes RequestData.Content property from json to a struct.

func (*RequestData) Get added in v0.1.1

func (data *RequestData) Get(key string) (value interface{})

Get additional info value.

func (*RequestData) GetBool added in v0.1.1

func (data *RequestData) GetBool(key string) bool

GetBool gets additional info value as bool.

func (*RequestData) GetFloat added in v0.1.1

func (data *RequestData) GetFloat(key string) float64

GetFloat gets additional ifno value as float64.

func (*RequestData) GetInt added in v0.1.1

func (data *RequestData) GetInt(key string) int

GetInt gets additional info value as int.

func (*RequestData) GetInt64 added in v0.1.1

func (data *RequestData) GetInt64(key string) int64

GetInt64 gets additional info value as int64.

func (*RequestData) GetString added in v0.1.1

func (data *RequestData) GetString(key string) string

GetString gets additional info value as string.

func (*RequestData) GetStruct added in v0.1.1

func (data *RequestData) GetStruct(key string, v interface{}) error

GetStruct unmarhal a struct in additional info map.

func (*RequestData) Set added in v0.1.1

func (data *RequestData) Set(key string, value interface{})

Set additional info value.

type RequestEncryptedData added in v0.1.1

type RequestEncryptedData struct {
	// Request Metadata
	Metadata string

	// Data contains more data
	Data string

	// You can to use this map to extend request information
	Info info.Info
}

RequestEncryptedData contains all encryptions information to process the API request.

type ResponseCode added in v0.1.1

type ResponseCode string

ResponseCode type

type ResponseData

type ResponseData struct {
	// Title of response
	Title string

	// Message descriptor response
	Message string

	// HTTP status code respose
	HTTPStatusCode int

	// Custom code of respoonse
	ResponseCode ResponseCode

	// Response type: error, success, warning, etc.
	ResponseType ResponseType

	// The user security token
	SecurityToken string

	// Indicate actions for devices.
	Actions string

	// Request unique identifier
	EventID string

	// Headers for HTTP response
	Headers map[string]string

	// You can to use this map to add custom informations to generate
	// your API response.
	Info map[string]string

	// You can use this property to add the body content for your
	// API response.
	Content interface{}
}

ResponseData contain all information to generate the HTTP API response.

func (*ResponseData) AddHeader

func (data *ResponseData) AddHeader(key, value string)

AddHeader adds new header to Headers map.

func (*ResponseData) AddInfo added in v0.1.1

func (data *ResponseData) AddInfo(key, value string)

AddInfo adds new item to AditionalInfo map.

type ResponseFormatted

type ResponseFormatted struct {
	Headers        map[string]string
	HTTPStatusCode int
	Body           interface{}
}

ResponseFormatted contain formatted information to be responsed.

type ResponseType

type ResponseType string

ResponseType contains all the response types identifers

Jump to

Keyboard shortcuts

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