-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathqRestAPI.cpp
738 lines (648 loc) · 22.1 KB
/
qRestAPI.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
/*==============================================================================
Library: qRestAPI
Copyright (c) 2010 Kitware Inc.
See Doc/copyright/copyright.txt
or http://www.slicer.org/copyright/copyright.txt for details.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This file was originally developed by Jean-Christophe Fillion-Robin, Kitware Inc.
and was partially funded by NIH grant 3P41RR013218-12S1
==============================================================================*/
// Qt includes
#include <QDebug>
#include <QEventLoop>
#include <QIODevice>
#include <QSslSocket>
#include <QStringList>
#include <QTimer>
#include <QUuid>
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
#include <QJSValueIterator>
#include <QUrlQuery>
#else
#include <QScriptValueIterator>
#endif
// qRestAPI includes
#include "qRestAPI.h"
#include "qRestAPI_p.h"
#include "qRestResult.h"
// --------------------------------------------------------------------------
// Static file local error messages
static QString unknownErrorStr = "Unknown error";
static QString unknownUuidStr = "Unknown uuid %1";
static QString timeoutErrorStr = "Request timed out";
// --------------------------------------------------------------------------
// qRestAPIPrivate methods
qRestAPIPrivate::StaticInit qRestAPIPrivate::_staticInit;
// --------------------------------------------------------------------------
qRestAPIPrivate::qRestAPIPrivate(qRestAPI* object)
: q_ptr(object)
, NetworkManager(NULL)
, TimeOut(0)
, SuppressSslErrors(true)
, ErrorCode(qRestAPI::UnknownError)
, ErrorString(unknownErrorStr)
{
}
// --------------------------------------------------------------------------
qRestAPIPrivate::~qRestAPIPrivate()
{
NetworkManager->deleteLater();
}
// --------------------------------------------------------------------------
void qRestAPIPrivate::staticInit()
{
qRegisterMetaType<QUuid>("QUuid");
qRegisterMetaType<QList<QVariantMap> >("QList<QVariantMap>");
}
// --------------------------------------------------------------------------
void qRestAPIPrivate::init()
{
this->NetworkManager = new QNetworkAccessManager();
QObject::connect(this->NetworkManager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(processReply(QNetworkReply*)));
#ifndef QRESTAPI_QT_NO_SSL
if (QSslSocket::supportsSsl())
{
QObject::connect(this->NetworkManager, SIGNAL(sslErrors(QNetworkReply*, QList<QSslError>)),
this, SLOT(onSslErrors(QNetworkReply*, QList<QSslError>)));
}
#endif
}
// --------------------------------------------------------------------------
void qRestAPI::setHttpNetworkProxy(const QNetworkProxy &proxy)
{
Q_D(qRestAPI);
if (d->NetworkManager != NULL)
{
d->NetworkManager->setProxy(proxy);
}
}
// --------------------------------------------------------------------------
QNetworkReply* qRestAPI::sendRequest(QNetworkAccessManager::Operation operation,
const QUrl& url,
const qRestAPI::RawHeaders& rawHeaders,
const QByteArray &data)
{
Q_D(qRestAPI);
QNetworkRequest queryRequest;
queryRequest.setUrl(url);
for (QMapIterator<QByteArray, QByteArray> it(d->DefaultRawHeaders); it.hasNext();)
{
it.next();
queryRequest.setRawHeader(it.key(), it.value());
}
for (QMapIterator<QByteArray, QByteArray> it(rawHeaders); it.hasNext();)
{
it.next();
queryRequest.setRawHeader(it.key(), it.value());
}
QNetworkReply* queryReply;
switch (operation)
{
case QNetworkAccessManager::GetOperation:
queryReply = d->NetworkManager->get(queryRequest);
break;
case QNetworkAccessManager::DeleteOperation:
queryReply = d->NetworkManager->deleteResource(queryRequest);
break;
case QNetworkAccessManager::PutOperation:
queryReply = d->NetworkManager->put(queryRequest, data);
break;
case QNetworkAccessManager::PostOperation:
queryReply = d->NetworkManager->post(queryRequest, data);
break;
case QNetworkAccessManager::HeadOperation:
queryReply = d->NetworkManager->head(queryRequest);
break;
default:
// TODO
return 0;
}
if (d->TimeOut > 0)
{
QTimer* timeOut = new QTimer(queryReply);
timeOut->setSingleShot(true);
QObject::connect(timeOut, SIGNAL(timeout()),
d, SLOT(queryTimeOut()));
timeOut->start(d->TimeOut);
}
QUuid queryId = QUuid::createUuid();
queryReply->setProperty("uuid", queryId.toString());
qRestResult* result = new qRestResult(queryId);
d->results[queryId] = result;
// QObject::connect(this, SIGNAL(resultReceived(QUuid,QList<QVariantMap>)),
// result, SLOT(setResult(QList<QVariantMap>)));
// QObject::connect(this, SIGNAL(errorReceived(QUuid,QString)),
// result, SLOT(setError(QString)));
return queryReply;
}
// --------------------------------------------------------------------------
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
QVariantMap qRestAPI::scriptValueToMap(const QJSValue& value)
#else
QVariantMap qRestAPI::scriptValueToMap(const QScriptValue& value)
#endif
{
#if QT_VERSION >= 0x040700
return value.toVariant().toMap();
#else
QVariantMap result;
for (QScriptValueIterator it(value); it.hasNext();)
{
it.next();
result.insert(it.name(), it.value().toVariant());
}
return result;
#endif
}
// --------------------------------------------------------------------------
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
void qRestAPI::appendScriptValueToVariantMapList(QList<QVariantMap>& result, const QJSValue& data)
#else
void qRestAPI::appendScriptValueToVariantMapList(QList<QVariantMap>& result, const QScriptValue& data)
#endif
{
QVariantMap map = scriptValueToMap(data);
if (!map.isEmpty())
{
result << scriptValueToMap(data);
}
}
// --------------------------------------------------------------------------
QVariantMap qRestAPI::qVariantMapFlattened(const QVariantMap& map)
{
QVariantMap output;
foreach(const QString& key, map.keys())
{
QVariant value = map.value(key);
if (value.canConvert<QVariantMap>())
{
value = QVariant::fromValue(QVariantList() << value.toMap());
}
if (value.canConvert<QVariantList>())
{
foreach(const QVariant& item, value.toList())
{
QVariantMap subMap = qRestAPI::qVariantMapFlattened(item.toMap());
foreach(const QString& subKey, subMap.keys())
{
output.insert(QString("%1.%2").arg(key).arg(subKey), subMap.value(subKey));
}
}
}
else
{
output.insert(key, value);
}
}
return output;
}
// --------------------------------------------------------------------------
void qRestAPIPrivate::processReply(QNetworkReply* reply)
{
Q_Q(qRestAPI);
QUuid queryId(reply->property("uuid").toString());
qRestResult* restResult = results[queryId];
Q_ASSERT(restResult);
if (reply->error() != QNetworkReply::NoError)
{
qRestAPI::ErrorType errorCode = qRestAPI::NetworkError;
switch (reply->error())
{
case QNetworkReply::TimeoutError:
errorCode = qRestAPI::TimeoutError;
break;
case QNetworkReply::SslHandshakeFailedError:
errorCode = qRestAPI::SslError;
break;
case QNetworkReply::AuthenticationRequiredError:
errorCode = qRestAPI::AuthenticationError;
break;
default:
;
}
restResult->setError(queryId.toString() + ": " +
QString::number(static_cast<int>(reply->error())) + ": " +
reply->errorString(),
errorCode);
}
else
{
restResult->Reponse = reply->readAll();
q->parseResponse(restResult, restResult->response());
}
#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
foreach(const QNetworkReply::RawHeaderPair& rawHeaderPair, reply->rawHeaderPairs())
{
restResult->setRawHeader(rawHeaderPair.first, rawHeaderPair.second);
}
#else
foreach(const QByteArray& headerName, reply->rawHeaderList())
{
restResult->setRawHeader(headerName, reply->rawHeader(headerName));
}
#endif
reply->close();
reply->deleteLater();
q->emit finished(queryId);
}
void qRestAPIPrivate::onSslErrors(QNetworkReply* reply, const QList<QSslError>& errors)
{
#ifdef QRESTAPI_QT_NO_SSL
Q_UNUSED(reply)
Q_UNUSED(errors)
#else
if (!this->SuppressSslErrors)
{
QString errorString;
foreach (const QSslError& error, errors)
{
if (!errorString.isEmpty())
{
errorString.append(", ");
}
errorString.append(error.errorString());
}
QString plural(errors.empty() ? " has" : "s have");
QString error = QString("SSL error%1 occurred: %2").arg(plural).arg(errorString);
QUuid queryId(reply->property("uuid").toString());
qRestResult* restResult = results[queryId];
restResult->setError(error, qRestAPI::SslError);
}
else
{
reply->ignoreSslErrors();
}
#endif
}
// --------------------------------------------------------------------------
void qRestAPIPrivate::queryProgress(qint64 bytesTransmitted, qint64 bytesTotal)
{
Q_UNUSED(bytesTransmitted);
Q_UNUSED(bytesTotal);
QNetworkReply* reply = qobject_cast<QNetworkReply*>(this->sender());
// We received some progress so we postpone the timeout if any.
QTimer* timer = reply->findChild<QTimer*>();
if (timer)
{
timer->start();
}
}
// --------------------------------------------------------------------------
void qRestAPIPrivate::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
Q_Q(qRestAPI);
QNetworkReply* reply = qobject_cast<QNetworkReply*>(this->sender());
QUuid queryId(reply->property("uuid").toString());
double progress = static_cast<double>(bytesReceived) / bytesTotal;
q->emit progress(queryId, progress);
}
// --------------------------------------------------------------------------
void qRestAPIPrivate::uploadProgress(qint64 bytesSent, qint64 bytesTotal)
{
Q_Q(qRestAPI);
QNetworkReply* reply = qobject_cast<QNetworkReply*>(this->sender());
Q_ASSERT(reply);
if (!reply)
{
return;
}
QUuid queryId(reply->property("uuid").toString());
double progress = static_cast<double>(bytesSent) / bytesTotal;
q->emit progress(queryId, progress);
}
// --------------------------------------------------------------------------
void qRestAPIPrivate::queryTimeOut()
{
QTimer* timer = qobject_cast<QTimer*>(this->sender());
Q_ASSERT(timer);
QNetworkReply* reply = qobject_cast<QNetworkReply*>(timer->parent());
Q_ASSERT(reply);
reply->abort();
//reply->setError(QNetworkReply::TimeoutError,
// q->tr("Time out: No progress for %1 seconds.").arg(timer->interval()));
}
// --------------------------------------------------------------------------
// qRestAPI methods
// --------------------------------------------------------------------------
qRestAPI::qRestAPI(QObject* _parent)
: Superclass(_parent)
, d_ptr(new qRestAPIPrivate(this))
{
Q_D(qRestAPI);
d->init();
}
// --------------------------------------------------------------------------
qRestAPI::~qRestAPI()
{
}
// --------------------------------------------------------------------------
QString qRestAPI::serverUrl()const
{
Q_D(const qRestAPI);
return d->ServerUrl;
}
// --------------------------------------------------------------------------
void qRestAPI::setServerUrl(const QString& serverUrl)
{
Q_D(qRestAPI);
d->ServerUrl = serverUrl;
}
// --------------------------------------------------------------------------
int qRestAPI::timeOut()const
{
Q_D(const qRestAPI);
return d->TimeOut;
}
// --------------------------------------------------------------------------
void qRestAPI::setTimeOut(int msecs)
{
Q_D(qRestAPI);
d->TimeOut = msecs;
}
// --------------------------------------------------------------------------
qRestAPI::RawHeaders qRestAPI::defaultRawHeaders()const
{
Q_D(const qRestAPI);
return d->DefaultRawHeaders;
}
// --------------------------------------------------------------------------
void qRestAPI::setDefaultRawHeaders(const qRestAPI::RawHeaders& defaultRawHeaders)
{
Q_D(qRestAPI);
d->DefaultRawHeaders = defaultRawHeaders;
}
// --------------------------------------------------------------------------
bool qRestAPI::suppressSslErrors()const
{
Q_D(const qRestAPI);
return d->SuppressSslErrors;
}
// --------------------------------------------------------------------------
void qRestAPI::setSuppressSslErrors(bool suppressSslErrors)
{
Q_D(qRestAPI);
d->SuppressSslErrors = suppressSslErrors;
}
// --------------------------------------------------------------------------
QUrl qRestAPI::createUrl(const QString& resource, const qRestAPI::Parameters& parameters)
{
Q_D(qRestAPI);
QUrl url(d->ServerUrl + resource);
#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
foreach(const QString& parameter, parameters.keys())
{
url.addQueryItem(parameter, parameters[parameter]);
}
#else
QUrlQuery urlQuery(url);
foreach(const QString& parameter, parameters.keys())
{
urlQuery.addQueryItem(parameter, parameters[parameter]);
}
url.setQuery(urlQuery);
#endif
return url;
}
// --------------------------------------------------------------------------
void qRestAPI::parseResponse(qRestResult* restResult, const QByteArray& response)
{
Q_UNUSED(response);
QList<QVariantMap> result;
restResult->setResult(result);
}
// --------------------------------------------------------------------------
QUuid qRestAPI::get(const QString& resource, const Parameters& parameters, const qRestAPI::RawHeaders& rawHeaders)
{
QUrl url = createUrl(resource, parameters);
QNetworkReply* queryReply = sendRequest(QNetworkAccessManager::GetOperation, url, rawHeaders);
QUuid queryId(queryReply->property("uuid").toString());
return queryId;
}
// --------------------------------------------------------------------------
QUuid qRestAPI::get(QIODevice* output, const QString& resource, const Parameters& parameters, const qRestAPI::RawHeaders& rawHeaders)
{
Q_D(qRestAPI);
QUrl url = createUrl(resource, parameters);
if (!output->open(QIODevice::WriteOnly))
{
QUuid queryId;
// TODO How to raise the error? We do not have a qRestResult object yet...
// emit errorReceived(queryId, "Cannot open device for writing.");
return queryId;
}
QNetworkReply* queryReply = sendRequest(QNetworkAccessManager::GetOperation, url, rawHeaders);
QUuid queryId = QUuid(queryReply->property("uuid").toString());
qRestResult* result = new qRestResult(queryId, queryReply);
result->ioDevice = output;
connect(queryReply, SIGNAL(downloadProgress(qint64,qint64)),
d, SLOT(downloadProgress(qint64,qint64)));
connect(queryReply, SIGNAL(readyRead()),
result, SLOT(downloadReadyRead()));
connect(queryReply, SIGNAL(finished()),
result, SLOT(downloadFinished()));
return queryId;
}
// --------------------------------------------------------------------------
QUuid qRestAPI::head(const QString resource, const Parameters& parameters, const qRestAPI::RawHeaders& rawHeaders)
{
QUrl url = createUrl(resource, parameters);
QNetworkReply* queryReply = sendRequest(QNetworkAccessManager::HeadOperation, url, rawHeaders);
QUuid queryId = QUuid(queryReply->property("uuid").toString());
return queryId;
}
// --------------------------------------------------------------------------
QUuid qRestAPI::download(const QString& fileName, const QString& resource, const Parameters& parameters, const qRestAPI::RawHeaders& rawHeaders)
{
Q_UNUSED(rawHeaders);
Q_D(qRestAPI);
QIODevice* output = new QFile(fileName);
QUuid queryId = get(output, resource, parameters);
output->setParent(d->results[queryId]);
return queryId;
}
// --------------------------------------------------------------------------
QUuid qRestAPI::del(const QString& resource, const Parameters& parameters, const qRestAPI::RawHeaders& rawHeaders)
{
QUrl url = createUrl(resource, parameters);
QNetworkReply* queryReply = sendRequest(QNetworkAccessManager::DeleteOperation, url, rawHeaders);
QUuid queryId(queryReply->property("uuid").toString());
return queryId;
}
// --------------------------------------------------------------------------
QUuid qRestAPI::post(const QString& resource, const Parameters& parameters, const qRestAPI::RawHeaders& rawHeaders)
{
QUrl url = createUrl(resource, parameters);
QNetworkReply* queryReply = sendRequest(QNetworkAccessManager::PostOperation, url, rawHeaders);
QUuid queryId(queryReply->property("uuid").toString());
return queryId;
}
// --------------------------------------------------------------------------
QUuid qRestAPI::put(const QString& resource, const Parameters& parameters, const qRestAPI::RawHeaders& rawHeaders)
{
QUrl url = createUrl(resource, parameters);
QNetworkReply* queryReply = sendRequest(QNetworkAccessManager::PutOperation, url, rawHeaders);
QUuid queryId(queryReply->property("uuid").toString());
return queryId;
}
QUuid qRestAPI::put(QIODevice *input, const QString &resource, const qRestAPI::Parameters ¶meters, const qRestAPI::RawHeaders &rawHeaders)
{
Q_D(qRestAPI);
QUrl url = createUrl(resource, parameters);
if (!input->isOpen() && !input->open(QIODevice::ReadOnly))
{
QUuid uid = QUuid::createUuid();
qRestResult* restResult = new qRestResult(uid);
restResult->setError(uid.toString() + ": " +
"Could not open file for upload!",
qRestAPI::FileError);
d->results[uid] = restResult;
return uid;
}
QByteArray data = input->readAll();
QNetworkReply* queryReply = sendRequest(QNetworkAccessManager::PutOperation, url, rawHeaders, data);
QUuid queryId (queryReply->property("uuid").toString());
qRestResult* result = d->results[queryId];
result->ioDevice = input;
connect(queryReply, SIGNAL(uploadProgress(qint64,qint64)),
d, SLOT(uploadProgress(qint64,qint64)));
connect(queryReply, SIGNAL(finished()),
result, SLOT(uploadFinished()));
return queryId;
}
// --------------------------------------------------------------------------
QUuid qRestAPI::upload(const QString& fileName, const QString& resource, const Parameters& parameters, const qRestAPI::RawHeaders& rawHeaders)
{
Q_D(qRestAPI);
QIODevice* input = new QFile(fileName);
QUuid queryId = this->put(input, resource, parameters, rawHeaders);
input->setParent(d->results[queryId]);
return queryId;
}
// --------------------------------------------------------------------------
bool qRestAPI::sync(const QUuid& queryId)
{
QList<QVariantMap> result;
return this->sync(queryId, result);
}
// --------------------------------------------------------------------------
bool qRestAPI::sync(const QUuid& queryId, QList<QVariantMap>& result)
{
Q_D(qRestAPI);
result.clear();
if (d->results.contains(queryId))
{
bool ok = d->results[queryId]->waitForDone();
qRestResult* queryResult = d->results.take(queryId);
if (!ok)
{
QVariantMap map;
map["queryError"] = queryResult->Error;
queryResult->Result.push_front(map);
d->ErrorCode = queryResult->errorType();
d->ErrorString = queryResult->error();
}
result = queryResult->Result;
delete queryResult;
return ok;
}
d->ErrorCode = UnknownUuidError;
d->ErrorString = unknownUuidStr.arg(queryId.toString());
return false;
}
// --------------------------------------------------------------------------
QString qRestAPI::qVariantMapListToString(const QList<QVariantMap>& list)
{
QStringList values;
foreach(const QVariantMap& map, list)
{
foreach(const QString& key, map.keys())
{
values << QString("%1: %2").arg(key, map[key].toString());
}
}
return values.join("\n");
}
// --------------------------------------------------------------------------
QString qRestAPI::qVariantToString(const QVariant &value, int indent)
{
QString output;
bool isVariantList = value.canConvert<QVariantList>();
bool isVariantMap = value.canConvert<QVariantMap>();
if ((isVariantList || isVariantMap) && indent > 0)
{
output += "\n";
}
if (isVariantList)
{
QVariantList list = value.toList();
for(int idx=0; idx < list.count(); ++idx)
{
output.append(QString("%1%2: ").arg(QString(indent, ' ')).arg(idx));
output.append(qRestAPI::qVariantToString(list.at(idx), indent + 2));
}
}
else if (isVariantMap)
{
QVariantMap map = value.toMap();
foreach(const QString& key, map.keys())
{
output.append(QString("%1%2: ").arg(QString(indent, ' ')).arg(key));
output.append(qRestAPI::qVariantToString(map.value(key), indent + 2));
}
}
else
{
return value.toString() + "\n";
}
return output;
}
// --------------------------------------------------------------------------
qRestResult* qRestAPI::takeResult(const QUuid& queryId)
{
Q_D(qRestAPI);
if (d->results.contains(queryId))
{
// Do /not/ try to .take() the query before calling waitForDone();
// the latter triggers SIGNALs which access d->results.
bool ok = d->results[queryId]->waitForDone();
qRestResult* result = d->results.take(queryId);
if (ok)
{
return result;
}
else
{
d->ErrorCode = result->errorType();
d->ErrorString = result->error();
delete result;
return NULL;
}
}
d->ErrorCode = UnknownUuidError;
d->ErrorString = QString::number(UnknownUuidError);
return NULL;
}
// --------------------------------------------------------------------------
qRestAPI::ErrorType qRestAPI::error() const
{
Q_D(const qRestAPI);
return d->ErrorCode;
}
// --------------------------------------------------------------------------
QString qRestAPI::errorString() const
{
Q_D(const qRestAPI);
return d->ErrorString;
}
//// --------------------------------------------------------------------------
//void qRestAPIPrivate::onAuthenticationRequired(QNetworkReply* reply, QAuthenticator* authenticator)
//{
// Q_D(qRestAPI);
// authenticator->setUser(d->userName);
// authenticator->setPassword(d->password);
//}