Remove unused "urlDelete" function from NetworkManager

Allow NetworkRequest to be constructed with a QUrl
This commit is contained in:
Rasmus Karlsson 2018-07-06 19:31:58 +00:00
parent 1f2968934c
commit 091b597fe9
4 changed files with 48 additions and 60 deletions

View file

@ -34,11 +34,11 @@ public:
worker->moveToThread(&NetworkManager::workerThread); worker->moveToThread(&NetworkManager::workerThread);
QObject::connect( QObject::connect(
&requester, &NetworkRequester::requestUrl, worker, &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); QNetworkReply *reply = NetworkManager::NaM.put(request, *data);
reply->connect(reply, &QNetworkReply::finished, reply->connect(reply, &QNetworkReply::finished,
[ worker, reply, onFinished = std::move(onFinished) ]() { [worker, reply, onFinished = std::move(onFinished)]() {
onFinished(reply); onFinished(reply);
delete worker; delete worker;
}); });
@ -56,33 +56,11 @@ public:
worker->moveToThread(&NetworkManager::workerThread); worker->moveToThread(&NetworkManager::workerThread);
QObject::connect( QObject::connect(
&requester, &NetworkRequester::requestUrl, worker, &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, ""); QNetworkReply *reply = NetworkManager::NaM.put(request, "");
reply->connect(reply, &QNetworkReply::finished, reply->connect(reply, &QNetworkReply::finished,
[ onFinished = std::move(onFinished), reply, worker ]() { [onFinished = std::move(onFinished), reply, worker]() {
onFinished(reply);
delete worker;
});
});
emit requester.requestUrl();
}
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,
[ 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(reply); onFinished(reply);
delete worker; delete worker;
}); });

View file

@ -19,6 +19,11 @@ NetworkRequest::NetworkRequest(const QString &url)
this->data.request.setUrl(QUrl(url)); this->data.request.setUrl(QUrl(url));
} }
NetworkRequest::NetworkRequest(QUrl url)
{
this->data.request.setUrl(url);
}
void NetworkRequest::setRequestType(RequestType newRequestType) void NetworkRequest::setRequestType(RequestType newRequestType)
{ {
this->data.requestType = newRequestType; this->data.requestType = newRequestType;
@ -148,8 +153,8 @@ void NetworkRequest::doRequest()
worker->moveToThread(&NetworkManager::workerThread); worker->moveToThread(&NetworkManager::workerThread);
if (this->data.caller != nullptr) { if (this->data.caller != nullptr) {
QObject::connect(worker, &NetworkWorker::doneUrl, QObject::connect(worker, &NetworkWorker::doneUrl, this->data.caller,
this->data.caller, [data = this->data](auto reply) mutable { [data = this->data](auto reply) mutable {
if (reply->error() != QNetworkReply::NetworkError::NoError) { if (reply->error() != QNetworkReply::NetworkError::NoError) {
if (data.onError) { if (data.onError) {
data.onError(reply->error()); data.onError(reply->error());
@ -172,7 +177,7 @@ void NetworkRequest::doRequest()
} }
QObject::connect(&requester, &NetworkRequester::requestUrl, worker, QObject::connect(&requester, &NetworkRequester::requestUrl, worker,
[ timer, data = std::move(this->data), worker ]() { [timer, data = std::move(this->data), worker]() {
QNetworkReply *reply = nullptr; QNetworkReply *reply = nullptr;
switch (data.requestType) { switch (data.requestType) {
case GetRequest: { case GetRequest: {
@ -208,7 +213,7 @@ void NetworkRequest::doRequest()
} }
QObject::connect(reply, &QNetworkReply::finished, worker, QObject::connect(reply, &QNetworkReply::finished, worker,
[ data = std::move(data), worker, reply ]() mutable { [data = std::move(data), worker, reply]() mutable {
if (data.caller == nullptr) { if (data.caller == nullptr) {
QByteArray bytes = reply->readAll(); QByteArray bytes = reply->readAll();
data.writeToCache(bytes); data.writeToCache(bytes);

View file

@ -114,6 +114,7 @@ public:
explicit NetworkRequest(const char *url); explicit NetworkRequest(const char *url);
explicit NetworkRequest(const std::string &url); explicit NetworkRequest(const std::string &url);
explicit NetworkRequest(const QString &url); explicit NetworkRequest(const QString &url);
NetworkRequest(QUrl url);
void setRequestType(RequestType newRequestType); void setRequestType(RequestType newRequestType);
@ -182,7 +183,7 @@ public:
if (this->data.caller != nullptr) { if (this->data.caller != nullptr) {
QObject::connect(worker, &NetworkWorker::doneUrl, this->data.caller, 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 (reply->error() != QNetworkReply::NetworkError::NoError) {
if (data.onError) { if (data.onError) {
data.onError(reply->error()); data.onError(reply->error());
@ -206,7 +207,7 @@ public:
QObject::connect( QObject::connect(
&requester, &NetworkRequester::requestUrl, worker, &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); QNetworkReply *reply = NetworkManager::NaM.get(data.request);
if (timer != nullptr) { if (timer != nullptr) {
@ -221,21 +222,21 @@ public:
data.onReplyCreated(reply); data.onReplyCreated(reply);
} }
QObject::connect(reply, &QNetworkReply::finished, worker, [ QObject::connect(reply, &QNetworkReply::finished, worker,
data = std::move(data), worker, reply, onFinished = std::move(onFinished) [data = std::move(data), worker, reply,
]() mutable { onFinished = std::move(onFinished)]() mutable {
if (data.caller == nullptr) { if (data.caller == nullptr) {
QByteArray bytes = reply->readAll(); QByteArray bytes = reply->readAll();
data.writeToCache(bytes); data.writeToCache(bytes);
onFinished(bytes); onFinished(bytes);
reply->deleteLater(); reply->deleteLater();
} else { } else {
emit worker->doneUrl(reply); emit worker->doneUrl(reply);
} }
delete worker; delete worker;
}); });
}); });
emit requester.requestUrl(); emit requester.requestUrl();
@ -244,7 +245,7 @@ public:
template <typename FinishedCallback> template <typename FinishedCallback>
void getJSON(FinishedCallback onFinished) 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); auto object = parseJSONFromData(bytes);
onFinished(object); onFinished(object);
@ -257,7 +258,7 @@ public:
template <typename FinishedCallback> template <typename FinishedCallback>
void getJSON2(FinishedCallback onFinished) 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); auto object = parseJSONFromData2(bytes);
onFinished(object); onFinished(object);

View file

@ -112,7 +112,9 @@ static void twitchApiPut(QUrl url, std::function<void(QJsonObject)> successCallb
static void twitchApiDelete(QUrl url, std::function<void()> successCallback) static void twitchApiDelete(QUrl url, std::function<void()> successCallback)
{ {
QNetworkRequest request(url); NetworkRequest request(url);
request.setRequestType(NetworkRequest::DeleteRequest);
request.setCaller(QThread::currentThread());
auto currentTwitchUser = getApp()->accounts->twitch.getCurrent(); auto currentTwitchUser = getApp()->accounts->twitch.getCurrent();
QByteArray oauthToken; QByteArray oauthToken;
@ -122,21 +124,23 @@ static void twitchApiDelete(QUrl url, std::function<void()> successCallback)
// XXX(pajlada): Bail out? // XXX(pajlada): Bail out?
} }
request.setRawHeader("Client-ID", getDefaultClientID()); request.makeAuthorizedV5(getDefaultClientID(), currentTwitchUser->getOAuthToken());
request.setRawHeader("Accept", "application/vnd.twitchtv.v5+json");
request.setRawHeader("Authorization", "OAuth " + oauthToken);
NetworkManager::urlDelete(std::move(request), [=](QNetworkReply *reply) { request.onError([successCallback](int code) {
if (reply->error() == QNetworkReply::NetworkError::NoError) { if (code >= 200 && code <= 299) {
int code = successCallback();
reply->attribute(QNetworkRequest::Attribute::HttpStatusCodeAttribute).toInt();
if (code >= 200 && code <= 299) {
successCallback();
}
} }
reply->deleteLater();
return true;
}); });
request.onSuccess([successCallback](const auto &document) {
successCallback();
return true;
});
request.execute();
} }
} // namespace chatterino } // namespace chatterino