mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-13 19:49:51 +01:00
60 lines
1.4 KiB
C++
60 lines
1.4 KiB
C++
|
#include "IvrApi.hpp"
|
||
|
|
||
|
#include "common/Outcome.hpp"
|
||
|
|
||
|
#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());
|
||
|
|
||
|
this->makeRequest("twitch/subage/" + userName + "/" + channelName, {})
|
||
|
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
|
||
|
auto root = result.parseJson();
|
||
|
|
||
|
successCallback(root);
|
||
|
|
||
|
return Success;
|
||
|
})
|
||
|
.onError([failureCallback](NetworkResult result) {
|
||
|
qDebug() << "Failed IVR API Call!" << result.status()
|
||
|
<< QString(result.getData());
|
||
|
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
|