From d399c562f5fad76ed8018c712a635f49715a8fed Mon Sep 17 00:00:00 2001 From: Steru Date: Sat, 3 Aug 2024 20:19:05 +0200 Subject: Added another filter and sort functions. Also using mock data to test. --- src/api/OlympicsAPI.cpp | 60 ++++++++++++++++++++++++++++++------------------- src/api/OlympicsAPI.h | 13 +++-------- 2 files changed, 40 insertions(+), 33 deletions(-) (limited to 'src/api') diff --git a/src/api/OlympicsAPI.cpp b/src/api/OlympicsAPI.cpp index edcb021..4fe9701 100644 --- a/src/api/OlympicsAPI.cpp +++ b/src/api/OlympicsAPI.cpp @@ -12,6 +12,7 @@ #include // json parsing +#include #include #include #include @@ -29,41 +30,54 @@ using namespace std; QJsonObject OlympicsAPI::getSportData(OlympicsAPI::Disciplines sport) { string shortName = this->getDisciplineShort(sport); - // create custom temporary event loop on stack - QEventLoop eventLoop; + if (USE_API_REQUEST) { + // create custom temporary event loop on stack + QEventLoop eventLoop; - // "quit()" the event-loop, when the network request "finished()" - QNetworkAccessManager mgr; - QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit())); + // "quit()" the event-loop, when the network request "finished()" + QNetworkAccessManager mgr; + QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit())); - QString endpoint = (API_LINK + shortName).c_str(); + QString endpoint = (API_LINK + shortName).c_str(); - // the HTTP request - QNetworkRequest req( (QUrl( endpoint )) ); - QNetworkReply *reply = mgr.get(req); - eventLoop.exec(); // blocks stack until "finished()" has been called + // the HTTP request + QNetworkRequest req( (QUrl( endpoint )) ); + QNetworkReply *reply = mgr.get(req); + eventLoop.exec(); // blocks stack until "finished()" has been called - if (reply->error() == QNetworkReply::NoError) { - //success + if (reply->error() == QNetworkReply::NoError) { + //success - QString strReply = (QString)reply->readAll(); + QString strReply = (QString)reply->readAll(); - //parse json - QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8()); + //parse json + QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8()); - QJsonObject jsonObj = jsonResponse.object(); + QJsonObject jsonObj = jsonResponse.object(); - delete reply; + delete reply; - return jsonObj; - } - else { - //failure - delete reply; + return jsonObj; + } + else { + //failure + delete reply; - throw invalid_argument("API request failed."); + throw invalid_argument("API request failed."); + } } + // if API is not used, open locally stored data + QString filePath = ("../../res/mock/" + shortName + ".json").c_str(); + QFile file( filePath ); + + if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + throw invalid_argument("Could not open file to read data of the given discipline."); + } else { + QString text = file.readAll(); + file.close(); + return (QJsonDocument::fromJson(text.toUtf8())).object(); + } } /** diff --git a/src/api/OlympicsAPI.h b/src/api/OlympicsAPI.h index 8efaa26..f1168b6 100644 --- a/src/api/OlympicsAPI.h +++ b/src/api/OlympicsAPI.h @@ -8,17 +8,10 @@ #include #include -using namespace std; +// TODO: change this to true to use the olympics api, instead of the mock date in res/mock/ +#define USE_API_REQUEST false -/* - * TODO: - * Replace api request code snippet in main with: - * - OlympicsAPI api; - QJsonObject archery = api.getSportData(api.Archery); - qDebug() << "Competitor:" << archery["units"][0]["competitors"][0]["name"].toString(); - * - */ +using namespace std; class OlympicsAPI { -- cgit v1.2.3