Skip to content

Commit

Permalink
Refs #99. Refactored SurveyComManager
Browse files Browse the repository at this point in the history
  • Loading branch information
SBriere committed Nov 28, 2024
1 parent d7cad17 commit b48c643
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 88 deletions.
2 changes: 2 additions & 0 deletions client/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ set(headers
editors/UserWidget.h

# Services
services/BaseServiceComManager.h
services/BaseServiceWidget.h
services/BaseServiceToolsWidget.h
services/BaseServiceSetupWidget.h
Expand Down Expand Up @@ -222,6 +223,7 @@ set(srcs
editors/UserWidget.cpp

# Services
services/BaseServiceComManager.cpp
services/BaseServiceWidget.cpp
services/BaseServiceToolsWidget.cpp
services/BaseServiceSetupWidget.cpp
Expand Down
141 changes: 141 additions & 0 deletions client/src/services/BaseServiceComManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#include "BaseServiceComManager.h"

BaseServiceComManager::BaseServiceComManager(ComManager *comManager, QString service_key, QObject *parent)
: BaseComManager{comManager->getServerUrl(), parent}
, m_comManager(comManager)
, m_serviceKey(service_key)
{
// Set initial user token
setCredentials(m_comManager->getCurrentToken());

// Connect signals
connectSignals();

// Query service information
QUrlQuery args;
args.addQueryItem(WEB_QUERY_SERVICE_KEY, m_serviceKey);
m_comManager->doGet(WEB_SERVICEINFO_PATH, args);

}

void BaseServiceComManager::doGet(const QString &path, const QUrlQuery &query_args, const bool &use_token)
{
BaseComManager::doGet(getServiceEndpoint(path), query_args, use_token);
}

void BaseServiceComManager::doPost(const QString &path, const QString &post_data, const bool &use_token)
{
BaseComManager::doPost(getServiceEndpoint(path), post_data, use_token);
}

void BaseServiceComManager::doPost(const QUrl &full_url, const QString &post_data, const bool &use_token)
{
BaseComManager::doPost(getServiceEndpoint(full_url.toString()), post_data, use_token);
}

void BaseServiceComManager::doPostWithParams(const QString &path, const QString &post_data, const QUrlQuery &query_args, const bool &use_token)
{
BaseComManager::doPostWithParams(getServiceEndpoint(path), post_data, query_args, use_token);
}

void BaseServiceComManager::doDelete(const QString &path, const int &id, const bool &use_token)
{
BaseComManager::doDelete(getServiceEndpoint(path), id, use_token);
}

void BaseServiceComManager::connectSignals()
{
connect(m_comManager, &ComManager::userTokenUpdated, this, &BaseServiceComManager::handleUserTokenUpdated);
connect(m_comManager, &ComManager::servicesReceived, this, &BaseServiceComManager::processServicesReply);
}

bool BaseServiceComManager::handleDataReply(const QString &reply_path, const QString &reply_data, const QUrlQuery &reply_query)
{
QJsonParseError json_error;

// Process reply
QString data_str = filterReplyString(reply_data);

QJsonDocument data_list = QJsonDocument::fromJson(data_str.toUtf8(), &json_error);
if (json_error.error!= QJsonParseError::NoError){
LOG_ERROR("Received a JSON string for " + reply_path + " with " + reply_query.toString() + " with error: " + json_error.errorString(), "DashboardsComManager::handleDataReply");
return false;
}

// Browse each items received
QList<QJsonObject> items;
if (data_list.isArray()){
QJsonArray data_list_array = data_list.array();
for (const QJsonValue &data:std::as_const(data_list_array)){
items.append(data.toObject());
}
}else{
items.append(data_list.object());
}
// Always emit generic signal
emit dataReceived(items, reply_path, reply_query);

return true;
}

QString BaseServiceComManager::getServiceEndpoint(const QString& path)
{
return m_service.getFieldValue("service_clientendpoint").toString() + "/" + path;
}

bool BaseServiceComManager::processNetworkReply(QNetworkReply *reply)
{
QString reply_path = reply->url().path();
QString reply_data = reply->readAll();
QUrlQuery reply_query = QUrlQuery(reply->url().query());
//qDebug() << reply_path << " ---> " << reply_data << ": " << reply->url().query();

bool handled = false;

if (reply->operation()==QNetworkAccessManager::GetOperation){
if (!handled){
// General case
handled = handleDataReply(reply_path, reply_data, reply_query);
if (handled) emit queryResultsOK(reply_path, reply_query);
}
}

if (reply->operation()==QNetworkAccessManager::PostOperation){
if (!handled){
handled = handleDataReply(reply_path, reply_data, reply_query);
if (handled) emit postResultsOK(reply_path, reply_data);
}
}

if (reply->operation()==QNetworkAccessManager::DeleteOperation){
// Extract id from url
int id = 0;
if (reply_query.hasQueryItem("id")){
id = reply_query.queryItemValue("id").toInt();
}
emit deleteResultsOK(reply_path, id);
handled=true;
}

return handled;
}

void BaseServiceComManager::handleUserTokenUpdated()
{
// Update token
setCredentials(m_comManager->getCurrentToken());
}

void BaseServiceComManager::processServicesReply(QList<TeraData> services, QUrlQuery reply_query)
{
for(const TeraData &service:services){
if (service.hasFieldName("service_key")){
if (service.getFieldValue("service_key").toString() == m_serviceKey){
m_service = service;
break;
}
}
}

disconnect(m_comManager, &ComManager::servicesReceived, this, &BaseServiceComManager::processServicesReply);
}
42 changes: 42 additions & 0 deletions client/src/services/BaseServiceComManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef BASESERVICECOMMANAGER_H
#define BASESERVICECOMMANAGER_H

#include <QObject>
#include "managers/BaseComManager.h"
#include "managers/ComManager.h"

class BaseServiceComManager : public BaseComManager
{
Q_OBJECT
public:
explicit BaseServiceComManager(ComManager* comManager, QString service_key, QObject *parent = nullptr);

void doGet(const QString &path, const QUrlQuery &query_args = QUrlQuery(), const bool &use_token=true) override;
void doPost(const QString &path, const QString &post_data, const bool &use_token=true) override;
void doPost(const QUrl &full_url, const QString &post_data, const bool &use_token=true) override;
void doPostWithParams(const QString &path, const QString &post_data, const QUrlQuery &query_args, const bool &use_token=true) override;
void doDelete(const QString &path, const int& id, const bool &use_token=true) override;

private:
void connectSignals();
QString getServiceEndpoint(const QString &path);

protected:
ComManager* m_comManager;
TeraData m_service;
QString m_serviceKey;

bool processNetworkReply(QNetworkReply* reply) override;
virtual bool handleDataReply(const QString& reply_path, const QString &reply_data, const QUrlQuery &reply_query);

private slots:
void handleUserTokenUpdated();

void processServicesReply(QList<TeraData> services, QUrlQuery reply_query);

signals:
void dataReceived(QList<QJsonObject> items, QString endpoint, QUrlQuery reply_query);

};

#endif // BASESERVICECOMMANAGER_H
81 changes: 4 additions & 77 deletions client/src/services/SurveyService/SurveyComManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,98 +2,25 @@
#include "SurveyServiceWebAPI.h"

SurveyComManager::SurveyComManager(ComManager *comManager, QObject *parent)
: BaseComManager{comManager->getServerUrl(), parent},
m_comManager(comManager)
: BaseServiceComManager{comManager, "SurveyJSService", parent}
{
// Set initial user token
setCredentials(m_comManager->getCurrentToken());

// Connect signals
connectSignals();

}

void SurveyComManager::connectSignals()
{
connect(m_comManager, &ComManager::userTokenUpdated, this, &SurveyComManager::handleUserTokenUpdated);
connect(this, &BaseServiceComManager::dataReceived, this, &SurveyComManager::onDataReceived);
}

bool SurveyComManager::processNetworkReply(QNetworkReply *reply)
void SurveyComManager::onDataReceived(QList<QJsonObject> items, QString reply_path, QUrlQuery reply_query)
{
QString reply_path = reply->url().path();
QString reply_data = reply->readAll();
QUrlQuery reply_query = QUrlQuery(reply->url().query());
//qDebug() << reply_path << " ---> " << reply_data << ": " << reply->url().query();

bool handled = false;

if (reply->operation()==QNetworkAccessManager::GetOperation){
if (!handled){
// General case
handled = handleDataReply(reply_path, reply_data, reply_query);
if (handled) emit queryResultsOK(reply_path, reply_query);
}
}

if (reply->operation()==QNetworkAccessManager::PostOperation){
if (!handled){
handled = handleDataReply(reply_path, reply_data, reply_query);
if (handled) emit postResultsOK(reply_path, reply_data);
}
}

if (reply->operation()==QNetworkAccessManager::DeleteOperation){
// Extract id from url
int id = 0;
if (reply_query.hasQueryItem("id")){
id = reply_query.queryItemValue("id").toInt();
}
emit deleteResultsOK(reply_path, id);
handled=true;
}

return handled;
}

bool SurveyComManager::handleDataReply(const QString &reply_path, const QString &reply_data, const QUrlQuery &reply_query)
{
QJsonParseError json_error;

// Process reply
QString data_str = filterReplyString(reply_data);

QJsonDocument data_list = QJsonDocument::fromJson(data_str.toUtf8(), &json_error);
if (json_error.error!= QJsonParseError::NoError){
LOG_ERROR("Received a JSON string for " + reply_path + " with " + reply_query.toString() + " with error: " + json_error.errorString(), "DashboardsComManager::handleDataReply");
return false;
}

// Browse each items received
QList<QJsonObject> items;
if (data_list.isArray()){
QJsonArray data_list_array = data_list.array();
for (const QJsonValue &data:std::as_const(data_list_array)){
items.append(data.toObject());
}
}else{
items.append(data_list.object());
}

Q_UNUSED(reply_query)
// Check to emit correct signals for specific data types
if (reply_path.endsWith(SURVEY_ACTIVE_PATH)){
if (!items.empty()){
emit activeSurveyReceived(items.first());
}
}

// Always emit generic signal
emit dataReceived(items, reply_query);

return true;
}

void SurveyComManager::handleUserTokenUpdated()
{
// Update token
setCredentials(m_comManager->getCurrentToken());
}
10 changes: 3 additions & 7 deletions client/src/services/SurveyService/SurveyComManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,23 @@
#define SURVEYCOMMANAGER_H

#include <QObject>
#include "managers/BaseComManager.h"
#include "services/BaseServiceComManager.h"
#include "managers/ComManager.h"

class SurveyComManager : public BaseComManager
class SurveyComManager : public BaseServiceComManager
{
Q_OBJECT
public:
explicit SurveyComManager(ComManager* comManager, QObject *parent = nullptr);

private:
ComManager* m_comManager;

void connectSignals();
bool processNetworkReply(QNetworkReply* reply) override;
bool handleDataReply(const QString& reply_path, const QString &reply_data, const QUrlQuery &reply_query);

private slots:
void handleUserTokenUpdated();
void onDataReceived(QList<QJsonObject> items, QString reply_path, QUrlQuery reply_query);

signals:
void dataReceived(QList<QJsonObject> items, QUrlQuery reply_query);
void activeSurveyReceived(QJsonObject survey);

};
Expand Down
5 changes: 3 additions & 2 deletions client/src/services/SurveyService/SurveyServiceConfigWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ private slots:
void on_btnSave_clicked();

void processTestTypesReply(QList<TeraData> ttp_list, QUrlQuery reply_query);

void processActiveSurveyReply(const QJsonObject survey);
void handleNetworkError(QNetworkReply::NetworkError error, QString error_msg, QNetworkAccessManager::Operation op, int status_code);
void nextMessageWasShown(Message current_message);
Expand All @@ -38,8 +39,8 @@ private slots:
private:
Ui::SurveyServiceConfigWidget *ui;

ComManager* m_comManager;
SurveyComManager* m_surveyComManager;
ComManager* m_comManager;
SurveyComManager* m_surveyComManager;

int m_id_project;
int m_current_id = 0;
Expand Down
4 changes: 2 additions & 2 deletions client/src/services/SurveyService/SurveyServiceWebAPI.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef SURVEYWEBAPI_H
#define SURVEYWEBAPI_H

#define SURVEY_ACTIVE_PATH "/surveys/api/surveys/active"
#define SURVEY_RESULTS_PATH "/surveys/api/surveys/results"
#define SURVEY_ACTIVE_PATH "api/surveys/active"
#define SURVEY_RESULTS_PATH "api/surveys/results"


#endif // SURVEYWEBAPI_H

0 comments on commit b48c643

Please sign in to comment.