Remove NetworkManager urlPut function

This commit is contained in:
Rasmus Karlsson 2018-07-06 19:49:10 +00:00
parent 091b597fe9
commit faa088b7e5
3 changed files with 18 additions and 63 deletions

View file

@ -24,50 +24,6 @@ public:
static void init();
static void deinit();
template <typename FinishedCallback>
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 <typename FinishedCallback>
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

View file

@ -80,9 +80,11 @@ static void twitchApiGetUserID(QString username, const QObject *caller,
successCallback(id.toString());
});
}
static void twitchApiPut(QUrl url, std::function<void(QJsonObject)> successCallback)
static void twitchApiPut(QUrl url, std::function<void(const rapidjson::Document &)> 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<void(QJsonObject)> 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();
request.onSuccess([successCallback](const auto &document) {
if (!document.IsNull()) {
successCallback(document);
}
successCallback(rootNode);
}
}
reply->deleteLater();
return true;
});
request.execute();
}
static void twitchApiDelete(QUrl url, std::function<void()> successCallback)

View file

@ -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));
});
}
}
};