aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2017-12-06 21:21:27 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2017-12-07 11:45:17 +0900
commite74de57832f684244d7da82fa743b63d459bf488 (patch)
tree363074591379ac1aa80af939a66b44c0ad2bff32
parent21952d689db64bfb11e1cd11a7da802b649f9ae8 (diff)
downloadlibquotient-e74de57832f684244d7da82fa743b63d459bf488.tar.gz
libquotient-e74de57832f684244d7da82fa743b63d459bf488.zip
Make BaseJob::Data consume QByteArray as well, not only QJsonObject
This is needed to support cases of content-repo, where the request/response bodies are not JSON.
-rw-r--r--jobs/basejob.h33
-rw-r--r--jobs/generated/banning.cpp4
-rw-r--r--jobs/generated/inviting.cpp2
-rw-r--r--jobs/generated/kicking.cpp2
-rw-r--r--jobs/generated/login.cpp2
-rw-r--r--jobs/generated/profile.cpp4
6 files changed, 25 insertions, 22 deletions
diff --git a/jobs/basejob.h b/jobs/basejob.h
index f8b367c6..66812774 100644
--- a/jobs/basejob.h
+++ b/jobs/basejob.h
@@ -23,6 +23,7 @@
#include <QtCore/QObject>
#include <QtCore/QJsonDocument>
#include <QtCore/QJsonObject>
+#include <QtCore/QJsonArray>
#include <QtCore/QUrlQuery>
#include <QtCore/QScopedPointer>
@@ -69,29 +70,31 @@ namespace QMatrixClient
}
};
/**
- * A simple wrapper around QJsonObject that represents a JSON data
- * section of an HTTP request to a Matrix server. Facilitates
- * creation from a list of key-value string pairs and dumping of
- * a resulting JSON to a QByteArray.
+ * A simple wrapper that represents the request body.
+ * Provides a unified interface to dump an unstructured byte stream
+ * as well as JSON (and possibly other structures in the future) to
+ * a QByteArray consumed by QNetworkAccessManager request methods.
*/
- class Data : public QJsonObject
+ class Data
{
public:
- using QJsonObject::QJsonObject;
Data() = default;
- explicit Data(const QJsonObject& o) : QJsonObject(o) { }
-#if (QT_VERSION < QT_VERSION_CHECK(5, 4, 0))
- // This method exists in QJsonObject of newer Qt versions
- Data(const std::initializer_list< QPair<QString, QJsonValue> >& l)
+ Data(const QByteArray& a) : _payload(a) { }
+ Data(const QJsonObject& jo)
+ : _payload(fromJson(QJsonDocument(jo))) { }
+ Data(const QJsonArray& ja)
+ : _payload(fromJson(QJsonDocument(ja))) { }
+ QByteArray serialize() const
{
- for (auto i: l)
- insert(i.first, i.second);
+ return _payload;
}
-#endif
- QByteArray serialize() const
+
+ private:
+ static QByteArray fromJson(const QJsonDocument& jd)
{
- return QJsonDocument(*this).toJson();
+ return jd.toJson(QJsonDocument::Compact);
}
+ QByteArray _payload;
};
/**
diff --git a/jobs/generated/banning.cpp b/jobs/generated/banning.cpp
index 61677da6..96f80ea8 100644
--- a/jobs/generated/banning.cpp
+++ b/jobs/generated/banning.cpp
@@ -19,7 +19,7 @@ BanJob::BanJob(const QString& roomId, const QString& userId, const QString& reas
Query { }
)
{
- Data _data;
+ QJsonObject _data;
_data.insert("user_id", toJson(userId));
if (!reason.isEmpty())
_data.insert("reason", toJson(reason));
@@ -32,7 +32,7 @@ UnbanJob::UnbanJob(const QString& roomId, const QString& userId)
Query { }
)
{
- Data _data;
+ QJsonObject _data;
_data.insert("user_id", toJson(userId));
setRequestData(_data);
}
diff --git a/jobs/generated/inviting.cpp b/jobs/generated/inviting.cpp
index 78c9a2f6..5f89adf7 100644
--- a/jobs/generated/inviting.cpp
+++ b/jobs/generated/inviting.cpp
@@ -19,7 +19,7 @@ InviteUserJob::InviteUserJob(const QString& roomId, const QString& userId)
Query { }
)
{
- Data _data;
+ QJsonObject _data;
_data.insert("user_id", toJson(userId));
setRequestData(_data);
}
diff --git a/jobs/generated/kicking.cpp b/jobs/generated/kicking.cpp
index 5d6f1a64..86dde629 100644
--- a/jobs/generated/kicking.cpp
+++ b/jobs/generated/kicking.cpp
@@ -19,7 +19,7 @@ KickJob::KickJob(const QString& roomId, const QString& userId, const QString& re
Query { }
)
{
- Data _data;
+ QJsonObject _data;
_data.insert("user_id", toJson(userId));
if (!reason.isEmpty())
_data.insert("reason", toJson(reason));
diff --git a/jobs/generated/login.cpp b/jobs/generated/login.cpp
index 892d0c81..4c159517 100644
--- a/jobs/generated/login.cpp
+++ b/jobs/generated/login.cpp
@@ -28,7 +28,7 @@ LoginJob::LoginJob(const QString& type, const QString& user, const QString& medi
Query { }, Data { }, false
), d(new Private)
{
- Data _data;
+ QJsonObject _data;
_data.insert("type", toJson(type));
if (!user.isEmpty())
_data.insert("user", toJson(user));
diff --git a/jobs/generated/profile.cpp b/jobs/generated/profile.cpp
index 42a2350f..6ec566f7 100644
--- a/jobs/generated/profile.cpp
+++ b/jobs/generated/profile.cpp
@@ -19,7 +19,7 @@ SetDisplayNameJob::SetDisplayNameJob(const QString& userId, const QString& displ
Query { }
)
{
- Data _data;
+ QJsonObject _data;
if (!displayname.isEmpty())
_data.insert("displayname", toJson(displayname));
setRequestData(_data);
@@ -61,7 +61,7 @@ SetAvatarUrlJob::SetAvatarUrlJob(const QString& userId, const QString& avatarUrl
Query { }
)
{
- Data _data;
+ QJsonObject _data;
if (!avatarUrl.isEmpty())
_data.insert("avatar_url", toJson(avatarUrl));
setRequestData(_data);