app/sources/serverconn.cpp

151 lines
4.8 KiB
C++
Raw Normal View History

2019-05-11 15:21:43 +02:00
/*
blueROCK - for digital rock
Copyright (C) 2019 Dorian Zedler
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "headers/serverconn.h"
ServerConn::ServerConn(QObject *parent) : QObject(parent)
{
}
2019-04-21 18:35:28 +02:00
QVariant ServerConn::getCalendar(QString nation, int year){
QVariantMap ret = this->senddata(QUrl("http://egw.ifsc-climbing.org/egw/ranking/json.php?year=" + QString::number(year) + "&nation=" + nation));
if(ret["status"] != 200){
// request was a failure
return QVariantMap({{"status", ret["status"]}, {"data", ""}});
}
QJsonDocument jsonReply = QJsonDocument::fromJson(ret["text"].toString().toUtf8());
QVariantMap rankingData = {{"status", 200}, {"data", jsonReply.toVariant()}};
return rankingData;
}
QVariant ServerConn::getRanking(int competiotionId, int categoryId, bool registrationData, bool rankingData, int routeNumber) {
QString requestUrl = "http://egw.ifsc-climbing.org/egw/ranking/json.php?" + QString(rankingData ? "cup":"comp") + "=" + QString::number( competiotionId ) + "&cat=" + QString::number( categoryId ) + (registrationData ? "&type=starters":"") + "&route=" + ((routeNumber == -2) ? "":QString::number(routeNumber));
qDebug() << requestUrl;
QVariantMap ret = this->senddata(QUrl(requestUrl));
if(ret["status"] != 200){
// request was a failure
return QVariantMap({{"status", ret["status"]}, {"data", ""}});
}
QJsonDocument jsonReply = QJsonDocument::fromJson(ret["text"].toString().toUtf8());
QVariantMap data = {{"status", 200}, {"data", jsonReply.toVariant()}};
return data;
}
QVariant ServerConn::getAthlete(int perId){
QString requestUrl = "http://egw.ifsc-climbing.org/egw/ranking/json.php?person=" + QString::number(perId);
qDebug() << requestUrl;
QVariantMap ret = this->senddata(QUrl(requestUrl));
if(ret["status"] != 200){
// request was a failure
return QVariantMap({{"status", ret["status"]}, {"data", ""}});
}
QJsonDocument jsonReply = QJsonDocument::fromJson(ret["text"].toString().toUtf8());
QVariantMap data = {{"status", 200}, {"data", jsonReply.toVariant()}};
return data;
}
// ------------------------
// --- Helper functions ---
// ------------------------
QVariantMap ServerConn::senddata(QUrl serviceUrl, QUrlQuery pdata)
{
// 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);
}
// -------------------------
// --- Functions for QML ---
// -------------------------