-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpsignal.cpp
More file actions
33 lines (29 loc) · 991 Bytes
/
httpsignal.cpp
File metadata and controls
33 lines (29 loc) · 991 Bytes
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
#include "httpsignal.h"
Q_INVOKABLE void HttpSignal::fetch(QString url) {
if (!manager) {
manager = new QNetworkAccessManager(this);
}
QUrl qurl(url);
if (!qurl.isValid() || qurl.isRelative()) {
qurl = QUrl("http://localhost:3000").resolved(qurl);
}
if (!qurl.isValid()) {
Q_EMIT error("Invalid URL" + qurl.toString(), qurl.toString());
return;
}
QNetworkRequest request{qurl};
QNetworkReply *reply = nullptr;
reply = manager->get(request);
QObject::connect(
reply, &QNetworkReply::finished, this,
[qurl, reply, this]() {
QByteArray response = reply->readAll();
if (reply->error() == QNetworkReply::NoError) {
Q_EMIT then(QString::fromUtf8(response), qurl.toString());
} else {
Q_EMIT error(reply->errorString(), qurl.toString());
}
reply->deleteLater();
},
Qt::SingleShotConnection);
}