mirror of
https://github.com/binary-kitchen/doorlockd
synced 2024-12-22 10:24:26 +01:00
Added proper Response handling
This commit is contained in:
parent
a522f1b70f
commit
3b7338ea6e
@ -30,6 +30,7 @@ main.cpp
|
||||
logger.cpp
|
||||
door.cpp
|
||||
logic.cpp
|
||||
response.cpp
|
||||
util.cpp
|
||||
daemon.cpp
|
||||
)
|
||||
|
22
doorlockd/response.cpp
Normal file
22
doorlockd/response.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
#include <json/json.h>
|
||||
|
||||
#include "response.h"
|
||||
|
||||
const std::string Response::_codeKey = "code";
|
||||
const std::string Response::_messageKey = "message";
|
||||
|
||||
Response::operator bool() const
|
||||
{
|
||||
return code == Response::Code::Success;
|
||||
}
|
||||
|
||||
std::string Response::toJson() const
|
||||
{
|
||||
Json::Value response;
|
||||
Json::StyledWriter writer;
|
||||
|
||||
response[_codeKey] = code;
|
||||
response[_messageKey] = message;
|
||||
|
||||
return writer.write(response);
|
||||
}
|
44
doorlockd/response.h
Normal file
44
doorlockd/response.h
Normal file
@ -0,0 +1,44 @@
|
||||
#ifndef RESPONSE_H
|
||||
#define RESPONSE_H
|
||||
|
||||
#include <string>
|
||||
|
||||
struct Response
|
||||
{
|
||||
enum Code {
|
||||
Success = 0, // Request successful
|
||||
Fail, // General non-specified error
|
||||
AlreadyUnlocked, // Authentication successful, but door is already unlocked
|
||||
AlreadyLocked, // Authentication successful, but door is already locked
|
||||
NotJson, // Request is not a valid JSON object
|
||||
JsonError, // Request is valid JSON, but does not contain necessary material
|
||||
InvalidToken, // Request contains invalid token
|
||||
InvalidCredentials, // Invalid LDAP credentials
|
||||
InvalidIP, // IP check failure
|
||||
UnknownAction, // Unknown action
|
||||
LDAPInit, // Ldap initialization failed
|
||||
} code;
|
||||
|
||||
std::string message;
|
||||
std::string toJson() const;
|
||||
operator bool() const;
|
||||
|
||||
Response() :
|
||||
code(Fail),
|
||||
message("General failure")
|
||||
{
|
||||
}
|
||||
|
||||
Response(Response::Code code, const std::string &message = "") :
|
||||
code(code),
|
||||
message(message)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
const static std::string _codeKey;
|
||||
const static std::string _messageKey;
|
||||
};
|
||||
|
||||
#endif // RESPONSE_H
|
Loading…
Reference in New Issue
Block a user