summaryrefslogtreecommitdiff
path: root/src/api/OlympicsAPI.cpp
blob: edcb021a5c1c8f34ea30158c0be14e5ba19ef1f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

#include "OlympicsAPI.h"
#include <string>
#include <stdexcept>

// networking
#include <QEventLoop>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QUrlQuery>

// json parsing
#include <QJsonValue>
#include <QJsonDocument>
#include <QJsonObject>
#include <QVariantMap>
#include <QJsonArray>


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
    }
}