2018-01-19 22:45:33 +01:00
|
|
|
#pragma once
|
|
|
|
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "Application.hpp"
|
2018-07-07 13:08:57 +02:00
|
|
|
#include "common/NetworkCommon.hpp"
|
|
|
|
#include "common/NetworkData.hpp"
|
2018-06-26 15:33:51 +02:00
|
|
|
#include "common/NetworkRequester.hpp"
|
2018-07-07 13:08:57 +02:00
|
|
|
#include "common/NetworkResult.hpp"
|
|
|
|
#include "common/NetworkTimer.hpp"
|
2018-06-26 15:33:51 +02:00
|
|
|
#include "common/NetworkWorker.hpp"
|
2018-01-19 22:45:33 +01:00
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
class NetworkRequest
|
|
|
|
{
|
2018-07-07 13:08:57 +02:00
|
|
|
// Stores all data about the request that needs to be passed around to each part of the request
|
2018-07-07 13:51:01 +02:00
|
|
|
std::shared_ptr<NetworkData> data;
|
2018-01-19 22:45:33 +01:00
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
// Timer that tracks the timeout
|
|
|
|
// By default, there's no explicit timeout for the request
|
|
|
|
// to enable the timer, the "setTimeout" function needs to be called before execute is called
|
|
|
|
std::unique_ptr<NetworkTimer> timer;
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
// The NetworkRequest destructor will assert if executed_ hasn't been set to true before dying
|
|
|
|
bool executed_ = false;
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
public:
|
|
|
|
NetworkRequest() = delete;
|
|
|
|
NetworkRequest(const NetworkRequest &other) = delete;
|
|
|
|
NetworkRequest &operator=(const NetworkRequest &other) = delete;
|
2018-01-19 22:45:33 +01:00
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
NetworkRequest(NetworkRequest &&other) = default;
|
|
|
|
NetworkRequest &operator=(NetworkRequest &&other) = default;
|
2018-01-19 22:45:33 +01:00
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
explicit NetworkRequest(const std::string &url,
|
|
|
|
NetworkRequestType requestType = NetworkRequestType::Get);
|
|
|
|
NetworkRequest(QUrl url, NetworkRequestType requestType = NetworkRequestType::Get);
|
2018-01-19 22:45:33 +01:00
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
~NetworkRequest();
|
2018-01-19 22:45:33 +01:00
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
void setRequestType(NetworkRequestType newRequestType);
|
2018-01-19 22:45:33 +01:00
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
void onReplyCreated(NetworkReplyCreatedCallback cb);
|
|
|
|
void onError(NetworkErrorCallback cb);
|
|
|
|
void onSuccess(NetworkSuccessCallback cb);
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
void setPayload(const QByteArray &payload);
|
2018-01-19 22:45:33 +01:00
|
|
|
void setUseQuickLoadCache(bool value);
|
2018-07-06 18:10:21 +02:00
|
|
|
void setCaller(const QObject *caller);
|
2018-07-06 17:56:11 +02:00
|
|
|
void setRawHeader(const char *headerName, const char *value);
|
|
|
|
void setRawHeader(const char *headerName, const QByteArray &value);
|
|
|
|
void setRawHeader(const char *headerName, const QString &value);
|
|
|
|
void setTimeout(int ms);
|
2018-07-06 22:00:03 +02:00
|
|
|
void makeAuthorizedV5(const QString &clientID, const QString &oauthToken = QString());
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-07-06 17:56:11 +02:00
|
|
|
void execute();
|
2018-05-12 20:34:13 +02:00
|
|
|
|
|
|
|
private:
|
2018-07-07 13:08:57 +02:00
|
|
|
// Returns true if the file was successfully loaded from cache
|
|
|
|
// Returns false if the cache file either didn't exist, or it contained "invalid" data
|
|
|
|
// "invalid" is specified by the onSuccess callback
|
|
|
|
bool tryLoadCachedFile();
|
|
|
|
|
2018-07-06 17:56:11 +02:00
|
|
|
void doRequest();
|
2018-07-07 13:08:57 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
// Helper creator functions
|
|
|
|
static NetworkRequest twitchRequest(QUrl url);
|
2018-05-16 15:42:45 +02:00
|
|
|
};
|
2018-01-19 22:45:33 +01:00
|
|
|
|
|
|
|
} // namespace chatterino
|