2018-07-07 13:08:57 +02:00
|
|
|
#include "providers/twitch/PartialTwitchUser.hpp"
|
|
|
|
|
|
|
|
#include "common/NetworkRequest.hpp"
|
|
|
|
#include "debug/Log.hpp"
|
|
|
|
#include "providers/twitch/TwitchCommon.hpp"
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
#include <QJsonArray>
|
2018-07-07 13:08:57 +02:00
|
|
|
#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)
|
2018-07-07 13:08:57 +02:00
|
|
|
{
|
|
|
|
assert(!this->username_.isEmpty());
|
|
|
|
|
|
|
|
if (caller == nullptr) {
|
|
|
|
caller = QThread::currentThread();
|
|
|
|
}
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
NetworkRequest request("https://api.twitch.tv/kraken/users?login=" +
|
|
|
|
this->username_);
|
2018-07-07 13:08:57 +02:00
|
|
|
request.setCaller(caller);
|
|
|
|
request.makeAuthorizedV5(getDefaultClientID());
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
request.onSuccess([successCallback](auto result) -> Outcome {
|
2018-07-07 13:08:57 +02:00
|
|
|
auto root = result.parseJson();
|
|
|
|
if (!root.value("users").isArray()) {
|
2018-08-11 14:20:53 +02:00
|
|
|
log("API Error while getting user id, users is not an array");
|
2018-08-02 14:23:27 +02:00
|
|
|
return Failure;
|
2018-07-07 13:08:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto users = root.value("users").toArray();
|
|
|
|
if (users.size() != 1) {
|
2018-08-11 14:20:53 +02:00
|
|
|
log("API Error while getting user id, users array size is not 1");
|
2018-08-02 14:23:27 +02:00
|
|
|
return Failure;
|
2018-07-07 13:08:57 +02:00
|
|
|
}
|
|
|
|
if (!users[0].isObject()) {
|
2018-08-11 14:20:53 +02:00
|
|
|
log("API Error while getting user id, first user is not an object");
|
2018-08-02 14:23:27 +02:00
|
|
|
return Failure;
|
2018-07-07 13:08:57 +02:00
|
|
|
}
|
|
|
|
auto firstUser = users[0].toObject();
|
|
|
|
auto id = firstUser.value("_id");
|
|
|
|
if (!id.isString()) {
|
2018-08-11 14:20:53 +02:00
|
|
|
log("API Error: while getting user id, first user object `_id` key "
|
2018-08-06 21:17:03 +02:00
|
|
|
"is not a "
|
2018-07-07 13:08:57 +02:00
|
|
|
"string");
|
2018-08-02 14:23:27 +02:00
|
|
|
return Failure;
|
2018-07-07 13:08:57 +02:00
|
|
|
}
|
|
|
|
successCallback(id.toString());
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
return Success;
|
2018-07-07 13:08:57 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
request.execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chatterino
|