summaryrefslogtreecommitdiffstats
path: root/src/network/access/qrestreply.h
blob: c32fff1d4e8632314843e7c11bbd243e37dfda7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QRESTREPLY_H
#define QRESTREPLY_H

#include <QtNetwork/qnetworkreply.h>

#include <QtCore/qpointer.h>

#include <optional>
#include <utility>

QT_BEGIN_NAMESPACE

class QByteArray;
class QDebug;
struct QJsonParseError;
class QJsonDocument;
class QString;

class QRestReplyPrivate;
class QT_TECH_PREVIEW_API QRestReply
{
public:
    Q_NETWORK_EXPORT explicit QRestReply(QNetworkReply *reply);
    Q_NETWORK_EXPORT ~QRestReply();

    QRestReply(QRestReply &&other) noexcept
        : wrapped(std::move(other.wrapped)),
          d(std::exchange(other.d, nullptr))
    {
    }
    QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QRestReply)
    void swap(QRestReply &other) noexcept
    {
        wrapped.swap(other.wrapped);
        qt_ptr_swap(d, other.d);
    }

    Q_NETWORK_EXPORT QNetworkReply *networkReply() const;

    Q_NETWORK_EXPORT std::optional<QJsonDocument> readJson(QJsonParseError *error = nullptr);
    Q_NETWORK_EXPORT QByteArray readBody();
    Q_NETWORK_EXPORT QString readText();

    bool isSuccess() const
    {
        return !hasError() && isHttpStatusSuccess();
    }
    Q_NETWORK_EXPORT int httpStatus() const;
    Q_NETWORK_EXPORT bool isHttpStatusSuccess() const;

    Q_NETWORK_EXPORT bool hasError() const;
    Q_NETWORK_EXPORT QNetworkReply::NetworkError error() const;
    Q_NETWORK_EXPORT QString errorString() const;

private:
#ifndef QT_NO_DEBUG_STREAM
    friend Q_NETWORK_EXPORT QDebug operator<<(QDebug debug, const QRestReply &reply);
#endif
    QPointer<QNetworkReply> wrapped;
    QRestReplyPrivate *d = nullptr;
    Q_DISABLE_COPY(QRestReply)
};

Q_DECLARE_SHARED(QRestReply)

QT_END_NAMESPACE

#endif // QRESTREPLY_H