Introduction
Bienvenue sur la documentation de l'API NexusZiaAPI !
Cette documentation a pour but de décrire les différents éléments de l'API, à savoir :
Interface | Description |
---|---|
IModuleCore | Interface décrivant un Module |
IAPIServer | Interface passé a chaque Module contenant les accès au ILogger et IHooks du serveur |
ILogger | Interface pour log des messages à différents niveaux (Info, etc..) |
IHooks | Interface pour être notifié d'un événement |
IHttpHeader | Interface pour stocker un header HTTP |
IHttpRequest | Interface pour stocker une requête HTTP |
IHttpResponse | Interface pour stocker une réponse HTTP |
IHttpSession | Interface pour définir une session HTTP |
IConfig | Interface pour un fichier de configuration d'un Module |
IConfigKey | Interface pour définir un key (Scope ou Value) dans un fichier de configuration |
IModuleCore
IModuleCore header
namespace nx {
class IModuleCore
{
public:
/**
* State for a module loading
* 0: OFF / 1: UP
*/
enum class State
{
OFF = 0,
UP = 1
};
/**
* Start module
*/
virtual void start(void) = 0;
/**
* Stop module
*/
virtual void stop(void) = 0;
/**
* Reload module
* @details A module is not stop then restart, it's always UP
*/
virtual void reload(void) = 0;
/**
* Set the name of the module
* @param name Name of the module
*/
virtual void setName(const std::string & name) = 0;
/**
* Get the config
* @return const IModuleConfig
*/
virtual IModuleConfig & getModuleConfig(void) = 0;
/**
* Set API Server
*/
virtual void setAPIServer(std::shared_ptr<IAPIServer> apiServer) = 0;
/**
* Get API Server
* @return const IModuleConfig
*/
virtual IAPIServer & getAPIServer(void) = 0;
/**
* Callback for registered and triggered events
* @param type Type of the event
* @param data Data of the event
* @return
*/
virtual IHooks::ReturnEvent triggerEvent(IHooks::Types type, void *data) = 0;
};
}
L'interface IModuleCore définit un ensemble de fonctions d'un module.
IAPIServer
IAPIServer header
namespace nx {
class IAPIServer {
public:
/**
* Get hooks from server's API
* @attention This function allows edition of references
* @return
*/
virtual IHooks & getHooks(void) = 0;
/**
* Get hooks from server's API
* @return
*/
virtual const IHooks & getHooks(void) const = 0;
/**
* Get logger from server's API
* @attention This function allows edit of reference
* @return
*/
virtual ILogger & getLogger(void) = 0;
/**
* Get logger from server's API
* @return
*/
virtual const ILogger & getLogger(void) const = 0;
};
}
L'interface IAPIServer est passé à chaque module pour leur permettre d'avoir accès au Logger (pour log des messages) et aux hooks (pour être notifié en cas d'événements).
ILogger
ILogger header
namespace nx {
class ILogger {
public:
/**
* @class Defines a level of log
*/
enum class Level {
DEFAULT = 0,
INFO = 1,
SUCCESS = 2,
WARNING = 3,
ERROR = 4,
FATAL_ERROR = 5
};
/**
* Log a message
* @param level Type of log
* @param msg Message
*/
virtual void log(const ILogger::Level & level, const std::string & msg) = 0;
/**
* Log default message
* @param msg
*/
virtual void logDefault(const std::string & msg) = 0;
/**
* Log info message
* @param msg
*/
virtual void logInfo(const std::string & msg) = 0;
/**
* Log success message
* @param msg
*/
virtual void logSuccess(const std::string & msg) = 0;
/**
* Log warning message
* @param msg
*/
virtual void logWarning(const std::string & msg) = 0;
/**
* Log error message
* @param msg
*/
virtual void logError(const std::string & msg) = 0;
/**
* Log fatal error message
* @param msg
*/
virtual void logFatalError(const std::string & msg) = 0;
};
}
L'interface ILogger permet de pouvoir log des informations.
Level | Description |
---|---|
DEFAULT | Niveau non défini |
INFO | Information |
SUCCESS | Succès |
WARNING | Avertissement |
ERROR | Erreur |
FATAL_ERROR | Erreur critique |
IHooks
IHooks header
namespace nx {
class IHooks {
public:
/**
* Defines the hook's type
*/
enum class Types {
DEFAULT = 0,
CONNECTION = 1,
REQUEST_BEFORE_PARSING = 2,
REQUEST_PARSING = 4,
REQUEST_AFTER_PARSING = 5,
RESPONSE_BEFORE_BUILD = 6,
RESPONSE_BUILD = 7,
RESPONSE_AFTER_BUILD = 8,
RESPONSE_SEND = 9
};
enum class ReturnEvent {
SUCCESS = 0,
ERROR = 1
};
/**
* Get a list of registered hooks
* @return
*/
virtual const std::unordered_map<nx::IHooks::Types, std::vector<std::string>, EnumClassHash> & getAllHooksRegister(void) const = 0;
/**
* Get registered module by type
* @param name Module's name
* @return
*/
virtual const std::vector<std::string> & getModuleRegisterForType(const nx::IHooks::Types type) const = 0;
/**
* Add hook registration
* @param type Hook's type
* @param name Module's name
*/
virtual void subscribe(const Types & type, const std::string & name) = 0;
/**
* Remove hook registration
* @param type Hook's type
* @param name Module's name
*/
virtual void unSubscribe(const Types & type, const std::string & name) = 0;
};
}
L'interface IHooks permet de pouvoir recevoir des notifications lors d'un événement.
Type | Description |
---|---|
CONNECTION | Connection d'un client |
REQUEST_BEFORE_PARSING | Avant le parsing de la requête HTTP |
REQUEST_PARSING | Parsing de la requête HTTP |
REQUEST_AFTER_PARSING | Après le parsing de la requête HTTP |
RESPONSE_BEFORE_BUILD | Avant la création de la réponse HTTP |
RESPONSE_BUILD | Création de la réponse HTTP |
RESPONSE_AFTER_BUILD | Après la création de la réponse HTTP |
RESPONSE_SEND | Envoi de la réponse |
IHttpHeader
IHttpHeader header
namespace nx {
class IHttpHeader {
public:
/**
* Set the key for the HTTP's header
* @param key
* @param value
* @return
*/
virtual void setKey(const std::string & key, const std::string & value) = 0;
/**
* Get the key for the HTTP's header
* @param key
* @return
*/
virtual const std::string & getKey(const std::string & key) const = 0;
};
}
L'interface IHttpHeader permet de définir un header Http.
IHttpRequest
IHttpRequest header
namespace nx {
class IHttpRequest : public IHttpHeader {
public:
enum class Method {
UNDEFINED = 0,
OPTIONS = 1,
GET,
HEAD,
POST,
PUT,
DELETE,
TRACE,
CONNECT,
PATCH
};
virtual const Method getMethod(void) const = 0;
virtual const std::string &getURI(void) const = 0;
virtual void setMethod(Method method) = 0;
virtual void setURI(const std::string &uri) = 0;
};
}
L'interface IHttpHeader permet de définir une requête HTTP.
IHttpResponse
IHttpResponse header
namespace nx {
class IHttpResponse : public IHttpHeader {
public:
enum class StatusCode {
UNDEFINED = 0,
// INFORMATION
CONTINUE = 100,
SWITCHING_PROTOCOLS = 101,
// SUCCESS
OK = 200,
CREATED = 201,
ACCEPTED = 202,
NON_AUTHORITATIVE_INFORMATION = 203,
NO_CONTENT = 204,
RESET_CONTENT = 205,
PARTIAL_CONTENT = 206,
// REDIRECTION
MULTIPLE_CHOICES = 300,
MOVED_PERMANENTLY = 301,
FOUND = 302,
SEE_OTHER = 303,
NOT_MODIFIED = 304,
USE_PROXY = 305,
TEMPORARY_REDIRECT = 307,
// CLIENT ERROR
BAD_REQUEST = 400,
UNAUTHORIZED = 401,
PAYMENT_REQUIRED = 402,
FORBIDDEN = 403,
NOT_FOUND = 404,
METHOD_NOT_ALLOWED = 405,
NOT_ACCEPTABLE = 406,
PROXY_AUTHENTICATION_REQUIRED = 407,
REQUEST_TIME_OUT = 408,
CONFLICT = 409,
GONE = 410,
LENGTH_REQUIRED = 411,
PRECONDITION_FAILED = 412,
REQUEST_ENTITY_TOO_LARGE = 413,
REQUEST_URI_TOO_LARGE = 414,
UNSUPPORTED_MEDIA_TYPE = 415,
REQUESTED_RANGE_NOT_SATISFIABLE = 416,
EXPECTATION_FAILED = 417,
// SERVER ERROR
INTERNAL_SERVER_ERROR = 500,
NOT_IMPLEMENTED = 501,
BAD_GATEWAY = 502,
SERVICE_UNAVAILABLE = 503,
GATEWAY_TIME_OUT = 504,
HTTP_VERSION_NOT_SUPPORTED = 505
};
virtual StatusCode getStatusCode(void) const = 0;
virtual const std::string &getReason(void) const = 0;
virtual const std::vector<char> & getData(void) = 0;
virtual void setStatusCode(StatusCode statusCode) = 0;
virtual void setReason(const std::string &reason) = 0;
virtual void setData(const std::vector<char> &data) = 0;
};
}
L'interface IHttpResponse permet de définir une réponse HTTP.
IHttpSession
IHttpSession header
namespace nx {
class IHttpSession {
public:
virtual const std::string &getIP(void) const = 0;
virtual void setIP(std::string ip) = 0;
};
}
L'interface IHttpSession permet de définir une session HTTP.
IConfig
IConfig header
namespace nx {
class IModuleConfig
{
public:
/**
* State of config for loading
*/
enum class State
{
UNLOADED = 0,
LOAD_START = 1,
LOADED = 2,
ERROR = 3
};
/**
* Init module's configuration
* Default value of each key, etc
*/
virtual void init(void) = 0;
/**
* Load module's configuration
*/
virtual void load(void) = 0;
/**
* Reload module's configuration
*/
virtual void reload(void) = 0;
/**
* Get state module's configuration
* "In loading", "is loaded", "error in config", etc
* @return
*/
virtual const State & getState(void) const = 0;
/**
*
* @param key
* @param configKey
* @return
*/
virtual void setKey(const std::string & key, const IConfigKey & configKey) = 0;
/**
* Get unique key in module's configuration
* @param key
* @return const IConfigKey &
*/
virtual const IConfigKey & getKeyOne(const std::string & key) const = 0;
/**
* Get list of keys in module's configuration
* @param key
* @return const std::vector<IConfigkey> &
*/
virtual const std::vector<IConfigKey> & getKey(const std::string & key) const = 0;
};
}
L'interface IConfig permet de définir un fichier de configuration d'un module.
Les fichiers de configuration sont au format JSON, comme dans l'exemple, en bas a droite.
Exemple de configuration
moduleName {
myScope {
mykey: "34"
myKey2: "45"
}
myScope2 {
myKey: "34"
}
}
IConfigKey
IConfigKey header
namespace nx {
class IConfigKey {
public:
enum class Type {
VALUE = 0,
SCOPE = 1
};
/**
* Set the type of the configuration's key
* @param type
*/
virtual void setType(const Type & type) = 0;
/**
* Get the type of the configuration's key
* @return
*/
virtual const Type & getType(void) const = 0;
/**
* Set name of the configuration's key
* @param type
*/
virtual void setName(const std::string & name) = 0;
/**
* Get name of the configuration's key
* @return
*/
virtual const std::string & getName(void) const = 0;
/**
* Set value of the configuration's key
* @attention Not for "scope" type
* @param type
*/
virtual void setValue(const std::string & value) = 0;
/**
* Get value of the configuration's key
* @attention Return "" for scope
* @return
*/
virtual const std::string & getValue(void) const = 0;
/**
* Get value of the configuration's key
* @attention Return IConfigKey empty for value
* @return
*/
virtual const IConfigKey & getScope(void) const = 0;
};
}
L'interface IConfigKey permet de définir une clé ou un scope dans un fichier de configuration.