From b28e2d697842f58e2994ead62635b31f48162cdc Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 8 Jun 2018 17:03:41 +0900 Subject: csapi: add jobs for device management Same as for account management jobs, auth objects are modeled as generic QJsonObjects for now. --- lib/csapi/definitions/client_device.cpp | 34 ++++++++++ lib/csapi/definitions/client_device.h | 31 +++++++++ lib/csapi/device_management.cpp | 111 ++++++++++++++++++++++++++++++++ lib/csapi/device_management.h | 87 +++++++++++++++++++++++++ 4 files changed, 263 insertions(+) create mode 100644 lib/csapi/definitions/client_device.cpp create mode 100644 lib/csapi/definitions/client_device.h create mode 100644 lib/csapi/device_management.cpp create mode 100644 lib/csapi/device_management.h diff --git a/lib/csapi/definitions/client_device.cpp b/lib/csapi/definitions/client_device.cpp new file mode 100644 index 00000000..60b3617c --- /dev/null +++ b/lib/csapi/definitions/client_device.cpp @@ -0,0 +1,34 @@ +/****************************************************************************** + * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN + */ + +#include "client_device.h" + +using namespace QMatrixClient; + +QJsonObject QMatrixClient::toJson(const Device& pod) +{ + QJsonObject _json; + addToJson<>(_json, "device_id", pod.deviceId); + addToJson(_json, "display_name", pod.displayName); + addToJson(_json, "last_seen_ip", pod.lastSeenIp); + addToJson(_json, "last_seen_ts", pod.lastSeenTs); + return _json; +} + +Device FromJson::operator()(const QJsonValue& jv) +{ + const auto& _json = jv.toObject(); + Device result; + result.deviceId = + fromJson(_json.value("device_id")); + result.displayName = + fromJson(_json.value("display_name")); + result.lastSeenIp = + fromJson(_json.value("last_seen_ip")); + result.lastSeenTs = + fromJson(_json.value("last_seen_ts")); + + return result; +} + diff --git a/lib/csapi/definitions/client_device.h b/lib/csapi/definitions/client_device.h new file mode 100644 index 00000000..4c09e13c --- /dev/null +++ b/lib/csapi/definitions/client_device.h @@ -0,0 +1,31 @@ +/****************************************************************************** + * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN + */ + +#pragma once + + +#include "converters.h" + +#include "converters.h" + +namespace QMatrixClient +{ + // Data structures + + struct Device + { + QString deviceId; + QString displayName; + QString lastSeenIp; + Omittable lastSeenTs; + }; + + QJsonObject toJson(const Device& pod); + + template <> struct FromJson + { + Device operator()(const QJsonValue& jv); + }; + +} // namespace QMatrixClient diff --git a/lib/csapi/device_management.cpp b/lib/csapi/device_management.cpp new file mode 100644 index 00000000..2248a0b4 --- /dev/null +++ b/lib/csapi/device_management.cpp @@ -0,0 +1,111 @@ +/****************************************************************************** + * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN + */ + +#include "device_management.h" + +#include "converters.h" + +#include + +using namespace QMatrixClient; + +static const auto basePath = QStringLiteral("/_matrix/client/r0"); + +class GetDevicesJob::Private +{ + public: + QVector devices; +}; + +QUrl GetDevicesJob::makeRequestUrl(QUrl baseUrl) +{ + return BaseJob::makeRequestUrl(std::move(baseUrl), + basePath % "/devices"); +} + +GetDevicesJob::GetDevicesJob() + : BaseJob(HttpVerb::Get, "GetDevicesJob", + basePath % "/devices") + , d(new Private) +{ +} + +GetDevicesJob::~GetDevicesJob() = default; + +const QVector& GetDevicesJob::devices() const +{ + return d->devices; +} + +BaseJob::Status GetDevicesJob::parseJson(const QJsonDocument& data) +{ + auto json = data.object(); + d->devices = fromJson>(json.value("devices")); + return Success; +} + +class GetDeviceJob::Private +{ + public: + Device data; +}; + +QUrl GetDeviceJob::makeRequestUrl(QUrl baseUrl, const QString& deviceId) +{ + return BaseJob::makeRequestUrl(std::move(baseUrl), + basePath % "/devices/" % deviceId); +} + +GetDeviceJob::GetDeviceJob(const QString& deviceId) + : BaseJob(HttpVerb::Get, "GetDeviceJob", + basePath % "/devices/" % deviceId) + , d(new Private) +{ +} + +GetDeviceJob::~GetDeviceJob() = default; + +const Device& GetDeviceJob::data() const +{ + return d->data; +} + +BaseJob::Status GetDeviceJob::parseJson(const QJsonDocument& data) +{ + auto json = data.object(); + if (!json.contains("data")) + return { JsonParseError, + "The key 'data' not found in the response" }; + d->data = fromJson(json.value("data")); + return Success; +} + +UpdateDeviceJob::UpdateDeviceJob(const QString& deviceId, const QString& displayName) + : BaseJob(HttpVerb::Put, "UpdateDeviceJob", + basePath % "/devices/" % deviceId) +{ + QJsonObject _data; + addToJson(_data, "display_name", displayName); + setRequestData(_data); +} + +DeleteDeviceJob::DeleteDeviceJob(const QString& deviceId, const QJsonObject& auth) + : BaseJob(HttpVerb::Delete, "DeleteDeviceJob", + basePath % "/devices/" % deviceId) +{ + QJsonObject _data; + addToJson(_data, "auth", auth); + setRequestData(_data); +} + +DeleteDevicesJob::DeleteDevicesJob(const QStringList& devices, const QJsonObject& auth) + : BaseJob(HttpVerb::Post, "DeleteDevicesJob", + basePath % "/delete_devices") +{ + QJsonObject _data; + addToJson<>(_data, "devices", devices); + addToJson(_data, "auth", auth); + setRequestData(_data); +} + diff --git a/lib/csapi/device_management.h b/lib/csapi/device_management.h new file mode 100644 index 00000000..f9213d22 --- /dev/null +++ b/lib/csapi/device_management.h @@ -0,0 +1,87 @@ +/****************************************************************************** + * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN + */ + +#pragma once + +#include "jobs/basejob.h" + +#include +#include +#include "converters.h" +#include "csapi/definitions/client_device.h" + +namespace QMatrixClient +{ + // Operations + + class GetDevicesJob : public BaseJob + { + public: + explicit GetDevicesJob(); + + /** Construct a URL out of baseUrl and usual parameters passed to + * GetDevicesJob. This function can be used when + * a URL for GetDevicesJob is necessary but the job + * itself isn't. + */ + static QUrl makeRequestUrl(QUrl baseUrl); + + ~GetDevicesJob() override; + + // Result properties + + const QVector& devices() const; + + protected: + Status parseJson(const QJsonDocument& data) override; + + private: + class Private; + QScopedPointer d; + }; + + class GetDeviceJob : public BaseJob + { + public: + explicit GetDeviceJob(const QString& deviceId); + + /** Construct a URL out of baseUrl and usual parameters passed to + * GetDeviceJob. This function can be used when + * a URL for GetDeviceJob is necessary but the job + * itself isn't. + */ + static QUrl makeRequestUrl(QUrl baseUrl, const QString& deviceId); + + ~GetDeviceJob() override; + + // Result properties + + const Device& data() const; + + protected: + Status parseJson(const QJsonDocument& data) override; + + private: + class Private; + QScopedPointer d; + }; + + class UpdateDeviceJob : public BaseJob + { + public: + explicit UpdateDeviceJob(const QString& deviceId, const QString& displayName = {}); + }; + + class DeleteDeviceJob : public BaseJob + { + public: + explicit DeleteDeviceJob(const QString& deviceId, const QJsonObject& auth = {}); + }; + + class DeleteDevicesJob : public BaseJob + { + public: + explicit DeleteDevicesJob(const QStringList& devices, const QJsonObject& auth = {}); + }; +} // namespace QMatrixClient -- cgit v1.2.3