mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-13 19:49:51 +01:00
56 lines
1.2 KiB
C++
56 lines
1.2 KiB
C++
#ifndef URLFETCH_H
|
|
#define URLFETCH_H
|
|
|
|
#include <QJsonArray>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QJsonValue>
|
|
#include <QNetworkAccessManager>
|
|
#include <QNetworkReply>
|
|
#include <QNetworkRequest>
|
|
#include <QString>
|
|
|
|
#include <functional>
|
|
|
|
namespace chatterino {
|
|
namespace util {
|
|
|
|
static void urlFetch(const QString &url, std::function<void(QNetworkReply &)> successCallback)
|
|
{
|
|
QNetworkAccessManager *manager = new QNetworkAccessManager();
|
|
|
|
QUrl requestUrl(url);
|
|
QNetworkRequest request(requestUrl);
|
|
|
|
QNetworkReply *reply = manager->get(request);
|
|
|
|
QObject::connect(reply, &QNetworkReply::finished, [=] {
|
|
if (reply->error() == QNetworkReply::NetworkError::NoError) {
|
|
successCallback(*reply);
|
|
}
|
|
|
|
reply->deleteLater();
|
|
manager->deleteLater();
|
|
});
|
|
}
|
|
|
|
static void urlJsonFetch(const QString &url, std::function<void(QJsonObject &)> successCallback)
|
|
{
|
|
urlFetch(url, [=](QNetworkReply &reply) {
|
|
QByteArray data = reply.readAll();
|
|
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
|
|
|
|
if (jsonDoc.isNull()) {
|
|
return;
|
|
}
|
|
|
|
QJsonObject rootNode = jsonDoc.object();
|
|
|
|
successCallback(rootNode);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
#endif // URLFETCH_H
|