magpie/API/api.cpp

252 lines
7.7 KiB
C++
Raw Normal View History

2023-11-24 23:48:01 +00:00
#include "api.h"
#include <QDebug>
#include <QJsonDocument>
#include <QJsonObject>
2023-12-16 01:44:25 +00:00
#include <QUrlQuery>
2023-11-24 23:48:01 +00:00
#include "codes.h"
2023-11-24 23:48:01 +00:00
constexpr const char* json = "application/json";
2023-12-16 01:44:25 +00:00
constexpr const char* urlEncoded = "application/x-www-form-urlencoded";
2023-11-24 23:48:01 +00:00
2023-12-25 20:07:51 +00:00
const std::map<QString, QMetaType::Type> testStructure({
{"type", QMetaType::QString},
{"version", QMetaType::QString},
});
const std::map<QString, QMetaType::Type> resultStructure({
{"result", QMetaType::Double},
});
const std::map<QString, QMetaType::Type> tokensStructure({
{"accessToken", QMetaType::QString},
{"renewToken", QMetaType::QString}
});
2023-11-24 23:48:01 +00:00
struct NetworkReplyDeleter {
void operator () (QNetworkReply* reply) {
reply->deleteLater();
}
};
API::API(const QUrl& address, QObject* parent):
QObject(parent),
address(address),
2023-12-16 01:44:25 +00:00
network(),
state(NoServer)
{}
2023-11-24 23:48:01 +00:00
2023-12-16 01:44:25 +00:00
QUrl API::getAddress() const {
return address;
}
API::State API::getState() const {
return state;
}
2023-12-25 20:07:51 +00:00
void API::setTokens(const QString access, const QString &renew) {
accessToken = access;
renewToken = renew;
state = Authenticating;
emit stateChanged(state);
}
2023-12-16 01:44:25 +00:00
void API::setAddress(const QUrl& path) {
if (address == path)
return;
if (state == Authenticated) {
//do something
}
address = path;
state = address.isEmpty() ? NoServer : NotAuthenticated;
emit addressChanged(address);
emit stateChanged(state);
2023-11-24 23:48:01 +00:00
}
2023-12-25 20:07:51 +00:00
std::optional<QVariantMap> API::readResult(QNetworkReply* reply) {
QVariant contentType = reply->header(QNetworkRequest::ContentTypeHeader);
if (!
contentType.isValid() ||
contentType.userType() != QMetaType::QString ||
contentType.toString() != json
)
return std::nullopt;
QByteArray data = reply->readAll();
QJsonDocument document = QJsonDocument::fromJson(data);
if (!document.isObject())
return std::nullopt;
QJsonObject object = document.object();
return object.toVariantMap();
}
bool API::validateResponse(const std::optional<QVariantMap>& data, const std::map<QString, QMetaType::Type>& structure) {
if (!data.has_value())
return false;
for (const std::pair<const QString, QMetaType::Type>& pair : structure) {
QVariantMap::ConstIterator itr = data->find(pair.first);
if (itr == data->end())
return false;
if (itr->userType() != pair.second)
return false;
}
return true;
}
2023-11-24 23:48:01 +00:00
void API::test(const QString& path, const QJSValue& finished) {
qDebug() << "Testing" << path;
2023-12-17 00:06:04 +00:00
if (state == Offline)
return callCallback(finished, "Need to be online to test");
2023-12-16 01:44:25 +00:00
2023-12-17 00:06:04 +00:00
QUrl address(path);
2023-11-24 23:48:01 +00:00
QNetworkRequest request(path + "/info");
request.setHeader(QNetworkRequest::ContentTypeHeader, json);
QNetworkReply* reply = network.get(request);
connect(reply, &QNetworkReply::finished,
2023-12-17 00:06:04 +00:00
std::bind(&API::onTestFinished, this, reply, address, finished)
2023-11-24 23:48:01 +00:00
);
}
2023-12-17 00:06:04 +00:00
void API::onTestFinished(QNetworkReply* reply, const QUrl& addr, const QJSValue& finished) {
2023-11-24 23:48:01 +00:00
std::unique_ptr<QNetworkReply, NetworkReplyDeleter> rpl(reply);
QNetworkReply::NetworkError error = reply->error();
2023-12-17 00:06:04 +00:00
if (error != QNetworkReply::NoError)
return callCallback(finished, reply->errorString());
2023-11-24 23:48:01 +00:00
2023-12-25 20:07:51 +00:00
std::optional<QVariantMap> data = readResult(reply);
if (!validateResponse(data, testStructure))
return callCallback(finished, "Malformed response");
2023-11-24 23:48:01 +00:00
2023-12-25 20:07:51 +00:00
QString type = data->value("type").toString();
if (type != "pica")
return callCallback(finished, "server of this type (" + type + ") is not supported");
2023-11-24 23:48:01 +00:00
2023-12-25 20:07:51 +00:00
QString version = data->value("version").toString();
if (version != "0.0.1")
return callCallback(finished, "server of this version (" + version + ") is not supported");
2023-11-24 23:48:01 +00:00
callCallback(finished, QString(), {QJSValue(true)});
2023-12-17 00:06:04 +00:00
address = ""; //to provoke singal change even if it's the same server
setAddress(addr);
2023-11-24 23:48:01 +00:00
}
void API::sendRegister(const QString& login, const QString& password, const QJSValue& finished) {
2023-12-16 01:44:25 +00:00
qDebug() << "Registering...";
2023-12-17 00:06:04 +00:00
if (state != NotAuthenticated)
return callCallback(finished, "Can not register in current state");
2023-12-16 01:44:25 +00:00
QUrlQuery params({
{"login", login},
{"password", password}
});
QString path = address.path();
QUrl regUrl = address;
regUrl.setPath(path + "/register");
QNetworkRequest request(regUrl);
2023-12-16 01:44:25 +00:00
request.setHeader(QNetworkRequest::ContentTypeHeader, urlEncoded);
QNetworkReply* reply = network.post(request, params.toString(QUrl::FullyEncoded).toUtf8());
connect(reply, &QNetworkReply::finished,
std::bind(&API::onRegisterFinished, this, reply, finished)
);
}
void API::sendLogin(const QString& login, const QString& password, const QJSValue& finished) {
qDebug() << "Logging in...";
if (state != NotAuthenticated)
return callCallback(finished, "Can not register in current state");
QUrlQuery params({
{"login", login},
{"password", password}
});
QString path = address.path();
QUrl url = address;
url.setPath(path + "/login");
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, urlEncoded);
QNetworkReply* reply = network.post(request, params.toString(QUrl::FullyEncoded).toUtf8());
connect(reply, &QNetworkReply::finished,
std::bind(&API::onLoginFinished, this, reply, finished)
);
}
void API::onRegisterFinished(QNetworkReply* reply, const QJSValue& finished) const {
2023-12-16 01:44:25 +00:00
std::unique_ptr<QNetworkReply, NetworkReplyDeleter> rpl(reply);
QNetworkReply::NetworkError error = reply->error();
2023-12-25 20:07:51 +00:00
std::optional<QVariantMap> data = readResult(reply);
std::optional<Codes::Register> code;
QString detail;
bool success = false;
if (validateResponse(data, resultStructure)) {
code = Codes::convertRegister(data->value("result").toInt());
success = code.value() == Codes::Register::success;
if (!success)
detail = Codes::description(code.value());
2023-12-16 01:44:25 +00:00
}
2023-12-25 20:07:51 +00:00
if (error != QNetworkReply::NoError)
return callCallback(finished, reply->errorString() + (success ? "" : ": " + detail));
2023-12-16 01:44:25 +00:00
2023-12-25 20:07:51 +00:00
if (!code)
return callCallback(finished, "Malformed result");
2023-12-16 01:44:25 +00:00
2023-12-25 20:07:51 +00:00
callCallback(finished, detail, {QJSValue(success)});
}
void API::onLoginFinished(QNetworkReply* reply, const QJSValue& finished) {
std::unique_ptr<QNetworkReply, NetworkReplyDeleter> rpl(reply);
QNetworkReply::NetworkError error = reply->error();
2023-12-25 20:07:51 +00:00
std::optional<QVariantMap> data = readResult(reply);
std::optional<Codes::Login> code;
QString detail;
bool success = false;
if (validateResponse(data, resultStructure)) {
code = Codes::convertLogin(data->value("result").toInt());
success = code.value() == Codes::Login::success;
if (!success)
detail = Codes::description(code.value());
}
if (error != QNetworkReply::NoError)
2023-12-25 20:07:51 +00:00
return callCallback(finished, reply->errorString() + (success ? "" : ": " + detail));
2023-12-25 20:07:51 +00:00
if (!code)
return callCallback(finished, "Malformed result");
if (!validateResponse(data, tokensStructure))
return callCallback(finished, "Malformed result: missing tokens");
2023-12-25 20:07:51 +00:00
callCallback(finished, detail, {QJSValue(success)});
2023-12-25 20:07:51 +00:00
state = Authenticated;
accessToken = data->value("accessToken").toString();
renewToken = data->value("renewToken").toString();
emit storeTokens(accessToken, renewToken);
emit stateChanged(state);
2023-12-16 01:44:25 +00:00
}
2023-11-24 23:48:01 +00:00
void API::callCallback(const QJSValue& callback, const QString& error, const QJSValueList& arguments) const {
if (callback.isCallable()) {
if (error.isEmpty())
callback.call(QJSValueList({QJSValue(QJSValue::NullValue)}) + arguments);
else
callback.call({QJSValue(error)});
}
}