app/sources/brprovider.cpp

75 lines
2.2 KiB
C++
Raw Normal View History

2020-10-31 15:16:06 +01:00
#include "../headers/brprovider.h"
BRProvider::BRProvider(QObject *parent) : QObject(parent)
{
}
BRCalendar* BRProvider::getCalendar(BRWidget::BRFederation federation, int year, int league) {
return new BRCalendar(this, federation, year, league);
}
QVariantMap BRProvider::serverRequest(QUrl serviceUrl, QUrlQuery pdata)
{
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);
request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/x-www-form-urlencoded");
//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(pdata.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, pdata.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);
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);
}