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
+74 -82
View File
@@ -52,7 +52,7 @@ void Helix::fetchUsers(QStringList userIds, QStringList userLogins,
}
// TODO: set on success and on error
this->makeRequest("users", urlQuery)
this->makeGet("users", urlQuery)
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
auto root = result.parseJson();
auto data = root.value("data");
@@ -142,7 +142,7 @@ void Helix::fetchUsersFollows(
}
// TODO: set on success and on error
this->makeRequest("users/follows", urlQuery)
this->makeGet("users/follows", urlQuery)
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
auto root = result.parseJson();
if (root.empty())
@@ -186,7 +186,7 @@ void Helix::fetchStreams(
}
// TODO: set on success and on error
this->makeRequest("streams", urlQuery)
this->makeGet("streams", urlQuery)
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
auto root = result.parseJson();
auto data = root.value("data");
@@ -279,7 +279,7 @@ void Helix::fetchGames(QStringList gameIds, QStringList gameNames,
}
// TODO: set on success and on error
this->makeRequest("games", urlQuery)
this->makeGet("games", urlQuery)
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
auto root = result.parseJson();
auto data = root.value("data");
@@ -315,7 +315,7 @@ void Helix::searchGames(QString gameName,
QUrlQuery urlQuery;
urlQuery.addQueryItem("query", gameName);
this->makeRequest("search/categories", urlQuery)
this->makeGet("search/categories", urlQuery)
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
auto root = result.parseJson();
auto data = root.value("data");
@@ -372,8 +372,7 @@ void Helix::createClip(QString channelId,
QUrlQuery urlQuery;
urlQuery.addQueryItem("broadcaster_id", channelId);
this->makeRequest("clips", urlQuery)
.type(NetworkRequestType::Post)
this->makePost("clips", urlQuery)
.header("Content-Type", "application/json")
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
auto root = result.parseJson();
@@ -425,7 +424,7 @@ void Helix::getChannel(QString broadcasterId,
QUrlQuery urlQuery;
urlQuery.addQueryItem("broadcaster_id", broadcasterId);
this->makeRequest("channels", urlQuery)
this->makeGet("channels", urlQuery)
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
auto root = result.parseJson();
auto data = root.value("data");
@@ -460,10 +459,8 @@ void Helix::createStreamMarker(
}
payload.insert("user_id", QJsonValue(broadcasterId));
this->makeRequest("streams/markers", QUrlQuery())
.type(NetworkRequestType::Post)
.header("Content-Type", "application/json")
.payload(QJsonDocument(payload).toJson(QJsonDocument::Compact))
this->makePost("streams/markers", QUrlQuery())
.json(payload)
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
auto root = result.parseJson();
auto data = root.value("data");
@@ -515,7 +512,7 @@ void Helix::loadBlocks(QString userId,
urlQuery.addQueryItem("broadcaster_id", userId);
urlQuery.addQueryItem("first", "100");
this->makeRequest("users/blocks", urlQuery)
this->makeGet("users/blocks", urlQuery)
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
auto root = result.parseJson();
auto data = root.value("data");
@@ -551,8 +548,7 @@ void Helix::blockUser(QString targetUserId,
QUrlQuery urlQuery;
urlQuery.addQueryItem("target_user_id", targetUserId);
this->makeRequest("users/blocks", urlQuery)
.type(NetworkRequestType::Put)
this->makePut("users/blocks", urlQuery)
.onSuccess([successCallback](auto /*result*/) -> Outcome {
successCallback();
return Success;
@@ -571,8 +567,7 @@ void Helix::unblockUser(QString targetUserId,
QUrlQuery urlQuery;
urlQuery.addQueryItem("target_user_id", targetUserId);
this->makeRequest("users/blocks", urlQuery)
.type(NetworkRequestType::Delete)
this->makeDelete("users/blocks", urlQuery)
.onSuccess([successCallback](auto /*result*/) -> Outcome {
successCallback();
return Success;
@@ -590,7 +585,6 @@ void Helix::updateChannel(QString broadcasterId, QString gameId,
HelixFailureCallback failureCallback)
{
QUrlQuery urlQuery;
auto data = QJsonDocument();
auto obj = QJsonObject();
if (!gameId.isEmpty())
{
@@ -611,12 +605,9 @@ void Helix::updateChannel(QString broadcasterId, QString gameId,
return;
}
data.setObject(obj);
urlQuery.addQueryItem("broadcaster_id", broadcasterId);
this->makeRequest("channels", urlQuery)
.type(NetworkRequestType::Patch)
.header("Content-Type", "application/json")
.payload(data.toJson())
this->makePatch("channels", urlQuery)
.json(obj)
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
successCallback(result);
return Success;
@@ -638,10 +629,8 @@ void Helix::manageAutoModMessages(
payload.insert("msg_id", msgID);
payload.insert("action", action);
this->makeRequest("moderation/automod/message", QUrlQuery())
.type(NetworkRequestType::Post)
.header("Content-Type", "application/json")
.payload(QJsonDocument(payload).toJson(QJsonDocument::Compact))
this->makePost("moderation/automod/message", QUrlQuery())
.json(payload)
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
successCallback();
return Success;
@@ -697,7 +686,7 @@ void Helix::getCheermotes(
urlQuery.addQueryItem("broadcaster_id", broadcasterId);
this->makeRequest("bits/cheermotes", urlQuery)
this->makeGet("bits/cheermotes", urlQuery)
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
auto root = result.parseJson();
auto data = root.value("data");
@@ -735,7 +724,7 @@ void Helix::getEmoteSetData(QString emoteSetId,
urlQuery.addQueryItem("emote_set_id", emoteSetId);
this->makeRequest("chat/emotes/set", urlQuery)
this->makeGet("chat/emotes/set", urlQuery)
.onSuccess([successCallback, failureCallback,
emoteSetId](auto result) -> Outcome {
QJsonObject root = result.parseJson();
@@ -767,7 +756,7 @@ void Helix::getChannelEmotes(
QUrlQuery urlQuery;
urlQuery.addQueryItem("broadcaster_id", broadcasterId);
this->makeRequest("chat/emotes", urlQuery)
this->makeGet("chat/emotes", urlQuery)
.onSuccess([successCallback,
failureCallback](NetworkResult result) -> Outcome {
QJsonObject root = result.parseJson();
@@ -807,10 +796,8 @@ void Helix::updateUserChatColor(
payload.insert("user_id", QJsonValue(userID));
payload.insert("color", QJsonValue(color));
this->makeRequest("chat/color", QUrlQuery())
.type(NetworkRequestType::Put)
.header("Content-Type", "application/json")
.payload(QJsonDocument(payload).toJson(QJsonDocument::Compact))
this->makePut("chat/color", QUrlQuery())
.json(payload)
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
auto obj = result.parseJson();
if (result.status() != 204)
@@ -887,8 +874,7 @@ void Helix::deleteChatMessages(
urlQuery.addQueryItem("message_id", messageID);
}
this->makeRequest("moderation/chat", urlQuery)
.type(NetworkRequestType::Delete)
this->makeDelete("moderation/chat", urlQuery)
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
if (result.status() != 204)
{
@@ -966,8 +952,7 @@ void Helix::addChannelModerator(
urlQuery.addQueryItem("broadcaster_id", broadcasterID);
urlQuery.addQueryItem("user_id", userID);
this->makeRequest("moderation/moderators", urlQuery)
.type(NetworkRequestType::Post)
this->makePost("moderation/moderators", urlQuery)
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
if (result.status() != 204)
{
@@ -1055,8 +1040,7 @@ void Helix::removeChannelModerator(
urlQuery.addQueryItem("broadcaster_id", broadcasterID);
urlQuery.addQueryItem("user_id", userID);
this->makeRequest("moderation/moderators", urlQuery)
.type(NetworkRequestType::Delete)
this->makeDelete("moderation/moderators", urlQuery)
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
if (result.status() != 204)
{
@@ -1142,10 +1126,8 @@ void Helix::sendChatAnnouncement(
std::string{magic_enum::enum_name<HelixAnnouncementColor>(color)};
body.insert("color", QString::fromStdString(colorStr).toLower());
this->makeRequest("chat/announcements", urlQuery)
.type(NetworkRequestType::Post)
.header("Content-Type", "application/json")
.payload(QJsonDocument(body).toJson(QJsonDocument::Compact))
this->makePost("chat/announcements", urlQuery)
.json(body)
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
if (result.status() != 204)
{
@@ -1214,8 +1196,7 @@ void Helix::addChannelVIP(
urlQuery.addQueryItem("broadcaster_id", broadcasterID);
urlQuery.addQueryItem("user_id", userID);
this->makeRequest("channels/vips", urlQuery)
.type(NetworkRequestType::Post)
this->makePost("channels/vips", urlQuery)
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
if (result.status() != 204)
{
@@ -1293,8 +1274,7 @@ void Helix::removeChannelVIP(
urlQuery.addQueryItem("broadcaster_id", broadcasterID);
urlQuery.addQueryItem("user_id", userID);
this->makeRequest("channels/vips", urlQuery)
.type(NetworkRequestType::Delete)
this->makeDelete("channels/vips", urlQuery)
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
if (result.status() != 204)
{
@@ -1383,8 +1363,7 @@ void Helix::unbanUser(
urlQuery.addQueryItem("moderator_id", moderatorID);
urlQuery.addQueryItem("user_id", userID);
this->makeRequest("moderation/bans", urlQuery)
.type(NetworkRequestType::Delete)
this->makeDelete("moderation/bans", urlQuery)
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
if (result.status() != 204)
{
@@ -1489,8 +1468,7 @@ void Helix::startRaid(
urlQuery.addQueryItem("from_broadcaster_id", fromBroadcasterID);
urlQuery.addQueryItem("to_broadcaster_id", toBroadcasterID);
this->makeRequest("raids", urlQuery)
.type(NetworkRequestType::Post)
this->makePost("raids", urlQuery)
.onSuccess(
[successCallback, failureCallback](auto /*result*/) -> Outcome {
successCallback();
@@ -1570,8 +1548,7 @@ void Helix::cancelRaid(
urlQuery.addQueryItem("broadcaster_id", broadcasterID);
this->makeRequest("raids", urlQuery)
.type(NetworkRequestType::Delete)
this->makeDelete("raids", urlQuery)
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
if (result.status() != 204)
{
@@ -1731,10 +1708,8 @@ void Helix::updateChatSettings(
urlQuery.addQueryItem("broadcaster_id", broadcasterID);
urlQuery.addQueryItem("moderator_id", moderatorID);
this->makeRequest("chat/settings", urlQuery)
.type(NetworkRequestType::Patch)
.header("Content-Type", "application/json")
.payload(QJsonDocument(payload).toJson(QJsonDocument::Compact))
this->makePatch("chat/settings", urlQuery)
.json(payload)
.onSuccess([successCallback](auto result) -> Outcome {
if (result.status() != 200)
{
@@ -1857,7 +1832,7 @@ void Helix::fetchChatters(
urlQuery.addQueryItem("after", after);
}
this->makeRequest("chat/chatters", urlQuery)
this->makeGet("chat/chatters", urlQuery)
.onSuccess([successCallback](auto result) -> Outcome {
if (result.status() != 200)
{
@@ -1966,7 +1941,7 @@ void Helix::fetchModerators(
urlQuery.addQueryItem("after", after);
}
this->makeRequest("moderation/moderators", urlQuery)
this->makeGet("moderation/moderators", urlQuery)
.onSuccess([successCallback](auto result) -> Outcome {
if (result.status() != 200)
{
@@ -2051,10 +2026,8 @@ void Helix::banUser(QString broadcasterID, QString moderatorID, QString userID,
payload["data"] = data;
}
this->makeRequest("moderation/bans", urlQuery)
.type(NetworkRequestType::Post)
.header("Content-Type", "application/json")
.payload(QJsonDocument(payload).toJson(QJsonDocument::Compact))
this->makePost("moderation/bans", urlQuery)
.json(payload)
.onSuccess([successCallback](auto result) -> Outcome {
if (result.status() != 200)
{
@@ -2150,10 +2123,8 @@ void Helix::sendWhisper(
QJsonObject payload;
payload["message"] = message;
this->makeRequest("whispers", urlQuery)
.type(NetworkRequestType::Post)
.header("Content-Type", "application/json")
.payload(QJsonDocument(payload).toJson(QJsonDocument::Compact))
this->makePost("whispers", urlQuery)
.json(payload)
.onSuccess([successCallback](auto result) -> Outcome {
if (result.status() != 204)
{
@@ -2295,8 +2266,7 @@ void Helix::getChannelVIPs(
// as the mod list can go over 100 (I assume, I see no limit)
urlQuery.addQueryItem("first", "100");
this->makeRequest("channels/vips", urlQuery)
.type(NetworkRequestType::Get)
this->makeGet("channels/vips", urlQuery)
.header("Content-Type", "application/json")
.onSuccess([successCallback](auto result) -> Outcome {
if (result.status() != 200)
@@ -2383,10 +2353,8 @@ void Helix::startCommercial(
payload.insert("broadcaster_id", QJsonValue(broadcasterID));
payload.insert("length", QJsonValue(length));
this->makeRequest("channels/commercial", QUrlQuery())
.type(NetworkRequestType::Post)
.header("Content-Type", "application/json")
.payload(QJsonDocument(payload).toJson(QJsonDocument::Compact))
this->makePost("channels/commercial", QUrlQuery())
.json(payload)
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
auto obj = result.parseJson();
if (obj.isEmpty())
@@ -2476,7 +2444,7 @@ void Helix::getGlobalBadges(
{
using Error = HelixGetGlobalBadgesError;
this->makeRequest("chat/badges/global", QUrlQuery())
this->makeGet("chat/badges/global", QUrlQuery())
.onSuccess([successCallback](auto result) -> Outcome {
if (result.status() != 200)
{
@@ -2523,7 +2491,7 @@ void Helix::getChannelBadges(
QUrlQuery urlQuery;
urlQuery.addQueryItem("broadcaster_id", broadcasterID);
this->makeRequest("chat/badges", urlQuery)
this->makeGet("chat/badges", urlQuery)
.onSuccess([successCallback](auto result) -> Outcome {
if (result.status() != 200)
{
@@ -2575,10 +2543,8 @@ void Helix::updateShieldMode(
QJsonObject payload;
payload["is_active"] = isActive;
this->makeRequest("moderation/shield_mode", urlQuery)
.type(NetworkRequestType::Put)
.header("Content-Type", "application/json")
.payload(QJsonDocument(payload).toJson(QJsonDocument::Compact))
this->makePut("moderation/shield_mode", urlQuery)
.json(payload)
.onSuccess([successCallback](auto result) -> Outcome {
if (result.status() != 200)
{
@@ -2631,7 +2597,8 @@ void Helix::updateShieldMode(
.execute();
}
NetworkRequest Helix::makeRequest(QString url, QUrlQuery urlQuery)
NetworkRequest Helix::makeRequest(const QString &url, const QUrlQuery &urlQuery,
NetworkRequestType type)
{
assert(!url.startsWith("/"));
@@ -2655,13 +2622,38 @@ NetworkRequest Helix::makeRequest(QString url, QUrlQuery urlQuery)
fullUrl.setQuery(urlQuery);
return NetworkRequest(fullUrl)
return NetworkRequest(fullUrl, type)
.timeout(5 * 1000)
.header("Accept", "application/json")
.header("Client-ID", this->clientId)
.header("Authorization", "Bearer " + this->oauthToken);
}
NetworkRequest Helix::makeGet(const QString &url, const QUrlQuery &urlQuery)
{
return this->makeRequest(url, urlQuery, NetworkRequestType::Get);
}
NetworkRequest Helix::makeDelete(const QString &url, const QUrlQuery &urlQuery)
{
return this->makeRequest(url, urlQuery, NetworkRequestType::Delete);
}
NetworkRequest Helix::makePost(const QString &url, const QUrlQuery &urlQuery)
{
return this->makeRequest(url, urlQuery, NetworkRequestType::Post);
}
NetworkRequest Helix::makePut(const QString &url, const QUrlQuery &urlQuery)
{
return this->makeRequest(url, urlQuery, NetworkRequestType::Put);
}
NetworkRequest Helix::makePatch(const QString &url, const QUrlQuery &urlQuery)
{
return this->makeRequest(url, urlQuery, NetworkRequestType::Patch);
}
void Helix::update(QString clientId, QString oauthToken)
{
this->clientId = std::move(clientId);