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