From 091b597fe9a9c0c9958b14ff21fde99462cd7eb4 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Fri, 6 Jul 2018 19:31:58 +0000 Subject: [PATCH] Remove unused "urlDelete" function from NetworkManager Allow NetworkRequest to be constructed with a QUrl --- src/common/NetworkManager.hpp | 30 ++++-------------------------- src/common/NetworkRequest.cpp | 13 +++++++++---- src/common/NetworkRequest.hpp | 35 ++++++++++++++++++----------------- src/common/UrlFetch.hpp | 30 +++++++++++++++++------------- 4 files changed, 48 insertions(+), 60 deletions(-) diff --git a/src/common/NetworkManager.hpp b/src/common/NetworkManager.hpp index 26de96d76..5e70e5d41 100644 --- a/src/common/NetworkManager.hpp +++ b/src/common/NetworkManager.hpp @@ -34,11 +34,11 @@ public: worker->moveToThread(&NetworkManager::workerThread); QObject::connect( &requester, &NetworkRequester::requestUrl, worker, - [ worker, data, onFinished = std::move(onFinished), request = std::move(request) ]() { + [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) ]() { + [worker, reply, onFinished = std::move(onFinished)]() { onFinished(reply); delete worker; }); @@ -56,33 +56,11 @@ public: worker->moveToThread(&NetworkManager::workerThread); QObject::connect( &requester, &NetworkRequester::requestUrl, worker, - [ onFinished = std::move(onFinished), request = std::move(request), 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(); - } - - template - static void urlDelete(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.deleteResource(request); - - reply->connect(reply, &QNetworkReply::finished, - [ onFinished = std::move(onFinished), reply, worker ]() { + [onFinished = std::move(onFinished), reply, worker]() { onFinished(reply); delete worker; }); diff --git a/src/common/NetworkRequest.cpp b/src/common/NetworkRequest.cpp index 796a54309..56a257d4a 100644 --- a/src/common/NetworkRequest.cpp +++ b/src/common/NetworkRequest.cpp @@ -19,6 +19,11 @@ NetworkRequest::NetworkRequest(const QString &url) this->data.request.setUrl(QUrl(url)); } +NetworkRequest::NetworkRequest(QUrl url) +{ + this->data.request.setUrl(url); +} + void NetworkRequest::setRequestType(RequestType newRequestType) { this->data.requestType = newRequestType; @@ -148,8 +153,8 @@ void NetworkRequest::doRequest() worker->moveToThread(&NetworkManager::workerThread); if (this->data.caller != nullptr) { - QObject::connect(worker, &NetworkWorker::doneUrl, - this->data.caller, [data = this->data](auto reply) mutable { + QObject::connect(worker, &NetworkWorker::doneUrl, this->data.caller, + [data = this->data](auto reply) mutable { if (reply->error() != QNetworkReply::NetworkError::NoError) { if (data.onError) { data.onError(reply->error()); @@ -172,7 +177,7 @@ void NetworkRequest::doRequest() } QObject::connect(&requester, &NetworkRequester::requestUrl, worker, - [ timer, data = std::move(this->data), worker ]() { + [timer, data = std::move(this->data), worker]() { QNetworkReply *reply = nullptr; switch (data.requestType) { case GetRequest: { @@ -208,7 +213,7 @@ void NetworkRequest::doRequest() } QObject::connect(reply, &QNetworkReply::finished, worker, - [ data = std::move(data), worker, reply ]() mutable { + [data = std::move(data), worker, reply]() mutable { if (data.caller == nullptr) { QByteArray bytes = reply->readAll(); data.writeToCache(bytes); diff --git a/src/common/NetworkRequest.hpp b/src/common/NetworkRequest.hpp index d9685a5b0..3d990cab7 100644 --- a/src/common/NetworkRequest.hpp +++ b/src/common/NetworkRequest.hpp @@ -114,6 +114,7 @@ public: explicit NetworkRequest(const char *url); explicit NetworkRequest(const std::string &url); explicit NetworkRequest(const QString &url); + NetworkRequest(QUrl url); void setRequestType(RequestType newRequestType); @@ -182,7 +183,7 @@ public: if (this->data.caller != nullptr) { QObject::connect(worker, &NetworkWorker::doneUrl, this->data.caller, - [ onFinished, data = this->data ](auto reply) mutable { + [onFinished, data = this->data](auto reply) mutable { if (reply->error() != QNetworkReply::NetworkError::NoError) { if (data.onError) { data.onError(reply->error()); @@ -206,7 +207,7 @@ public: QObject::connect( &requester, &NetworkRequester::requestUrl, worker, - [ timer, 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) { @@ -221,21 +222,21 @@ public: data.onReplyCreated(reply); } - QObject::connect(reply, &QNetworkReply::finished, worker, [ - data = std::move(data), worker, reply, onFinished = std::move(onFinished) - ]() mutable { - if (data.caller == nullptr) { - QByteArray bytes = reply->readAll(); - data.writeToCache(bytes); - onFinished(bytes); + QObject::connect(reply, &QNetworkReply::finished, worker, + [data = std::move(data), worker, reply, + onFinished = std::move(onFinished)]() mutable { + if (data.caller == nullptr) { + QByteArray bytes = reply->readAll(); + data.writeToCache(bytes); + onFinished(bytes); - reply->deleteLater(); - } else { - emit worker->doneUrl(reply); - } + reply->deleteLater(); + } else { + emit worker->doneUrl(reply); + } - delete worker; - }); + delete worker; + }); }); emit requester.requestUrl(); @@ -244,7 +245,7 @@ public: template void getJSON(FinishedCallback onFinished) { - this->get([onFinished{std::move(onFinished)}](const QByteArray &bytes)->bool { + this->get([onFinished{std::move(onFinished)}](const QByteArray &bytes) -> bool { auto object = parseJSONFromData(bytes); onFinished(object); @@ -257,7 +258,7 @@ public: template void getJSON2(FinishedCallback onFinished) { - this->get([onFinished{std::move(onFinished)}](const QByteArray &bytes)->bool { + this->get([onFinished{std::move(onFinished)}](const QByteArray &bytes) -> bool { auto object = parseJSONFromData2(bytes); onFinished(object); diff --git a/src/common/UrlFetch.hpp b/src/common/UrlFetch.hpp index f0d86b123..83e57abeb 100644 --- a/src/common/UrlFetch.hpp +++ b/src/common/UrlFetch.hpp @@ -112,7 +112,9 @@ static void twitchApiPut(QUrl url, std::function successCallb static void twitchApiDelete(QUrl url, std::function successCallback) { - QNetworkRequest request(url); + NetworkRequest request(url); + request.setRequestType(NetworkRequest::DeleteRequest); + request.setCaller(QThread::currentThread()); auto currentTwitchUser = getApp()->accounts->twitch.getCurrent(); QByteArray oauthToken; @@ -122,21 +124,23 @@ static void twitchApiDelete(QUrl url, std::function successCallback) // 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::urlDelete(std::move(request), [=](QNetworkReply *reply) { - if (reply->error() == QNetworkReply::NetworkError::NoError) { - int code = - reply->attribute(QNetworkRequest::Attribute::HttpStatusCodeAttribute).toInt(); - - if (code >= 200 && code <= 299) { - successCallback(); - } + request.onError([successCallback](int code) { + if (code >= 200 && code <= 299) { + successCallback(); } - reply->deleteLater(); + + return true; }); + + request.onSuccess([successCallback](const auto &document) { + successCallback(); + + return true; + }); + + request.execute(); } } // namespace chatterino