2018-01-19 22:45:33 +01:00
|
|
|
#pragma once
|
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
#include "common/NetworkCommon.hpp"
|
|
|
|
#include "common/NetworkResult.hpp"
|
2018-01-19 22:45:33 +01:00
|
|
|
|
2020-07-05 14:32:10 +02:00
|
|
|
#include <QHttpMultiPart>
|
2019-08-04 14:31:57 +02: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
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
// The NetworkRequest destructor will assert if executed_ hasn't been set to
|
|
|
|
// true before dying
|
2018-07-07 13:08:57 +02:00
|
|
|
bool executed_ = false;
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
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();
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2019-08-20 18:51:23 +02:00
|
|
|
NetworkRequest type(NetworkRequestType newRequestType) &&;
|
|
|
|
|
|
|
|
NetworkRequest onReplyCreated(NetworkReplyCreatedCallback cb) &&;
|
|
|
|
NetworkRequest onError(NetworkErrorCallback cb) &&;
|
|
|
|
NetworkRequest onSuccess(NetworkSuccessCallback cb) &&;
|
2021-01-16 18:25:56 +01:00
|
|
|
NetworkRequest finally(NetworkFinallyCallback cb) &&;
|
2019-08-20 18:51:23 +02:00
|
|
|
|
|
|
|
NetworkRequest payload(const QByteArray &payload) &&;
|
2019-08-20 20:08:49 +02:00
|
|
|
NetworkRequest cache() &&;
|
2019-08-20 23:29:11 +02:00
|
|
|
/// NetworkRequest makes sure that the `caller` object still exists when the
|
|
|
|
/// callbacks are executed. Cannot be used with concurrent() since we can't
|
|
|
|
/// make sure that the object doesn't get deleted while the callback is
|
|
|
|
/// running.
|
2019-08-20 18:51:23 +02:00
|
|
|
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) &&;
|
2021-04-17 13:49:19 +02:00
|
|
|
NetworkRequest headerList(
|
|
|
|
const std::vector<std::pair<QByteArray, QByteArray>> &headers) &&;
|
2019-08-20 18:51:23 +02:00
|
|
|
NetworkRequest timeout(int ms) &&;
|
|
|
|
NetworkRequest concurrent() &&;
|
|
|
|
NetworkRequest authorizeTwitchV5(const QString &clientID,
|
|
|
|
const QString &oauthToken = QString()) &&;
|
2019-09-19 18:26:38 +02:00
|
|
|
NetworkRequest multiPart(QHttpMultiPart *payload) &&;
|
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
|
|
|
|
2019-08-20 20:08:49 +02:00
|
|
|
static NetworkRequest twitchRequest(QUrl url);
|
2018-09-20 13:37:20 +02:00
|
|
|
|
2018-05-12 20:34:13 +02:00
|
|
|
private:
|
2019-08-03 11:20:19 +02:00
|
|
|
void initializeDefaultValues();
|
2018-05-16 15:42:45 +02:00
|
|
|
};
|
2018-01-19 22:45:33 +01:00
|
|
|
|
|
|
|
} // namespace chatterino
|