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

53 lines
1.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/NetworkRequest.hpp"
#include "debug/Log.hpp"
#include "providers/twitch/TwitchCommon.hpp"
#include <QString>
namespace chatterino {
void TwitchApi::findUserId(const QString user, std::function<void(QString)> successCallback)
{
QString requestUrl("https://api.twitch.tv/kraken/users?login=" + user);
NetworkRequest request(requestUrl);
request.setCaller(QThread::currentThread());
request.makeAuthorizedV5(getDefaultClientID());
request.setTimeout(30000);
2018-08-02 14:23:27 +02:00
request.onSuccess([successCallback](auto result) mutable -> Outcome {
2018-07-12 10:52:18 +02:00
auto root = result.parseJson();
if (!root.value("users").isArray()) {
Log("API Error while getting user id, users is not an array");
successCallback("");
2018-08-02 14:23:27 +02:00
return Failure;
2018-07-12 10:52:18 +02:00
}
auto users = root.value("users").toArray();
if (users.size() != 1) {
Log("API Error while getting user id, users array size is not 1");
successCallback("");
2018-08-02 14:23:27 +02:00
return Failure;
2018-07-12 10:52:18 +02:00
}
if (!users[0].isObject()) {
Log("API Error while getting user id, first user is not an object");
successCallback("");
2018-08-02 14:23:27 +02:00
return Failure;
2018-07-12 10:52:18 +02:00
}
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("");
2018-08-02 14:23:27 +02:00
return Failure;
2018-07-12 10:52:18 +02:00
}
successCallback(id.toString());
2018-08-02 14:23:27 +02:00
return Success;
2018-07-12 10:52:18 +02:00
});
request.execute();
}
} // namespace chatterino