makes JsonApiJob share common code with SimpleApiJob
authorMatthieu Gallien <matthieu.gallien@nextcloud.com>
Thu, 13 Oct 2022 10:14:22 +0000 (12:14 +0200)
committerMatthieu Gallien <matthieu.gallien@nextcloud.com>
Mon, 17 Oct 2022 07:01:36 +0000 (09:01 +0200)
Signed-off-by: Matthieu Gallien <matthieu.gallien@nextcloud.com>
src/libsync/networkjobs.cpp
src/libsync/networkjobs.h

index 81d43ffb343a871089caf6313289766fce72ff73..a2a17567f385ceead27f5d295db0340640bfcf08 100644 (file)
@@ -823,64 +823,23 @@ bool EntityExistsJob::finished()
 /*********************************************************************************************/
 
 JsonApiJob::JsonApiJob(const AccountPtr &account, const QString &path, QObject *parent)
-    : AbstractNetworkJob(account, path, parent)
-{
-}
-
-void JsonApiJob::addQueryParams(const QUrlQuery &params)
-{
-    _additionalParams = params;
-}
-
-void JsonApiJob::addRawHeader(const QByteArray &headerName, const QByteArray &value)
+    : SimpleApiJob(account, path, parent)
 {
-   _request.setRawHeader(headerName, value);
 }
 
 void JsonApiJob::setBody(const QJsonDocument &body)
 {
-    _body = body.toJson();
-    qCDebug(lcJsonApiJob) << "Set body for request:" << _body;
-    if (!_body.isEmpty()) {
-        _request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
-    }
-}
-
-
-void JsonApiJob::setVerb(Verb value)
-{
-    _verb = value;
-}
-
-
-QByteArray JsonApiJob::verbToString() const
-{
-    switch (_verb) {
-    case Verb::Get:
-        return "GET";
-    case Verb::Post:
-        return "POST";
-    case Verb::Put:
-        return "PUT";
-    case Verb::Delete:
-        return "DELETE";
+    SimpleApiJob::setBody(body.toJson());
+    qCDebug(lcJsonApiJob) << "Set body for request:" << SimpleApiJob::body();
+    if (!SimpleApiJob::body().isEmpty()) {
+        request().setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
     }
-    return "GET";
 }
 
 void JsonApiJob::start()
 {
-    addRawHeader("OCS-APIREQUEST", "true");
-    auto query = _additionalParams;
-    query.addQueryItem(QLatin1String("format"), QLatin1String("json"));
-    QUrl url = Utility::concatUrlPath(account()->url(), path(), query);
-    const auto httpVerb = verbToString();
-    if (!_body.isEmpty()) {
-        sendRequest(httpVerb, url, _request, _body);
-    } else {
-        sendRequest(httpVerb, url, _request);
-    }
-    AbstractNetworkJob::start();
+    additionalParams().addQueryItem(QLatin1String("format"), QLatin1String("json"));
+    SimpleApiJob::start();
 }
 
 bool JsonApiJob::finished()
@@ -1219,15 +1178,14 @@ QByteArray SimpleApiJob::verbToString() const
 
 void SimpleApiJob::start()
 {
-    qCDebug(lcSimpleApiJob) << "send: " << path() << _body;
-
-    _request.setRawHeader("OCS-APIREQUEST", "true");
-    const auto url = Utility::concatUrlPath(account()->url(), path());
+    addRawHeader("OCS-APIREQUEST", "true");
+    auto query = _additionalParams;
+    QUrl url = Utility::concatUrlPath(account()->url(), path(), query);
     const auto httpVerb = verbToString();
-    if (!_body.isEmpty()) {
-        sendRequest(httpVerb, url, _request, _body);
+    if (!SimpleApiJob::body().isEmpty()) {
+        sendRequest(httpVerb, url, request(), SimpleApiJob::body());
     } else {
-        sendRequest(httpVerb, url, _request);
+        sendRequest(httpVerb, url, request());
     }
     AbstractNetworkJob::start();
 }
@@ -1240,4 +1198,29 @@ bool SimpleApiJob::finished()
     return true;
 }
 
+QNetworkRequest& SimpleApiJob::request()
+{
+    return _request;
+}
+
+QByteArray& SimpleApiJob::body()
+{
+    return _body;
+}
+
+QUrlQuery &SimpleApiJob::additionalParams()
+{
+    return _additionalParams;
+}
+
+void SimpleApiJob::addQueryParams(const QUrlQuery &params)
+{
+    _additionalParams = params;
+}
+
+void SimpleApiJob::addRawHeader(const QByteArray &headerName, const QByteArray &value)
+{
+    request().setRawHeader(headerName, value);
+}
+
 } // namespace OCC
index c89368c45c3d430bd96f36a5dfbf5ab14e10a735..1416e69315ff080f7df71cdd2ece2c1456563172 100644 (file)
@@ -382,22 +382,7 @@ private slots:
     bool finished() override;
 };
 
-/**
- * @brief Job to check an API that return JSON
- *
- * Note! you need to be in the connected state before calling this because of a server bug:
- * https://github.com/owncloud/core/issues/12930
- *
- * To be used like this:
- * \code
- * _job = new JsonApiJob(account, QLatin1String("ocs/v1.php/foo/bar"), this);
- * connect(job, SIGNAL(jsonReceived(QJsonDocument)), ...)
- * The received QVariantMap is null in case of error
- * \encode
- *
- * @ingroup libsync
- */
-class OWNCLOUDSYNC_EXPORT JsonApiJob : public AbstractNetworkJob
+class OWNCLOUDSYNC_EXPORT SimpleApiJob : public AbstractNetworkJob
 {
     Q_OBJECT
 public:
@@ -406,9 +391,13 @@ public:
         Post,
         Put,
         Delete,
-    };
+        };
 
-    explicit JsonApiJob(const AccountPtr &account, const QString &path, QObject *parent = nullptr);
+    explicit SimpleApiJob(const AccountPtr &account, const QString &path, QObject *parent = nullptr);
+
+    void setBody(const QByteArray &body);
+
+    void setVerb(Verb value);
 
     /**
      * @brief addQueryParams - add more parameters to the ocs call
@@ -423,76 +412,72 @@ public:
     void addQueryParams(const QUrlQuery &params);
     void addRawHeader(const QByteArray &headerName, const QByteArray &value);
 
-    void setBody(const QJsonDocument &body);
-
-    void setVerb(Verb value);
-
 public slots:
     void start() override;
 
+Q_SIGNALS:
+
+    void resultReceived(int statusCode);
+
 protected:
     bool finished() override;
-signals:
-
-    /**
-     * @brief jsonReceived - signal to report the json answer from ocs
-     * @param json - the parsed json document
-     * @param statusCode - the OCS status code: 100 (!) for success
-     */
-    void jsonReceived(const QJsonDocument &json, int statusCode);
 
-    /**
-     * @brief etagResponseHeaderReceived - signal to report the ETag response header value
-     * from ocs api v2
-     * @param value - the ETag response header value
-     * @param statusCode - the OCS status code: 100 (!) for success
-     */
-    void etagResponseHeaderReceived(const QByteArray &value, int statusCode);
+    [[nodiscard]] QNetworkRequest& request();
+    [[nodiscard]] QByteArray& body();
+    [[nodiscard]] QUrlQuery& additionalParams();
+    [[nodiscard]] QByteArray verbToString() const;
 
 private:
     QByteArray _body;
     QUrlQuery _additionalParams;
     QNetworkRequest _request;
-
     Verb _verb = Verb::Get;
-
-    [[nodiscard]] QByteArray verbToString() const;
 };
 
-class OWNCLOUDSYNC_EXPORT SimpleApiJob : public AbstractNetworkJob
+/**
+ * @brief Job to check an API that return JSON
+ *
+ * Note! you need to be in the connected state before calling this because of a server bug:
+ * https://github.com/owncloud/core/issues/12930
+ *
+ * To be used like this:
+ * \code
+ * _job = new JsonApiJob(account, QLatin1String("ocs/v1.php/foo/bar"), this);
+ * connect(job, SIGNAL(jsonReceived(QJsonDocument)), ...)
+ * The received QVariantMap is null in case of error
+ * \encode
+ *
+ * @ingroup libsync
+ */
+class OWNCLOUDSYNC_EXPORT JsonApiJob : public SimpleApiJob
 {
     Q_OBJECT
 public:
-    enum class Verb {
-        Get,
-        Post,
-        Put,
-        Delete,
-        };
-
-    explicit SimpleApiJob(const AccountPtr &account, const QString &path, QObject *parent = nullptr);
-
-    void setBody(const QByteArray &body);
+    explicit JsonApiJob(const AccountPtr &account, const QString &path, QObject *parent = nullptr);
 
-    void setVerb(Verb value);
+    void setBody(const QJsonDocument &body);
 
 public slots:
     void start() override;
 
-Q_SIGNALS:
-
-    void resultReceived(int statusCode);
-
 protected:
     bool finished() override;
+signals:
 
-private:
-    QByteArray _body;
-    QNetworkRequest _request;
-
-    Verb _verb = Verb::Get;
+    /**
+     * @brief jsonReceived - signal to report the json answer from ocs
+     * @param json - the parsed json document
+     * @param statusCode - the OCS status code: 100 (!) for success
+     */
+    void jsonReceived(const QJsonDocument &json, int statusCode);
 
-    [[nodiscard]] QByteArray verbToString() const;
+    /**
+     * @brief etagResponseHeaderReceived - signal to report the ETag response header value
+     * from ocs api v2
+     * @param value - the ETag response header value
+     * @param statusCode - the OCS status code: 100 (!) for success
+     */
+    void etagResponseHeaderReceived(const QByteArray &value, int statusCode);
 };
 
 /**