mirror-chatterino2/src/common/NetworkRequest.hpp

105 lines
3.6 KiB
C++
Raw Normal View History

2018-01-19 22:45:33 +01:00
#pragma once
#include "common/NetworkCommon.hpp"
2018-06-26 15:33:51 +02:00
#include "common/NetworkRequester.hpp"
#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
#include <memory>
2018-01-19 22:45:33 +01:00
namespace chatterino {
2018-09-30 19:15:17 +02:00
struct NetworkData;
2018-01-19 22:45:33 +01:00
2018-12-02 17:49:15 +01:00
class NetworkRequest final
2018-01-19 22:45:33 +01:00
{
2018-08-06 21:17:03 +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
// Timer that tracks the timeout
// By default, there's no explicit timeout for the request
2018-08-06 21:17:03 +02:00
// to enable the timer, the "setTimeout" function needs to be called before
// execute is called
std::shared_ptr<NetworkTimer> timer;
2018-08-06 21:17:03 +02:00
// The NetworkRequest destructor will assert if executed_ hasn't been set to
// true before dying
bool executed_ = false;
public:
2018-08-06 21:17:03 +02:00
explicit NetworkRequest(
const std::string &url,
NetworkRequestType requestType = NetworkRequestType::Get);
explicit NetworkRequest(
QUrl url, NetworkRequestType requestType = NetworkRequestType::Get);
2018-01-19 22:45:33 +01:00
2019-08-20 18:51:23 +02:00
// Enable move
2018-08-10 18:56:17 +02:00
NetworkRequest(NetworkRequest &&other) = default;
NetworkRequest &operator=(NetworkRequest &&other) = default;
2019-08-20 18:51:23 +02:00
// Disable copy
NetworkRequest(const NetworkRequest &other) = delete;
NetworkRequest &operator=(const NetworkRequest &other) = delete;
2018-01-19 22:45:33 +01:00
2019-08-20 18:51:23 +02:00
~NetworkRequest();
2019-08-20 18:51:23 +02:00
// old
2019-08-20 18:53:26 +02:00
[[deprecated]] void type(NetworkRequestType newRequestType) &;
[[deprecated]] void onReplyCreated(NetworkReplyCreatedCallback cb) &;
[[deprecated]] void onError(NetworkErrorCallback cb) &;
[[deprecated]] void onSuccess(NetworkSuccessCallback cb) &;
[[deprecated]] void setPayload(const QByteArray &payload) &;
[[deprecated]] void setUseQuickLoadCache(bool value) &;
[[deprecated]] void setCaller(const QObject *caller) &;
[[deprecated]] void setRawHeader(const char *headerName,
const char *value) &;
[[deprecated]] void setRawHeader(const char *headerName,
const QByteArray &value) &;
[[deprecated]] void setRawHeader(const char *headerName,
const QString &value) &;
[[deprecated]] void setTimeout(int ms) &;
[[deprecated]] void setExecuteConcurrently(bool value) &;
[[deprecated]] void makeAuthorizedV5(
const QString &clientID, const QString &oauthToken = QString()) &;
2019-08-20 18:51:23 +02:00
// new
NetworkRequest type(NetworkRequestType newRequestType) &&;
NetworkRequest onReplyCreated(NetworkReplyCreatedCallback cb) &&;
NetworkRequest onError(NetworkErrorCallback cb) &&;
NetworkRequest onSuccess(NetworkSuccessCallback cb) &&;
NetworkRequest payload(const QByteArray &payload) &&;
NetworkRequest quickLoad() &&;
NetworkRequest caller(const QObject *caller) &&;
NetworkRequest header(const char *headerName, const char *value) &&;
NetworkRequest header(const char *headerName, const QByteArray &value) &&;
NetworkRequest header(const char *headerName, const QString &value) &&;
NetworkRequest timeout(int ms) &&;
NetworkRequest concurrent() &&;
NetworkRequest authorizeTwitchV5(const QString &clientID,
const QString &oauthToken = QString()) &&;
2018-07-06 17:56:11 +02:00
void execute();
[[nodiscard]] QString urlString() const;
private:
void initializeDefaultValues();
2018-08-06 21:17:03 +02:00
// "invalid" data "invalid" is specified by the onSuccess callback
2018-08-02 14:23:27 +02:00
Outcome tryLoadCachedFile();
2018-07-06 17:56:11 +02:00
void doRequest();
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