fetchItems() uses the more generic sendGetRequest function.
This commit is contained in:
@ -51,28 +51,7 @@ void ServerCommunicator::setServerConfiguration(const QString url,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerCommunicator::fetchItems() {
|
void ServerCommunicator::fetchItems() { sendGetRequest(ROUTE_ITEMS); }
|
||||||
/// 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::postItems(const QByteArray& jsonData) {
|
void ServerCommunicator::postItems(const QByteArray& jsonData) {
|
||||||
QNetworkReply* reply = m_restManager->post(m_serviceApi->createRequest(ROUTE_ITEMS), jsonData);
|
QNetworkReply* reply = m_restManager->post(m_serviceApi->createRequest(ROUTE_ITEMS), jsonData);
|
||||||
@ -110,3 +89,52 @@ void ServerCommunicator::deleteItem(const QString& id) {
|
|||||||
reply->deleteLater();
|
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) {}
|
||||||
|
|||||||
@ -44,6 +44,10 @@ class ServerCommunicator : public QObject {
|
|||||||
QString m_email;
|
QString m_email;
|
||||||
QString m_password;
|
QString m_password;
|
||||||
QString m_authToken;
|
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
|
#endif // SERVERCOMMUNICATOR_H
|
||||||
|
|||||||
Reference in New Issue
Block a user