File indexing completed on 2024-04-28 16:44:07

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 
0014 #include <KJob>
0015 
0016 namespace KIO
0017 {
0018 class TransferJob;
0019 } // namespace KIO
0020 
0021 namespace Bugzilla
0022 {
0023 class APIJob : public KJob
0024 {
0025     Q_OBJECT
0026 public:
0027     using KJob::KJob;
0028 
0029     /**
0030      * @throws ProtocolException on unexpected HTTP statuses or KIO errors
0031      * @throws APIException when the API returned an error object
0032      * @return json document returned by api request
0033      */
0034     QJsonDocument document() const;
0035 
0036     /**
0037      * This is a convenience function on top of document() and may throw all
0038      * the same exceptions.
0039      * @return json object of document (may be in valid if the doc has none)
0040      */
0041     QJsonObject object() const;
0042 
0043     void setAutoStart(bool start);
0044 
0045 public:
0046     // Should be protected but since we call it for testing I don't care.
0047     virtual QByteArray data() const = 0;
0048 
0049 private:
0050     void start() override
0051     {
0052     }
0053     void connectNotify(const QMetaMethod &signal) override;
0054 
0055     bool m_autostart = true;
0056 };
0057 
0058 class TransferAPIJob : public APIJob
0059 {
0060     Q_OBJECT
0061     friend class HTTPConnection; // constructs us, ctor is private though
0062 public:
0063     QByteArray data() const override
0064     {
0065         return m_data;
0066     }
0067 
0068 private:
0069     explicit TransferAPIJob(KIO::TransferJob *transferJob, QObject *parent = nullptr);
0070 
0071     void setPutData(const QByteArray &data);
0072     void addMetaData(const QString &key, const QString &value);
0073 
0074     KIO::TransferJob *m_transferJob = nullptr;
0075     QByteArray m_data;
0076     QByteArray m_putData;
0077     QList<QByteArray> m_dataSegments;
0078 };
0079 
0080 } // namespace Bugzilla
0081 
0082 #endif // APIJOB_H