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);
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 <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 = std::move(onFinished), reply, worker]() {
onFinished(reply);
delete worker;
});

View file

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

View file

@ -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 <typename FinishedCallback>
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 <typename FinishedCallback>
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);

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)
{
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<void()> 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