mirror of
https://gitlab.com/spaceti-app/integrations/acs/colnod-connector.git
synced 2026-07-12 17:50:37 +02:00
Add class for proccesing data
This commit is contained in:
parent
06e5d7a9c4
commit
d16b8e67ba
@ -3,6 +3,7 @@ INCLUDEPATH += $$PWD
|
||||
message($$PWD)
|
||||
SOURCES += \
|
||||
$$PWD/databasemanager.cpp \
|
||||
$$PWD/dataprocessor.cpp \
|
||||
$$PWD/entity.cpp \
|
||||
$$PWD/requestmanager.cpp \
|
||||
$$PWD/subject.cpp \
|
||||
@ -10,6 +11,7 @@ SOURCES += \
|
||||
|
||||
HEADERS += \
|
||||
$$PWD/databasemanager.h \
|
||||
$$PWD/dataprocessor.h \
|
||||
$$PWD/entity.h \
|
||||
$$PWD/requestmanager.h \
|
||||
$$PWD/subject.h \
|
||||
|
||||
@ -1,19 +1,29 @@
|
||||
#include "databasemanager.h"
|
||||
|
||||
DatabaseManager::DatabaseManager(QString dbName, QString user, QString password)
|
||||
DatabaseManager::DatabaseManager(QString host, QString dbName, QString user, QString password)
|
||||
{
|
||||
|
||||
db = QSqlDatabase::addDatabase("QPSQL");
|
||||
|
||||
db.setDatabaseName(dbName);
|
||||
db.setHostName("localhost");
|
||||
//db.setHostName("192.168.1.201");
|
||||
db.setHostName(host);
|
||||
db.setUserName(user);
|
||||
db.setPassword(password);
|
||||
if (!db.open())
|
||||
opened = db.open();
|
||||
if (!opened)
|
||||
{
|
||||
qDebug() << "Opening error" + db.lastError().text();
|
||||
}
|
||||
query = QSqlQuery("query", db);
|
||||
}
|
||||
|
||||
void DatabaseManager::saveSubject(Subject subject, QString table)
|
||||
{
|
||||
//TODO
|
||||
}
|
||||
|
||||
void DatabaseManager::saveToken(Subject subject, QString table)
|
||||
{
|
||||
//TODO
|
||||
}
|
||||
|
||||
|
||||
@ -7,17 +7,28 @@
|
||||
|
||||
#include <QVariant>
|
||||
#include <QDebug>
|
||||
#include "subject.h"
|
||||
#include "token.h"
|
||||
|
||||
|
||||
|
||||
class DatabaseManager
|
||||
class DatabaseManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
DatabaseManager(QString dbName, QString user, QString password);
|
||||
DatabaseManager(QString host, QString dbName, QString user, QString password);
|
||||
|
||||
void saveSubject(Subject subject, QString table);
|
||||
void saveToken(Subject subject, QString table);
|
||||
|
||||
bool getOpened() {return opened;}
|
||||
|
||||
private:
|
||||
QSqlDatabase db;
|
||||
QSqlQuery query;
|
||||
|
||||
bool opened;
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // DATABASEMANAGER_H
|
||||
|
||||
56
src/colnod/dataprocessor.cpp
Normal file
56
src/colnod/dataprocessor.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
#include "dataprocessor.h"
|
||||
|
||||
DataProcessor::DataProcessor()
|
||||
{
|
||||
|
||||
db = new DatabaseManager("192.168.1.94", "deloitte", "postgres", "34rjkciea12");
|
||||
reqMan = new RequestManager;
|
||||
}
|
||||
|
||||
void DataProcessor::processSubjects()
|
||||
{
|
||||
reqMan->getSubjects();
|
||||
QEventLoop loop;
|
||||
QObject::connect(reqMan, &RequestManager::responseReady, &loop, &QEventLoop::quit);
|
||||
loop.exec();
|
||||
prepareSubjectJson(reqMan->getResponse());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
QUuid DataProcessor::getSubjectId(const QString &email) const
|
||||
{
|
||||
auto subject = this->getSubjectByEmail(email);
|
||||
if (!subject)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
return subject->getId();
|
||||
}
|
||||
|
||||
Subject *DataProccesor::getSubjectById(const QUuid &id) const
|
||||
{
|
||||
return this->subjectsById.value(id);
|
||||
}
|
||||
|
||||
Subject DataProcessor::getSubjectByEmail(const QString &email) const
|
||||
{
|
||||
return this->subjectsByEmail.value(email);
|
||||
}
|
||||
|
||||
Token *Client::getTokenById(const QUuid &id) const
|
||||
{
|
||||
return this->tokensById.value(id);
|
||||
}
|
||||
|
||||
Token *Client::getTokenByData(const QString &data) const
|
||||
{
|
||||
return this->tokensByData.value(data);
|
||||
}*/
|
||||
|
||||
void DataProcessor::prepareSubjectJson(QString json)
|
||||
{
|
||||
QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8());
|
||||
subjectArr = doc.object()["items"].toArray();
|
||||
}
|
||||
41
src/colnod/dataprocessor.h
Normal file
41
src/colnod/dataprocessor.h
Normal file
@ -0,0 +1,41 @@
|
||||
#ifndef DATAPROCESSOR_H
|
||||
#define DATAPROCESSOR_H
|
||||
|
||||
#include "token.h"
|
||||
#include "subject.h"
|
||||
#include "requestmanager.h"
|
||||
#include "databasemanager.h"
|
||||
#include <QJsonDocument>
|
||||
#include <qjsonarray.h>
|
||||
|
||||
|
||||
|
||||
class DataProcessor : public QObject
|
||||
{
|
||||
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
DataProcessor();
|
||||
|
||||
void processSubjects();
|
||||
|
||||
Subject *getSubjectById(const QUuid &id) const;
|
||||
Subject *getSubjectByEmail(const QString &email) const;
|
||||
QUuid getSubjectId(const QString &email) const;
|
||||
|
||||
Token *getTokenById(const QUuid &id) const;
|
||||
Token *getTokenByData(const QString &data) const;
|
||||
|
||||
private slots:
|
||||
void prepareSubjectJson(QString json);
|
||||
|
||||
private:
|
||||
RequestManager *reqMan;
|
||||
DatabaseManager *db;
|
||||
QJsonArray subjectArr;
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // DATAPROCESSOR_H
|
||||
@ -6,6 +6,8 @@ RequestManager::RequestManager()
|
||||
token = "";
|
||||
loginStatus = false;
|
||||
|
||||
|
||||
|
||||
QFile configFile(QDir::current().filePath("config.json"));
|
||||
|
||||
if (!configFile.open(QIODevice::ReadOnly)) {
|
||||
@ -40,11 +42,11 @@ void RequestManager::login(QString host, QString username, QString password)
|
||||
{
|
||||
qDebug() << reply->errorString();
|
||||
loginStatus = false;
|
||||
qDebug() << reply->readAll();
|
||||
return;
|
||||
}
|
||||
jsonResponse = reply->readAll();
|
||||
prepareAuthHeader();
|
||||
loginStatus = true;
|
||||
emit successLogin();
|
||||
reply->deleteLater();
|
||||
});
|
||||
@ -52,6 +54,13 @@ void RequestManager::login(QString host, QString username, QString password)
|
||||
|
||||
void RequestManager::getRequest(QString endpoint)
|
||||
{
|
||||
if (!loginStatus)
|
||||
{
|
||||
login(config.object()["host"].toString(), config.object()["username"].toString(), config.object()["password"].toString());
|
||||
QEventLoop loop;
|
||||
QObject::connect(this, &RequestManager::successLogin, &loop, &QEventLoop::quit);
|
||||
loop.exec();
|
||||
}
|
||||
QNetworkRequest request;
|
||||
request.setUrl(host + endpoint);
|
||||
request.setRawHeader("Authorization", authHeader.toLocal8Bit());
|
||||
@ -61,6 +70,8 @@ void RequestManager::getRequest(QString endpoint)
|
||||
{
|
||||
qDebug() << reply->errorString();
|
||||
qDebug() << reply->readAll();
|
||||
loginStatus = false;
|
||||
getRequest(endpoint);
|
||||
return;
|
||||
}
|
||||
qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
||||
@ -109,3 +120,8 @@ void RequestManager::onSSLError(QNetworkReply *reply, const QList<QSslError> &er
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void RequestManager::getSubjects()
|
||||
{
|
||||
getRequest("/SubjectManager/getSubjects");
|
||||
}
|
||||
|
||||
@ -20,24 +20,35 @@
|
||||
#include <QDir>
|
||||
#include <QSslError>
|
||||
|
||||
|
||||
#include "databasemanager.h"
|
||||
#include "token.h"
|
||||
#include "subject.h"
|
||||
|
||||
class RequestManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
RequestManager();
|
||||
void getSubjects();
|
||||
void login(QString endpoint, QString username, QString password);
|
||||
void getRequest(QString endpoint);
|
||||
void fetchSubjects(QString endpoint);
|
||||
void loadSubjectsFromColnodToDB();
|
||||
|
||||
|
||||
|
||||
const QJsonDocument &getConfig() const {return config;}
|
||||
const QString &getResponse() const {return jsonResponse;}
|
||||
QString getToken(){return token;}
|
||||
|
||||
private:
|
||||
|
||||
QString getFromJson(QString json, const QString &member);
|
||||
QString encryptedPassword(QString password);
|
||||
QString getLoginHeader(QString username, QString hash);
|
||||
void prepareAuthHeader();
|
||||
|
||||
private slots:
|
||||
void onSSLError(QNetworkReply *reply, const QList<QSslError> &errors);
|
||||
|
||||
@ -48,14 +59,13 @@ signals:
|
||||
|
||||
private:
|
||||
QNetworkAccessManager* manager;
|
||||
|
||||
QJsonDocument config;
|
||||
QString authHeader;
|
||||
QString host;
|
||||
QString token;
|
||||
QString jsonResponse;
|
||||
bool loginStatus;
|
||||
QString tenant;
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
{
|
||||
"host": "https://localhost:8080",
|
||||
"host": "https://192.168.1.3",
|
||||
"username": "spaceti",
|
||||
"password": "SuperSecretPassword"
|
||||
"password": "spaceti1"
|
||||
}
|
||||
58
src/subjects.json
Normal file
58
src/subjects.json
Normal file
@ -0,0 +1,58 @@
|
||||
{
|
||||
"items":[
|
||||
{
|
||||
"id":"6d809c12-3255-4b0a-bf8f-3af86912a40f",
|
||||
"name":"Person2",
|
||||
"description":"",
|
||||
"lastUpdate":1543843828247,
|
||||
"attributes":{
|
||||
"items":{
|
||||
|
||||
}
|
||||
},
|
||||
"tokenFlags":[
|
||||
"NoPin"
|
||||
],
|
||||
"pin":"",
|
||||
"pinFlags":[
|
||||
"Access"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id":"1a23c502-1665-4492-94ea-039bb7505cae",
|
||||
"name":"Person3",
|
||||
"description":"",
|
||||
"lastUpdate":1553868160516,
|
||||
"attributes":{
|
||||
"items":{
|
||||
"email":"person3@gmail.com"
|
||||
}
|
||||
},
|
||||
"tokenFlags":[
|
||||
"NoPin"
|
||||
],
|
||||
"pin":"",
|
||||
"pinFlags":[
|
||||
"Access"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id":"7ad58bd8-a1ec-40b4-a54f-347ee6a4ba29",
|
||||
"name":"Person1",
|
||||
"description":"",
|
||||
"lastUpdate":1560786050717,
|
||||
"attributes":{
|
||||
"items":{
|
||||
"email":"person1@gmail.com"
|
||||
}
|
||||
},
|
||||
"tokenFlags":[
|
||||
"NoPin"
|
||||
],
|
||||
"pin":"",
|
||||
"pinFlags":[
|
||||
"Access"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -19,7 +19,7 @@ void UnitTest::loginSuccessfully()
|
||||
void UnitTest::getSubjects()
|
||||
{
|
||||
RequestManager* testAccess = new RequestManager();
|
||||
testAccess->login(host, email, password);
|
||||
// testAccess->login(host, email, password);
|
||||
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
||||
testAccess->getRequest("/SubjectManager/getSubjects");
|
||||
spy.wait();
|
||||
@ -37,7 +37,7 @@ void UnitTest::getSubjects()
|
||||
void UnitTest::getTokens()
|
||||
{
|
||||
RequestManager* testAccess = new RequestManager();
|
||||
testAccess->login(host, email, password);
|
||||
// testAccess->login(host, email, password);
|
||||
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
||||
testAccess->getRequest("/TokenManager/getTokens");
|
||||
QCOMPARE(spy.count(), 0);
|
||||
@ -50,7 +50,7 @@ void UnitTest::getTokens()
|
||||
void UnitTest::getTokensOpenManager()
|
||||
{
|
||||
RequestManager* testAccess = new RequestManager();
|
||||
testAccess->login(host, email, password);
|
||||
// testAccess->login(host, email, password);
|
||||
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
||||
testAccess->getRequest("/OpenManager/getTokenSubject");
|
||||
QCOMPARE(spy.count(), 0);
|
||||
@ -79,7 +79,7 @@ void UnitTest::subjectJSONParser()
|
||||
*/
|
||||
|
||||
RequestManager* testAccess = new RequestManager();
|
||||
testAccess->login(host, email, password);
|
||||
// testAccess->login(host, email, password);
|
||||
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
||||
testAccess->getRequest("/SubjectManager/getSubjects");
|
||||
spy.wait();
|
||||
@ -88,9 +88,36 @@ void UnitTest::subjectJSONParser()
|
||||
QJsonArray arr = doc.object()["items"].toArray();
|
||||
auto testSubject = new Subject();
|
||||
testSubject->fromJSON(arr[1].toObject());
|
||||
qDebug() << testSubject->getName();
|
||||
qDebug() << testSubject->getName() << " Size: " << arr.size();
|
||||
QVERIFY(!testSubject->getName().isEmpty());
|
||||
}
|
||||
|
||||
void UnitTest::tokenJSONParser()
|
||||
{
|
||||
RequestManager* testAccess = new RequestManager();
|
||||
// testAccess->login(host, email, password);
|
||||
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
||||
testAccess->getRequest("/TokenManager/getTokens");
|
||||
spy.wait();
|
||||
QString response = testAccess->getResponse();
|
||||
|
||||
//TODO
|
||||
/* QJsonDocument doc = QJsonDocument::fromJson(response.toUtf8());
|
||||
QJsonArray arr = doc.object()["items"].toArray();
|
||||
auto testSubject = new Subject();
|
||||
testSubject->fromJSON(arr[1].toObject());
|
||||
qDebug() << testSubject->getName();
|
||||
QVERIFY(!testSubject->getName().isEmpty());*/
|
||||
QVERIFY(true);
|
||||
|
||||
}
|
||||
|
||||
void UnitTest::connectToDB()
|
||||
{
|
||||
DatabaseManager *db = new DatabaseManager("192.168.1.94", "deloitte", "postgres", "34rjkciea12");
|
||||
QVERIFY(db->getOpened());
|
||||
QVERIFY(true);
|
||||
|
||||
}
|
||||
|
||||
QTEST_MAIN(UnitTest)
|
||||
|
||||
@ -1,5 +0,0 @@
|
||||
{
|
||||
"host": "https://localhost:8080",
|
||||
"username": "spaceti",
|
||||
"password": "SuperSecretPassword"
|
||||
}
|
||||
@ -4,6 +4,7 @@
|
||||
#include <QSignalSpy>
|
||||
#include <requestmanager.h>
|
||||
#include "subject.h"
|
||||
#include "databasemanager.h"
|
||||
|
||||
|
||||
class UnitTest : public QObject
|
||||
@ -17,6 +18,9 @@ private slots:
|
||||
void getTokens(); // /TokenManager/getTokens
|
||||
void getTokensOpenManager(); // /OpenManager/getTokenSubject
|
||||
void subjectJSONParser();
|
||||
void tokenJSONParser();
|
||||
void connectToDB();
|
||||
|
||||
private:
|
||||
// QString host = "https://localhost:8080"; //colnod server (ssh -L 8080:192.168.254.11:8443 penta-master)
|
||||
QString host = "https://192.168.1.3:8443"; //local testing server
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user