From: Matthieu Gallien Date: Thu, 13 Oct 2022 10:14:22 +0000 (+0200) Subject: makes JsonApiJob share common code with SimpleApiJob X-Git-Tag: archive/raspbian/3.16.7-1_deb13u1+rpi1~1^2~12^2~11^2~204^2~1 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=615c02e3d3ee5daa62f8780e43d28bb631b55df8;p=nextcloud-desktop.git makes JsonApiJob share common code with SimpleApiJob Signed-off-by: Matthieu Gallien --- diff --git a/src/libsync/networkjobs.cpp b/src/libsync/networkjobs.cpp index 81d43ffb3..a2a17567f 100644 --- a/src/libsync/networkjobs.cpp +++ b/src/libsync/networkjobs.cpp @@ -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 ¶ms) -{ - _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 ¶ms) +{ + _additionalParams = params; +} + +void SimpleApiJob::addRawHeader(const QByteArray &headerName, const QByteArray &value) +{ + request().setRawHeader(headerName, value); +} + } // namespace OCC diff --git a/src/libsync/networkjobs.h b/src/libsync/networkjobs.h index c89368c45..1416e6931 100644 --- a/src/libsync/networkjobs.h +++ b/src/libsync/networkjobs.h @@ -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 ¶ms); 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); }; /**