1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QRESTREPLY_H
6#define QRESTREPLY_H
7
8#include <QtNetwork/qnetworkreply.h>
9
10#include <QtCore/qpointer.h>
11
12#include <optional>
13#include <utility>
14
15QT_BEGIN_NAMESPACE
16
17class QByteArray;
18class QDebug;
19struct QJsonParseError;
20class QJsonDocument;
21class QString;
22
23class QRestReplyPrivate;
24class QRestReply
25{
26public:
27 Q_NETWORK_EXPORT explicit QRestReply(QNetworkReply *reply);
28 Q_NETWORK_EXPORT ~QRestReply();
29
30 QRestReply(QRestReply &&other) noexcept
31 : wrapped(std::move(other.wrapped)),
32 d(std::exchange(obj&: other.d, new_val: nullptr))
33 {
34 }
35 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QRestReply)
36 void swap(QRestReply &other) noexcept
37 {
38 wrapped.swap(other&: other.wrapped);
39 qt_ptr_swap(lhs&: d, rhs&: other.d);
40 }
41
42 Q_NETWORK_EXPORT QNetworkReply *networkReply() const;
43
44 Q_NETWORK_EXPORT std::optional<QJsonDocument> readJson(QJsonParseError *error = nullptr);
45 Q_NETWORK_EXPORT QByteArray readBody();
46 Q_NETWORK_EXPORT QString readText();
47
48 bool isSuccess() const
49 {
50 return !hasError() && isHttpStatusSuccess();
51 }
52 Q_NETWORK_EXPORT int httpStatus() const;
53 Q_NETWORK_EXPORT bool isHttpStatusSuccess() const;
54
55 Q_NETWORK_EXPORT bool hasError() const;
56 Q_NETWORK_EXPORT QNetworkReply::NetworkError error() const;
57 Q_NETWORK_EXPORT QString errorString() const;
58
59private:
60#ifndef QT_NO_DEBUG_STREAM
61 friend Q_NETWORK_EXPORT QDebug operator<<(QDebug debug, const QRestReply &reply);
62#endif
63 QPointer<QNetworkReply> wrapped;
64 QRestReplyPrivate *d = nullptr;
65 Q_DISABLE_COPY(QRestReply)
66};
67
68Q_DECLARE_SHARED(QRestReply)
69
70QT_END_NAMESPACE
71
72#endif // QRESTREPLY_H
73

source code of qtbase/src/network/access/qrestreply.h