mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
attempt to implement urlFetch with a timeout
This commit is contained in:
parent
1d8795ef1b
commit
c2e67e4b90
9 changed files with 130 additions and 15 deletions
3
.gitmodules
vendored
3
.gitmodules
vendored
|
@ -7,3 +7,6 @@
|
||||||
[submodule "lib/humanize"]
|
[submodule "lib/humanize"]
|
||||||
path = lib/humanize
|
path = lib/humanize
|
||||||
url = https://github.com/pajlada/humanize.git
|
url = https://github.com/pajlada/humanize.git
|
||||||
|
[submodule "lib/signals"]
|
||||||
|
path = lib/signals
|
||||||
|
url = https://github.com/pajlada/signals.git
|
||||||
|
|
|
@ -92,7 +92,8 @@ SOURCES += \
|
||||||
src/accountmanager.cpp \
|
src/accountmanager.cpp \
|
||||||
src/twitch/twitchuser.cpp \
|
src/twitch/twitchuser.cpp \
|
||||||
src/ircaccount.cpp \
|
src/ircaccount.cpp \
|
||||||
src/widgets/accountpopup.cpp
|
src/widgets/accountpopup.cpp \
|
||||||
|
src/messagefactory.cpp
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
src/asyncexec.hpp \
|
src/asyncexec.hpp \
|
||||||
|
@ -150,7 +151,9 @@ HEADERS += \
|
||||||
src/twitch/twitchuser.hpp \
|
src/twitch/twitchuser.hpp \
|
||||||
src/ircaccount.hpp \
|
src/ircaccount.hpp \
|
||||||
src/widgets/accountpopup.hpp \
|
src/widgets/accountpopup.hpp \
|
||||||
src/util/distancebetweenpoints.hpp
|
src/util/distancebetweenpoints.hpp \
|
||||||
|
src/messagefactory.hpp \
|
||||||
|
src/widgets/basewidget.hpp
|
||||||
|
|
||||||
PRECOMPILED_HEADER =
|
PRECOMPILED_HEADER =
|
||||||
|
|
||||||
|
@ -197,4 +200,5 @@ werr {
|
||||||
# External dependencies
|
# External dependencies
|
||||||
include(dependencies/rapidjson.pri)
|
include(dependencies/rapidjson.pri)
|
||||||
include(dependencies/settings.pri)
|
include(dependencies/settings.pri)
|
||||||
|
include(dependencies/signals.pri)
|
||||||
include(dependencies/humanize.pri)
|
include(dependencies/humanize.pri)
|
||||||
|
|
3
dependencies/settings.pri
vendored
3
dependencies/settings.pri
vendored
|
@ -4,6 +4,3 @@ SOURCES += \
|
||||||
$$PWD/../lib/settings/src/settings/settingmanager.cpp
|
$$PWD/../lib/settings/src/settings/settingmanager.cpp
|
||||||
|
|
||||||
INCLUDEPATH += $$PWD/../lib/settings/include/
|
INCLUDEPATH += $$PWD/../lib/settings/include/
|
||||||
|
|
||||||
# signals
|
|
||||||
INCLUDEPATH += $$PWD/../lib/settings/external/signals/include
|
|
||||||
|
|
2
dependencies/signals.pri
vendored
Normal file
2
dependencies/signals.pri
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
# signals
|
||||||
|
INCLUDEPATH += $$PWD/../lib/signals/include/
|
1
lib/signals
Submodule
1
lib/signals
Submodule
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit 2c9c92b971f4a1313eeb4d9daf8ea59565d3c691
|
6
src/messagefactory.cpp
Normal file
6
src/messagefactory.cpp
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
#include "messagefactory.hpp"
|
||||||
|
|
||||||
|
MessageFactory::MessageFactory()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
11
src/messagefactory.hpp
Normal file
11
src/messagefactory.hpp
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
#ifndef MESSAGEFACTORY_HPP
|
||||||
|
#define MESSAGEFACTORY_HPP
|
||||||
|
|
||||||
|
|
||||||
|
class MessageFactory
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
MessageFactory();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // MESSAGEFACTORY_HPP
|
|
@ -1,5 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <QEventLoop>
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
|
@ -8,15 +9,22 @@
|
||||||
#include <QNetworkReply>
|
#include <QNetworkReply>
|
||||||
#include <QNetworkRequest>
|
#include <QNetworkRequest>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
#include <QTimer>
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
namespace chatterino {
|
namespace chatterino {
|
||||||
namespace util {
|
namespace util {
|
||||||
|
|
||||||
static void urlFetch(const QString &url, std::function<void(QNetworkReply &)> successCallback)
|
static void urlFetch(const QString &url, std::function<void(QNetworkReply &)> successCallback,
|
||||||
|
QNetworkAccessManager *manager = nullptr)
|
||||||
{
|
{
|
||||||
QNetworkAccessManager *manager = new QNetworkAccessManager();
|
bool customManager = true;
|
||||||
|
|
||||||
|
if (manager == nullptr) {
|
||||||
|
manager = new QNetworkAccessManager();
|
||||||
|
customManager = false;
|
||||||
|
}
|
||||||
|
|
||||||
QUrl requestUrl(url);
|
QUrl requestUrl(url);
|
||||||
QNetworkRequest request(requestUrl);
|
QNetworkRequest request(requestUrl);
|
||||||
|
@ -39,13 +47,17 @@ static void urlFetch(const QString &url, std::function<void(QNetworkReply &)> su
|
||||||
}
|
}
|
||||||
|
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
|
if (!customManager) {
|
||||||
manager->deleteLater();
|
manager->deleteLater();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static void urlJsonFetch(const QString &url, std::function<void(QJsonObject &)> successCallback)
|
static void urlFetchJSON(const QString &url, std::function<void(QJsonObject &)> successCallback,
|
||||||
|
QNetworkAccessManager *manager = nullptr)
|
||||||
{
|
{
|
||||||
urlFetch(url, [=](QNetworkReply &reply) {
|
urlFetch(url,
|
||||||
|
[=](QNetworkReply &reply) {
|
||||||
QByteArray data = reply.readAll();
|
QByteArray data = reply.readAll();
|
||||||
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
|
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
|
||||||
|
|
||||||
|
@ -56,7 +68,82 @@ static void urlJsonFetch(const QString &url, std::function<void(QJsonObject &)>
|
||||||
QJsonObject rootNode = jsonDoc.object();
|
QJsonObject rootNode = jsonDoc.object();
|
||||||
|
|
||||||
successCallback(rootNode);
|
successCallback(rootNode);
|
||||||
|
},
|
||||||
|
manager);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void urlFetchTimeout(const QString &url,
|
||||||
|
std::function<void(QNetworkReply &)> successCallback, int timeoutMs,
|
||||||
|
QNetworkAccessManager *manager = nullptr)
|
||||||
|
{
|
||||||
|
bool customManager = true;
|
||||||
|
|
||||||
|
if (manager == nullptr) {
|
||||||
|
manager = new QNetworkAccessManager();
|
||||||
|
customManager = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
QUrl requestUrl(url);
|
||||||
|
QNetworkRequest request(requestUrl);
|
||||||
|
|
||||||
|
QNetworkReply *reply = manager->get(request);
|
||||||
|
|
||||||
|
QTimer timer;
|
||||||
|
timer.setSingleShot(true);
|
||||||
|
|
||||||
|
QEventLoop loop;
|
||||||
|
QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
|
||||||
|
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
|
||||||
|
QObject::connect(reply, &QNetworkReply::finished, [=] {
|
||||||
|
/* uncomment to follow redirects
|
||||||
|
QVariant replyStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
||||||
|
if (replyStatus >= 300 && replyStatus <= 304) {
|
||||||
|
QString newUrl =
|
||||||
|
reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toString();
|
||||||
|
urlFetch(newUrl, successCallback);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (reply->error() == QNetworkReply::NetworkError::NoError) {
|
||||||
|
successCallback(*reply);
|
||||||
|
}
|
||||||
|
|
||||||
|
reply->deleteLater();
|
||||||
|
if (!customManager) {
|
||||||
|
manager->deleteLater();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
timer.start(timeoutMs);
|
||||||
|
loop.exec();
|
||||||
|
|
||||||
|
if (!timer.isActive()) {
|
||||||
|
qDebug() << "TIMED OUT";
|
||||||
|
QObject::disconnect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
|
||||||
|
reply->abort();
|
||||||
|
} else {
|
||||||
|
// qDebug() << "XDDD HEHEHE";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void urlFetchJSONTimeout(const QString &url,
|
||||||
|
std::function<void(QJsonObject &)> successCallback, int timeoutMs,
|
||||||
|
QNetworkAccessManager *manager = nullptr)
|
||||||
|
{
|
||||||
|
urlFetchTimeout(url,
|
||||||
|
[=](QNetworkReply &reply) {
|
||||||
|
QByteArray data = reply.readAll();
|
||||||
|
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
|
||||||
|
|
||||||
|
if (jsonDoc.isNull()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonObject rootNode = jsonDoc.object();
|
||||||
|
|
||||||
|
successCallback(rootNode);
|
||||||
|
},
|
||||||
|
timeoutMs, manager);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace util
|
} // namespace util
|
||||||
|
|
4
src/widgets/basewidget.hpp
Normal file
4
src/widgets/basewidget.hpp
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
#ifndef BASEWIDGET_HPP
|
||||||
|
#define BASEWIDGET_HPP
|
||||||
|
|
||||||
|
#endif // BASEWIDGET_HPP
|
Loading…
Reference in a new issue