Changeset View
Changeset View
Standalone View
Standalone View
src/bugzillaintegration/libbugzilla/apijob.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 APIJOB_H | ||||
22 | #define APIJOB_H | ||||
23 | | ||||
24 | #include <QJsonArray> // Not used here but included so clients don't have to | ||||
25 | #include <QJsonDocument> | ||||
26 | #include <QJsonObject> | ||||
27 | | ||||
28 | #include <KJob> | ||||
29 | | ||||
30 | namespace KIO { | ||||
31 | class TransferJob; | ||||
32 | } | ||||
33 | | ||||
34 | namespace Bugzilla { | ||||
35 | | ||||
36 | class APIJob : public KJob | ||||
37 | { | ||||
38 | Q_OBJECT | ||||
39 | public: | ||||
40 | using KJob::KJob; | ||||
41 | | ||||
42 | /** | ||||
43 | * @throws ProtocolException on unexpected HTTP statuses or KIO errors | ||||
44 | * @throws APIException when the API returned an error object | ||||
45 | * @return json document returned by api request | ||||
46 | */ | ||||
47 | QJsonDocument document() const; | ||||
48 | | ||||
49 | /** | ||||
50 | * This is a convenience function on top of document() and may throw all | ||||
51 | * the same exceptions. | ||||
52 | * @return json object of document (may be in valid if the doc has none) | ||||
53 | */ | ||||
54 | QJsonObject object() const; | ||||
55 | | ||||
56 | void setAutoStart(bool start); | ||||
57 | | ||||
58 | public: | ||||
59 | // Should be protected but since we call it for testing I don't care. | ||||
60 | virtual QByteArray data() const = 0; | ||||
61 | | ||||
62 | private: | ||||
63 | virtual void start() override {} | ||||
64 | virtual void connectNotify(const QMetaMethod &signal) override; | ||||
65 | | ||||
66 | bool m_autostart = true; | ||||
67 | }; | ||||
68 | | ||||
69 | class TransferAPIJob : public APIJob | ||||
70 | { | ||||
71 | Q_OBJECT | ||||
72 | friend class HTTPConnection; // constructs us, ctor is private though | ||||
73 | public: | ||||
74 | virtual QByteArray data() const override { return m_data; } | ||||
75 | | ||||
76 | private: | ||||
77 | explicit TransferAPIJob(KIO::TransferJob *transferJob, QObject *parent = nullptr); | ||||
78 | | ||||
79 | void setPutData(const QByteArray &data); | ||||
80 | void addMetaData(const QString &key, const QString &value); | ||||
81 | | ||||
82 | KIO::TransferJob *m_transferJob = nullptr; | ||||
83 | QByteArray m_data; | ||||
84 | QByteArray m_putData; | ||||
85 | QList<QByteArray> m_dataSegments; | ||||
86 | }; | ||||
87 | | ||||
88 | } // namespace Bugzilla | ||||
89 | | ||||
90 | #endif // APIJOB_H |