2026-01-25 10:47:19 +01:00
|
|
|
#include "servercommunicator.h"
|
|
|
|
|
#include "apiroutes.h"
|
|
|
|
|
|
|
|
|
|
#include <QJsonArray>
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
#include <QRestReply>
|
|
|
|
|
|
|
|
|
|
using namespace Qt::StringLiterals;
|
|
|
|
|
|
|
|
|
|
ServerCommunicator::ServerCommunicator(QObject* parent)
|
|
|
|
|
: QObject{parent} {
|
|
|
|
|
m_netManager.setAutoDeleteReplies(true);
|
|
|
|
|
m_restManager = std::make_shared<QRestAccessManager>(&m_netManager);
|
|
|
|
|
m_serviceApi = std::make_shared<QNetworkRequestFactory>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ServerCommunicator::sslSupported() {
|
|
|
|
|
#if QT_CONFIG(ssl)
|
|
|
|
|
return QSslSocket::supportsSsl();
|
|
|
|
|
#else
|
|
|
|
|
return false;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QUrl ServerCommunicator::url() const { return m_serviceApi->baseUrl(); }
|
|
|
|
|
|
|
|
|
|
void ServerCommunicator::setUrl(const QUrl& url) {
|
|
|
|
|
if (m_serviceApi->baseUrl() == url) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_serviceApi->setBaseUrl(url);
|
|
|
|
|
QHttpHeaders authenticationHeaders;
|
2026-01-29 08:54:47 +01:00
|
|
|
authenticationHeaders.append(QHttpHeaders::WellKnownHeader::ContentType, "application/json");
|
2026-01-25 10:47:19 +01:00
|
|
|
m_serviceApi->setCommonHeaders(authenticationHeaders);
|
|
|
|
|
emit urlChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-27 08:02:17 +02:00
|
|
|
void ServerCommunicator::setServerConfiguration(const QString url,
|
|
|
|
|
const QString email,
|
|
|
|
|
const QString password,
|
|
|
|
|
const QString authToken) {
|
|
|
|
|
setUrl(url);
|
|
|
|
|
|
|
|
|
|
m_email = email;
|
|
|
|
|
m_password = password;
|
|
|
|
|
m_authToken = authToken;
|
|
|
|
|
|
|
|
|
|
if (!authToken.isEmpty()) {
|
|
|
|
|
m_serviceApi->setBearerToken(authToken.toLatin1());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-03 09:44:32 +02:00
|
|
|
void ServerCommunicator::fetchItems() { sendGetRequest(ROUTE_ITEMS); }
|
2026-01-29 08:54:47 +01:00
|
|
|
|
2026-01-29 13:13:32 +01:00
|
|
|
void ServerCommunicator::postItems(const QByteArray& jsonData) {
|
2026-01-29 08:54:47 +01:00
|
|
|
QNetworkReply* reply = m_restManager->post(m_serviceApi->createRequest(ROUTE_ITEMS), jsonData);
|
|
|
|
|
|
|
|
|
|
QObject::connect(reply, &QNetworkReply::finished, [=]() {
|
|
|
|
|
if (reply->error() == QNetworkReply::NoError) {
|
|
|
|
|
QByteArray responseData = reply->readAll();
|
|
|
|
|
const QString message = QString("POST successful! Response: %1").arg(responseData);
|
|
|
|
|
qInfo() << message;
|
2026-02-02 16:13:44 +01:00
|
|
|
emit postRequestSuccessful(responseData);
|
2026-01-29 08:54:47 +01:00
|
|
|
} else {
|
|
|
|
|
const QString message = QString("Error: %1").arg(reply->errorString());
|
|
|
|
|
qDebug() << message;
|
|
|
|
|
emit postRequestFailure(message);
|
|
|
|
|
}
|
|
|
|
|
reply->deleteLater();
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-02-02 16:15:20 +01:00
|
|
|
|
|
|
|
|
void ServerCommunicator::deleteItem(const QString& id) {
|
|
|
|
|
const QString deleteRoute = QString("%1/%2").arg(ROUTE_ITEMS, id);
|
|
|
|
|
QNetworkReply* reply = m_restManager->deleteResource(m_serviceApi->createRequest(deleteRoute));
|
|
|
|
|
|
|
|
|
|
QObject::connect(reply, &QNetworkReply::finished, [=]() {
|
|
|
|
|
if (reply->error() == QNetworkReply::NoError) {
|
|
|
|
|
QByteArray responseData = reply->readAll();
|
|
|
|
|
const QString message = QString("DELETE successful! Response: %1").arg(responseData);
|
|
|
|
|
qInfo() << message;
|
|
|
|
|
emit deleteRequestSuccessful(responseData);
|
|
|
|
|
} else {
|
|
|
|
|
const QString message = QString("Error: %1").arg(reply->errorString());
|
|
|
|
|
qDebug() << message;
|
|
|
|
|
emit deleteRequestFailure(message);
|
|
|
|
|
}
|
|
|
|
|
reply->deleteLater();
|
|
|
|
|
});
|
2026-05-03 09:44:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ServerCommunicator::sendGetRequest(const QString& path) {
|
|
|
|
|
// TODO check for valid path, instead of emptiness
|
|
|
|
|
if (path.isEmpty()) {
|
|
|
|
|
qDebug() << "Empty path -> Not sending a request.";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QNetworkRequest request = m_serviceApi->createRequest(path);
|
|
|
|
|
m_restManager->get(request, this, [this, path](QRestReply& reply) {
|
|
|
|
|
if (reply.isSuccess()) {
|
|
|
|
|
qInfo() << "Request successful.";
|
|
|
|
|
const QJsonDocument doc = reply.readJson().value();
|
|
|
|
|
onGetReplySuccessful(path, doc);
|
|
|
|
|
} else {
|
|
|
|
|
if (reply.hasError()) {
|
|
|
|
|
const QString errorString = reply.errorString();
|
|
|
|
|
qWarning() << "Network error:" << errorString;
|
|
|
|
|
onGetReplyFailure(path, errorString);
|
|
|
|
|
} else {
|
|
|
|
|
int statusCode = reply.httpStatus();
|
|
|
|
|
qWarning() << "Request not successful:" << statusCode;
|
|
|
|
|
onGetReplyFailure(path, QString("HTTP status code: %1").arg(statusCode));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ServerCommunicator::onGetReplySuccessful(const QString& path, const QJsonDocument doc) {
|
|
|
|
|
if (path == ROUTE_ITEMS) {
|
|
|
|
|
emit itemsFetched(doc.toJson());
|
|
|
|
|
} else {
|
|
|
|
|
qWarning() << "Can't match request path:" << path;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ServerCommunicator::onGetReplyFailure(const QString& path, const QString errorString) {
|
|
|
|
|
if (path == ROUTE_ITEMS) {
|
|
|
|
|
emit itemsFetchFailure(errorString);
|
|
|
|
|
} else {
|
|
|
|
|
qWarning() << "Can't match request path:" << path;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ServerCommunicator::onSendPostRequestTriggered(const QString& path, const QByteArray data) {}
|
|
|
|
|
|
|
|
|
|
void ServerCommunicator::onPostReplySuccessful(const QString& path, const QJsonDocument doc) {}
|
|
|
|
|
|
|
|
|
|
void ServerCommunicator::onPostReplyFailure(const QString& path, const QString errorString) {}
|