File indexing completed on 2024-05-12 05:29:09

0001 /*
0002     SPDX-FileCopyrightText: 2019 Harald Sitter <sitter@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #ifndef APIJOB_H
0008 #define APIJOB_H
0009 
0010 #include <QJsonArray> // Not used here but included so clients don't have to
0011 #include <QJsonDocument>
0012 #include <QJsonObject>
0013 #include <QNetworkAccessManager>
0014 
0015 #include <KJob>
0016 
0017 class QNetworkRequest;
0018 class QNetworkReply;
0019 
0020 namespace KIO
0021 {
0022 class TransferJob;
0023 } // namespace KIO
0024 
0025 namespace Bugzilla
0026 {
0027 class APIJob : public KJob
0028 {
0029     Q_OBJECT
0030 public:
0031     using KJob::KJob;
0032 
0033     /**
0034      * @throws ProtocolException on unexpected HTTP statuses or KIO errors
0035      * @throws APIException when the API returned an error object
0036      * @return json document returned by api request
0037      */
0038     QJsonDocument document() const;
0039 
0040     /**
0041      * This is a convenience function on top of document() and may throw all
0042      * the same exceptions.
0043      * @return json object of document (may be in valid if the doc has none)
0044      */
0045     QJsonObject object() const;
0046 
0047     void setAutoStart(bool start);
0048 
0049 public:
0050     // Should be protected but since we call it for testing I don't care.
0051     virtual QByteArray data() const = 0;
0052 
0053 private:
0054     void start() override
0055     {
0056     }
0057     void connectNotify(const QMetaMethod &signal) override;
0058 
0059     bool m_autostart = true;
0060 };
0061 
0062 class NetworkAPIJob : public APIJob
0063 {
0064     Q_OBJECT
0065     friend class HTTPConnection; // constructs us, ctor is private though
0066 public:
0067     QByteArray data() const override
0068     {
0069         return m_data;
0070     }
0071 
0072 private:
0073     explicit NetworkAPIJob(const QUrl &url,
0074                            const std::function<QNetworkReply *(QNetworkAccessManager &, QNetworkRequest &)> &starter,
0075                            QObject *parent = nullptr);
0076 
0077     QByteArray m_data;
0078     QByteArray m_putData;
0079     QList<QByteArray> m_dataSegments;
0080 
0081     QNetworkAccessManager m_manager;
0082 };
0083 
0084 } // namespace Bugzilla
0085 
0086 #endif // APIJOB_H