app/sources/provider/brprovider.cpp

117 lines
3.7 KiB
C++
Raw Permalink Normal View History

#include "brprovider.h"
2020-10-31 15:16:06 +01:00
BRProvider::BRProvider(QObject *parent) : QObject(parent)
{
}
BRCalendar* BRProvider::getCalendar(BRWidget::BRFederation federation) {
return new BRCalendar(this, federation);
2020-10-31 15:16:06 +01:00
}
BRSeason* BRProvider::getSeason(BRWidget::BRFederation federation, int id, BRSeason::BRSeasonData initialData) {
return new BRSeason(this, federation, id, initialData);
}
BRLeague* BRProvider::getLeague(BRWidget::BRFederation federation, int id, BRLeague::BRLeagueData initialData) {
return new BRLeague(this, federation, id, initialData);
}
BRCompetition* BRProvider::getCompetition(BRWidget::BRFederation federation, int id, BRCompetition::BRCompetitionData initialData) {
return new BRCompetition(this, federation, id, initialData);
}
BRCup* BRProvider::getCup(BRWidget::BRFederation federation, int id, BRCup::BRCupData initialData) {
return new BRCup(this, federation, id, initialData);
}
BRCategory* BRProvider::getCategory(BRWidget::BRFederation federation, int id, BRCategory::BRCategoryData initialData) {
return new BRCategory(this, federation, id, initialData);
}
BRAthlete* BRProvider::getAthlete(BRWidget::BRFederation federation, int id, BRAthlete::BRAthleteData initialData) {
return new BRAthlete(this, federation, id, initialData);
}
BRRound* BRProvider::getRound(BRWidget::BRFederation federation, int id, BRRound::BRRoundData initialData, bool generalResult) {
return new BRRound(this, federation, id, initialData, generalResult);
}
BRResult* BRProvider::getResult(BRResult::BRResultData initialData) {
return new BRResult(initialData);
}
QVariantMap BRProvider::serverRequest(QUrl serviceUrl, QList<QStringList> additionalHeaders, QUrlQuery postData)
2020-10-31 15:16:06 +01:00
{
qDebug() << "requesting: " << serviceUrl;
// create network manager
QNetworkAccessManager * networkManager = new QNetworkAccessManager();
QVariantMap ret; //this is a custom type to store the return-data
// Create network request
QNetworkRequest request(serviceUrl);
// insert additional headers
for(QStringList additionalHeader : additionalHeaders) {
if(additionalHeader.length() == 2)
request.setRawHeader(additionalHeader[0].toUtf8(), additionalHeader[1].toUtf8());
}
2020-10-31 15:16:06 +01:00
//QSslConfiguration config = QSslConfiguration::defaultConfiguration();
//config.setProtocol(QSsl::TlsV1_2);
//request.setSslConfiguration(config);
//send a POST request with the given url and data to the server
QNetworkReply *reply;
if(postData.isEmpty()){
2020-10-31 15:16:06 +01:00
// if no post data is given -> send a GET request
reply = networkManager->get(request);
}
else {
// if post data is given -> send POST request
reply = networkManager->post(request, postData.toString(QUrl::FullyEncoded).toUtf8());
2020-10-31 15:16:06 +01:00
}
// loop to wait until the request has finished before processing the data
QEventLoop loop;
// timer to cancel the request after 3 seconds
QTimer timer;
timer.setSingleShot(true);
// quit the loop when the request finised
loop.connect(networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(quit()));
// or the timer timed out
loop.connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
// start the timer
timer.start(10000);
// start the loop
loop.exec();
//get the status code
QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
qDebug() << "Finished: " << status_code;
2020-10-31 15:16:06 +01:00
ret.insert("status", status_code.toInt());
//get the full text response
ret.insert("text", QString::fromUtf8(reply->readAll()));
// delete the reply object
delete reply;
// delete the newtwork access manager object
delete networkManager;
//return the data
return(ret);
}