2017-06-06 21:18:05 +02:00
|
|
|
#pragma once
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-10-27 20:09:02 +02:00
|
|
|
#include "credentials.hpp"
|
2017-12-22 15:13:42 +01:00
|
|
|
#include "debug/log.hpp"
|
2018-01-18 18:20:40 +01:00
|
|
|
#include "singletons/accountmanager.hpp"
|
2017-10-27 20:09:02 +02:00
|
|
|
#include "util/networkmanager.hpp"
|
2018-01-19 22:45:33 +01:00
|
|
|
#include "util/networkrequest.hpp"
|
2017-08-19 15:37:56 +02:00
|
|
|
|
2017-12-28 17:47:00 +01:00
|
|
|
#include <rapidjson/document.h>
|
|
|
|
#include <rapidjson/error/en.h>
|
|
|
|
#include <rapidjson/error/error.h>
|
2017-06-26 15:53:43 +02:00
|
|
|
#include <QEventLoop>
|
2017-04-12 17:46:44 +02:00
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonValue>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QString>
|
2017-10-27 20:09:02 +02:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
#include <functional>
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
namespace chatterino {
|
|
|
|
namespace util {
|
2017-10-27 20:09:02 +02:00
|
|
|
namespace twitch {
|
2017-06-26 15:53:43 +02:00
|
|
|
|
2017-10-27 20:09:02 +02:00
|
|
|
static void get(QString url, const QObject *caller,
|
2018-01-19 22:45:33 +01:00
|
|
|
std::function<void(const QJsonObject &)> successCallback)
|
2017-10-27 20:09:02 +02:00
|
|
|
{
|
|
|
|
util::NetworkRequest req(url);
|
|
|
|
req.setCaller(caller);
|
|
|
|
req.setRawHeader("Client-ID", getDefaultClientID());
|
|
|
|
req.setRawHeader("Accept", "application/vnd.twitchtv.v5+json");
|
2018-01-19 22:45:33 +01:00
|
|
|
|
|
|
|
req.getJSON([=](const QJsonObject &node) {
|
|
|
|
successCallback(node); //
|
2017-10-27 20:09:02 +02:00
|
|
|
});
|
2017-06-26 15:53:43 +02:00
|
|
|
}
|
|
|
|
|
2017-12-28 17:47:00 +01:00
|
|
|
static void get2(QString url, const QObject *caller,
|
2018-01-19 22:45:33 +01:00
|
|
|
std::function<void(const rapidjson::Document &)> successCallback)
|
2017-12-28 17:47:00 +01:00
|
|
|
{
|
|
|
|
util::NetworkRequest req(url);
|
|
|
|
req.setCaller(caller);
|
|
|
|
req.setRawHeader("Client-ID", getDefaultClientID());
|
|
|
|
req.setRawHeader("Accept", "application/vnd.twitchtv.v5+json");
|
2018-01-19 22:45:33 +01:00
|
|
|
req.setUseQuickLoadCache(true);
|
|
|
|
|
|
|
|
req.getJSON2([=](const rapidjson::Document &document) {
|
|
|
|
successCallback(document); //
|
2017-12-28 17:47:00 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-22 17:36:32 +01:00
|
|
|
static void getAuthorized(QString url, const QString &clientID, const QString &oauthToken,
|
2018-01-19 22:45:33 +01:00
|
|
|
const QObject *caller,
|
|
|
|
std::function<void(const QJsonObject &)> successCallback)
|
2017-12-22 17:36:32 +01:00
|
|
|
{
|
|
|
|
util::NetworkRequest req(url);
|
|
|
|
req.setCaller(caller);
|
|
|
|
req.setRawHeader("Client-ID", clientID.toUtf8());
|
|
|
|
req.setRawHeader("Authorization", "OAuth " + oauthToken.toUtf8());
|
|
|
|
req.setRawHeader("Accept", "application/vnd.twitchtv.v5+json");
|
2018-01-19 22:45:33 +01:00
|
|
|
|
|
|
|
req.getJSON([=](const QJsonObject &node) {
|
|
|
|
successCallback(node); //
|
2017-12-22 17:36:32 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-27 20:09:02 +02:00
|
|
|
static void getUserID(QString username, const QObject *caller,
|
|
|
|
std::function<void(QString)> successCallback)
|
2017-06-26 15:53:43 +02:00
|
|
|
{
|
2017-10-27 20:09:02 +02:00
|
|
|
get("https://api.twitch.tv/kraken/users?login=" + username, caller,
|
|
|
|
[=](const QJsonObject &root) {
|
|
|
|
if (!root.value("users").isArray()) {
|
2017-12-22 15:13:42 +01:00
|
|
|
debug::Log("API Error while getting user id, users is not an array");
|
2017-10-27 20:09:02 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-06-26 15:53:43 +02:00
|
|
|
|
2017-10-27 20:09:02 +02:00
|
|
|
auto users = root.value("users").toArray();
|
|
|
|
if (users.size() != 1) {
|
2017-12-22 15:13:42 +01:00
|
|
|
debug::Log("API Error while getting user id, users array size is not 1");
|
2017-10-27 20:09:02 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!users[0].isObject()) {
|
2017-12-22 15:13:42 +01:00
|
|
|
debug::Log("API Error while getting user id, first user is not an object");
|
2017-10-27 20:09:02 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto firstUser = users[0].toObject();
|
|
|
|
auto id = firstUser.value("_id");
|
|
|
|
if (!id.isString()) {
|
2017-12-22 15:13:42 +01:00
|
|
|
debug::Log("API Error: while getting user id, first user object `_id` key is not a "
|
|
|
|
"string");
|
2017-10-27 20:09:02 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
successCallback(id.toString());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
static void put(QUrl url, std::function<void(QJsonObject)> successCallback)
|
|
|
|
{
|
|
|
|
QNetworkRequest request(url);
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
auto &accountManager = singletons::AccountManager::getInstance();
|
2017-12-16 02:21:06 +01:00
|
|
|
auto currentTwitchUser = accountManager.Twitch.getCurrent();
|
|
|
|
QByteArray oauthToken;
|
|
|
|
if (currentTwitchUser) {
|
|
|
|
oauthToken = currentTwitchUser->getOAuthToken().toUtf8();
|
|
|
|
} else {
|
|
|
|
// XXX(pajlada): Bail out?
|
|
|
|
}
|
|
|
|
|
2017-10-27 20:09:02 +02:00
|
|
|
request.setRawHeader("Client-ID", getDefaultClientID());
|
|
|
|
request.setRawHeader("Accept", "application/vnd.twitchtv.v5+json");
|
2017-12-16 02:21:06 +01:00
|
|
|
request.setRawHeader("Authorization", "OAuth " + oauthToken);
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-10-27 20:09:02 +02:00
|
|
|
NetworkManager::urlPut(std::move(request), [=](QNetworkReply *reply) {
|
2017-06-26 15:53:43 +02:00
|
|
|
if (reply->error() == QNetworkReply::NetworkError::NoError) {
|
2017-10-27 20:09:02 +02:00
|
|
|
QByteArray data = reply->readAll();
|
|
|
|
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
|
|
|
|
if (!jsonDoc.isNull()) {
|
|
|
|
QJsonObject rootNode = jsonDoc.object();
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-10-27 20:09:02 +02:00
|
|
|
successCallback(rootNode);
|
|
|
|
}
|
2017-06-26 15:53:43 +02:00
|
|
|
}
|
2017-10-27 20:09:02 +02:00
|
|
|
reply->deleteLater();
|
2017-04-12 17:46:44 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-18 18:20:40 +01:00
|
|
|
static void sendDelete(QUrl url, std::function<void()> successCallback)
|
|
|
|
{
|
|
|
|
QNetworkRequest request(url);
|
|
|
|
|
|
|
|
auto &accountManager = singletons::AccountManager::getInstance();
|
|
|
|
auto currentTwitchUser = accountManager.Twitch.getCurrent();
|
|
|
|
QByteArray oauthToken;
|
|
|
|
if (currentTwitchUser) {
|
|
|
|
oauthToken = currentTwitchUser->getOAuthToken().toUtf8();
|
|
|
|
} else {
|
|
|
|
// XXX(pajlada): Bail out?
|
|
|
|
}
|
|
|
|
|
|
|
|
request.setRawHeader("Client-ID", getDefaultClientID());
|
|
|
|
request.setRawHeader("Accept", "application/vnd.twitchtv.v5+json");
|
|
|
|
request.setRawHeader("Authorization", "OAuth " + oauthToken);
|
|
|
|
|
|
|
|
NetworkManager::urlDelete(std::move(request), [=](QNetworkReply *reply) {
|
|
|
|
if (reply->error() == QNetworkReply::NetworkError::NoError) {
|
|
|
|
int code =
|
|
|
|
reply->attribute(QNetworkRequest::Attribute::HttpStatusCodeAttribute).toInt();
|
|
|
|
|
|
|
|
if (code >= 200 && code <= 299) {
|
|
|
|
successCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reply->deleteLater();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-27 20:09:02 +02:00
|
|
|
} // namespace twitch
|
2017-06-06 21:18:05 +02:00
|
|
|
} // namespace util
|
|
|
|
} // namespace chatterino
|