mirror-chatterino2/src/common/NetworkResult.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

55 lines
1.4 KiB
C++
Raw Normal View History

#pragma once
#include <QJsonArray>
#include <QJsonObject>
2023-07-01 14:59:59 +02:00
#include <QNetworkReply>
#include <rapidjson/document.h>
2023-07-01 14:59:59 +02:00
#include <optional>
namespace chatterino {
class NetworkResult
{
public:
2023-07-01 14:59:59 +02:00
using NetworkError = QNetworkReply::NetworkError;
NetworkResult(NetworkError error, const QVariant &httpStatusCode,
QByteArray data);
2019-09-03 23:32:22 +02:00
/// Parses the result as json and returns the root as an object.
/// Returns empty object if parsing failed.
QJsonObject parseJson() const;
2019-09-03 23:32:22 +02:00
/// Parses the result as json and returns the root as an array.
/// Returns empty object if parsing failed.
QJsonArray parseJsonArray() const;
/// Parses the result as json and returns the document.
rapidjson::Document parseRapidJson() const;
2018-08-02 14:23:27 +02:00
const QByteArray &getData() const;
2023-07-01 14:59:59 +02:00
/// The error code of the reply.
/// In case of a successful reply, this will be NoError (0)
NetworkError error() const
{
return this->error_;
}
/// The HTTP status code if a response was received.
std::optional<int> status() const
{
return this->status_;
}
/// Formats the error.
/// If a reply is received, returns the HTTP status otherwise, the network error.
QString formatError() const;
2018-08-02 14:23:27 +02:00
private:
QByteArray data_;
2023-07-01 14:59:59 +02:00
NetworkError error_;
std::optional<int> status_;
};
} // namespace chatterino