Move timeout logic to NetworkRequest
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "debug/log.hpp"
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QThread>
|
||||
#include <QTimer>
|
||||
#include <QUrl>
|
||||
|
||||
namespace chatterino {
|
||||
@@ -32,6 +35,7 @@ class NetworkRequest
|
||||
QNetworkRequest request;
|
||||
const QObject *caller = nullptr;
|
||||
std::function<void(QNetworkReply *)> onReplyCreated;
|
||||
int timeoutMS = -1;
|
||||
} data;
|
||||
|
||||
public:
|
||||
@@ -67,9 +71,19 @@ public:
|
||||
this->data.request.setRawHeader(headerName, value);
|
||||
}
|
||||
|
||||
void setTimeout(int ms)
|
||||
{
|
||||
this->data.timeoutMS = ms;
|
||||
}
|
||||
|
||||
template <typename FinishedCallback>
|
||||
void get(FinishedCallback onFinished)
|
||||
{
|
||||
QTimer *timer = nullptr;
|
||||
if (this->data.timeoutMS > 0) {
|
||||
timer = new QTimer;
|
||||
}
|
||||
|
||||
NetworkRequester requester;
|
||||
NetworkWorker *worker = new NetworkWorker;
|
||||
|
||||
@@ -83,11 +97,23 @@ public:
|
||||
});
|
||||
}
|
||||
|
||||
if (timer != nullptr) {
|
||||
timer->start(this->data.timeoutMS);
|
||||
}
|
||||
|
||||
QObject::connect(
|
||||
&requester, &NetworkRequester::requestUrl, worker,
|
||||
[ data = std::move(this->data), worker, onFinished{std::move(onFinished)} ]() {
|
||||
[ timer, data = std::move(this->data), worker, onFinished{std::move(onFinished)} ]() {
|
||||
QNetworkReply *reply = NetworkManager::NaM.get(data.request);
|
||||
|
||||
if (timer != nullptr) {
|
||||
QObject::connect(timer, &QTimer::timeout, worker, [reply, timer]() {
|
||||
debug::Log("Aborted!");
|
||||
reply->abort();
|
||||
timer->deleteLater();
|
||||
});
|
||||
}
|
||||
|
||||
if (data.onReplyCreated) {
|
||||
data.onReplyCreated(reply);
|
||||
}
|
||||
|
||||
@@ -38,51 +38,6 @@ static QJsonObject parseJSONFromReply(QNetworkReply *reply)
|
||||
return jsonDoc.object();
|
||||
}
|
||||
|
||||
static void urlFetchTimeout(const QString &url, const QObject *caller,
|
||||
std::function<void(QNetworkReply *)> successCallback, int timeoutMs)
|
||||
{
|
||||
QTimer *timer = new QTimer;
|
||||
timer->setSingleShot(true);
|
||||
|
||||
QEventLoop *loop = new QEventLoop;
|
||||
|
||||
util::NetworkRequest req(url);
|
||||
req.setCaller(loop);
|
||||
req.setOnReplyCreated([loop, timer](QNetworkReply *reply) {
|
||||
QObject::connect(timer, &QTimer::timeout, loop, [=]() {
|
||||
QObject::disconnect(reply, &QNetworkReply::finished, loop, &QEventLoop::quit);
|
||||
reply->abort();
|
||||
reply->deleteLater();
|
||||
});
|
||||
});
|
||||
req.get([=](QNetworkReply *reply) {
|
||||
if (reply->error() == QNetworkReply::NetworkError::NoError) {
|
||||
successCallback(reply);
|
||||
}
|
||||
|
||||
reply->deleteLater();
|
||||
loop->quit();
|
||||
});
|
||||
|
||||
QObject::connect(timer, SIGNAL(timeout()), loop, SLOT(quit()));
|
||||
|
||||
timer->start(timeoutMs);
|
||||
loop->exec();
|
||||
delete timer;
|
||||
delete loop;
|
||||
}
|
||||
|
||||
static void urlFetchJSONTimeout(const QString &url, const QObject *caller,
|
||||
std::function<void(QJsonObject &)> successCallback, int timeoutMs)
|
||||
{
|
||||
urlFetchTimeout(url, caller,
|
||||
[=](QNetworkReply *reply) {
|
||||
auto node = parseJSONFromReply(reply);
|
||||
successCallback(node);
|
||||
},
|
||||
timeoutMs);
|
||||
}
|
||||
|
||||
namespace twitch {
|
||||
|
||||
static void get(QString url, const QObject *caller,
|
||||
|
||||
Reference in New Issue
Block a user