Changeset View
Changeset View
Standalone View
Standalone View
src/bugzillaintegration/libbugzilla/connection.h
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright 2019 Harald Sitter <sitter@kde.org> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Lesser General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | later version accepted by the membership of KDE e.V. (or its | ||||
9 | successor approved by the membership of KDE e.V.), which shall | ||||
10 | act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | | ||||
12 | This library is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | Lesser General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU Lesser General Public | ||||
18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #ifndef CONNECTION_H | ||||
22 | #define CONNECTION_H | ||||
23 | | ||||
24 | #include <QObject> | ||||
25 | #include <QUrl> | ||||
26 | | ||||
27 | #include <QNetworkReply> | ||||
28 | | ||||
29 | #include <QJsonDocument> | ||||
30 | | ||||
31 | #include <QJsonObject> | ||||
32 | #include <QJsonArray> | ||||
33 | | ||||
34 | #include <QPointer> | ||||
35 | | ||||
36 | #include <QFuture> | ||||
37 | #include <QUrlQuery> | ||||
38 | #include <KJob> | ||||
39 | | ||||
40 | class QNetworkAccessManager; | ||||
41 | | ||||
42 | typedef QPointer<QNetworkReply> QNReqPtr; | ||||
43 | typedef QPointer<QNetworkReply> QNRepPtr; | ||||
44 | | ||||
45 | namespace KIO { | ||||
46 | class TransferJob; | ||||
47 | } | ||||
48 | | ||||
49 | namespace Bugzilla { | ||||
50 | | ||||
51 | class APIJob; | ||||
52 | | ||||
53 | class Exception : public QException | ||||
54 | { | ||||
55 | public: | ||||
56 | using QException::QException; | ||||
57 | | ||||
58 | virtual QString whatString() const = 0; | ||||
59 | virtual const char *what() const noexcept override; | ||||
60 | }; | ||||
61 | | ||||
62 | class ProtocolException : public Exception | ||||
63 | { | ||||
64 | public: | ||||
65 | ProtocolException(const APIJob *job); | ||||
66 | ProtocolException(const ProtocolException &other); | ||||
67 | | ||||
68 | virtual void raise() const override { throw *this; } | ||||
69 | virtual ProtocolException *clone() const override { return new ProtocolException(*this); } | ||||
70 | virtual QString whatString() const override; | ||||
71 | | ||||
72 | static void maybeThrow(const APIJob *job); | ||||
73 | | ||||
74 | private: | ||||
75 | const APIJob *m_job = nullptr; | ||||
76 | }; | ||||
77 | | ||||
78 | class APIException : public Exception | ||||
79 | { | ||||
80 | public: | ||||
81 | APIException(const QJsonDocument &document); | ||||
82 | APIException(const APIException &other); | ||||
83 | | ||||
84 | virtual void raise() const override { throw *this; } | ||||
85 | virtual APIException *clone() const override { return new APIException(*this); } | ||||
86 | virtual QString whatString() const override; | ||||
87 | | ||||
88 | bool isError() const { return m_isError; } | ||||
89 | | ||||
90 | static void maybeThrow(const QJsonDocument &document); | ||||
91 | | ||||
92 | private: | ||||
93 | bool m_isError = false; | ||||
94 | QString m_message; | ||||
95 | int m_code = -1; | ||||
96 | }; | ||||
97 | | ||||
98 | class APIJob : public KJob | ||||
99 | { | ||||
100 | Q_OBJECT | ||||
101 | public: | ||||
102 | enum { | ||||
103 | UnknownError = KJob::UserDefinedError, | ||||
104 | }; | ||||
105 | | ||||
106 | using KJob::KJob; | ||||
107 | | ||||
108 | QJsonDocument document() const; | ||||
109 | | ||||
110 | // Should be protected but since we call it for testing I don't care. | ||||
111 | virtual QByteArray data() const = 0; | ||||
112 | | ||||
113 | private: | ||||
114 | virtual void start() override {} | ||||
115 | virtual void connectNotify(const QMetaMethod &signal) override; | ||||
116 | }; | ||||
117 | | ||||
118 | class TransferAPIJob : public APIJob | ||||
119 | { | ||||
120 | Q_OBJECT | ||||
121 | friend class HTTPConnection; | ||||
122 | public: | ||||
123 | virtual QByteArray data() const override { return m_data; } | ||||
124 | | ||||
125 | private: | ||||
126 | explicit TransferAPIJob(KIO::TransferJob *transferJob, QObject *parent = nullptr); | ||||
127 | | ||||
128 | void setPutData(const QByteArray &data); | ||||
129 | void addMetaData(const QString &key, const QString &value); | ||||
130 | | ||||
131 | KIO::TransferJob *m_transferJob = nullptr; | ||||
132 | QByteArray m_data; | ||||
133 | QByteArray m_putData; | ||||
134 | QList<QByteArray> m_dataSegments; | ||||
135 | }; | ||||
136 | | ||||
137 | class Connection : public QObject | ||||
138 | { | ||||
139 | Q_OBJECT | ||||
140 | public: | ||||
141 | using QObject::QObject; | ||||
142 | | ||||
143 | virtual void setToken(const QString &authToken) = 0; | ||||
144 | | ||||
145 | virtual APIJob *get(const QString &path, const QUrlQuery &query = QUrlQuery()) const = 0; | ||||
146 | virtual APIJob *post(const QString &path, const QByteArray &data, const QUrlQuery &query = QUrlQuery()) const = 0; | ||||
147 | virtual APIJob *put(const QString &path, const QByteArray &data, const QUrlQuery &query = QUrlQuery()) const = 0; | ||||
148 | }; | ||||
149 | | ||||
150 | class HTTPConnection : public Connection | ||||
151 | { | ||||
152 | Q_OBJECT | ||||
153 | public: | ||||
154 | explicit HTTPConnection(const QUrl &root = QUrl(), QObject *parent = nullptr); | ||||
155 | ~HTTPConnection(); | ||||
156 | | ||||
157 | virtual void setToken(const QString &authToken) override; | ||||
158 | | ||||
159 | virtual APIJob *get(const QString &path, const QUrlQuery &query = QUrlQuery()) const override; | ||||
160 | virtual APIJob *post(const QString &path, const QByteArray &data, const QUrlQuery &query = QUrlQuery()) const override; | ||||
161 | virtual APIJob *put(const QString &path, const QByteArray &data, const QUrlQuery &query = QUrlQuery()) const override; | ||||
162 | | ||||
163 | private: | ||||
164 | QUrl url(const QString &appendix, QUrlQuery query) const; | ||||
165 | | ||||
166 | QUrl m_root; | ||||
167 | QString m_token; | ||||
168 | }; | ||||
169 | | ||||
170 | Connection &connection(); | ||||
171 | void setConnection(Connection *newConnection); | ||||
172 | | ||||
173 | } // namespace Bugzilla | ||||
174 | | ||||
175 | #endif // CONNECTION_H |