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

@ -68,28 +68,6 @@ public:
emit requester.requestUrl(); 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);
delete worker;
});
});
emit requester.requestUrl();
}
}; };
} // namespace chatterino } // namespace chatterino

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());

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);
@ -221,9 +222,9 @@ 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);

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) {
if (reply->error() == QNetworkReply::NetworkError::NoError) {
int code =
reply->attribute(QNetworkRequest::Attribute::HttpStatusCodeAttribute).toInt();
request.onError([successCallback](int code) {
if (code >= 200 && code <= 299) { if (code >= 200 && code <= 299) {
successCallback(); successCallback();
} }
}
reply->deleteLater(); return true;
}); });
request.onSuccess([successCallback](const auto &document) {
successCallback();
return true;
});
request.execute();
} }
} // namespace chatterino } // namespace chatterino