-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpapi.cpp
355 lines (289 loc) · 12 KB
/
httpapi.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "httpapi.h"
#include "mainwindow.h"
#define DEBUG
class MainWindow;
HttpApi::HttpApi(QSqlDatabase db, QString accessToken, QObject *parent)
: QObject{parent}
{
this->db = db;
this->accessToken = accessToken;
}
void HttpApi::SendQso(QVariantList data) {
if (accessToken.length() == 0) {
emit emptyToken();
return;
}
if (m_reply) {
m_reply->abort();
m_reply->deleteLater();
m_reply = nullptr;
}
QNetworkRequest request((QUrl("https://api.qso.su/method/v1/sendLog")));
request.setHeader(QNetworkRequest::UserAgentHeader, "QSO.SU Agent");
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
request.setRawHeader(QByteArrayLiteral("Authorization"), QString("Bearer " + accessToken).toUtf8());
request.setSslConfiguration(QSslConfiguration::defaultConfiguration());
int dbid = data.value(0).toInt();
QJsonObject body;
body["id_station_callsign"] = data.value(1).toInt();
body["id_operator"] = data.value(2).toInt();
body["call"] = data.value(3).toString();
body["band"] = data.value(4).toString();
body["mode"] = data.value(5).toString();
body["freq"] = data.value(6).toLongLong();
body["datetime"] = data.value(7).toString();
body["name"] = data.value(8).toString();
body["rsts"] = data.value(9).toString();
body["rstr"] = data.value(10).toString();
body["qth"] = data.value(11).toString();
body["cnty"] = data.value(12).toString();
body["gridsquare"] = data.value(13).toString();
body["my_cnty"] = data.value(14).toString(); //Add bugFix
body["my_gridsquare"] = data.value(15).toString(); //Add bugFix
QJsonDocument doc(body);
QByteArray jsonBA = doc.toJson();
qDebug().noquote() << "Sending QSO data to service." << jsonBA;
QNetworkReply *reply = m_manager.post(request, jsonBA);
connect(reply, &QNetworkReply::finished, this, [=]() {
QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
qDebug() << "Network reply finished. Code:" << status_code.toInt();
switch(status_code.toInt()) {
case 201:
case 409:
emit synced(dbid);
break;
default:
emit syncerror(dbid);
}
reply->deleteLater();
});
}
/* TEST */
void HttpApi::getCallsign()
{
if (accessToken.length() == 0) {
emit emptyToken();
return;
}
if (m_reply) {
m_reply->abort();
m_reply->deleteLater();
m_reply = nullptr;
}
callsigns.clear();
QNetworkRequest request((QUrl("https://api.qso.su/method/v1/getCallsign")));
request.setHeader(QNetworkRequest::UserAgentHeader, "QSO.SU Agent");
request.setRawHeader(QByteArrayLiteral("Authorization"), QString("Bearer " + accessToken).toUtf8());
request.setSslConfiguration(QSslConfiguration::defaultConfiguration());
QNetworkReply *reply = m_manager.get(request);
connect(reply, &QNetworkReply::finished, this, [=]() {
if (reply->error() == QNetworkReply::NoError) {
QByteArray data = reply->readAll();
QJsonDocument jsonDocument = QJsonDocument::fromJson(data);
if (jsonDocument.object().contains("error")) {
QJsonObject errorObject = jsonDocument["error"].toObject();
qDebug() << "ERROR:" << errorObject["name"].toString() << errorObject["message"].toString();
return;
}
QJsonObject response = jsonDocument["response"].toObject();
QJsonArray callsignsArray = response["station_callsign"].toArray();
foreach (const QJsonValue &c, callsignsArray) {
callsigns.append(c.toObject().toVariantMap());
}
emit callsignsUpdated();
} else {
emit error(reply->error());
}
reply->deleteLater();
});
}
//--------------------------------------------------------------------------------------------------------------------
void HttpApi::addCallsign(QVariantList data)
{
if (accessToken.length() == 0) {
emit emptyToken();
return;
}
if (m_reply) {
m_reply->abort();
m_reply->deleteLater();
m_reply = nullptr;
}
QNetworkRequest request(QUrl("https://api.qso.su/method/v1/addCallsign"));
request.setHeader(QNetworkRequest::UserAgentHeader, "QSO.SU Agent");
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
request.setRawHeader(QByteArrayLiteral("Authorization"), QString("Bearer " + accessToken).toUtf8());
request.setSslConfiguration(QSslConfiguration::defaultConfiguration());
QJsonObject body;
body["callsign"] = data.value(0).toString();
body["type"] = data.value(1).toInt();
body["location"] = data.value(2).toString();
body["rda"] = data.value(3).toString();
body["ituz"] = data.value(4).toInt();
body["cqz"] = data.value(5).toInt();
QDateTime dt;
body["start_data"] = dt.fromSecsSinceEpoch(data.value(6).toInt()).toString("yyyy-MM-dd hh:mm:ss.zzz");
body["stop_data"] = dt.fromSecsSinceEpoch(data.value(7).toInt()).toString("yyyy-MM-dd hh:mm:ss.zzz");
QJsonDocument doc(body);
QByteArray jsonBA = doc.toJson();
qDebug().noquote() << "Sending Callsign for validate to service." << jsonBA;
QNetworkReply *reply = m_manager.post(request, jsonBA);
connect(reply, &QNetworkReply::finished, this, [=]() {
QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
qDebug() << "Network reply finished. Code:" << status_code.toInt();
reply->deleteLater();
});
}
//--------------------------------------------------------------------------------------------------------------------
/*
void HttpApi::checkStatusCallsign(QString callsign)
{
if (accessToken.length() == 0) {
emit emptyToken();
return;
}
if (m_reply) {
m_reply->abort();
m_reply->deleteLater();
m_reply = nullptr;
}
QNetworkRequest request = QNetworkRequest(QUrl("https://api.qso.su/method/v1/checkStatusCallsign"));
request.setHeader(QNetworkRequest::UserAgentHeader, "QSO.SU Agent");
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
request.setRawHeader(QByteArrayLiteral("Authorization"), QString("Bearer " + accessToken).toUtf8());
request.setSslConfiguration(QSslConfiguration::defaultConfiguration());
QJsonObject body;
body["callsign"] = callsign;
QJsonDocument doc(body);
QByteArray jsonBA = doc.toJson();
qDebug().noquote() << "Checking Callsign from service." << jsonBA;
QNetworkReply *reply = m_manager.get(request);
//QNetworkReply *reply = m_manager.get(request, jsonBA);
connect(reply, &QNetworkReply::finished, this, [=]() {
if (reply->error() == QNetworkReply::NoError) {
QByteArray data = reply->readAll();
qDebug() << data;
QJsonDocument jsonDocument = QJsonDocument::fromJson(data);
if (jsonDocument.object().contains("error")) {
QJsonObject errorObject = jsonDocument["error"].toObject();
qDebug() << "ERROR:" << errorObject["name"].toString() << errorObject["message"].toString();
return;
}
QJsonObject response = jsonDocument["response"].toObject();
QJsonValue callsign_status = response["status"].toInt();
emit callsignStatus(callsign_status.toInt());
}
reply->deleteLater();
});
}
*/
//--------------------------------------------------------------------------------------------------------------------
void HttpApi::checkStatusCallsign(QString callsign)
{
if (accessToken.length() == 0) {
emit emptyToken();
return;
}
if (m_reply) {
m_reply->abort();
m_reply->deleteLater();
m_reply = nullptr;
}
QJsonObject body;
body["callsign"] = callsign;
QJsonDocument doc(body);
QNetworkRequest request = QNetworkRequest(QUrl("https://api.qso.su/method/v1/checkStatusCallsign"));
request.setHeader(QNetworkRequest::UserAgentHeader, "QSO.SU Agent");
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
request.setRawHeader(QByteArrayLiteral("Authorization"), QString("Bearer " + accessToken).toUtf8());
request.setSslConfiguration(QSslConfiguration::defaultConfiguration());
QByteArray postDataByteArray = doc.toJson();
QBuffer *buff = new QBuffer;
buff->setData(postDataByteArray);
buff->open(QIODevice::ReadOnly);
QNetworkReply *reply = m_manager.sendCustomRequest(request, "GET", buff);
buff->setParent(reply);
connect(reply, &QNetworkReply::finished, this, [=]() {
if (reply->error() == QNetworkReply::NoError) {
QByteArray data = reply->readAll();
qDebug() << data;
QJsonDocument jsonDocument = QJsonDocument::fromJson(data);
if (jsonDocument.object().contains("error")) {
QJsonObject errorObject = jsonDocument["error"].toObject();
qDebug() << "ERROR:" << errorObject["name"].toString() << errorObject["message"].toString();
return;
}
QJsonObject response = jsonDocument["response"].toObject();
QJsonValue callsign_status = response["status"].toInt();
emit callsignStatus(callsign_status.toInt());
}
reply->deleteLater();
});
}
//--------------------------------------------------------------------------------------------------------------------
void HttpApi::getListSubmodeDropDown()
{
if (accessToken.length() == 0) {
emit emptyToken();
return;
}
if (m_reply) {
m_reply->abort();
m_reply->deleteLater();
m_reply = nullptr;
}
QNetworkRequest request((QUrl("https://api.qso.su/method/v1/getListSubmodeDropDown")));
request.setHeader(QNetworkRequest::UserAgentHeader, "QSO.SU Agent");
request.setRawHeader(QByteArrayLiteral("Authorization"), QString("Bearer " + accessToken).toUtf8());
request.setSslConfiguration(QSslConfiguration::defaultConfiguration());
QNetworkReply *reply = m_manager.get(request);
connect(reply, &QNetworkReply::finished, this, [=]() {
if (reply->error() == QNetworkReply::NoError) {
QByteArray data = reply->readAll();
QJsonDocument jsonDocument = QJsonDocument::fromJson(data);
QJsonObject response = jsonDocument["response"].toObject();
for(int j = 0; j < response.count(); j++)
{
modulations.append(response.value(response.keys().at(j)).toString());
}
emit modesUpdated();
} else {
emit error(reply->error());
}
reply->deleteLater();
});
}
//--------------------------------------------------------------------------------------------------------------------
void HttpApi::getListBand()
{
if (accessToken.length() == 0) {
emit emptyToken();
return;
}
if (m_reply) {
m_reply->abort();
m_reply->deleteLater();
m_reply = nullptr;
}
QNetworkRequest request((QUrl("https://api.qso.su/method/v1/getListBand")));
request.setHeader(QNetworkRequest::UserAgentHeader, "QSO.SU Agent");
request.setRawHeader(QByteArrayLiteral("Authorization"), QString("Bearer " + accessToken).toUtf8());
request.setSslConfiguration(QSslConfiguration::defaultConfiguration());
QNetworkReply *reply = m_manager.get(request);
connect(reply, &QNetworkReply::finished, this, [=]() {
if (reply->error() == QNetworkReply::NoError) {
QByteArray data = reply->readAll();
QJsonDocument jsonDocument = QJsonDocument::fromJson(data);
QJsonObject response = jsonDocument["response"].toObject();
for(int j = 0; j < response.count(); j++)
{
bands.append(response.value(response.keys().at(j)).toString());
}
emit bandsUpdated();
} else {
emit error(reply->error());
}
reply->deleteLater();
});
}
//--------------------------------------------------------------------------------------------------------------------