forked from ondra/colnod-connector
Add property for save data from colnod to backend DB
This commit is contained in:
parent
0efd39d4ae
commit
a510938999
@ -1,10 +1,20 @@
|
|||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include "requestmanager.h"
|
|
||||||
|
#include "data.h"
|
||||||
|
#include "datamanager.h"
|
||||||
|
#include "colnodapi.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
QCoreApplication a(argc, argv);
|
QCoreApplication a(argc, argv);
|
||||||
//RequestManager *reqman = new RequestManager();
|
ColnodAPI *api = new ColnodAPI();
|
||||||
|
|
||||||
|
api->downloadData(); // this will start downloading data
|
||||||
|
QObject::connect(api, &ColnodAPI::dataDownloaded, [=](){
|
||||||
|
qDebug() << "now I have all data";
|
||||||
|
api->syncData();
|
||||||
|
QCoreApplication::quit();
|
||||||
|
});
|
||||||
|
|
||||||
return a.exec();
|
return a.exec();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,20 +2,24 @@ INCLUDEPATH += $$PWD
|
|||||||
#SRC_DIR = $$PWD
|
#SRC_DIR = $$PWD
|
||||||
message($$PWD)
|
message($$PWD)
|
||||||
SOURCES += \
|
SOURCES += \
|
||||||
|
$$PWD/colnodapi.cpp \
|
||||||
$$PWD/data.cpp \
|
$$PWD/data.cpp \
|
||||||
$$PWD/databasemanager.cpp \
|
$$PWD/databasemanager.cpp \
|
||||||
$$PWD/dataprocessor.cpp \
|
$$PWD/datamanager.cpp \
|
||||||
$$PWD/entity.cpp \
|
$$PWD/entity.cpp \
|
||||||
$$PWD/requestmanager.cpp \
|
$$PWD/requestmanager.cpp \
|
||||||
|
$$PWD/response.cpp \
|
||||||
$$PWD/subject.cpp \
|
$$PWD/subject.cpp \
|
||||||
$$PWD/token.cpp
|
$$PWD/token.cpp
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
|
$$PWD/colnodapi.h \
|
||||||
$$PWD/data.h \
|
$$PWD/data.h \
|
||||||
$$PWD/databasemanager.h \
|
$$PWD/databasemanager.h \
|
||||||
$$PWD/dataprocessor.h \
|
$$PWD/datamanager.h \
|
||||||
$$PWD/entity.h \
|
$$PWD/entity.h \
|
||||||
$$PWD/requestmanager.h \
|
$$PWD/requestmanager.h \
|
||||||
|
$$PWD/response.h \
|
||||||
$$PWD/subject.h \
|
$$PWD/subject.h \
|
||||||
$$PWD/token.h
|
$$PWD/token.h
|
||||||
message($$SOURCES)
|
message($$SOURCES)
|
||||||
|
|||||||
@ -2,5 +2,53 @@
|
|||||||
|
|
||||||
ColnodAPI::ColnodAPI()
|
ColnodAPI::ColnodAPI()
|
||||||
{
|
{
|
||||||
|
reqMan = new RequestManager();
|
||||||
|
//data = new Data();
|
||||||
|
dataMan = new DataManager();
|
||||||
|
connect(this, &ColnodAPI::partReady, this, &ColnodAPI::checkPartOfData);
|
||||||
|
connect(this, &ColnodAPI::partReady, this, &ColnodAPI::areDataReady);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// PUBLIC FUNCTIONS
|
||||||
|
void ColnodAPI::downloadData()
|
||||||
|
{
|
||||||
|
Response* resp0 = reqMan->getSubjects();
|
||||||
|
connect(resp0, &Response::finished, [=](){
|
||||||
|
dataMan->localSaveSubjects(resp0->reply->readAll());
|
||||||
|
emit partReady();
|
||||||
|
});
|
||||||
|
|
||||||
|
Response* resp1 = reqMan->getTokens();
|
||||||
|
connect(resp1, &Response::finished, [=](){
|
||||||
|
dataMan->localSaveTokens(resp1->reply->readAll());
|
||||||
|
emit partReady();
|
||||||
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
QNetworkReply* reply2 = reqMan->getTokens();
|
||||||
|
connect(reply2, &QNetworkReply::finished, [=](){
|
||||||
|
data->saveTokens(reply2->readAll());
|
||||||
|
});
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ColnodAPI::syncData()
|
||||||
|
{
|
||||||
|
dataMan->pushDataToDB();
|
||||||
|
}
|
||||||
|
|
||||||
|
// PRIVATE FUNCTIONS
|
||||||
|
void ColnodAPI::areDataReady()
|
||||||
|
{
|
||||||
|
if (index >= numOfMembers)
|
||||||
|
{
|
||||||
|
emit dataDownloaded();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ColnodAPI::checkPartOfData()
|
||||||
|
{
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,39 @@
|
|||||||
#ifndef COLNODAPI_H
|
#ifndef COLNODAPI_H
|
||||||
#define COLNODAPI_H
|
#define COLNODAPI_H
|
||||||
|
|
||||||
|
#include "requestmanager.h"
|
||||||
|
#include "data.h"
|
||||||
|
#include "datamanager.h"
|
||||||
|
|
||||||
class ColnodAPI
|
|
||||||
|
|
||||||
|
|
||||||
|
class ColnodAPI : public QObject
|
||||||
{
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
ColnodAPI();
|
ColnodAPI();
|
||||||
|
void downloadData();
|
||||||
|
void syncData();
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void dataDownloaded();
|
||||||
|
void partReady();
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void areDataReady();
|
||||||
|
void checkPartOfData();
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
RequestManager* reqMan = nullptr;
|
||||||
|
// Data* data = nullptr;
|
||||||
|
DataManager* dataMan = nullptr;
|
||||||
|
bool dataReady[2] = {false};
|
||||||
|
int index = 0;
|
||||||
|
const int numOfMembers = 2;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // COLNODAPI_H
|
#endif // COLNODAPI_H
|
||||||
|
|||||||
@ -4,3 +4,4 @@ Data::Data()
|
|||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
#include "subject.h"
|
#include "subject.h"
|
||||||
#include "token.h"
|
#include "token.h"
|
||||||
|
#include <qjsondocument.h>
|
||||||
|
#include <qjsonarray.h>
|
||||||
|
|
||||||
class Data : public QObject
|
class Data : public QObject
|
||||||
{
|
{
|
||||||
@ -13,9 +15,9 @@ public:
|
|||||||
Data();
|
Data();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QList<Subject> subjects;
|
|
||||||
QList<Token> tokens;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -17,14 +17,24 @@ DatabaseManager::DatabaseManager(QString host, QString dbName, QString user, QSt
|
|||||||
query = QSqlQuery("query", db);
|
query = QSqlQuery("query", db);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DatabaseManager::saveSubject(Subject subject, QString table)
|
void DatabaseManager::saveSubject(Subject* subject, QString table)
|
||||||
{
|
{
|
||||||
//TODO
|
query.prepare("INSERT INTO subject (id, name) VALUES (:id, :name)");
|
||||||
|
query.bindValue(":table", table);
|
||||||
|
query.bindValue(":id",subject->getId().toString());
|
||||||
|
qDebug() << subject->getId().toString() << subject->getName();
|
||||||
|
query.bindValue(":name", subject->getName());
|
||||||
|
query.exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DatabaseManager::saveToken(Subject subject, QString table)
|
void DatabaseManager::saveToken(Token* token, QString table)
|
||||||
{
|
{
|
||||||
//TODO
|
query.prepare("INSERT INTO :table (id, name) VALUES (:id, :data, :subject_id)");
|
||||||
|
query.bindValue(":table", table);
|
||||||
|
query.bindValue(":id",token->getId());
|
||||||
|
query.bindValue(":data", token->getTokenData());
|
||||||
|
query.bindValue(":subject_id", token->getSubjectId());
|
||||||
|
query.exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
QString DatabaseManager::getTestMessage(int id)
|
QString DatabaseManager::getTestMessage(int id)
|
||||||
@ -42,8 +52,13 @@ void DatabaseManager::createTestRecord(QString msg)
|
|||||||
query.exec("SELECT MAX(id) FROM test");
|
query.exec("SELECT MAX(id) FROM test");
|
||||||
query.next();
|
query.next();
|
||||||
int id = query.value(0).toInt() + 1;
|
int id = query.value(0).toInt() + 1;
|
||||||
QString statement = QString("INSERT INTO test (id, message) VALUES (%1, '%2')").arg(id).arg(msg);
|
//QString statement = QString("INSERT INTO test (id, message) VALUES (%1, '%2')").arg(id).arg(msg);
|
||||||
query.exec(statement);
|
//query.exec(statement);
|
||||||
|
|
||||||
|
query.prepare("INSERT INTO test (id, message) VALUES (:id, :msg)");
|
||||||
|
query.bindValue(":id", id);
|
||||||
|
query.bindValue(":msg", msg);
|
||||||
|
query.exec();
|
||||||
query.next();
|
query.next();
|
||||||
qDebug() << "U inserted new row, id: " << id << "msg: " << getTestMessage(id);
|
qDebug() << "U inserted new row, id: " << id << "msg: " << getTestMessage(id);
|
||||||
|
|
||||||
|
|||||||
@ -17,8 +17,8 @@ class DatabaseManager : public QObject
|
|||||||
public:
|
public:
|
||||||
DatabaseManager(QString host, QString dbName, QString user, QString password);
|
DatabaseManager(QString host, QString dbName, QString user, QString password);
|
||||||
|
|
||||||
void saveSubject(Subject subject, QString table);
|
void saveSubject(Subject* subject, QString table);
|
||||||
void saveToken(Subject subject, QString table);
|
void saveToken(Token* token, QString table);
|
||||||
|
|
||||||
QString getTestMessage(int id);
|
QString getTestMessage(int id);
|
||||||
void createTestRecord(QString msg);
|
void createTestRecord(QString msg);
|
||||||
|
|||||||
44
src/colnod/datamanager.cpp
Normal file
44
src/colnod/datamanager.cpp
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#include "datamanager.h"
|
||||||
|
|
||||||
|
DataManager::DataManager()
|
||||||
|
{
|
||||||
|
db = new DatabaseManager("192.168.1.94", "deloitte", "postgres", "34rjkciea12");
|
||||||
|
}
|
||||||
|
|
||||||
|
void DataManager::localSaveSubjects(QString response)
|
||||||
|
{
|
||||||
|
QJsonDocument doc = QJsonDocument::fromJson(response.toUtf8());
|
||||||
|
QJsonArray items = doc.object()["items"].toArray();
|
||||||
|
for (QJsonValueRef item : items)
|
||||||
|
{
|
||||||
|
auto subject = new Subject();
|
||||||
|
subject->fromJSON(item.toObject());
|
||||||
|
subjects.append(subject);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void DataManager::localSaveTokens(QString response)
|
||||||
|
{
|
||||||
|
QJsonDocument doc = QJsonDocument::fromJson(response.toUtf8());
|
||||||
|
QJsonArray items = doc.object()["items"].toArray();
|
||||||
|
for (QJsonValueRef item : items)
|
||||||
|
{
|
||||||
|
auto token = new Token();
|
||||||
|
token->fromJSON(item.toObject());
|
||||||
|
tokens.append(token);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void DataManager::pushDataToDB()
|
||||||
|
{
|
||||||
|
for (auto subject : subjects)
|
||||||
|
{
|
||||||
|
db->saveSubject(subject, "subject");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto token : tokens)
|
||||||
|
{
|
||||||
|
db->saveToken(token, "Subjects");
|
||||||
|
}
|
||||||
|
}
|
||||||
35
src/colnod/datamanager.h
Normal file
35
src/colnod/datamanager.h
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
#ifndef DATAMANAGER_H
|
||||||
|
#define DATAMANAGER_H
|
||||||
|
|
||||||
|
#include "token.h"
|
||||||
|
#include "subject.h"
|
||||||
|
#include "requestmanager.h"
|
||||||
|
#include "databasemanager.h"
|
||||||
|
#include <QJsonDocument>
|
||||||
|
#include <qjsonarray.h>
|
||||||
|
#include "data.h"
|
||||||
|
|
||||||
|
|
||||||
|
class DataManager : public QObject
|
||||||
|
{
|
||||||
|
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
DataManager();
|
||||||
|
|
||||||
|
void localSaveSubjects(QString jsonResponse);
|
||||||
|
void localSaveTokens(QString jsonResponse);
|
||||||
|
void pushDataToDB();
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
DatabaseManager *db = nullptr;
|
||||||
|
QList<QPointer<Subject>> subjects;
|
||||||
|
QList<QPointer<Token>> tokens;
|
||||||
|
//Data *dataToProcess = nullptr;
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // DATAMANAGER_H
|
||||||
@ -1,55 +0,0 @@
|
|||||||
#include "dataprocessor.h"
|
|
||||||
|
|
||||||
DataProcessor::DataProcessor(Data *data)
|
|
||||||
{
|
|
||||||
|
|
||||||
db = new DatabaseManager("192.168.1.94", "deloitte", "postgres", "34rjkciea12");
|
|
||||||
reqMan = new RequestManager();
|
|
||||||
dataToProcess = data;
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
@ -1,43 +0,0 @@
|
|||||||
#ifndef DATAPROCESSOR_H
|
|
||||||
#define DATAPROCESSOR_H
|
|
||||||
|
|
||||||
#include "token.h"
|
|
||||||
#include "subject.h"
|
|
||||||
#include "requestmanager.h"
|
|
||||||
#include "databasemanager.h"
|
|
||||||
#include <QJsonDocument>
|
|
||||||
#include <qjsonarray.h>
|
|
||||||
#include "data.h"
|
|
||||||
|
|
||||||
|
|
||||||
class DataProcessor : public QObject
|
|
||||||
{
|
|
||||||
|
|
||||||
Q_OBJECT
|
|
||||||
|
|
||||||
public:
|
|
||||||
DataProcessor(Data *data);
|
|
||||||
|
|
||||||
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 = nullptr;
|
|
||||||
DatabaseManager *db = nullptr;
|
|
||||||
QJsonArray subjectArr;
|
|
||||||
|
|
||||||
Data *dataToProcess = nullptr;
|
|
||||||
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // DATAPROCESSOR_H
|
|
||||||
@ -18,16 +18,17 @@ RequestManager::RequestManager()
|
|||||||
|
|
||||||
void RequestManager::login(QString host, QString username, QString password)
|
void RequestManager::login(QString host, QString username, QString password)
|
||||||
{
|
{
|
||||||
|
|
||||||
QNetworkRequest request;
|
QNetworkRequest request;
|
||||||
QString hash = encryptedPassword(password);
|
QString hash = encryptedPassword(this->password);
|
||||||
authHeader = getLoginHeader(username, hash);
|
authHeader = getLoginHeader(this->user, hash);
|
||||||
QJsonObject reqBody = {
|
QJsonObject reqBody = {
|
||||||
{"password", hash},
|
{"password", hash},
|
||||||
{"username", username}
|
{"username", this->user}
|
||||||
};
|
};
|
||||||
this->host = host;
|
//this->host = host;
|
||||||
|
|
||||||
request.setUrl(host + "/AaaManager/authSession");
|
request.setUrl(this->host + "/AaaManager/authSession");
|
||||||
request.setRawHeader("Authorization", authHeader.toLocal8Bit());
|
request.setRawHeader("Authorization", authHeader.toLocal8Bit());
|
||||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json; charset=UTF-8");
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json; charset=UTF-8");
|
||||||
|
|
||||||
@ -41,6 +42,8 @@ void RequestManager::login(QString host, QString username, QString password)
|
|||||||
loginStatus = false;
|
loginStatus = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (loginStatus)
|
||||||
|
return;
|
||||||
jsonResponse = reply->readAll();
|
jsonResponse = reply->readAll();
|
||||||
prepareAuthHeader();
|
prepareAuthHeader();
|
||||||
loginStatus = true;
|
loginStatus = true;
|
||||||
@ -49,17 +52,8 @@ void RequestManager::login(QString host, QString username, QString password)
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void RequestManager::getRequest(QString endpoint)
|
QNetworkReply* RequestManager::getRequest(QString endpoint)
|
||||||
{
|
{
|
||||||
if (!loginStatus)
|
|
||||||
{
|
|
||||||
// login(config.object()["host"].toString(), config.object()["username"].toString(), config.object()["password"].toString());
|
|
||||||
login(host, user, password);
|
|
||||||
|
|
||||||
QEventLoop loop;
|
|
||||||
QObject::connect(this, &RequestManager::successLogin, &loop, &QEventLoop::quit);
|
|
||||||
loop.exec();
|
|
||||||
}
|
|
||||||
QNetworkRequest request;
|
QNetworkRequest request;
|
||||||
request.setUrl(host + endpoint);
|
request.setUrl(host + endpoint);
|
||||||
qDebug() << "Auth: " << authHeader;
|
qDebug() << "Auth: " << authHeader;
|
||||||
@ -73,9 +67,10 @@ void RequestManager::getRequest(QString endpoint)
|
|||||||
//getRequest(endpoint);
|
//getRequest(endpoint);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
jsonResponse = reply->readAll();
|
|
||||||
emit responseReady();
|
emit responseReady();
|
||||||
});
|
});
|
||||||
|
return reply;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString RequestManager::getFromJson(QString json, const QString &member)
|
QString RequestManager::getFromJson(QString json, const QString &member)
|
||||||
@ -119,7 +114,62 @@ void RequestManager::onSSLError(QNetworkReply *reply, const QList<QSslError> &er
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void RequestManager::getSubjects()
|
Response* RequestManager::getSubjects()
|
||||||
{
|
{
|
||||||
getRequest("/SubjectManager/getSubjects");
|
auto response = new Response;
|
||||||
|
if (!loginStatus){
|
||||||
|
login(host, user, password);
|
||||||
|
connect(this, &RequestManager::successLogin, [=](){
|
||||||
|
auto subjectReply = getRequest("/SubjectManager/getSubjects");
|
||||||
|
response->reply = subjectReply;
|
||||||
|
connect(subjectReply, &QNetworkReply::finished, response, &Response::finished);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
QNetworkReply* reply = getRequest("/SubjectManager/getSubjects");
|
||||||
|
connect(reply, &QNetworkReply::finished, response, &Response::finished);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
Response* RequestManager::getTokens()
|
||||||
|
{
|
||||||
|
auto response = new Response;
|
||||||
|
if (!loginStatus){
|
||||||
|
login(host, user, password);
|
||||||
|
connect(this, &RequestManager::successLogin, [=](){
|
||||||
|
auto subjectReply = getRequest("/TokenManager/getTokens");
|
||||||
|
response->reply = subjectReply;
|
||||||
|
connect(subjectReply, &QNetworkReply::finished, response, &Response::finished);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
QNetworkReply* reply = getRequest("/TokenManager/getTokens");
|
||||||
|
connect(reply, &QNetworkReply::finished, response, &Response::finished);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
Response* RequestManager::getTokenSubject()
|
||||||
|
{
|
||||||
|
auto response = new Response;
|
||||||
|
if (!loginStatus){
|
||||||
|
login(host, user, password);
|
||||||
|
connect(this, &RequestManager::successLogin, [=](){
|
||||||
|
auto subjectReply = getRequest("/OpenManager/getTokenSubject");
|
||||||
|
response->reply = subjectReply;
|
||||||
|
connect(subjectReply, &QNetworkReply::finished, response, &Response::finished);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
QNetworkReply* reply = getRequest("/OpenManager/getTokenSubject");
|
||||||
|
connect(reply, &QNetworkReply::finished, response, &Response::finished);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,10 +21,7 @@
|
|||||||
#include <QSslError>
|
#include <QSslError>
|
||||||
|
|
||||||
|
|
||||||
#include "databasemanager.h"
|
#include "response.h"
|
||||||
#include "token.h"
|
|
||||||
#include "subject.h"
|
|
||||||
#include "data.h"
|
|
||||||
|
|
||||||
class RequestManager : public QObject
|
class RequestManager : public QObject
|
||||||
{
|
{
|
||||||
@ -32,19 +29,16 @@ class RequestManager : public QObject
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
RequestManager();
|
RequestManager();
|
||||||
void getSubjects();
|
Response* getSubjects();
|
||||||
void login(QString endpoint, QString username, QString password);
|
Response* getTokens();
|
||||||
void getRequest(QString endpoint);
|
Response* getTokenSubject();
|
||||||
void loadSubjectsFromColnodToDB();
|
void login(QString endpoint, QString username, QString password); //I want it private, but unittest???
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const QJsonDocument &getConfig() const {return config;}
|
const QJsonDocument &getConfig() const {return config;}
|
||||||
const QString &getResponse() const {return jsonResponse;}
|
const QString &getResponse() const {return jsonResponse;}
|
||||||
QString getToken(){return token;}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
QNetworkReply* getRequest(QString endpoint);
|
||||||
QString getFromJson(QString json, const QString &member);
|
QString getFromJson(QString json, const QString &member);
|
||||||
QString encryptedPassword(QString password);
|
QString encryptedPassword(QString password);
|
||||||
QString getLoginHeader(QString username, QString hash);
|
QString getLoginHeader(QString username, QString hash);
|
||||||
@ -53,6 +47,7 @@ private:
|
|||||||
private slots:
|
private slots:
|
||||||
void onSSLError(QNetworkReply *reply, const QList<QSslError> &errors);
|
void onSSLError(QNetworkReply *reply, const QList<QSslError> &errors);
|
||||||
|
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void successLogin();
|
void successLogin();
|
||||||
void responseReady();
|
void responseReady();
|
||||||
@ -70,8 +65,6 @@ private:
|
|||||||
QString token;
|
QString token;
|
||||||
QString jsonResponse;
|
QString jsonResponse;
|
||||||
bool loginStatus;
|
bool loginStatus;
|
||||||
Data *data = nullptr;
|
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
6
src/colnod/response.cpp
Normal file
6
src/colnod/response.cpp
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#include "response.h"
|
||||||
|
|
||||||
|
Response::Response()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
25
src/colnod/response.h
Normal file
25
src/colnod/response.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#ifndef RESPONSE_H
|
||||||
|
#define RESPONSE_H
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
#include <qnetworkreply.h>
|
||||||
|
|
||||||
|
class Response : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
Response();
|
||||||
|
|
||||||
|
public:
|
||||||
|
QNetworkReply *reply = nullptr;
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void finished();
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // RESPONSE_H
|
||||||
@ -20,12 +20,11 @@ void UnitTest::getSubjects()
|
|||||||
{
|
{
|
||||||
RequestManager* testAccess = new RequestManager();
|
RequestManager* testAccess = new RequestManager();
|
||||||
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
||||||
testAccess->getRequest("/SubjectManager/getSubjects");
|
testAccess->getSubjects();
|
||||||
spy.wait();
|
spy.wait();
|
||||||
QVERIFY(spy.isValid());
|
QVERIFY(spy.isValid());
|
||||||
QCOMPARE(spy.count(), 1);
|
QCOMPARE(spy.count(), 1);
|
||||||
QString json = testAccess->getResponse();
|
QString json = testAccess->getResponse();
|
||||||
qDebug() << json;
|
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8());
|
QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8());
|
||||||
QVERIFY(doc.isObject());
|
QVERIFY(doc.isObject());
|
||||||
QVERIFY2(doc.object().value("items").isArray(), "No parameter 'id' in response");
|
QVERIFY2(doc.object().value("items").isArray(), "No parameter 'id' in response");
|
||||||
@ -38,13 +37,12 @@ void UnitTest::getTokens()
|
|||||||
{
|
{
|
||||||
RequestManager* testAccess = new RequestManager();
|
RequestManager* testAccess = new RequestManager();
|
||||||
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
||||||
testAccess->getRequest("/TokenManager/getTokens");
|
testAccess->getTokens();
|
||||||
QCOMPARE(spy.count(), 0);
|
QCOMPARE(spy.count(), 0);
|
||||||
spy.wait();
|
spy.wait();
|
||||||
QVERIFY(spy.isValid());
|
QVERIFY(spy.isValid());
|
||||||
QCOMPARE(spy.count(), 1);
|
QCOMPARE(spy.count(), 1);
|
||||||
QString json = testAccess->getResponse();
|
QString json = testAccess->getResponse();
|
||||||
qDebug() << json;
|
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8());
|
QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8());
|
||||||
QVERIFY(doc.isObject());
|
QVERIFY(doc.isObject());
|
||||||
QVERIFY2(doc.object().value("items").isArray(), "No parameter 'id' in response");
|
QVERIFY2(doc.object().value("items").isArray(), "No parameter 'id' in response");
|
||||||
@ -52,11 +50,11 @@ void UnitTest::getTokens()
|
|||||||
QVERIFY2(doc.object().value("items").toArray()[0].toObject()["id"].isString(), "ada");
|
QVERIFY2(doc.object().value("items").toArray()[0].toObject()["id"].isString(), "ada");
|
||||||
}
|
}
|
||||||
|
|
||||||
void UnitTest::getTokensOpenManager()
|
void UnitTest::getTokenSubject()
|
||||||
{
|
{
|
||||||
RequestManager* testAccess = new RequestManager();
|
RequestManager* testAccess = new RequestManager();
|
||||||
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
||||||
testAccess->getRequest("/OpenManager/getTokenSubject");
|
testAccess->getTokenSubject();
|
||||||
QCOMPARE(spy.count(), 0);
|
QCOMPARE(spy.count(), 0);
|
||||||
spy.wait();
|
spy.wait();
|
||||||
QVERIFY(spy.isValid());
|
QVERIFY(spy.isValid());
|
||||||
@ -84,7 +82,7 @@ void UnitTest::subjectJSONParser()
|
|||||||
|
|
||||||
RequestManager* testAccess = new RequestManager();
|
RequestManager* testAccess = new RequestManager();
|
||||||
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
||||||
testAccess->getRequest("/SubjectManager/getSubjects");
|
testAccess->getSubjects();
|
||||||
spy.wait();
|
spy.wait();
|
||||||
QString response = testAccess->getResponse();
|
QString response = testAccess->getResponse();
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(response.toUtf8());
|
QJsonDocument doc = QJsonDocument::fromJson(response.toUtf8());
|
||||||
@ -99,7 +97,7 @@ void UnitTest::tokenJSONParser()
|
|||||||
{
|
{
|
||||||
RequestManager* testAccess = new RequestManager();
|
RequestManager* testAccess = new RequestManager();
|
||||||
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
||||||
testAccess->getRequest("/TokenManager/getTokens");
|
testAccess->getTokens();
|
||||||
spy.wait();
|
spy.wait();
|
||||||
QString response = testAccess->getResponse();
|
QString response = testAccess->getResponse();
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(response.toUtf8());
|
QJsonDocument doc = QJsonDocument::fromJson(response.toUtf8());
|
||||||
@ -117,7 +115,7 @@ void UnitTest::connectToDB()
|
|||||||
qDebug() << db->getTestMessage(1);
|
qDebug() << db->getTestMessage(1);
|
||||||
QVERIFY(true);
|
QVERIFY(true);
|
||||||
QCOMPARE(db->getTestMessage(1), QStringLiteral("Hello world"));
|
QCOMPARE(db->getTestMessage(1), QStringLiteral("Hello world"));
|
||||||
db->createTestRecord("Does it work?");
|
db->createTestRecord("Now it's diffrent");
|
||||||
QCOMPARE(db->getTestMessage(2), QStringLiteral("Does it work?"));
|
QCOMPARE(db->getTestMessage(2), QStringLiteral("Does it work?"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,5 +158,14 @@ void UnitTest::debugConnection()
|
|||||||
QVERIFY(true);
|
QVERIFY(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UnitTest::saveDataToDB()
|
||||||
|
{
|
||||||
|
DataManager* dataMan = new DataManager();
|
||||||
|
|
||||||
|
// dataMan->localSaveTokens();
|
||||||
|
QVERIFY(true);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
QTEST_MAIN(UnitTest)
|
QTEST_MAIN(UnitTest)
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
#include "subject.h"
|
#include "subject.h"
|
||||||
#include "databasemanager.h"
|
#include "databasemanager.h"
|
||||||
#include <QSslSocket>
|
#include <QSslSocket>
|
||||||
|
#include "datamanager.h"
|
||||||
|
|
||||||
|
|
||||||
class UnitTest : public QObject
|
class UnitTest : public QObject
|
||||||
@ -18,11 +19,12 @@ private slots:
|
|||||||
void loginSuccessfully(); // /AaaManager/authSession
|
void loginSuccessfully(); // /AaaManager/authSession
|
||||||
void getSubjects(); // /SubjectManager/getSubjects
|
void getSubjects(); // /SubjectManager/getSubjects
|
||||||
void getTokens(); // /TokenManager/getTokens
|
void getTokens(); // /TokenManager/getTokens
|
||||||
void getTokensOpenManager(); // /OpenManager/getTokenSubject
|
void getTokenSubject(); // /OpenManager/getTokenSubject
|
||||||
void subjectJSONParser();
|
void subjectJSONParser();
|
||||||
void tokenJSONParser();
|
void tokenJSONParser();
|
||||||
void connectToDB();
|
void connectToDB();
|
||||||
void debugConnection();
|
void debugConnection();
|
||||||
|
void saveDataToDB();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//QString host = "http://localhost:8080"; //colnod server (ssh -L 8080:192.168.254.11:8443 penta-master)
|
//QString host = "http://localhost:8080"; //colnod server (ssh -L 8080:192.168.254.11:8443 penta-master)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user