forked from DOMjudge/DOMjura
-
Notifications
You must be signed in to change notification settings - Fork 0
/
domjudgeapimanager.cpp
227 lines (193 loc) · 8.35 KB
/
domjudgeapimanager.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "domjudgeapimanager.h"
#include <QUrlQuery>
#include <QJsonParseError>
#include <functional>
namespace DJ
{
namespace Shared
{
void DomjudgeApiManager::setConnectionInfo(QString protocol, QString url, QString username, QString password)
{
this->protocol = protocol;
this->url = url;
this->username = username;
this->password = password;
}
void DomjudgeApiManager::loadUserData()
{
DomjudgeApiRequest request("user");
this->userRequests.append(request);
this->accessManager->get(request);
}
void DomjudgeApiManager::loadContestsData()
{
DomjudgeApiRequest request("contests");
this->contestsRequests.append(request);
this->accessManager->get(request);
}
void DomjudgeApiManager::loadGroupsData(QString cid)
{
DomjudgeApiRequest request("contests/" + cid + "/groups");
this->groupsRequests.append(request);
this->accessManager->get(request);
}
void DomjudgeApiManager::loadTeamData(QString cid)
{
DomjudgeApiRequest request("contests/" + cid + "/teams");
this->teamsRequests.append(request);
this->accessManager->get(request);
}
void DomjudgeApiManager::loadProblemData(QString cid)
{
DomjudgeApiRequest request("contests/" + cid + "/problems");
this->problemRequests.append(request);
this->accessManager->get(request);
}
void DomjudgeApiManager::loadSubmissions(QString cid)
{
DomjudgeApiRequest request("contests/" + cid + "/submissions");
this->submissionRequests.append(request);
this->accessManager->get(request);
}
void DomjudgeApiManager::loadJudgings(QString cid)
{
DomjudgeApiRequest request("contests/" + cid + "/judgements");
this->judgingRequests.append(request);
this->accessManager->get(request);
}
DomjudgeApiManager::DomjudgeApiRequest::DomjudgeApiRequest(QString method, QList<QPair<QString, QString>> arguments)
{
DomjudgeApiManager *apiManager = DomjudgeApiManager::sharedApiManager();
QUrl url(apiManager->protocol + apiManager->url + "/api/" + method);
QUrlQuery urlQuery;
foreach (auto argument, arguments)
{
urlQuery.addQueryItem(argument.first, argument.second);
}
url.setQuery(urlQuery);
this->setUrl(url);
if (!apiManager->username.isEmpty())
{
QString concatenated = apiManager->username + ":" + apiManager->password;
QByteArray data = concatenated.toLocal8Bit().toBase64();
QString headerData = "Basic " + data;
this->setRawHeader("Authorization", headerData.toLocal8Bit());
}
}
DomjudgeApiManager::DomjudgeApiManager()
{
this->accessManager = new QNetworkAccessManager(this);
connect(this->accessManager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));
}
void DomjudgeApiManager::replyFinished(QNetworkReply *reply)
{
DomjudgeApiManager *apiManager = DomjudgeApiManager::sharedApiManager();
// Process all types of replies
if (this->processReply(reply, &apiManager->userRequests,
&DomjudgeApiManager::userDataFailedLoading,
&DomjudgeApiManager::userLoaded))
{
return;
}
if (this->processReply(reply, &apiManager->contestsRequests,
&DomjudgeApiManager::contestsDataFailedLoading,
&DomjudgeApiManager::contestsDataLoaded))
{
return;
}
if (this->processReply(reply, &apiManager->groupsRequests,
&DomjudgeApiManager::groupsDataFailedLoading,
&DomjudgeApiManager::groupsDataLoaded))
{
return;
}
if (this->processReply(reply, &apiManager->teamsRequests,
&DomjudgeApiManager::teamsDataFailedLoading,
&DomjudgeApiManager::teamsDataLoaded))
{
return;
}
if (this->processReply(reply, &apiManager->problemRequests,
&DomjudgeApiManager::problemsDataFailedLoading,
&DomjudgeApiManager::problemsDataLoaded))
{
return;
}
if (this->processReply(reply, &apiManager->submissionRequests,
&DomjudgeApiManager::submissionsDataFailedLoading,
&DomjudgeApiManager::submissionsDataLoaded))
{
return;
}
if (this->processReply(reply, &apiManager->judgingRequests,
&DomjudgeApiManager::judgingsDataFailedLoading,
&DomjudgeApiManager::judgingsDataLoaded))
{
return;
}
}
template <typename... ArgsError, typename... ArgsSuccess>
bool DomjudgeApiManager::processReply(QNetworkReply *reply,
QList<QNetworkRequest> *requests,
void (DomjudgeApiManager::*errorFunc)(ArgsError... args),
void (DomjudgeApiManager::*successFunc)(ArgsSuccess... args))
{
foreach (QNetworkRequest r, *requests)
{
if (r.url() != reply->url())
continue;
DomjudgeApiManager *apiManager = DomjudgeApiManager::sharedApiManager();
// See if we must do a redirect and if so, do one
auto possibleRedirectURL = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
if (!possibleRedirectURL.isEmpty())
{
auto newRequest = QNetworkRequest(possibleRedirectURL);
if (requests->contains(reply->request()))
{
requests->append(newRequest);
requests->removeOne(reply->request());
}
apiManager->accessManager->get(newRequest);
return true;
}
int status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
QString error = "General error";
if (status_code == 401)
{
error = "Authentication required. Please enter correct username and password";
}
else if (status_code == 0)
{
error = "Can not find server. Please enter correct URL";
}
else if (status_code == 404)
{
error = "Can not find this URL; please enter the correct one";
}
if (status_code != 200)
{
std::function<void(DomjudgeApiManager *, ArgsError...)> func = errorFunc;
emit func(this, error);
}
else
{
QJsonParseError error;
QJsonDocument document = QJsonDocument::fromJson(reply->readAll(), &error);
if (error.error != QJsonParseError::NoError)
{
std::function<void(DomjudgeApiManager *, ArgsError...)> func = errorFunc;
emit func(this, "Error parsing JSON");
}
else
{
std::function<void(DomjudgeApiManager *, ArgsSuccess...)> func = successFunc;
emit func(this, document);
}
}
requests->removeOne(reply->request());
return true;
}
return false;
}
}
}