2020-10-04 18:32:52 +02:00
|
|
|
#include "IvrApi.hpp"
|
|
|
|
|
|
|
|
#include "common/Outcome.hpp"
|
2020-11-21 16:20:10 +01:00
|
|
|
#include "common/QLogging.hpp"
|
2020-10-04 18:32:52 +02:00
|
|
|
|
|
|
|
#include <QUrlQuery>
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
static IvrApi *instance = nullptr;
|
|
|
|
|
|
|
|
void IvrApi::getSubage(QString userName, QString channelName,
|
|
|
|
ResultCallback<IvrSubage> successCallback,
|
|
|
|
IvrFailureCallback failureCallback)
|
|
|
|
{
|
|
|
|
assert(!userName.isEmpty() && !channelName.isEmpty());
|
|
|
|
|
2021-03-21 15:42:45 +01:00
|
|
|
this->makeRequest(
|
|
|
|
QString("twitch/subage/%1/%2").arg(userName).arg(channelName), {})
|
2020-10-04 18:32:52 +02:00
|
|
|
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
|
|
|
|
auto root = result.parseJson();
|
|
|
|
|
|
|
|
successCallback(root);
|
|
|
|
|
|
|
|
return Success;
|
|
|
|
})
|
2021-03-21 15:42:45 +01:00
|
|
|
.onError([failureCallback](auto result) {
|
|
|
|
qCWarning(chatterinoIvr)
|
|
|
|
<< "Failed IVR API Call!" << result.status()
|
|
|
|
<< QString(result.getData());
|
|
|
|
failureCallback();
|
|
|
|
})
|
|
|
|
.execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IvrApi::getBulkEmoteSets(QString emoteSetList,
|
|
|
|
ResultCallback<QJsonArray> successCallback,
|
|
|
|
IvrFailureCallback failureCallback)
|
|
|
|
{
|
|
|
|
QUrlQuery urlQuery;
|
|
|
|
urlQuery.addQueryItem("set_id", emoteSetList);
|
|
|
|
|
|
|
|
this->makeRequest("twitch/emoteset", urlQuery)
|
|
|
|
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
|
|
|
|
auto root = result.parseJsonArray();
|
|
|
|
|
|
|
|
successCallback(root);
|
|
|
|
|
|
|
|
return Success;
|
|
|
|
})
|
|
|
|
.onError([failureCallback](auto result) {
|
2020-11-21 16:20:10 +01:00
|
|
|
qCWarning(chatterinoIvr)
|
|
|
|
<< "Failed IVR API Call!" << result.status()
|
|
|
|
<< QString(result.getData());
|
2020-10-04 18:32:52 +02:00
|
|
|
failureCallback();
|
|
|
|
})
|
|
|
|
.execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkRequest IvrApi::makeRequest(QString url, QUrlQuery urlQuery)
|
|
|
|
{
|
|
|
|
assert(!url.startsWith("/"));
|
|
|
|
|
|
|
|
const QString baseUrl("https://api.ivr.fi/");
|
|
|
|
QUrl fullUrl(baseUrl + url);
|
|
|
|
fullUrl.setQuery(urlQuery);
|
|
|
|
|
|
|
|
return NetworkRequest(fullUrl).timeout(5 * 1000).header("Accept",
|
|
|
|
"application/json");
|
|
|
|
}
|
|
|
|
|
|
|
|
void IvrApi::initialize()
|
|
|
|
{
|
|
|
|
assert(instance == nullptr);
|
|
|
|
|
|
|
|
instance = new IvrApi();
|
|
|
|
}
|
|
|
|
|
|
|
|
IvrApi *getIvr()
|
|
|
|
{
|
|
|
|
assert(instance != nullptr);
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chatterino
|