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

View file

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

View file

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