From a767d83d81755710fc760a6d3dc040b4a379c16a Mon Sep 17 00:00:00 2001 From: Steru Date: Fri, 2 Aug 2024 16:07:15 +0200 Subject: Extracted API call in seperate class. Started class to handle data and filter (Sport). --- src/api/OlympicsAPI.cpp | 126 +++++++++++++++++++++++++++++++++++++++++++++++ src/api/OlympicsAPI.h | 84 +++++++++++++++++++++++++++++++ src/discipline/Sport.cpp | 48 ++++++++++++++++++ src/discipline/Sport.h | 41 +++++++++++++++ 4 files changed, 299 insertions(+) create mode 100644 src/api/OlympicsAPI.cpp create mode 100644 src/api/OlympicsAPI.h create mode 100644 src/discipline/Sport.cpp create mode 100644 src/discipline/Sport.h (limited to 'src') diff --git a/src/api/OlympicsAPI.cpp b/src/api/OlympicsAPI.cpp new file mode 100644 index 0000000..edcb021 --- /dev/null +++ b/src/api/OlympicsAPI.cpp @@ -0,0 +1,126 @@ + +#include "OlympicsAPI.h" +#include +#include + +// networking +#include +#include +#include +#include +#include +#include + +// json parsing +#include +#include +#include +#include +#include + + +using namespace std; + +/** + * @brief OlympicsAPI::getSportData Requests the current data from the Olympics API of a certain discipline. + * @param sport The discipline to request. + * @return The current information provided by the API endpoint. + */ +QJsonObject OlympicsAPI::getSportData(OlympicsAPI::Disciplines sport) { + string shortName = this->getDisciplineShort(sport); + + // 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())); + + 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 + + if (reply->error() == QNetworkReply::NoError) { + //success + + QString strReply = (QString)reply->readAll(); + + //parse json + QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8()); + + QJsonObject jsonObj = jsonResponse.object(); + + delete reply; + + return jsonObj; + } + else { + //failure + delete reply; + + throw invalid_argument("API request failed."); + } + +} + +/** + * @brief OlympicsAPI::getDisciplineShort Get the discipline's short name defined by the IOC (International Olympic Committee) + * @param sport The sport you want to get the name from. + * @return The short name as a string. + */ +string OlympicsAPI::getDisciplineShort(OlympicsAPI::Disciplines sport) { + switch (sport) { + case OlympicsAPI::Disciplines::AquaticsArtisticSwimming: return "SWA"; + case OlympicsAPI::Disciplines::AquaticsDiving: return "DIV"; + case OlympicsAPI::Disciplines::AquaticsMarathonSwimming: return "OWS"; + case OlympicsAPI::Disciplines::AquaticsSwimming: return "SWM"; + case OlympicsAPI::Disciplines::AquaticsWaterPolo: return "WPO"; + case OlympicsAPI::Disciplines::Archery: return "ARC"; + case OlympicsAPI::Disciplines::Athletics: return "ATH"; + case OlympicsAPI::Disciplines::Badminton: return "BDM"; + case OlympicsAPI::Disciplines::Basketball3v3: return "BK3"; + case OlympicsAPI::Disciplines::Basketball: return "BKB"; + case OlympicsAPI::Disciplines::Boxing: return "BOX"; + case OlympicsAPI::Disciplines::Breaking: return "BKG"; + case OlympicsAPI::Disciplines::CanoeingSprint: return "CSP"; + case OlympicsAPI::Disciplines::CanoeingSlalom: return "CSL"; + case OlympicsAPI::Disciplines::CyclingBMXFreestyle: return "BMF"; + case OlympicsAPI::Disciplines::CyclingBMXRacing: return "BMX"; + case OlympicsAPI::Disciplines::CyclingMaountainBike: return "MTB"; + case OlympicsAPI::Disciplines::CyclingRoad: return "CRD"; + case OlympicsAPI::Disciplines::CyclingTrack: return "CTR"; + case OlympicsAPI::Disciplines::EquestrianDressage: return "EDR"; + case OlympicsAPI::Disciplines::EquestrianEventing: return "EVE"; + case OlympicsAPI::Disciplines::EquestrianJumping: return "EJP"; + case OlympicsAPI::Disciplines::Fencing: return "FEN"; + case OlympicsAPI::Disciplines::FieldHockey: return "HOC"; + case OlympicsAPI::Disciplines::Football: return "FBL"; + case OlympicsAPI::Disciplines::Golf: return "GLF"; + case OlympicsAPI::Disciplines::GymnasticsArtistic: return "GAR"; + case OlympicsAPI::Disciplines::GymnasticsRhythmic: return "GRY"; + case OlympicsAPI::Disciplines::GymnasticsTrampoline: return "GTR"; + case OlympicsAPI::Disciplines::HandballIndoor: return "HBL"; + case OlympicsAPI::Disciplines::Judo: return "JUD"; + case OlympicsAPI::Disciplines::ModernPentathlon: return "MPN"; + case OlympicsAPI::Disciplines::Rowing: return "ROW"; + case OlympicsAPI::Disciplines::Rugby7: return "RU7"; + case OlympicsAPI::Disciplines::Sailing: return "SAL"; break; + case OlympicsAPI::Disciplines::Shooting: return "SHO"; break; + case OlympicsAPI::Disciplines::Skateboarding: return "SKB"; break; + case OlympicsAPI::Disciplines::SportClimbing: return "CLB"; break; + case OlympicsAPI::Disciplines::Surfing: return "SRF"; break; + case OlympicsAPI::Disciplines::TableTennis: return "TTE"; break; + case OlympicsAPI::Disciplines::Taekwondo: return "TKW"; break; + case OlympicsAPI::Disciplines::Tennis: return "TEN"; break; + case OlympicsAPI::Disciplines::Triathlon: return "TRI"; break; + case OlympicsAPI::Disciplines::VolleyballBeach: return "VBV"; break; + case OlympicsAPI::Disciplines::VolleyballIndoor: return "VVO"; break; + case OlympicsAPI::Disciplines::Weightlifting: return "WLF"; break; + case OlympicsAPI::Disciplines::WrestlingFreestyle: return "WRE"; break; + case OlympicsAPI::Disciplines::WrestlingGrecoRoman: return "WRG"; break; + default: return "ARC"; // default, which should not be possible, because of enum + } +} diff --git a/src/api/OlympicsAPI.h b/src/api/OlympicsAPI.h new file mode 100644 index 0000000..8efaa26 --- /dev/null +++ b/src/api/OlympicsAPI.h @@ -0,0 +1,84 @@ + +#ifndef ITAT_CHALLANGE_OLYMPICS_OLYMPICSAPI_H +#define ITAT_CHALLANGE_OLYMPICS_OLYMPICSAPI_H + + +#define API_LINK "https://sph-s-api.olympics.com/summer/schedules/api/ENG/schedule/discipline/" + +#include +#include + +using namespace std; + +/* + * 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(); + * + */ + +class OlympicsAPI { + +public: + + enum Disciplines { + AquaticsArtisticSwimming, + AquaticsDiving, + AquaticsMarathonSwimming, + AquaticsSwimming, + AquaticsWaterPolo, + Archery, + Athletics, + Badminton, + Basketball3v3, + Basketball, + Boxing, + Breaking, + CanoeingSprint, + CanoeingSlalom, + CyclingBMXFreestyle, + CyclingBMXRacing, + CyclingMaountainBike, + CyclingRoad, + CyclingTrack, + EquestrianDressage, + EquestrianEventing, + EquestrianJumping, + Fencing, + FieldHockey, + Football, + Golf, + GymnasticsArtistic, + GymnasticsRhythmic, + GymnasticsTrampoline, + HandballIndoor, + Judo, + ModernPentathlon, + Rowing, + Rugby7, + Sailing, + Shooting, + Skateboarding, + SportClimbing, + Surfing, + TableTennis, + Taekwondo, + Tennis, + Triathlon, + VolleyballBeach, + VolleyballIndoor, + Weightlifting, + WrestlingFreestyle, + WrestlingGrecoRoman + }; + + QJsonObject getSportData(Disciplines sport); + string getDisciplineShort(Disciplines sport); + +}; + + +#endif //ITAT_CHALLANGE_OLYMPICS_OLYMPICSAPI_H diff --git a/src/discipline/Sport.cpp b/src/discipline/Sport.cpp new file mode 100644 index 0000000..ed8331d --- /dev/null +++ b/src/discipline/Sport.cpp @@ -0,0 +1,48 @@ + +#include "Sport.h" +#include + +#include +#include +#include +#include + + +/** + * @brief Sport::getCategories Reads all possible categories (also called units). + * @return A set of all category names. + */ +set Sport::getCategories() { + set categoryNames; + + for (const QJsonValueRef& unit : this->discipline["units"].toArray()) { + categoryNames.insert(unit.toObject()["eventUnitName"].toString()); + } + + return categoryNames; +} + +/** + * @brief Sport::getCompetitorsByCategory Searches for all competitors, who took part in the given category. + * @param category The category to search in. + * @return An QJsonArray with all competitors as QJsonValueRef, which can be casted to QJsonObject. + */ +QJsonArray Sport::getCompetitorsByCategory(QString category) { + QJsonArray competitors; + + for (const QJsonValueRef& unitRef : this->discipline["units"].toArray()) { + QJsonObject unit = unitRef.toObject(); + + // search all units with the same category + if (QString::compare(unit["eventUnitName"].toString(), category, Qt::CaseSensitive) == 0) { + + // add all competitors from one unit + for (const QJsonValueRef& comp : unit["competitors"].toArray()) { + competitors.push_back(comp.toObject()); + } + + } + } + + return competitors; +} diff --git a/src/discipline/Sport.h b/src/discipline/Sport.h new file mode 100644 index 0000000..9c993fb --- /dev/null +++ b/src/discipline/Sport.h @@ -0,0 +1,41 @@ + +#ifndef ITAT_CHALLANGE_OLYMPICS_SPORT_H +#define ITAT_CHALLANGE_OLYMPICS_SPORT_H + + +#include + +#include +#include +#include + +using namespace std; + + +class Sport { + +public: + + Sport(QJsonObject discipline) { + this->discipline = discipline; + } + + set getCategories(); + QJsonArray getCompetitorsByCategory(QString category); + + // chainable + QJsonObject filterByName(QJsonObject discipline, QString name); + QJsonObject filterByCountry(QJsonObject discipline, QString name); + + void setDiscipline(QJsonObject discipline) { + this->discipline = discipline; + } + +private: + + QJsonObject discipline; + +}; + + +#endif //ITAT_CHALLANGE_OLYMPICS_SPORT_H -- cgit v1.2.3