fetchItems() uses the more generic sendGetRequest function.

This commit is contained in:
2026-05-03 09:44:32 +02:00
parent b1f7fbff8b
commit 760c778c3b
2 changed files with 55 additions and 23 deletions

View File

@ -51,28 +51,7 @@ void ServerCommunicator::setServerConfiguration(const QString url,
}
}
void ServerCommunicator::fetchItems() {
/// Set up a GET request
m_restManager->get(m_serviceApi->createRequest(ROUTE_ITEMS), this, [this](QRestReply& reply) {
if (reply.isSuccess()) {
qInfo() << "Fetching items successful.";
const QJsonDocument doc = reply.readJson().value();
emit itemsFetched(doc.toJson());
} else {
if (reply.hasError()) {
const QString errorString = reply.errorString();
qCritical() << "ERROR:" << errorString;
emit itemsFetchFailure(errorString);
} else {
int statusCode = reply.httpStatus();
qCritical() << "ERROR:" << statusCode;
emit itemsFetchFailure(QString::number(statusCode));
emit itemsFetchFailure(QString("HTTP status code: %1").arg(statusCode));
}
}
});
}
void ServerCommunicator::fetchItems() { sendGetRequest(ROUTE_ITEMS); }
void ServerCommunicator::postItems(const QByteArray& jsonData) {
QNetworkReply* reply = m_restManager->post(m_serviceApi->createRequest(ROUTE_ITEMS), jsonData);
@ -109,4 +88,53 @@ void ServerCommunicator::deleteItem(const QString& id) {
}
reply->deleteLater();
});
}
}
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) {}

View File

@ -44,6 +44,10 @@ class ServerCommunicator : public QObject {
QString m_email;
QString m_password;
QString m_authToken;
void sendGetRequest(const QString& path);
void onGetReplySuccessful(const QString& path, const QJsonDocument doc);
void onGetReplyFailure(const QString& path, const QString errorString);
};
#endif // SERVERCOMMUNICATOR_H