/*********************************************************************************************/
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()
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();
}
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
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:
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
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);
};
/**