Initial commit based on GenericQtClient v0.3.0
This commit is contained in:
12
libs/GenericCore/network/apiroutes.h
Normal file
12
libs/GenericCore/network/apiroutes.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef APIROUTES_H
|
||||
#define APIROUTES_H
|
||||
|
||||
#include <QString>
|
||||
|
||||
// TODO add namespace
|
||||
|
||||
static const QString apiPrefix = "/api/";
|
||||
|
||||
static const QString ROUTE_ITEMS = apiPrefix + "items";
|
||||
|
||||
#endif // APIROUTES_H
|
||||
106
libs/GenericCore/network/servercommunicator.cpp
Normal file
106
libs/GenericCore/network/servercommunicator.cpp
Normal file
@ -0,0 +1,106 @@
|
||||
#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;
|
||||
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(const QByteArray& jsonData) {
|
||||
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(responseData);
|
||||
} else {
|
||||
const QString message = QString("Error: %1").arg(reply->errorString());
|
||||
qDebug() << message;
|
||||
emit postRequestFailure(message);
|
||||
}
|
||||
reply->deleteLater();
|
||||
});
|
||||
}
|
||||
|
||||
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();
|
||||
});
|
||||
}
|
||||
|
||||
void ServerCommunicator::setServerConfiguration(const QString url,
|
||||
const QString email,
|
||||
const QString password) {
|
||||
setUrl(url);
|
||||
|
||||
m_email = email;
|
||||
m_password = password;
|
||||
}
|
||||
46
libs/GenericCore/network/servercommunicator.h
Normal file
46
libs/GenericCore/network/servercommunicator.h
Normal file
@ -0,0 +1,46 @@
|
||||
#ifndef SERVERCOMMUNICATOR_H
|
||||
#define SERVERCOMMUNICATOR_H
|
||||
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkRequestFactory>
|
||||
#include <QObject>
|
||||
#include <QRestAccessManager>
|
||||
|
||||
class ServerCommunicator : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ServerCommunicator(QObject* parent = nullptr);
|
||||
|
||||
bool sslSupported();
|
||||
|
||||
QUrl url() const;
|
||||
void setUrl(const QUrl& url);
|
||||
|
||||
void setServerConfiguration(const QString url, const QString email, const QString password);
|
||||
|
||||
public slots:
|
||||
void fetchItems();
|
||||
void postItems(const QByteArray& jsonData);
|
||||
void deleteItem(const QString& id);
|
||||
|
||||
signals:
|
||||
void urlChanged();
|
||||
|
||||
void itemsFetched(const QByteArray jsonDoc);
|
||||
void itemsFetchFailure(const QString errorString);
|
||||
void postRequestSuccessful(const QByteArray responseData);
|
||||
void postRequestFailure(const QString errorString);
|
||||
void deleteRequestSuccessful(const QByteArray responseData);
|
||||
void deleteRequestFailure(const QString errorString);
|
||||
|
||||
private:
|
||||
QNetworkAccessManager m_netManager;
|
||||
std::shared_ptr<QRestAccessManager> m_restManager;
|
||||
std::shared_ptr<QNetworkRequestFactory> m_serviceApi;
|
||||
|
||||
QString m_email;
|
||||
QString m_password;
|
||||
// QString m_authToken;
|
||||
};
|
||||
|
||||
#endif // SERVERCOMMUNICATOR_H
|
||||
Reference in New Issue
Block a user