api

package
v1.4.5-alpha1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2019 License: MPL-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package api represents API services abstraction. It provides a set of interfaces and helpers for a packages to extend application with new API endpoints as well as to interact with other components of application through them.

As application components (actor packages) should not interact between them directly them should use this package to make indirect calls between the. Interaction with a session manager also should happen through this package.

Index

Constants

View Source
const (
	ConstRESTActionParameter = "action"

	ConstSessionKeyAdminRights = "adminRights"   // session key used to flag that user have admin rights
	ConstSessionCookieName     = "OTTEMOSESSION" // cookie name which should contain sessionID
	ConstSessionKeyTimeZone    = "timeZone"      // session key for setting time zone

	ConstGETAuthParamName            = "auth"
	ConstConfigPathStoreRootLogin    = "general.store.root_login"
	ConstConfigPathStoreRootPassword = "general.store.root_password"

	ConstErrorModule = "api"
	ConstErrorLevel  = env.ConstErrorLevelHelper
)

Package global constants

Variables

This section is empty.

Functions

func GetArgumentOrContentValue

func GetArgumentOrContentValue(context InterfaceApplicationContext, name string) interface{}

GetArgumentOrContentValue looks for a given name within request parameters and content (map only), returns first occurrence according to mentioned sequence or nil if not found.

func GetContentValue

func GetContentValue(context InterfaceApplicationContext, name string) interface{}

GetContentValue looks for a given name within request content (map only), returns value or nil if not found

func GetRequestContentAsMap

func GetRequestContentAsMap(context InterfaceApplicationContext) (map[string]interface{}, error)

GetRequestContentAsMap tries to represent HTTP request content in map[string]interface{} format

func IsAdminSession

func IsAdminSession(context InterfaceApplicationContext) bool

IsAdminSession returns true if session with admin rights

func OnRestServiceStart

func OnRestServiceStart() error

OnRestServiceStart fires RESTFul service start event (callback handling)

func RegisterOnRestServiceStart

func RegisterOnRestServiceStart(callback func() error)

RegisterOnRestServiceStart registers new callback on RESTFul service start

func RegisterRestService

func RegisterRestService(newService InterfaceRestService) error

RegisterRestService registers RESTFul service in the system

  • will cause error if there are couple candidates for that role

func RegisterSessionService

func RegisterSessionService(newService InterfaceSessionService) error

RegisterSessionService registers session managing service in the system

  • will cause error if there are couple candidates for that role

func ValidateAdminRights

func ValidateAdminRights(context InterfaceApplicationContext) error

ValidateAdminRights returns nil if session contains admin rights

Types

type ApplicationContext

type ApplicationContext struct{ InterfaceApplicationContext }

ApplicationContext is a type you can embed in your model for application context support

func (*ApplicationContext) GetApplicationContext

func (it *ApplicationContext) GetApplicationContext() InterfaceApplicationContext

GetApplicationContext returns current application context or nil

func (*ApplicationContext) SetApplicationContext

func (it *ApplicationContext) SetApplicationContext(context InterfaceApplicationContext) error

SetApplicationContext assigns given application context to type

type FuncAPIHandler

type FuncAPIHandler func(context InterfaceApplicationContext) (interface{}, error)

FuncAPIHandler is an API handler callback function

func AsyncHandler

func AsyncHandler(nextHandler FuncAPIHandler, resultHandler FuncAPIResultHandler) FuncAPIHandler

AsyncHandler runs FuncAPIHandler in async. If resultHandler declared, the result of call will be put to it.

func IsAdminHandler

func IsAdminHandler(next FuncAPIHandler) FuncAPIHandler

IsAdminHandler returns middleware API Handler that checks admin rights

type FuncAPIResultHandler

type FuncAPIResultHandler func(context InterfaceApplicationContext, result interface{}, err error)

FuncAPIResultHandler is an API result handler callback function. It suppesod to be called by async API handler like api.APIAsyncHandler.

type InterfaceApplicationContext

type InterfaceApplicationContext interface {
	GetRequest() interface{}
	GetResponse() interface{}

	GetSession() InterfaceSession
	SetSession(session InterfaceSession) error

	GetContextValues() map[string]interface{}
	GetContextValue(key string) interface{}
	SetContextValue(key string, value interface{})

	GetResponseWriter() io.Writer

	GetRequestArguments() map[string]string
	GetRequestArgument(name string) string
	GetRequestFiles() map[string]io.Reader
	GetRequestFile(name string) io.Reader
	GetRequestSettings() map[string]interface{}
	GetRequestSetting(name string) interface{}
	GetRequestContent() interface{}
	GetRequestContentType() string

	GetResponseContentType() string
	SetResponseContentType(mimeType string) error
	GetResponseSetting(name string) interface{}
	SetResponseSetting(name string, value interface{}) error
	GetResponseResult() interface{}
	SetResponseResult(value interface{}) error

	SetResponseStatus(code int)
	SetResponseStatusBadRequest()
	SetResponseStatusForbidden()
	SetResponseStatusNotFound()
	SetResponseStatusInternalServerError()
}

InterfaceApplicationContext is an interface representing context where current execution happens

type InterfaceApplicationContextSupport

type InterfaceApplicationContextSupport interface {
	GetApplicationContext() InterfaceApplicationContext
	SetApplicationContext(context InterfaceApplicationContext) error
}

InterfaceApplicationContextSupport is an interface to assign/get application context to object

type InterfaceRestService

type InterfaceRestService interface {
	GetName() string

	Run() error
	GET(resource string, handler FuncAPIHandler)
	PUT(resource string, handler FuncAPIHandler)
	POST(resource string, handler FuncAPIHandler)
	DELETE(resource string, handler FuncAPIHandler)

	http.Handler
}

InterfaceRestService is an interface to interact with RESTFul API service

func GetRestService

func GetRestService() InterfaceRestService

GetRestService returns currently using RESTFul service implementation

type InterfaceSession

type InterfaceSession interface {
	GetID() string

	Get(key string) interface{}
	Set(key string, value interface{})

	IsEmpty() bool

	Touch() error
	Close() error
}

InterfaceSession is an interface represents private storage for particular API request

func GetSessionByID

func GetSessionByID(sessionID string, create bool) (InterfaceSession, error)

GetSessionByID returns session instance by id or nil

func NewSession

func NewSession() (InterfaceSession, error)

NewSession returns new session instance

func StartSession

func StartSession(context InterfaceApplicationContext) (InterfaceSession, error)

StartSession returns session object for request or creates new one. To use a secure session cookie in HTTPS, please set the environment variable OTTEMOCOOKIE. It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. Any other value returns an error.

type InterfaceSessionService

type InterfaceSessionService interface {
	GetName() string

	GC() error

	New() (InterfaceSession, error)
	Get(sessionID string, create bool) (InterfaceSession, error)

	IsEmpty(sessionID string) bool

	Touch(sessionID string) error
	Close(sessionID string) error

	GetKey(sessionID string, key string) interface{}
	SetKey(sessionID string, key string, value interface{})
}

InterfaceSessionService is an interface to access session managing service

func GetSessionService

func GetSessionService() InterfaceSessionService

GetSessionService returns currently using session service implementation

type StructRestRedirect

type StructRestRedirect struct {
	Result   interface{}
	Location string

	DoRedirect bool
}

StructRestRedirect is a structure you should return in API handler function if redirect needed

Directories

Path Synopsis
Package rest is a default implementation of a RESTful server.
Package rest is a default implementation of a RESTful server.
Package session is a default implementation of InterfaceSessionService and InterfaceSession declared in "github.com/ottemo/commerce/api" package Package session is a default implementation of a application Session manager.
Package session is a default implementation of InterfaceSessionService and InterfaceSession declared in "github.com/ottemo/commerce/api" package Package session is a default implementation of a application Session manager.

Jump to

Keyboard shortcuts

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