app/sources/brprovider.cpp
Dorian Zedler c44380d1bf
Many more implementations!
Calendar works now for VL and DR API
2020-11-03 15:56:43 +01:00

102 lines
3.2 KiB
C++

#include "../headers/brprovider.h"
BRProvider::BRProvider(QObject *parent) : QObject(parent)
{
}
BRCalendar* BRProvider::getCalendar(BRWidget::BRFederation federation) {
return new BRCalendar(this, federation);
}
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);
}
QVariantMap BRProvider::serverRequest(QUrl serviceUrl, QList<QStringList> additionalHeaders, QUrlQuery postData)
{
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());
}
//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()){
// 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());
}
// 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;
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);
}