Refactor/Cleanup NetworkRequest and Related Code (#4633)

Cleanup unused code (Twitch v5)
Add json methods that simplify sending JSON. This also sets the Accepts header, since here, when JSON is sent, only JSON is accepted as a response (this is only done in Helix).
Clarify helix request creations
Cleaned some clang-tidy suggestions in Network{Request,Private}
This commit is contained in:
nerix
2023-05-16 17:28:20 +02:00
committed by GitHub
parent 4fa2cc26c9
commit ce47d27d41
6 changed files with 184 additions and 170 deletions
+34 -32
View File
@@ -1,14 +1,8 @@
#include "common/NetworkRequest.hpp"
#include "common/NetworkPrivate.hpp"
#include "common/Outcome.hpp"
#include "common/QLogging.hpp"
#include "common/Version.hpp"
#include "debug/AssertInGuiThread.hpp"
#include "providers/twitch/TwitchCommon.hpp"
#include "singletons/Paths.hpp"
#include "util/DebugCount.hpp"
#include "util/PostToThread.hpp"
#include <QDebug>
#include <QFile>
@@ -28,7 +22,7 @@ NetworkRequest::NetworkRequest(const std::string &url,
this->initializeDefaultValues();
}
NetworkRequest::NetworkRequest(QUrl url, NetworkRequestType requestType)
NetworkRequest::NetworkRequest(const QUrl &url, NetworkRequestType requestType)
: data(new NetworkData)
{
this->data->request_.setUrl(url);
@@ -37,10 +31,7 @@ NetworkRequest::NetworkRequest(QUrl url, NetworkRequestType requestType)
this->initializeDefaultValues();
}
NetworkRequest::~NetworkRequest()
{
//assert(!this->data || this->executed_);
}
NetworkRequest::~NetworkRequest() = default;
NetworkRequest NetworkRequest::type(NetworkRequestType newRequestType) &&
{
@@ -63,25 +54,25 @@ NetworkRequest NetworkRequest::caller(const QObject *caller) &&
NetworkRequest NetworkRequest::onReplyCreated(NetworkReplyCreatedCallback cb) &&
{
this->data->onReplyCreated_ = cb;
this->data->onReplyCreated_ = std::move(cb);
return std::move(*this);
}
NetworkRequest NetworkRequest::onError(NetworkErrorCallback cb) &&
{
this->data->onError_ = cb;
this->data->onError_ = std::move(cb);
return std::move(*this);
}
NetworkRequest NetworkRequest::onSuccess(NetworkSuccessCallback cb) &&
{
this->data->onSuccess_ = cb;
this->data->onSuccess_ = std::move(cb);
return std::move(*this);
}
NetworkRequest NetworkRequest::finally(NetworkFinallyCallback cb) &&
{
this->data->finally_ = cb;
this->data->finally_ = std::move(cb);
return std::move(*this);
}
@@ -106,6 +97,13 @@ NetworkRequest NetworkRequest::header(const char *headerName,
return std::move(*this);
}
NetworkRequest NetworkRequest::header(QNetworkRequest::KnownHeaders header,
const QVariant &value) &&
{
this->data->request_.setHeader(header, value);
return std::move(*this);
}
NetworkRequest NetworkRequest::headerList(
const std::vector<std::pair<QByteArray, QByteArray>> &headers) &&
{
@@ -129,20 +127,6 @@ NetworkRequest NetworkRequest::concurrent() &&
return std::move(*this);
}
NetworkRequest NetworkRequest::authorizeTwitchV5(const QString &clientID,
const QString &oauthToken) &&
{
// TODO: make two overloads, with and without oauth token
auto tmp = std::move(*this)
.header("Client-ID", clientID)
.header("Accept", "application/vnd.twitchtv.v5+json");
if (!oauthToken.isEmpty())
return std::move(tmp).header("Authorization", "OAuth " + oauthToken);
else
return tmp;
}
NetworkRequest NetworkRequest::multiPart(QHttpMultiPart *payload) &&
{
payload->setParent(this->data->lifetimeManager_);
@@ -207,10 +191,28 @@ void NetworkRequest::initializeDefaultValues()
this->data->request_.setRawHeader("User-Agent", userAgent);
}
// Helper creator functions
NetworkRequest NetworkRequest::twitchRequest(QUrl url)
NetworkRequest NetworkRequest::json(const QJsonArray &root) &&
{
return NetworkRequest(url).authorizeTwitchV5(getDefaultClientID());
return std::move(*this).json(QJsonDocument(root));
}
NetworkRequest NetworkRequest::json(const QJsonObject &root) &&
{
return std::move(*this).json(QJsonDocument(root));
}
NetworkRequest NetworkRequest::json(const QJsonDocument &document) &&
{
return std::move(*this).json(document.toJson(QJsonDocument::Compact));
}
NetworkRequest NetworkRequest::json(const QByteArray &payload) &&
{
return std::move(*this)
.payload(payload)
.header(QNetworkRequest::ContentTypeHeader, "application/json")
.header(QNetworkRequest::ContentLengthHeader, payload.length())
.header("Accept", "application/json");
}
} // namespace chatterino