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

86 lines
2.5 KiB
C++
Raw Normal View History

#include "providers/twitch/PartialTwitchUser.hpp"
#include "common/Common.hpp"
#include "common/NetworkRequest.hpp"
#include "providers/twitch/TwitchCommon.hpp"
2018-08-02 14:23:27 +02:00
#include <QJsonArray>
#include <cassert>
namespace chatterino {
PartialTwitchUser PartialTwitchUser::byName(const QString &username)
{
PartialTwitchUser user;
user.username_ = username;
return user;
}
PartialTwitchUser PartialTwitchUser::byId(const QString &id)
{
PartialTwitchUser user;
user.id_ = id;
return user;
}
2018-08-06 21:17:03 +02:00
void PartialTwitchUser::getId(std::function<void(QString)> successCallback,
const QObject *caller)
{
getId(
successCallback, [] {}, caller);
}
void PartialTwitchUser::getId(std::function<void(QString)> successCallback,
std::function<void()> failureCallback,
const QObject *caller)
{
assert(!this->username_.isEmpty());
2019-08-20 21:50:36 +02:00
NetworkRequest("https://api.twitch.tv/kraken/users?login=" +
this->username_)
.caller(caller)
.authorizeTwitchV5(getDefaultClientID())
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
2019-08-20 21:50:36 +02:00
auto root = result.parseJson();
if (!root.value("users").isArray())
{
qDebug()
<< "API Error while getting user id, users is not an array";
failureCallback();
2019-08-20 21:50:36 +02:00
return Failure;
}
2019-08-20 21:50:36 +02:00
auto users = root.value("users").toArray();
if (users.size() != 1)
{
qDebug() << "API Error while getting user id, users array size "
"is not 1";
failureCallback();
2019-08-20 21:50:36 +02:00
return Failure;
}
if (!users[0].isObject())
{
qDebug() << "API Error while getting user id, first user is "
"not an object";
failureCallback();
2019-08-20 21:50:36 +02:00
return Failure;
}
auto firstUser = users[0].toObject();
auto id = firstUser.value("_id");
if (!id.isString())
{
qDebug() << "API Error: while getting user id, first user "
"object `_id` key is not a string";
failureCallback();
2019-08-20 21:50:36 +02:00
return Failure;
}
successCallback(id.toString());
2019-08-20 21:50:36 +02:00
return Success;
})
.execute();
}
} // namespace chatterino