diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2016-10-13 09:07:15 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2016-10-15 14:50:53 +0900 |
commit | 4943cbea505aacdd33bf93c3365588ba73785d4d (patch) | |
tree | 35cd89970af3ecbb953a8347117094999cd5f46d /jobs/basejob.h | |
parent | 6536124821c1df791a69a58de21a71322d9363f6 (diff) | |
download | libquotient-4943cbea505aacdd33bf93c3365588ba73785d4d.tar.gz libquotient-4943cbea505aacdd33bf93c3365588ba73785d4d.zip |
BaseJob: Use saved parameters instead of overriding apiPath(), query() and data() in each job class
Diffstat (limited to 'jobs/basejob.h')
-rw-r--r-- | jobs/basejob.h | 53 |
1 files changed, 45 insertions, 8 deletions
diff --git a/jobs/basejob.h b/jobs/basejob.h index 07b1f1dd..f7e4b948 100644 --- a/jobs/basejob.h +++ b/jobs/basejob.h @@ -32,8 +32,8 @@ namespace QMatrixClient { class ConnectionData; - enum class JobHttpType { GetJob, PutJob, PostJob }; - + enum class JobHttpType { GetJob, PutJob, PostJob, DeleteJob }; + class BaseJob: public QObject { Q_OBJECT @@ -51,6 +51,42 @@ namespace QMatrixClient }; /** + * A simple wrapper around QUrlQuery that allows its creation from + * a list of string pairs + */ + class Query : public QUrlQuery + { + public: + using QUrlQuery::QUrlQuery; + Query() = default; + Query(const QList< QPair<QString, QString> >& l) + { + setQueryItems(l); + } + }; + /** + * 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. + */ + class Data : public QJsonObject + { + public: + using QJsonObject::QJsonObject; + Data() = default; + Data(const QList< QPair<QString, QString> >& l) + { + for (auto i: l) + insert(i.first, i.second); + } + QByteArray serialize() const + { + return QJsonDocument(*this).toJson(); + } + }; + + /** * This structure stores the status of a server call job. The status consists * of a code, that is described (but not delimited) by the respective enum, * and a freeform message. @@ -72,8 +108,9 @@ namespace QMatrixClient }; public: - BaseJob(ConnectionData* connection, JobHttpType type, - QString name, bool needsToken=true); + BaseJob(ConnectionData* connection, JobHttpType type, QString name, + QString endpoint, Query query = Query(), Data data = Data(), + bool needsToken = true); virtual ~BaseJob(); void start(); @@ -138,10 +175,10 @@ namespace QMatrixClient protected: ConnectionData* connection() const; - // to implement - virtual QString apiPath() const = 0; - virtual QUrlQuery query() const; - virtual QJsonObject data() const; + const QUrlQuery& query() const; + void setRequestQuery(const QUrlQuery& query); + const Data& requestData() const; + void setRequestData(const Data& data); /** * Used by gotReply() slot to check the received reply for general |