Bidding round control communicating with the server.
This commit is contained in:
@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user