QtRestClient  3.0.0
A library for generic JSON-based REST-APIs, with a mechanism to map JSON to Qt objects
qmlrestclass.h
1 #ifndef QTRESTCLIENT_QMLRESTCLASS_H
2 #define QTRESTCLIENT_QMLRESTCLASS_H
3 
4 #include <tuple>
5 #include <QtCore/QObject>
6 #include <QtQml/QQmlParserStatus>
7 #include <QtQml/QQmlListProperty>
8 #include <QtQml/QJSValue>
9 
10 #include <QtRestClient/RestClass>
11 
12 #include "qmlrestreply.h"
13 
14 #ifdef DOXYGEN_RUN
16 
22 class RestClass
23 {
24 #else
25 namespace QtRestClient {
26 
27 class QmlRestClass : public QObject, public QQmlParserStatus
28 {
29  Q_OBJECT
30  Q_INTERFACES(QQmlParserStatus)
31 #endif
32 
42  Q_PROPERTY(QtRestClient::RestClass* restClass READ restClass NOTIFY restClassChanged)
51  Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged)
52 #ifdef DOXYGEN_RUN
53 
61 #else
62  Q_PROPERTY(QQmlListProperty<QtRestClient::QmlRestClass> classes READ classes)
63 #endif
64 
65  Q_CLASSINFO("DefaultProperty", "classes")
66 
67 public:
69  explicit QmlRestClass(QObject *parent = nullptr);
70 
72  RestClass* restClass() const;
74  QString path() const;
76  QQmlListProperty<QmlRestClass> classes();
77 
79  void classBegin() override;
81  void componentComplete() override;
82 
102  Q_INVOKABLE QtRestClient::QmlRestReply *call(const QString &verb,
103  QJSValue optPath = {},
104  QJSValue optBody = {},
105  QJSValue optParams = {},
106  QJSValue optAsPost = {},
107  QJSValue optHeaders = {});
108 
125  Q_INVOKABLE QtRestClient::QmlRestReply *get(QJSValue optPath = {},
126  QJSValue optParams = {},
127  QJSValue optHeaders = {});
145  Q_INVOKABLE QtRestClient::QmlRestReply *post(QJSValue optPath = {},
146  QJSValue optBodyParams = {},
147  QJSValue optParams = {},
148  QJSValue optHeaders = {});
166  Q_INVOKABLE QtRestClient::QmlRestReply *put(QJSValue optPathOrBody,
167  QJSValue body = {},
168  QJSValue optParams = {},
169  QJSValue optHeaders = {});
186  Q_INVOKABLE QtRestClient::QmlRestReply *deleteResource(QJSValue optPath = {},
187  QJSValue optParams = {},
188  QJSValue optHeaders = {});
206  Q_INVOKABLE QtRestClient::QmlRestReply *patch(QJSValue optPathOrBody,
207  QJSValue body = {},
208  QJSValue optParams = {},
209  QJSValue optHeaders = {});
226  Q_INVOKABLE QtRestClient::QmlRestReply *head(QJSValue optPath = {},
227  QJSValue optParams = {},
228  QJSValue optHeaders = {});
229 
230 public Q_SLOTS:
232  void setPath(QString path);
233 
234 Q_SIGNALS:
236  void restClassChanged(QtRestClient::RestClass *restClass);
238  void pathChanged(QString path);
239 
240 private Q_SLOTS:
241  void revaluateClass();
242 
243 private:
244  RestClass *_class = nullptr;
245  QString _path;
246  QList<QmlRestClass*> _childClasses;
247  bool _init = false;
248 
249  // moves first into second recursively, if last is undefined
250  template <typename... TValues>
251  bool shift(QJSValue &first, QJSValue &second, TValues& ...values) const;
252  bool shift(QJSValue &end) const;
253 
254  template <typename... TValues>
255  std::optional<std::variant<QString, QUrl>> getPath(QJSValue &optPath, TValues&... values) const;
256  template <typename... TValues>
257  std::optional<std::variant<QCborValue, QJsonValue>> getBody(QJSValue &optBody, TValues&... values) const;
258  template <typename... TValues>
259  std::optional<QVariantHash> getParams(QJSValue &optParams, TValues&... values) const;
260  template <typename... TValues>
261  bool getAsPost(bool hasBody, QJSValue &optParams, TValues&... values) const;
262  std::optional<HeaderHash> getHeaders(QJSValue &optHeaders) const;
263 
264  QmlRestReply *makeQmlReply(RestReply *reply);
265  QmlRestReply *callImpl(const QByteArray &verb,
266  const std::optional<std::variant<QString, QUrl>> &optPath,
267  const std::optional<std::variant<QCborValue, QJsonValue>> &optBody,
268  const std::optional<QVariantHash> &optParams,
269  bool optAsPost,
270  const std::optional<HeaderHash> &optHeaders);
271 };
272 
273 }
274 
275 #endif // QTRESTCLIENT_QMLRESTCLASS_H
de::skycoder42::QtRestClient::RestClass::post
Q_INVOKABLE QtRestClient::QmlRestReply * post(QJSValue optPath={}, QJSValue optBodyParams={}, QJSValue optParams={}, QJSValue optHeaders={})
Perform a HTTP-POST-Request on this class.
QtRestClient
The Namespace containing all classes of the QtRestClient module.
Definition: genericrestreply.h:14
de::skycoder42::QtRestClient::RestClass::QmlRestClass
QmlRestClass(QObject *parent=nullptr)
Default constructor with object parent.
de::skycoder42::QtRestClient::RestClass::deleteResource
Q_INVOKABLE QtRestClient::QmlRestReply * deleteResource(QJSValue optPath={}, QJSValue optParams={}, QJSValue optHeaders={})
Perform a HTTP-DELETE-Request on this class.
de::skycoder42::QtRestClient::RestClass::head
Q_INVOKABLE QtRestClient::QmlRestReply * head(QJSValue optPath={}, QJSValue optParams={}, QJSValue optHeaders={})
Perform a HTTP-HEAD-Request on this class.
de::skycoder42::QtRestClient::RestClass::pathChanged
void pathChanged(QString path)
NOTIFY accessor for RestClass::path.
QList< QmlRestClass * >
de::skycoder42::QtRestClient
The QML import for the QtRestClient QML module.
Definition: qmlgenericrestreply.h:14
de::skycoder42::QtRestClient::RestClass::restClassChanged
void restClassChanged(QtRestClient::RestClass *restClass)
NOTIFY accessor for RestClass::restClass.
de::skycoder42::QtRestClient::RestClass
The QML version of QtRestClient::RestClass.
Definition: qmlrestclass.h:22
QtRestClient::RestClass
A class to perform requests to an API.
Definition: restclass.h:19
de::skycoder42::QtRestClient::RestClass::get
Q_INVOKABLE QtRestClient::QmlRestReply * get(QJSValue optPath={}, QJSValue optParams={}, QJSValue optHeaders={})
Perform a HTTP-GET-Request on this class.
QString
QQmlListProperty
de::skycoder42::QtRestClient::RestClass::put
Q_INVOKABLE QtRestClient::QmlRestReply * put(QJSValue optPathOrBody, QJSValue body={}, QJSValue optParams={}, QJSValue optHeaders={})
Perform a HTTP-PUT-Request on this class.
de::skycoder42::QtRestClient::RestClass::patch
Q_INVOKABLE QtRestClient::QmlRestReply * patch(QJSValue optPathOrBody, QJSValue body={}, QJSValue optParams={}, QJSValue optHeaders={})
Perform a HTTP-PATCH-Request on this class.
QJSValue
QObject
QQmlParserStatus
de::skycoder42::QtRestClient::RestClass::call
Q_INVOKABLE QtRestClient::QmlRestReply * call(const QString &verb, QJSValue optPath={}, QJSValue optBody={}, QJSValue optParams={}, QJSValue optAsPost={}, QJSValue optHeaders={})
Perform a HTTP-Request call on this class.
QByteArray