93 lines
2.8 KiB
C++
93 lines
2.8 KiB
C++
#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>();
|
|
setUrl(baseUrl);
|
|
}
|
|
|
|
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;
|
|
authenticationHeaders.append(QHttpHeaders::WellKnownHeader::ContentType, "application/json");
|
|
m_serviceApi->setCommonHeaders(authenticationHeaders);
|
|
emit urlChanged();
|
|
}
|
|
|
|
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::postItems() {
|
|
QJsonObject itemObject;
|
|
itemObject["name"] = "Post test 1";
|
|
itemObject["description"] = "Post description 1";
|
|
itemObject["info"] = "Post info 1";
|
|
itemObject["amount"] = 1;
|
|
itemObject["factor"] = 5.3;
|
|
|
|
QJsonObject rootObject;
|
|
rootObject.insert("item", itemObject);
|
|
|
|
QJsonDocument jsonDoc(rootObject);
|
|
QByteArray jsonData = jsonDoc.toJson();
|
|
|
|
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;
|
|
emit postRequestSuccessful(message);
|
|
} else {
|
|
const QString message = QString("Error: %1").arg(reply->errorString());
|
|
qDebug() << message;
|
|
emit postRequestFailure(message);
|
|
}
|
|
reply->deleteLater();
|
|
});
|
|
}
|