mirror-chatterino2/src/util/urlfetch.h

56 lines
1.2 KiB
C
Raw Normal View History

2017-04-12 17:46:44 +02:00
#ifndef URLFETCH_H
#define URLFETCH_H
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QString>
#include <functional>
2017-04-14 17:52:22 +02:00
namespace chatterino {
namespace util {
2017-04-12 17:46:44 +02:00
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