Bidding round control communicating with the server.
This commit is contained in:
@ -9,4 +9,10 @@ static const QString apiPrefix = "/api/";
|
||||
|
||||
static const QString ROUTE_ITEMS = apiPrefix + "items";
|
||||
|
||||
static const QString ROUTE_BIDDINGROUNDS = apiPrefix + "bidding_rounds";
|
||||
static const QString ROUTE_CURRENT_BIDDINGROUND = ROUTE_BIDDINGROUNDS + "/get_current";
|
||||
static const QString ROUTE_START_BIDDINGROUND = ROUTE_BIDDINGROUNDS + "/start_new";
|
||||
static const QString ROUTE_RESTART_BIDDINGROUND = ROUTE_BIDDINGROUNDS + "/restart";
|
||||
static const QString ROUTE_STOP_BIDDINGROUND = ROUTE_BIDDINGROUNDS + "/stop";
|
||||
|
||||
#endif // APIROUTES_H
|
||||
|
||||
@ -1,21 +1,27 @@
|
||||
#include "servercommunicator.h"
|
||||
#include "apiroutes.h"
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QRestAccessManager>
|
||||
#include <QRestReply>
|
||||
|
||||
#include "../genericcore.h"
|
||||
#include "apiroutes.h"
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
|
||||
ServerCommunicator::ServerCommunicator(QObject* parent)
|
||||
: QObject{parent} {
|
||||
ServerCommunicator::ServerCommunicator(GenericCore* core)
|
||||
: m_core(core)
|
||||
, QObject{core} {
|
||||
m_netManager.setAutoDeleteReplies(true);
|
||||
m_restManager = std::make_shared<QRestAccessManager>(&m_netManager);
|
||||
m_serviceApi = std::make_shared<QNetworkRequestFactory>();
|
||||
|
||||
connect(m_core, &GenericCore::sendGetRequest, this,
|
||||
&ServerCommunicator::onSendGetRequestTriggered);
|
||||
}
|
||||
|
||||
bool ServerCommunicator::sslSupported() {
|
||||
bool ServerCommunicator::sslSupported() const {
|
||||
#if QT_CONFIG(ssl)
|
||||
return QSslSocket::supportsSsl();
|
||||
#else
|
||||
@ -104,3 +110,80 @@ void ServerCommunicator::setServerConfiguration(const QString url,
|
||||
m_email = email;
|
||||
m_password = password;
|
||||
}
|
||||
|
||||
void ServerCommunicator::onSendGetRequestTriggered(const GetRequestTypes type) {
|
||||
QString path;
|
||||
switch (type) {
|
||||
case GetCurrentBiddingRound:
|
||||
path = ROUTE_CURRENT_BIDDINGROUND;
|
||||
break;
|
||||
case StartNewBiddingRound:
|
||||
path = ROUTE_START_BIDDINGROUND;
|
||||
break;
|
||||
case RestartLastBiddingRound:
|
||||
path = ROUTE_RESTART_BIDDINGROUND;
|
||||
break;
|
||||
case StopCurrentBiddingRound:
|
||||
path = ROUTE_STOP_BIDDINGROUND;
|
||||
break;
|
||||
default:
|
||||
qWarning() << "No route found for GetRequestType:" << type;
|
||||
break;
|
||||
}
|
||||
|
||||
// TODO move into default case of switch statement?
|
||||
if (path.isEmpty()) {
|
||||
qDebug() << "Empty path -> Not sending a request.";
|
||||
return;
|
||||
}
|
||||
|
||||
const QNetworkRequest request = m_serviceApi->createRequest(path);
|
||||
|
||||
m_restManager->get(request, this, [this, type](QRestReply& reply) {
|
||||
if (reply.isSuccess()) {
|
||||
qInfo() << "Request successful.";
|
||||
const QJsonDocument doc = reply.readJson().value();
|
||||
onGetReplySuccessful(type, doc);
|
||||
} else {
|
||||
if (reply.hasError()) {
|
||||
const QString errorString = reply.errorString();
|
||||
qWarning() << "Network error:" << errorString;
|
||||
onGetReplyFailure(type, errorString);
|
||||
} else {
|
||||
int statusCode = reply.httpStatus();
|
||||
qWarning() << "Request not successful:" << statusCode;
|
||||
onGetReplyFailure(type, QString("HTTP status code: %1").arg(statusCode));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void ServerCommunicator::onGetReplySuccessful(const GetRequestTypes type, const QJsonDocument doc) {
|
||||
switch (type) {
|
||||
case GetCurrentBiddingRound:
|
||||
case StartNewBiddingRound:
|
||||
case RestartLastBiddingRound:
|
||||
case StopCurrentBiddingRound:
|
||||
currentBiddingRoundChangedReply(doc);
|
||||
break;
|
||||
default:
|
||||
qWarning() << "Can't match request type:" << type;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ServerCommunicator::onGetReplyFailure(const GetRequestTypes type, const QString errorString) {
|
||||
const QString message =
|
||||
QString("Request of type %1 returned: %2").arg(QString::number(type), errorString);
|
||||
m_core->displayStatusMessage(message);
|
||||
}
|
||||
|
||||
void ServerCommunicator::currentBiddingRoundChangedReply(const QJsonDocument jsonDoc) {
|
||||
qInfo() << "Current bidding round received.";
|
||||
const QJsonObject rootObject = jsonDoc["data"].toObject();
|
||||
const int roundNumber = rootObject["round_number"].toInt();
|
||||
const bool stopped = rootObject["stopped"].toBool();
|
||||
const bool isActive = !stopped;
|
||||
|
||||
emit m_core->currentBiddingRoundChanged(roundNumber, isActive);
|
||||
}
|
||||
|
||||
@ -1,17 +1,22 @@
|
||||
#ifndef SERVERCOMMUNICATOR_H
|
||||
#define SERVERCOMMUNICATOR_H
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkRequestFactory>
|
||||
#include <QObject>
|
||||
#include <QRestAccessManager>
|
||||
|
||||
#include "../model/metadata.h"
|
||||
|
||||
class GenericCore;
|
||||
|
||||
class ServerCommunicator : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ServerCommunicator(QObject* parent = nullptr);
|
||||
explicit ServerCommunicator(GenericCore* core);
|
||||
|
||||
bool sslSupported();
|
||||
bool sslSupported() const;
|
||||
|
||||
QUrl url() const;
|
||||
void setUrl(const QUrl& url);
|
||||
@ -19,6 +24,10 @@ class ServerCommunicator : public QObject {
|
||||
void setServerConfiguration(const QString url, const QString email, const QString password);
|
||||
|
||||
public slots:
|
||||
void onSendGetRequestTriggered(const GetRequestTypes type);
|
||||
void onGetReplySuccessful(const GetRequestTypes type, const QJsonDocument doc);
|
||||
void onGetReplyFailure(const GetRequestTypes type, const QString errorString);
|
||||
|
||||
void fetchItems();
|
||||
void postItems(const QByteArray& jsonData);
|
||||
void deleteItem(const QString& id);
|
||||
@ -34,6 +43,8 @@ class ServerCommunicator : public QObject {
|
||||
void deleteRequestFailure(const QString errorString);
|
||||
|
||||
private:
|
||||
GenericCore* m_core = nullptr;
|
||||
|
||||
QNetworkAccessManager m_netManager;
|
||||
std::shared_ptr<QRestAccessManager> m_restManager;
|
||||
std::shared_ptr<QNetworkRequestFactory> m_serviceApi;
|
||||
@ -41,6 +52,9 @@ class ServerCommunicator : public QObject {
|
||||
QString m_email;
|
||||
QString m_password;
|
||||
// QString m_authToken;
|
||||
|
||||
/// reply parser
|
||||
void currentBiddingRoundChangedReply(const QJsonDocument jsonDoc);
|
||||
};
|
||||
|
||||
#endif // SERVERCOMMUNICATOR_H
|
||||
|
||||
Reference in New Issue
Block a user