2018-06-26 15:33:51 +02:00
|
|
|
#include "common/NetworkRequest.hpp"
|
2018-01-19 22:45:33 +01:00
|
|
|
|
2018-08-11 22:23:06 +02:00
|
|
|
#include "common/NetworkData.hpp"
|
2019-08-20 20:08:49 +02:00
|
|
|
//#include "common/NetworkManager.hpp"
|
2018-08-11 22:23:06 +02:00
|
|
|
#include "common/Outcome.hpp"
|
2019-08-03 11:20:19 +02:00
|
|
|
#include "common/Version.hpp"
|
2019-08-15 20:38:11 +02:00
|
|
|
#include "debug/AssertInGuiThread.hpp"
|
2018-08-07 23:46:00 +02:00
|
|
|
#include "debug/Log.hpp"
|
2018-07-07 13:08:57 +02:00
|
|
|
#include "providers/twitch/TwitchCommon.hpp"
|
|
|
|
#include "singletons/Paths.hpp"
|
2018-08-07 23:46:00 +02:00
|
|
|
#include "util/DebugCount.hpp"
|
2019-08-15 20:38:11 +02:00
|
|
|
#include "util/PostToThread.hpp"
|
2018-07-07 13:08:57 +02:00
|
|
|
|
2019-08-20 20:08:49 +02:00
|
|
|
#include <QDebug>
|
2018-07-07 13:08:57 +02:00
|
|
|
#include <QFile>
|
2018-08-10 18:56:17 +02:00
|
|
|
#include <QtConcurrent>
|
2018-08-19 16:20:14 +02:00
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
#include <cassert>
|
2018-04-27 22:11:19 +02:00
|
|
|
|
2018-01-19 22:45:33 +01:00
|
|
|
namespace chatterino {
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
NetworkRequest::NetworkRequest(const std::string &url,
|
|
|
|
NetworkRequestType requestType)
|
2018-07-07 13:51:01 +02:00
|
|
|
: data(new NetworkData)
|
2018-01-19 22:45:33 +01:00
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->request_.setUrl(QUrl(QString::fromStdString(url)));
|
|
|
|
this->data->requestType_ = requestType;
|
2019-08-03 11:20:19 +02:00
|
|
|
|
|
|
|
this->initializeDefaultValues();
|
2018-01-19 22:45:33 +01:00
|
|
|
}
|
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
NetworkRequest::NetworkRequest(QUrl url, NetworkRequestType requestType)
|
2018-07-07 13:51:01 +02:00
|
|
|
: data(new NetworkData)
|
2018-01-19 22:45:33 +01:00
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->request_.setUrl(url);
|
|
|
|
this->data->requestType_ = requestType;
|
2019-08-03 11:20:19 +02:00
|
|
|
|
|
|
|
this->initializeDefaultValues();
|
2018-01-19 22:45:33 +01:00
|
|
|
}
|
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
NetworkRequest::~NetworkRequest()
|
2018-01-19 22:45:33 +01:00
|
|
|
{
|
2019-08-20 20:08:49 +02:00
|
|
|
//assert(this->executed_);
|
2018-01-19 22:45:33 +01:00
|
|
|
}
|
|
|
|
|
2019-08-20 18:51:23 +02:00
|
|
|
// old
|
|
|
|
void NetworkRequest::type(NetworkRequestType newRequestType) &
|
2018-07-06 21:31:58 +02:00
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->requestType_ = newRequestType;
|
2018-07-06 21:31:58 +02:00
|
|
|
}
|
|
|
|
|
2019-08-20 18:51:23 +02:00
|
|
|
void NetworkRequest::setCaller(const QObject *caller) &
|
2018-07-06 17:56:11 +02:00
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->caller_ = caller;
|
2018-07-06 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
2019-08-20 18:51:23 +02:00
|
|
|
void NetworkRequest::onReplyCreated(NetworkReplyCreatedCallback cb) &
|
2018-07-07 13:08:57 +02:00
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->onReplyCreated_ = cb;
|
2018-07-07 13:08:57 +02:00
|
|
|
}
|
|
|
|
|
2019-08-20 18:51:23 +02:00
|
|
|
void NetworkRequest::onError(NetworkErrorCallback cb) &
|
2018-07-06 17:56:11 +02:00
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->onError_ = cb;
|
2018-07-06 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
2019-08-20 18:51:23 +02:00
|
|
|
void NetworkRequest::onSuccess(NetworkSuccessCallback cb) &
|
2018-07-06 17:56:11 +02:00
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->onSuccess_ = cb;
|
2018-07-06 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
2019-08-20 18:51:23 +02:00
|
|
|
void NetworkRequest::setRawHeader(const char *headerName, const char *value) &
|
2018-07-06 17:56:11 +02:00
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->request_.setRawHeader(headerName, value);
|
2018-07-06 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
void NetworkRequest::setRawHeader(const char *headerName,
|
2019-08-20 18:51:23 +02:00
|
|
|
const QByteArray &value) &
|
2018-07-06 17:56:11 +02:00
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->request_.setRawHeader(headerName, value);
|
2018-07-06 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
2019-08-20 18:51:23 +02:00
|
|
|
void NetworkRequest::setRawHeader(const char *headerName,
|
|
|
|
const QString &value) &
|
2018-07-06 17:56:11 +02:00
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->request_.setRawHeader(headerName, value.toUtf8());
|
2018-07-06 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
2019-08-20 18:51:23 +02:00
|
|
|
void NetworkRequest::setTimeout(int ms) &
|
2018-07-06 17:56:11 +02:00
|
|
|
{
|
2019-08-20 20:08:49 +02:00
|
|
|
this->data->hasTimeout_ = true;
|
|
|
|
this->data->timer_.setInterval(ms);
|
2018-07-06 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
2019-08-20 18:51:23 +02:00
|
|
|
void NetworkRequest::setExecuteConcurrently(bool value) &
|
2018-08-10 18:56:17 +02:00
|
|
|
{
|
|
|
|
this->data->executeConcurrently = value;
|
|
|
|
}
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
void NetworkRequest::makeAuthorizedV5(const QString &clientID,
|
2019-08-20 18:51:23 +02:00
|
|
|
const QString &oauthToken) &
|
2018-07-06 17:56:11 +02:00
|
|
|
{
|
|
|
|
this->setRawHeader("Client-ID", clientID);
|
|
|
|
this->setRawHeader("Accept", "application/vnd.twitchtv.v5+json");
|
2018-10-21 13:43:02 +02:00
|
|
|
if (!oauthToken.isEmpty())
|
|
|
|
{
|
2018-07-06 22:00:03 +02:00
|
|
|
this->setRawHeader("Authorization", "OAuth " + oauthToken);
|
|
|
|
}
|
2018-07-06 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
2019-08-20 18:51:23 +02:00
|
|
|
void NetworkRequest::setPayload(const QByteArray &payload) &
|
2018-01-19 22:45:33 +01:00
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->payload_ = payload;
|
2018-01-19 22:45:33 +01:00
|
|
|
}
|
|
|
|
|
2019-08-20 18:51:23 +02:00
|
|
|
void NetworkRequest::setUseQuickLoadCache(bool value) &
|
2018-01-19 22:45:33 +01:00
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->useQuickLoadCache_ = value;
|
2018-01-19 22:45:33 +01:00
|
|
|
}
|
|
|
|
|
2019-08-20 18:51:23 +02:00
|
|
|
// new
|
|
|
|
NetworkRequest NetworkRequest::type(NetworkRequestType newRequestType) &&
|
|
|
|
{
|
|
|
|
this->data->requestType_ = newRequestType;
|
|
|
|
return std::move(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkRequest NetworkRequest::caller(const QObject *caller) &&
|
|
|
|
{
|
|
|
|
this->data->caller_ = caller;
|
|
|
|
return std::move(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkRequest NetworkRequest::onReplyCreated(NetworkReplyCreatedCallback cb) &&
|
|
|
|
{
|
|
|
|
this->data->onReplyCreated_ = cb;
|
|
|
|
return std::move(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkRequest NetworkRequest::onError(NetworkErrorCallback cb) &&
|
|
|
|
{
|
|
|
|
this->data->onError_ = cb;
|
|
|
|
return std::move(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkRequest NetworkRequest::onSuccess(NetworkSuccessCallback cb) &&
|
|
|
|
{
|
|
|
|
this->data->onSuccess_ = cb;
|
|
|
|
return std::move(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkRequest NetworkRequest::header(const char *headerName,
|
|
|
|
const char *value) &&
|
|
|
|
{
|
|
|
|
this->data->request_.setRawHeader(headerName, value);
|
|
|
|
return std::move(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkRequest NetworkRequest::header(const char *headerName,
|
|
|
|
const QByteArray &value) &&
|
|
|
|
{
|
|
|
|
this->data->request_.setRawHeader(headerName, value);
|
|
|
|
return std::move(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkRequest NetworkRequest::header(const char *headerName,
|
|
|
|
const QString &value) &&
|
|
|
|
{
|
|
|
|
this->data->request_.setRawHeader(headerName, value.toUtf8());
|
|
|
|
return std::move(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkRequest NetworkRequest::timeout(int ms) &&
|
|
|
|
{
|
2019-08-20 20:08:49 +02:00
|
|
|
this->data->hasTimeout_ = true;
|
|
|
|
this->data->timer_.setInterval(ms);
|
2019-08-20 18:51:23 +02:00
|
|
|
return std::move(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkRequest NetworkRequest::concurrent() &&
|
|
|
|
{
|
|
|
|
this->data->executeConcurrently = true;
|
|
|
|
return std::move(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkRequest NetworkRequest::authorizeTwitchV5(const QString &clientID,
|
|
|
|
const QString &oauthToken) &&
|
|
|
|
{
|
2019-08-20 18:53:26 +02:00
|
|
|
// TODO: make two overloads, with and without oauth token
|
|
|
|
auto tmp = std::move(*this)
|
|
|
|
.header("Client-ID", clientID)
|
|
|
|
.header("Accept", "application/vnd.twitchtv.v5+json");
|
|
|
|
|
2019-08-20 18:51:23 +02:00
|
|
|
if (!oauthToken.isEmpty())
|
2019-08-20 18:53:26 +02:00
|
|
|
return std::move(tmp).header("Authorization", "OAuth " + oauthToken);
|
|
|
|
else
|
|
|
|
return tmp;
|
2019-08-20 18:51:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
NetworkRequest NetworkRequest::payload(const QByteArray &payload) &&
|
|
|
|
{
|
|
|
|
this->data->payload_ = payload;
|
|
|
|
return std::move(*this);
|
|
|
|
}
|
|
|
|
|
2019-08-20 20:08:49 +02:00
|
|
|
NetworkRequest NetworkRequest::cache() &&
|
2019-08-20 18:51:23 +02:00
|
|
|
{
|
|
|
|
this->data->useQuickLoadCache_ = true;
|
|
|
|
return std::move(*this);
|
|
|
|
}
|
|
|
|
|
2018-07-06 17:56:11 +02:00
|
|
|
void NetworkRequest::execute()
|
|
|
|
{
|
2018-07-07 13:08:57 +02:00
|
|
|
this->executed_ = true;
|
|
|
|
|
2019-08-20 20:08:49 +02:00
|
|
|
// Only allow caching for GET request
|
|
|
|
if (this->data->useQuickLoadCache_ &&
|
|
|
|
this->data->requestType_ != NetworkRequestType::Get)
|
2018-10-21 13:43:02 +02:00
|
|
|
{
|
2019-08-20 20:08:49 +02:00
|
|
|
qDebug() << "Can only cache GET requests!";
|
|
|
|
this->data->useQuickLoadCache_ = false;
|
2018-07-06 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
2019-08-20 20:08:49 +02:00
|
|
|
load(std::move(this->data));
|
2018-09-20 13:37:20 +02:00
|
|
|
}
|
|
|
|
|
2019-08-03 11:20:19 +02:00
|
|
|
void NetworkRequest::initializeDefaultValues()
|
|
|
|
{
|
|
|
|
const auto userAgent = QString("chatterino/%1 (%2)")
|
|
|
|
.arg(CHATTERINO_VERSION, CHATTERINO_GIT_HASH)
|
|
|
|
.toUtf8();
|
|
|
|
|
|
|
|
this->data->request_.setRawHeader("User-Agent", userAgent);
|
|
|
|
}
|
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
// Helper creator functions
|
|
|
|
NetworkRequest NetworkRequest::twitchRequest(QUrl url)
|
2018-07-06 17:56:11 +02:00
|
|
|
{
|
2019-08-20 20:08:49 +02:00
|
|
|
return NetworkRequest(url).authorizeTwitchV5(getDefaultClientID());
|
2018-07-06 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
2018-01-19 22:45:33 +01:00
|
|
|
} // namespace chatterino
|