named enums
This commit is contained in:
parent
1bb76af35f
commit
67b0fbd4b1
@ -8,6 +8,7 @@
|
|||||||
#include "miscellaneous/application.h"
|
#include "miscellaneous/application.h"
|
||||||
|
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
|
#include <QMetaEnum>
|
||||||
|
|
||||||
ApiServer::ApiServer(QObject* parent) : HttpServer(parent) {}
|
ApiServer::ApiServer(QObject* parent) : HttpServer(parent) {}
|
||||||
|
|
||||||
@ -53,7 +54,9 @@ void ApiServer::answerClient(QTcpSocket* socket, const HttpRequest& request) {
|
|||||||
|
|
||||||
reply_message += json_data;
|
reply_message += json_data;
|
||||||
|
|
||||||
|
#if !defined(NDEBUG)
|
||||||
IOFactory::writeFile("a.out", json_data);
|
IOFactory::writeFile("a.out", json_data);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
socket->write(reply_message);
|
socket->write(reply_message);
|
||||||
@ -115,12 +118,26 @@ ApiResponse ApiServer::processUnknown() const {
|
|||||||
return ApiResponse(ApiResponse::Result::Error, ApiRequest::Method::Unknown, QSL("unknown method"));
|
return ApiResponse(ApiResponse::Result::Error, ApiRequest::Method::Unknown, QSL("unknown method"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ApiResponse::ApiResponse(Result result, ApiRequest::Method method, const QJsonValue& response)
|
||||||
|
: m_result(result), m_method(method), m_response(response) {}
|
||||||
|
|
||||||
QJsonDocument ApiResponse::toJson() const {
|
QJsonDocument ApiResponse::toJson() const {
|
||||||
QJsonObject obj;
|
QJsonObject obj;
|
||||||
|
|
||||||
obj.insert("method", int(m_method));
|
static QMetaEnum enumer_method = QMetaEnum::fromType<ApiRequest::Method>();
|
||||||
obj.insert("result", int(m_result));
|
static QMetaEnum enumer_result = QMetaEnum::fromType<ApiResponse::Result>();
|
||||||
obj.insert("data", m_response);
|
|
||||||
|
obj.insert(QSL("method"), enumer_method.valueToKey(int(m_method)));
|
||||||
|
obj.insert(QSL("result"), enumer_result.valueToKey(int(m_result)));
|
||||||
|
obj.insert(QSL("data"), m_response);
|
||||||
|
|
||||||
return QJsonDocument(obj);
|
return QJsonDocument(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ApiRequest::ApiRequest(const QJsonDocument& data) : m_method(), m_parameters(data.object().value(QSL("data"))) {
|
||||||
|
static QMetaEnum enumer = QMetaEnum::fromType<ApiRequest::Method>();
|
||||||
|
|
||||||
|
QByteArray method_name = data.object().value(QSL("method")).toString().toLocal8Bit();
|
||||||
|
|
||||||
|
m_method = Method(enumer.keysToValue(method_name.constData()));
|
||||||
|
}
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
|
|
||||||
struct ApiRequest {
|
struct ApiRequest {
|
||||||
|
Q_GADGET
|
||||||
|
|
||||||
public:
|
public:
|
||||||
enum class Method {
|
enum class Method {
|
||||||
Unknown = 0,
|
Unknown = 0,
|
||||||
@ -16,22 +18,26 @@ struct ApiRequest {
|
|||||||
ArticlesFromFeed = 2
|
ArticlesFromFeed = 2
|
||||||
};
|
};
|
||||||
|
|
||||||
explicit ApiRequest(const QJsonDocument& data)
|
Q_ENUM(Method)
|
||||||
: m_method(Method(data.object().value("method").toInt())), m_parameters(data.object().value("data")) {}
|
|
||||||
|
explicit ApiRequest(const QJsonDocument& data);
|
||||||
|
|
||||||
Method m_method;
|
Method m_method;
|
||||||
QJsonValue m_parameters;
|
QJsonValue m_parameters;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ApiResponse {
|
struct ApiResponse {
|
||||||
|
Q_GADGET
|
||||||
|
|
||||||
public:
|
public:
|
||||||
enum class Result {
|
enum class Result {
|
||||||
Success = 1,
|
Success = 1,
|
||||||
Error = 2
|
Error = 2
|
||||||
};
|
};
|
||||||
|
|
||||||
explicit ApiResponse(Result result, ApiRequest::Method method, const QJsonValue& response)
|
Q_ENUM(Result)
|
||||||
: m_result(result), m_method(method), m_response(response) {}
|
|
||||||
|
explicit ApiResponse(Result result, ApiRequest::Method method, const QJsonValue& response);
|
||||||
|
|
||||||
Result m_result;
|
Result m_result;
|
||||||
ApiRequest::Method m_method;
|
ApiRequest::Method m_method;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user