mirror-chatterino2/src/providers/twitch/TwitchApi.cpp

88 lines
2.7 KiB
C++
Raw Normal View History

2018-07-14 08:53:28 +02:00
#include "providers/twitch/TwitchApi.hpp"
2018-07-12 10:52:18 +02:00
#include "common/Common.hpp"
2018-07-12 10:52:18 +02:00
#include "common/NetworkRequest.hpp"
#include "debug/Log.hpp"
#include "providers/twitch/TwitchCommon.hpp"
#include <QString>
2018-08-29 19:25:37 +02:00
#include <QThread>
2018-07-12 10:52:18 +02:00
namespace chatterino {
2018-08-06 21:17:03 +02:00
void TwitchApi::findUserId(const QString user,
std::function<void(QString)> successCallback)
2018-07-12 10:52:18 +02:00
{
QString requestUrl("https://api.twitch.tv/kraken/users?login=" + user);
2019-08-20 21:50:36 +02:00
NetworkRequest(requestUrl)
2019-08-20 23:30:39 +02:00
2019-08-20 21:50:36 +02:00
.authorizeTwitchV5(getDefaultClientID())
.timeout(30000)
.onSuccess([successCallback](auto result) mutable -> Outcome {
auto root = result.parseJson();
if (!root.value("users").isArray())
{
log("API Error while getting user id, users is not an array");
successCallback("");
return Failure;
}
auto users = root.value("users").toArray();
if (users.size() != 1)
{
log("API Error while getting user id, users array size is not "
"1");
successCallback("");
return Failure;
}
if (!users[0].isObject())
{
log("API Error while getting user id, first user is not an "
"object");
successCallback("");
return Failure;
}
auto firstUser = users[0].toObject();
auto id = firstUser.value("_id");
if (!id.isString())
{
log("API Error: while getting user id, first user object `_id` "
"key "
"is not a "
"string");
successCallback("");
return Failure;
}
successCallback(id.toString());
return Success;
})
.execute();
2018-07-12 10:52:18 +02:00
}
2019-02-26 21:00:57 +01:00
void TwitchApi::findUserName(const QString userid,
std::function<void(QString)> successCallback)
{
QString requestUrl("https://api.twitch.tv/kraken/users/" + userid);
2019-08-20 21:50:36 +02:00
NetworkRequest(requestUrl)
2019-08-20 23:30:39 +02:00
2019-08-20 21:50:36 +02:00
.authorizeTwitchV5(getDefaultClientID())
.timeout(30000)
.onSuccess([successCallback](auto result) mutable -> Outcome {
auto root = result.parseJson();
auto name = root.value("name");
if (!name.isString())
{
log("API Error: while getting user name, `name` is not a "
"string");
successCallback("");
return Failure;
}
successCallback(name.toString());
return Success;
})
.execute();
2019-02-26 21:00:57 +01:00
}
2018-07-12 10:52:18 +02:00
} // namespace chatterino