2017-10-27 20:09:02 +02:00
|
|
|
#pragma once
|
|
|
|
|
2017-10-27 22:04:05 +02:00
|
|
|
#include "debug/log.hpp"
|
2018-01-19 22:45:33 +01:00
|
|
|
#include "util/networkrequester.hpp"
|
|
|
|
#include "util/networkworker.hpp"
|
2017-10-27 22:04:05 +02:00
|
|
|
|
2017-10-27 21:22:06 +02:00
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
2017-10-27 20:09:02 +02:00
|
|
|
#include <QNetworkAccessManager>
|
2017-10-27 21:22:06 +02:00
|
|
|
#include <QNetworkReply>
|
2017-10-27 20:09:02 +02:00
|
|
|
#include <QThread>
|
2017-10-27 22:04:05 +02:00
|
|
|
#include <QTimer>
|
2017-10-27 20:09:02 +02:00
|
|
|
#include <QUrl>
|
|
|
|
|
|
|
|
namespace chatterino {
|
2017-10-27 21:22:06 +02:00
|
|
|
namespace util {
|
2017-10-27 20:09:02 +02:00
|
|
|
|
2017-10-27 21:22:06 +02:00
|
|
|
static QJsonObject parseJSONFromReplyxD(QNetworkReply *reply)
|
|
|
|
{
|
|
|
|
if (reply->error() != QNetworkReply::NetworkError::NoError) {
|
|
|
|
return QJsonObject();
|
|
|
|
}
|
2017-10-27 20:09:02 +02:00
|
|
|
|
2017-10-27 21:22:06 +02:00
|
|
|
QByteArray data = reply->readAll();
|
|
|
|
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
|
2017-10-27 20:09:02 +02:00
|
|
|
|
2017-10-27 21:22:06 +02:00
|
|
|
if (jsonDoc.isNull()) {
|
|
|
|
return QJsonObject();
|
|
|
|
}
|
2017-10-27 20:09:02 +02:00
|
|
|
|
2017-10-27 21:22:06 +02:00
|
|
|
return jsonDoc.object();
|
|
|
|
}
|
2017-10-27 20:09:02 +02:00
|
|
|
|
|
|
|
class NetworkManager : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
static QThread workerThread;
|
|
|
|
static QNetworkAccessManager NaM;
|
|
|
|
|
|
|
|
static void init();
|
|
|
|
static void deinit();
|
|
|
|
|
|
|
|
template <typename FinishedCallback>
|
|
|
|
static void urlFetch(QNetworkRequest request, FinishedCallback onFinished)
|
|
|
|
{
|
|
|
|
NetworkRequester requester;
|
|
|
|
NetworkWorker *worker = new NetworkWorker;
|
|
|
|
|
|
|
|
worker->moveToThread(&NetworkManager::workerThread);
|
2017-11-04 16:56:38 +01:00
|
|
|
QObject::connect(
|
|
|
|
&requester, &NetworkRequester::requestUrl, worker,
|
2018-03-31 13:44:15 +02:00
|
|
|
[worker, onFinished = std::move(onFinished), request = std::move(request)]() {
|
2017-11-04 16:56:38 +01:00
|
|
|
QNetworkReply *reply = NetworkManager::NaM.get(request);
|
2017-10-27 20:09:02 +02:00
|
|
|
|
2017-11-04 16:56:38 +01:00
|
|
|
QObject::connect(reply, &QNetworkReply::finished,
|
2018-03-31 13:44:15 +02:00
|
|
|
[worker, reply, onFinished = std::move(onFinished)]() {
|
2017-11-04 16:56:38 +01:00
|
|
|
onFinished(reply);
|
|
|
|
delete worker;
|
|
|
|
});
|
|
|
|
});
|
2017-10-27 20:09:02 +02:00
|
|
|
|
|
|
|
emit requester.requestUrl();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename FinishedCallback>
|
|
|
|
static void urlFetch(const QUrl &url, FinishedCallback onFinished)
|
|
|
|
{
|
|
|
|
urlFetch(QNetworkRequest(url), std::move(onFinished));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Callback, typename ReplyCreatedCallback = void (*)(QNetworkReply *)>
|
|
|
|
static void urlFetch(QNetworkRequest request, const QObject *caller, Callback callback,
|
|
|
|
ReplyCreatedCallback onReplyCreated = [](QNetworkReply *) { return; })
|
|
|
|
{
|
|
|
|
NetworkRequester requester;
|
|
|
|
NetworkWorker *worker = new NetworkWorker;
|
|
|
|
|
|
|
|
worker->moveToThread(&NetworkManager::workerThread);
|
|
|
|
|
|
|
|
QObject::connect(&requester, &NetworkRequester::requestUrl, worker, [=]() {
|
|
|
|
QNetworkReply *reply = NetworkManager::NaM.get(request);
|
|
|
|
|
|
|
|
onReplyCreated(reply);
|
|
|
|
|
|
|
|
QObject::connect(reply, &QNetworkReply::finished, worker,
|
|
|
|
[=]() { emit worker->doneUrl(reply); });
|
|
|
|
});
|
|
|
|
|
|
|
|
QObject::connect(worker, &NetworkWorker::doneUrl, caller, [=](QNetworkReply *reply) {
|
|
|
|
callback(reply);
|
|
|
|
delete worker;
|
|
|
|
});
|
|
|
|
emit requester.requestUrl();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Callback, typename ReplyCreatedCallback = void (*)(QNetworkReply *)>
|
|
|
|
static void urlFetch(const QUrl &url, const QObject *caller, Callback callback,
|
|
|
|
ReplyCreatedCallback onReplyCreated = [](QNetworkReply *) { return; })
|
|
|
|
{
|
|
|
|
urlFetch(QNetworkRequest(url), caller, callback, onReplyCreated);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename FinishedCallback>
|
|
|
|
static void urlPut(QNetworkRequest request, FinishedCallback onFinished, QByteArray *data)
|
|
|
|
{
|
|
|
|
NetworkRequester requester;
|
|
|
|
NetworkWorker *worker = new NetworkWorker;
|
|
|
|
|
|
|
|
worker->moveToThread(&NetworkManager::workerThread);
|
|
|
|
QObject::connect(
|
|
|
|
&requester, &NetworkRequester::requestUrl, worker,
|
2018-03-31 13:44:15 +02:00
|
|
|
[worker, data, onFinished = std::move(onFinished), request = std::move(request)]() {
|
2017-10-27 20:09:02 +02:00
|
|
|
QNetworkReply *reply = NetworkManager::NaM.put(request, *data);
|
|
|
|
|
|
|
|
QObject::connect(reply, &QNetworkReply::finished,
|
2018-03-31 13:44:15 +02:00
|
|
|
[worker, reply, onFinished = std::move(onFinished)]() {
|
2017-10-27 20:09:02 +02:00
|
|
|
onFinished(reply);
|
|
|
|
delete worker;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
emit requester.requestUrl();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename FinishedCallback>
|
|
|
|
static void urlPut(QNetworkRequest request, FinishedCallback onFinished)
|
|
|
|
{
|
|
|
|
NetworkRequester requester;
|
|
|
|
NetworkWorker *worker = new NetworkWorker;
|
|
|
|
|
|
|
|
worker->moveToThread(&NetworkManager::workerThread);
|
|
|
|
QObject::connect(
|
|
|
|
&requester, &NetworkRequester::requestUrl, worker,
|
2018-03-31 13:44:15 +02:00
|
|
|
[onFinished = std::move(onFinished), request = std::move(request), worker]() {
|
2017-10-27 20:09:02 +02:00
|
|
|
QNetworkReply *reply = NetworkManager::NaM.put(request, "");
|
|
|
|
|
|
|
|
QObject::connect(reply, &QNetworkReply::finished,
|
2018-03-31 13:44:15 +02:00
|
|
|
[onFinished = std::move(onFinished), reply, worker]() {
|
2017-10-27 20:09:02 +02:00
|
|
|
onFinished(reply);
|
|
|
|
delete worker;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
emit requester.requestUrl();
|
|
|
|
}
|
2018-01-18 18:20:40 +01:00
|
|
|
|
|
|
|
template <typename FinishedCallback>
|
|
|
|
static void urlDelete(QNetworkRequest request, FinishedCallback onFinished)
|
|
|
|
{
|
|
|
|
NetworkRequester requester;
|
|
|
|
NetworkWorker *worker = new NetworkWorker;
|
|
|
|
|
|
|
|
worker->moveToThread(&NetworkManager::workerThread);
|
|
|
|
QObject::connect(
|
|
|
|
&requester, &NetworkRequester::requestUrl, worker,
|
2018-03-31 13:44:15 +02:00
|
|
|
[onFinished = std::move(onFinished), request = std::move(request), worker]() {
|
2018-01-18 18:20:40 +01:00
|
|
|
QNetworkReply *reply = NetworkManager::NaM.deleteResource(request);
|
|
|
|
|
|
|
|
QObject::connect(reply, &QNetworkReply::finished,
|
2018-03-31 13:44:15 +02:00
|
|
|
[onFinished = std::move(onFinished), reply, worker]() {
|
2018-01-18 18:20:40 +01:00
|
|
|
onFinished(reply);
|
|
|
|
delete worker;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
emit requester.requestUrl();
|
|
|
|
}
|
2017-10-27 20:09:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace util
|
|
|
|
} // namespace chatterino
|