mirror-chatterino2/src/common/NetworkRequest.hpp
nerix ce47d27d41
Refactor/Cleanup NetworkRequest and Related Code (#4633)
Cleanup unused code (Twitch v5)
Add json methods that simplify sending JSON. This also sets the Accepts header, since here, when JSON is sent, only JSON is accepted as a response (this is only done in Helix).
Clarify helix request creations
Cleaned some clang-tidy suggestions in Network{Request,Private}
2023-05-16 17:28:20 +02:00

84 lines
2.9 KiB
C++

#pragma once
#include "common/NetworkCommon.hpp"
#include <QHttpMultiPart>
#include <memory>
class QJsonArray;
class QJsonObject;
class QJsonDocument;
namespace chatterino {
struct NetworkData;
class NetworkRequest final
{
// Stores all data about the request that needs to be passed around to each
// part of the request
std::shared_ptr<NetworkData> data;
// The NetworkRequest destructor will assert if executed_ hasn't been set to
// true before dying
bool executed_ = false;
public:
explicit NetworkRequest(
const std::string &url,
NetworkRequestType requestType = NetworkRequestType::Get);
explicit NetworkRequest(const QUrl &url, NetworkRequestType requestType =
NetworkRequestType::Get);
// Enable move
NetworkRequest(NetworkRequest &&other) = default;
NetworkRequest &operator=(NetworkRequest &&other) = default;
// Disable copy
NetworkRequest(const NetworkRequest &other) = delete;
NetworkRequest &operator=(const NetworkRequest &other) = delete;
~NetworkRequest();
NetworkRequest type(NetworkRequestType newRequestType) &&;
NetworkRequest onReplyCreated(NetworkReplyCreatedCallback cb) &&;
NetworkRequest onError(NetworkErrorCallback cb) &&;
NetworkRequest onSuccess(NetworkSuccessCallback cb) &&;
NetworkRequest finally(NetworkFinallyCallback cb) &&;
NetworkRequest payload(const QByteArray &payload) &&;
NetworkRequest cache() &&;
/// 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.
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 header(QNetworkRequest::KnownHeaders header,
const QVariant &value) &&;
NetworkRequest headerList(
const std::vector<std::pair<QByteArray, QByteArray>> &headers) &&;
NetworkRequest timeout(int ms) &&;
NetworkRequest concurrent() &&;
NetworkRequest multiPart(QHttpMultiPart *payload) &&;
/**
* This will change `RedirectPolicyAttribute`.
* `QNetworkRequest`'s defaults are used by default (Qt 5: no-follow, Qt 6: follow).
*/
NetworkRequest followRedirects(bool on) &&;
NetworkRequest json(const QJsonObject &root) &&;
NetworkRequest json(const QJsonArray &root) &&;
NetworkRequest json(const QJsonDocument &document) &&;
NetworkRequest json(const QByteArray &payload) &&;
void execute();
private:
void initializeDefaultValues();
};
} // namespace chatterino