From faa088b7e5e88b3b123c2229e398f78445073226 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Fri, 6 Jul 2018 19:49:10 +0000 Subject: [PATCH] Remove NetworkManager urlPut function --- src/common/NetworkManager.hpp | 44 --------------------------- src/common/UrlFetch.hpp | 27 ++++++++-------- src/widgets/dialogs/UserInfoPopup.cpp | 10 +++--- 3 files changed, 18 insertions(+), 63 deletions(-) diff --git a/src/common/NetworkManager.hpp b/src/common/NetworkManager.hpp index 5e70e5d41..0e59c0540 100644 --- a/src/common/NetworkManager.hpp +++ b/src/common/NetworkManager.hpp @@ -24,50 +24,6 @@ public: static void init(); static void deinit(); - - template - 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, - [worker, data, onFinished = std::move(onFinished), request = std::move(request)]() { - QNetworkReply *reply = NetworkManager::NaM.put(request, *data); - - reply->connect(reply, &QNetworkReply::finished, - [worker, reply, onFinished = std::move(onFinished)]() { - onFinished(reply); - delete worker; - }); - }); - - emit requester.requestUrl(); - } - - template - static void urlPut(QNetworkRequest request, FinishedCallback onFinished) - { - NetworkRequester requester; - NetworkWorker *worker = new NetworkWorker; - - worker->moveToThread(&NetworkManager::workerThread); - QObject::connect( - &requester, &NetworkRequester::requestUrl, worker, - [onFinished = std::move(onFinished), request = std::move(request), worker]() { - QNetworkReply *reply = NetworkManager::NaM.put(request, ""); - - reply->connect(reply, &QNetworkReply::finished, - [onFinished = std::move(onFinished), reply, worker]() { - onFinished(reply); - delete worker; - }); - }); - - emit requester.requestUrl(); - } }; } // namespace chatterino diff --git a/src/common/UrlFetch.hpp b/src/common/UrlFetch.hpp index 83e57abeb..c677076d4 100644 --- a/src/common/UrlFetch.hpp +++ b/src/common/UrlFetch.hpp @@ -80,9 +80,11 @@ static void twitchApiGetUserID(QString username, const QObject *caller, successCallback(id.toString()); }); } -static void twitchApiPut(QUrl url, std::function successCallback) +static void twitchApiPut(QUrl url, std::function successCallback) { - QNetworkRequest request(url); + NetworkRequest request(url); + request.setRequestType(NetworkRequest::PutRequest); + request.setCaller(QThread::currentThread()); auto currentTwitchUser = getApp()->accounts->twitch.getCurrent(); QByteArray oauthToken; @@ -92,22 +94,17 @@ static void twitchApiPut(QUrl url, std::function successCallb // XXX(pajlada): Bail out? } - request.setRawHeader("Client-ID", getDefaultClientID()); - request.setRawHeader("Accept", "application/vnd.twitchtv.v5+json"); - request.setRawHeader("Authorization", "OAuth " + oauthToken); + request.makeAuthorizedV5(getDefaultClientID(), currentTwitchUser->getOAuthToken()); - NetworkManager::urlPut(std::move(request), [=](QNetworkReply *reply) { - if (reply->error() == QNetworkReply::NetworkError::NoError) { - QByteArray data = reply->readAll(); - QJsonDocument jsonDoc(QJsonDocument::fromJson(data)); - if (!jsonDoc.isNull()) { - QJsonObject rootNode = jsonDoc.object(); - - successCallback(rootNode); - } + request.onSuccess([successCallback](const auto &document) { + if (!document.IsNull()) { + successCallback(document); } - reply->deleteLater(); + + return true; }); + + request.execute(); } static void twitchApiDelete(QUrl url, std::function successCallback) diff --git a/src/widgets/dialogs/UserInfoPopup.cpp b/src/widgets/dialogs/UserInfoPopup.cpp index 2bf826a20..9047c1c4f 100644 --- a/src/widgets/dialogs/UserInfoPopup.cpp +++ b/src/widgets/dialogs/UserInfoPopup.cpp @@ -178,7 +178,7 @@ void UserInfoPopup::installEvents() this->ui_.follow->setEnabled(false); if (this->ui_.follow->isChecked()) { - twitchApiPut(requestUrl, [this](QJsonObject) { this->ui_.follow->setEnabled(true); }); + twitchApiPut(requestUrl, [this](const auto &) { this->ui_.follow->setEnabled(true); }); } else { twitchApiDelete(requestUrl, [this] { this->ui_.follow->setEnabled(true); }); } @@ -373,9 +373,11 @@ UserInfoPopup::TimeoutWidget::TimeoutWidget() } a->setBorderColor(color1); - QObject::connect(a.getElement(), &RippleEffectLabel2::clicked, [ - this, timeout = std::get<1>(item) - ] { this->buttonClicked.invoke(std::make_pair(Action::Timeout, timeout)); }); + QObject::connect( + a.getElement(), &RippleEffectLabel2::clicked, + [this, timeout = std::get<1>(item)] { + this->buttonClicked.invoke(std::make_pair(Action::Timeout, timeout)); + }); } } };