mirror-chatterino2/src/common/NetworkRequest.cpp

242 lines
6 KiB
C++
Raw Normal View History

2018-06-26 15:33:51 +02:00
#include "common/NetworkRequest.hpp"
2018-01-19 22:45:33 +01:00
2019-08-20 20:27:16 +02:00
#include "common/NetworkPrivate.hpp"
#include "common/Outcome.hpp"
#include "common/Version.hpp"
#include "debug/AssertInGuiThread.hpp"
#include "debug/Log.hpp"
#include "providers/twitch/TwitchCommon.hpp"
#include "singletons/Paths.hpp"
#include "util/DebugCount.hpp"
#include "util/PostToThread.hpp"
2019-08-20 20:08:49 +02:00
#include <QDebug>
#include <QFile>
2018-08-10 18:56:17 +02:00
#include <QtConcurrent>
#include <cassert>
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;
this->initializeDefaultValues();
2018-01-19 22:45:33 +01: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;
this->initializeDefaultValues();
2018-01-19 22:45:33 +01: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-07 13:51:01 +02:00
this->data->requestType_ = newRequestType;
}
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:51:01 +02:00
this->data->onReplyCreated_ = cb;
}
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()
{
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));
}
void NetworkRequest::initializeDefaultValues()
{
const auto userAgent = QString("chatterino/%1 (%2)")
.arg(CHATTERINO_VERSION, CHATTERINO_GIT_HASH)
.toUtf8();
this->data->request_.setRawHeader("User-Agent", userAgent);
}
// 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