Refactor IvrApi::getSubage to use APIRequest<T,U> as an example usage

This commit is contained in:
Mm2PL 2022-09-16 23:23:29 +02:00
parent f7194e820c
commit 0d3f0b7585
No known key found for this signature in database
GPG key ID: 94AC9B80EFA15ED9
3 changed files with 29 additions and 26 deletions

View file

@ -1,5 +1,9 @@
#include "IvrApi.hpp"
#include <qurl.h>
#include "common/APIRequest.hpp"
#include "common/NetworkCommon.hpp"
#include "common/NetworkResult.hpp"
#include "common/Outcome.hpp"
#include "common/QLogging.hpp"
@ -9,28 +13,24 @@ namespace chatterino {
static IvrApi *instance = nullptr;
void IvrApi::getSubage(QString userName, QString channelName,
ResultCallback<IvrSubage> successCallback,
IvrFailureCallback failureCallback)
APIRequest<IvrSubage, APIRequestNoErrorValue> IvrApi::subage(
const QString &userName, const QString &channelName)
{
assert(!userName.isEmpty() && !channelName.isEmpty());
this->makeRequest(
QString("twitch/subage/%1/%2").arg(userName).arg(channelName), {})
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
auto root = result.parseJson();
successCallback(root);
return Success;
})
.onError([failureCallback](auto result) {
auto url = QString("https://api.ivr.fi/twitch/subage/%1/%2")
.arg(userName)
.arg(channelName);
return APIRequest<IvrSubage, APIRequestNoErrorValue>(
QUrl(url), NetworkRequestType::Get,
[](NetworkResult result) -> IvrSubage {
return result.parseJson();
},
[](NetworkResult result) {
qCWarning(chatterinoIvr)
<< "Failed IVR API Call!" << result.status()
<< QString(result.getData());
failureCallback();
})
.execute();
return APIRequestNoErrorValue{};
});
}
void IvrApi::getBulkEmoteSets(QString emoteSetList,

View file

@ -1,5 +1,6 @@
#pragma once
#include "common/APIRequest.hpp"
#include "common/NetworkRequest.hpp"
#include "messages/Link.hpp"
#include "providers/twitch/TwitchEmotes.hpp"
@ -77,9 +78,8 @@ class IvrApi final : boost::noncopyable
{
public:
// https://api.ivr.fi/docs#tag/Twitch/paths/~1twitch~1subage~1{username}~1{channel}/get
void getSubage(QString userName, QString channelName,
ResultCallback<IvrSubage> resultCallback,
IvrFailureCallback failureCallback);
APIRequest<IvrSubage, APIRequestNoErrorValue> subage(
const QString &userName, const QString &channelName);
// https://api.ivr.fi/v2/docs/static/index.html#/Twitch/get_twitch_emotes_sets
void getBulkEmoteSets(QString emoteSetList,

View file

@ -3,6 +3,7 @@
#include "Application.hpp"
#include "common/Channel.hpp"
#include "common/NetworkRequest.hpp"
#include "common/Outcome.hpp"
#include "common/QLogging.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "controllers/highlights/HighlightBlacklistUser.hpp"
@ -821,12 +822,13 @@ void UserInfoPopup::updateUserData()
this->ui_.ignoreHighlights->setChecked(isIgnoringHighlights);
// get followage and subage
getIvr()->getSubage(
this->userName_, this->underlyingChannel_->getName(),
[this, hack](const IvrSubage &subageInfo) {
getIvr()
->subage(this->userName_, this->underlyingChannel_->getName())
.onSuccess([this, hack](NetworkResult,
const IvrSubage &subageInfo) -> Outcome {
if (!hack.lock())
{
return;
return Success;
}
if (!subageInfo.followingSince.isEmpty())
@ -856,8 +858,9 @@ void UserInfoPopup::updateUserData()
QString("★ Previously subscribed for %1 months")
.arg(subageInfo.totalSubMonths));
}
},
[] {});
return Success;
})
.execute();
};
getHelix()->getUserByName(this->userName_, onUserFetched,