mirror-chatterino2/src/common/NetworkRequest.cpp

256 lines
6.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
2018-06-26 14:09:39 +02:00
#include "Application.hpp"
#include "common/NetworkManager.hpp"
#include "providers/twitch/TwitchCommon.hpp"
#include "singletons/Paths.hpp"
#include <QFile>
#include <cassert>
2018-01-19 22:45:33 +01:00
namespace chatterino {
NetworkRequest::NetworkRequest(const std::string &url, NetworkRequestType requestType)
2018-07-07 13:51:01 +02:00
: data(new NetworkData)
, timer(new NetworkTimer)
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;
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)
, timer(new NetworkTimer)
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;
2018-01-19 22:45:33 +01:00
}
NetworkRequest::~NetworkRequest()
2018-01-19 22:45:33 +01:00
{
2018-07-07 12:03:37 +02:00
// assert(this->executed_);
2018-01-19 22:45:33 +01:00
}
void NetworkRequest::setRequestType(NetworkRequestType newRequestType)
{
2018-07-07 13:51:01 +02:00
this->data->requestType_ = newRequestType;
}
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
}
void NetworkRequest::onReplyCreated(NetworkReplyCreatedCallback cb)
{
2018-07-07 13:51:01 +02:00
this->data->onReplyCreated_ = cb;
}
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
}
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
}
void NetworkRequest::setRawHeader(const char *headerName, const char *value)
{
2018-07-07 13:51:01 +02:00
this->data->request_.setRawHeader(headerName, value);
2018-07-06 17:56:11 +02:00
}
void NetworkRequest::setRawHeader(const char *headerName, const QByteArray &value)
{
2018-07-07 13:51:01 +02:00
this->data->request_.setRawHeader(headerName, value);
2018-07-06 17:56:11 +02:00
}
void NetworkRequest::setRawHeader(const char *headerName, const QString &value)
{
2018-07-07 13:51:01 +02:00
this->data->request_.setRawHeader(headerName, value.toUtf8());
2018-07-06 17:56:11 +02:00
}
void NetworkRequest::setTimeout(int ms)
{
this->timer->timeoutMS_ = ms;
2018-07-06 17:56:11 +02:00
}
void NetworkRequest::makeAuthorizedV5(const QString &clientID, const QString &oauthToken)
{
this->setRawHeader("Client-ID", clientID);
this->setRawHeader("Accept", "application/vnd.twitchtv.v5+json");
2018-07-06 22:00:03 +02:00
if (!oauthToken.isEmpty()) {
this->setRawHeader("Authorization", "OAuth " + oauthToken);
}
2018-07-06 17:56:11 +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
}
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
}
2018-07-06 17:56:11 +02:00
void NetworkRequest::execute()
{
this->executed_ = true;
2018-07-07 13:51:01 +02:00
switch (this->data->requestType_) {
case NetworkRequestType::Get: {
// Get requests try to load from cache, then perform the request
2018-07-07 13:51:01 +02:00
if (this->data->useQuickLoadCache_) {
if (this->tryLoadCachedFile()) {
// Successfully loaded from cache
return;
}
}
this->doRequest();
2018-07-06 17:56:11 +02:00
} break;
case NetworkRequestType::Put: {
// Put requests cannot be cached, therefore the request is called immediately
this->doRequest();
2018-07-06 17:56:11 +02:00
} break;
case NetworkRequestType::Delete: {
// Delete requests cannot be cached, therefore the request is called immediately
this->doRequest();
2018-07-06 17:56:11 +02:00
} break;
default: {
Log("[Execute] Unhandled request type");
2018-07-06 17:56:11 +02:00
} break;
}
}
2018-08-02 14:23:27 +02:00
Outcome NetworkRequest::tryLoadCachedFile()
2018-07-06 17:56:11 +02:00
{
auto app = getApp();
2018-07-06 17:56:11 +02:00
2018-07-07 13:51:01 +02:00
QFile cachedFile(app->paths->cacheDirectory + "/" + this->data->getHash());
2018-07-06 17:56:11 +02:00
if (!cachedFile.exists()) {
// File didn't exist
2018-08-02 14:23:27 +02:00
return Failure;
}
2018-07-06 17:56:11 +02:00
if (!cachedFile.open(QIODevice::ReadOnly)) {
// File could not be opened
2018-08-02 14:23:27 +02:00
return Failure;
}
2018-07-06 17:56:11 +02:00
QByteArray bytes = cachedFile.readAll();
NetworkResult result(bytes);
2018-07-06 17:56:11 +02:00
2018-08-02 14:23:27 +02:00
auto outcome = this->data->onSuccess_(result);
2018-07-06 17:56:11 +02:00
cachedFile.close();
2018-07-06 17:56:11 +02:00
// XXX: If success is false, we should invalidate the cache file somehow/somewhere
2018-07-06 17:56:11 +02:00
2018-08-02 14:23:27 +02:00
return outcome;
2018-07-06 17:56:11 +02:00
}
void NetworkRequest::doRequest()
{
NetworkRequester requester;
NetworkWorker *worker = new NetworkWorker;
worker->moveToThread(&NetworkManager::workerThread);
this->timer->start();
2018-07-06 17:56:11 +02:00
auto onUrlRequested = [data = this->data, timer = this->timer, worker]() mutable {
auto reply = [&]() -> QNetworkReply * {
switch (data->requestType_) {
case NetworkRequestType::Get:
return NetworkManager::NaM.get(data->request_);
case NetworkRequestType::Put:
return NetworkManager::NaM.put(data->request_, data->payload_);
case NetworkRequestType::Delete:
return NetworkManager::NaM.deleteResource(data->request_);
default:
return nullptr;
}
}();
if (reply == nullptr) {
Log("Unhandled request type");
return;
}
if (timer->isStarted()) {
timer->onTimeout(worker, [reply, data]() {
Log("Aborted!");
reply->abort();
2018-07-07 13:51:01 +02:00
if (data->onError_) {
data->onError_(-2);
}
});
}
2018-07-07 13:51:01 +02:00
if (data->onReplyCreated_) {
data->onReplyCreated_(reply);
}
auto handleReply = [data, timer, reply]() mutable {
// TODO(pajlada): A reply was received, kill the timeout timer
if (reply->error() != QNetworkReply::NetworkError::NoError) {
2018-07-07 13:51:01 +02:00
if (data->onError_) {
data->onError_(reply->error());
}
return;
}
2018-07-14 14:24:18 +02:00
QByteArray bytes = reply->readAll();
2018-07-07 13:51:01 +02:00
data->writeToCache(bytes);
NetworkResult result(bytes);
2018-07-07 13:51:01 +02:00
data->onSuccess_(result);
2018-07-06 17:56:11 +02:00
reply->deleteLater();
};
2018-07-07 13:51:01 +02:00
if (data->caller_ != nullptr) {
QObject::connect(worker, &NetworkWorker::doneUrl, data->caller_, handleReply);
QObject::connect(reply, &QNetworkReply::finished, worker, [worker]() mutable {
emit worker->doneUrl();
delete worker;
});
} else {
QObject::connect(reply, &QNetworkReply::finished, worker,
[handleReply, worker]() mutable {
handleReply();
delete worker;
});
}
};
QObject::connect(&requester, &NetworkRequester::requestUrl, worker, onUrlRequested);
2018-07-06 17:56:11 +02:00
emit requester.requestUrl();
}
// Helper creator functions
NetworkRequest NetworkRequest::twitchRequest(QUrl url)
2018-07-06 17:56:11 +02:00
{
NetworkRequest request(url);
2018-07-06 17:56:11 +02:00
request.makeAuthorizedV5(getDefaultClientID());
2018-07-06 17:56:11 +02:00
return request;
2018-07-06 17:56:11 +02:00
}
2018-01-19 22:45:33 +01:00
} // namespace chatterino