QtRestClient  3.0.0
A library for generic JSON-based REST-APIs, with a mechanism to map JSON to Qt objects
restclass.h
Go to the documentation of this file.
1 #ifndef QTRESTCLIENT_RESTCLASS_H
2 #define QTRESTCLIENT_RESTCLASS_H
3 
4 #include "QtRestClient/qtrestclient_global.h"
5 #include "QtRestClient/requestbuilder.h"
6 #include "QtRestClient/restreply.h"
7 #include "QtRestClient/restclient.h"
8 
9 #ifndef Q_RESTCLIENT_NO_JSON_SERIALIZER
10 #include "QtRestClient/genericrestreply.h"
11 #endif
12 
13 #include <QtCore/qobject.h>
14 
15 namespace QtRestClient {
16 
17 class RestClassPrivate;
19 class Q_RESTCLIENT_EXPORT RestClass : public QObject
20 {
21  Q_OBJECT
22  friend class RestClient;
23  friend class RestClientPrivate;
24 
25 public:
27  static const QByteArray GetVerb;
29  static const QByteArray PostVerb;
31  static const QByteArray PutVerb;
33  static const QByteArray DeleteVerb;
35  static const QByteArray PatchVerb;
37  static const QByteArray HeadVerb;
38 
40  RestClient *client() const;
42  RestClass *subClass(const QString &path, QObject *parent = nullptr) const;
43 
44  //general calls (json or cbor based)
47  RestReply *callRaw(const QByteArray &verb, const QString &methodPath, const QVariantHash &parameters = {}, const HeaderHash &headers = {}, bool paramsAsBody = false) const;
48  RestReply *callRaw(const QByteArray &verb, const QString &methodPath, const QCborValue &body, const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const;
49  RestReply *callRaw(const QByteArray &verb, const QString &methodPath, const QJsonValue &body, const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const;
50 
51  RestReply *callRaw(const QByteArray &verb, const QVariantHash &parameters = {}, const HeaderHash &headers = {}, bool paramsAsBody = false) const;
52  RestReply *callRaw(const QByteArray &verb, const QCborValue &body, const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const;
53  RestReply *callRaw(const QByteArray &verb, const QJsonValue &body, const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const;
54 
55  RestReply *callRaw(const QByteArray &verb, const QUrl &relativeUrl, const QVariantHash &parameters = {}, const HeaderHash &headers = {}, bool paramsAsBody = false) const;
56  RestReply *callRaw(const QByteArray &verb, const QUrl &relativeUrl, const QCborValue &body, const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const;
57  RestReply *callRaw(const QByteArray &verb, const QUrl &relativeUrl, const QJsonValue &body, const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const;
59 
60 #ifndef Q_RESTCLIENT_NO_JSON_SERIALIZER
61  template<typename DT = QObject*, typename ET = QObject*>
64  GenericRestReply<DT, ET> *call(const QByteArray &verb, const QString &methodPath, const QVariantHash &parameters = {}, const HeaderHash &headers = {}, bool paramsAsBody = false) const;
65  template<typename DT = QObject*, typename ET = QObject*, typename RO = QObject*>
66  GenericRestReply<DT, ET> *call(const QByteArray &verb, const QString &methodPath, const RO &body, const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const;
67 
68  template<typename DT = QObject*, typename ET = QObject*>
69  GenericRestReply<DT, ET> *call(const QByteArray &verb, const QVariantHash &parameters = {}, const HeaderHash &headers = {}, bool paramsAsBody = false) const;
70  template<typename DT = QObject*, typename ET = QObject*, typename RO = QObject*>
71  GenericRestReply<DT, ET> *call(const QByteArray &verb, const RO &body, const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const;
72 
73  template<typename DT = QObject*, typename ET = QObject*>
74  GenericRestReply<DT, ET> *call(const QByteArray &verb, const QUrl &relativeUrl, const QVariantHash &parameters = {}, const HeaderHash &headers = {}, bool paramsAsBody = false) const;
75  template<typename DT = QObject*, typename ET = QObject*, typename RO = QObject*>
76  GenericRestReply<DT, ET> *call(const QByteArray &verb, const QUrl &relativeUrl, const RO &body, const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const;
78 
81  template<typename DT = QObject*, typename ET = QObject*>
82  inline GenericRestReply<DT, ET> *get(const QString &methodPath, const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const {
83  return call<DT, ET>(GetVerb, methodPath, parameters, headers);
84  }
85  template<typename DT = QObject*, typename ET = QObject*>
86  inline GenericRestReply<DT, ET> *get(const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const {
87  return call<DT, ET>(GetVerb, parameters, headers);
88  }
89  template<typename DT = QObject*, typename ET = QObject*>
90  inline GenericRestReply<DT, ET> *get(const QUrl &relativeUrl, const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const {
91  return call<DT, ET>(GetVerb, relativeUrl, parameters, headers);
92  }
94 
97  template<typename DT = QObject*, typename ET = QObject*>
98  inline GenericRestReply<DT, ET> *post(const QString &methodPath, const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const {
99  return call<DT, ET>(PostVerb, methodPath, parameters, headers, true);
100  }
101  template<typename DT = QObject*, typename ET = QObject*, typename RO = QObject*>
102  inline GenericRestReply<DT, ET> *post(const QString &methodPath, const RO &body, const HeaderHash &headers = {}) const {
103  return call<DT, ET>(PostVerb, methodPath, body, {}, headers);
104  }
105 
106  template<typename DT = QObject*, typename ET = QObject*>
107  inline GenericRestReply<DT, ET> *post(const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const {
108  return call<DT, ET>(PostVerb, parameters, headers, true);
109  }
110  template<typename DT = QObject*, typename ET = QObject*, typename RO = QObject*>
111  inline GenericRestReply<DT, ET> *post(const RO &body, const HeaderHash &headers = {}) const {
112  return call<DT, ET>(PostVerb, body, {}, headers);
113  }
114 
115  template<typename DT = QObject*, typename ET = QObject*>
116  inline GenericRestReply<DT, ET> *post(const QUrl &relativeUrl, const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const {
117  return call<DT, ET>(PostVerb, relativeUrl, parameters, headers, true);
118  }
119  template<typename DT = QObject*, typename ET = QObject*, typename RO = QObject*>
120  inline GenericRestReply<DT, ET> *post(const QUrl &relativeUrl, const RO &body, const HeaderHash &headers = {}) const {
121  return call<DT, ET>(PostVerb, relativeUrl, body, {}, headers);
122  }
124 
127  template<typename DT = QObject*, typename ET = QObject*, typename RO = QObject*>
128  inline GenericRestReply<DT, ET> *put(const QString &methodPath, const RO &body, const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const {
129  return call<DT, ET>(PutVerb, methodPath, body, parameters, headers);
130  }
131  template<typename DT = QObject*, typename ET = QObject*, typename RO = QObject*>
132  inline GenericRestReply<DT, ET> *put(const RO &body, const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const {
133  return call<DT, ET>(PutVerb, body, parameters, headers);
134  }
135  template<typename DT = QObject*, typename ET = QObject*, typename RO = QObject*>
136  inline GenericRestReply<DT, ET> *put(const QUrl &relativeUrl, const RO &body, const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const {
137  return call<DT, ET>(PutVerb, relativeUrl, body, parameters, headers);
138  }
140 
143  template<typename DT = QObject*, typename ET = QObject*>
144  inline GenericRestReply<DT, ET> *deleteResource(const QString &methodPath, const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const {
145  return call<DT, ET>(DeleteVerb, methodPath, parameters, headers);
146  }
147  template<typename DT = QObject*, typename ET = QObject*>
148  inline GenericRestReply<DT, ET> *deleteResource(const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const {
149  return call<DT, ET>(DeleteVerb, parameters, headers);
150  }
151  template<typename DT = QObject*, typename ET = QObject*>
152  inline GenericRestReply<DT, ET> *deleteResource(const QUrl &relativeUrl, const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const {
153  return call<DT, ET>(DeleteVerb, relativeUrl, parameters, headers);
154  }
156 
159  template<typename DT = QObject*, typename ET = QObject*, typename RO = QObject*>
160  inline GenericRestReply<DT, ET> *patch(const QString &methodPath, const RO &body, const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const {
161  return call<DT, ET>(PatchVerb, methodPath, body, parameters, headers);
162  }
163  template<typename DT = QObject*, typename ET = QObject*, typename RO = QObject*>
164  inline GenericRestReply<DT, ET> *patch(const RO &body, const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const {
165  return call<DT, ET>(PatchVerb, body, parameters, headers);
166  }
167  template<typename DT = QObject*, typename ET = QObject*, typename RO = QObject*>
168  inline GenericRestReply<DT, ET> *patch(const QUrl &relativeUrl, const RO &body, const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const {
169  return call<DT, ET>(PatchVerb, relativeUrl, body, parameters, headers);
170  }
172 
175  template<typename DT = QObject*, typename ET = QObject*>
176  inline GenericRestReply<DT, ET> *head(const QString &methodPath, const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const {
177  return call<DT, ET>(HeadVerb, methodPath, parameters, headers);
178  }
179  template<typename DT = QObject*, typename ET = QObject*>
180  inline GenericRestReply<DT, ET> *head(const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const {
181  return call<DT, ET>(HeadVerb, parameters, headers);
182  }
183  template<typename DT = QObject*, typename ET = QObject*>
184  inline GenericRestReply<DT, ET> *head(const QUrl &relativeUrl, const QVariantHash &parameters = {}, const HeaderHash &headers = {}) const {
185  return call<DT, ET>(HeadVerb, relativeUrl, parameters, headers);
186  }
188 #endif
189 
191  virtual RequestBuilder builder() const;
192 
195  static inline QVariantHash concatParams() {return {};}
196  template<typename... Args>
197  static QVariantHash concatParams(const QString &key, const QVariant &value, Args... parameters);
199 
200 protected:
202  explicit RestClass(RestClient *client, QStringList subPath, QObject *parent);
204  explicit RestClass(RestClassPrivate &dd, QObject *parent = nullptr);
205 
206 private:
207  Q_DECLARE_PRIVATE(RestClass)
208 
209 #ifdef QT_RESTCLIENT_USE_ASYNC
210  using CreateResult = std::variant<QNetworkReply*, QFuture<QNetworkReply*>>;
211 #else
212  using CreateResult = std::variant<QNetworkReply*>;
213 #endif
214 
215  CreateResult create(const QByteArray &verb, const QString &methodPath, const QVariantHash &parameters, const HeaderHash &headers, bool paramsAsBody) const;
216  CreateResult create(const QByteArray &verb, const QString &methodPath, const QCborValue &body, const QVariantHash &parameters, const HeaderHash &headers) const;
217  CreateResult create(const QByteArray &verb, const QString &methodPath, const QJsonValue &body, const QVariantHash &parameters, const HeaderHash &headers) const;
218  CreateResult create(const QByteArray &verb, const QVariantHash &parameters, const HeaderHash &headers, bool paramsAsBody) const;
219  CreateResult create(const QByteArray &verb, const QCborValue &body, const QVariantHash &parameters, const HeaderHash &headers) const;
220  CreateResult create(const QByteArray &verb, const QJsonValue &body, const QVariantHash &parameters, const HeaderHash &headers) const;
221  CreateResult create(const QByteArray &verb, const QUrl &relativeUrl, const QVariantHash &parameters, const HeaderHash &headers, bool paramsAsBody) const;
222  CreateResult create(const QByteArray &verb, const QUrl &relativeUrl, const QCborValue &body, const QVariantHash &parameters, const HeaderHash &headers) const;
223  CreateResult create(const QByteArray &verb, const QUrl &relativeUrl, const QJsonValue &body, const QVariantHash &parameters, const HeaderHash &headers) const;
224 };
225 
227 #define CONCAT_PARAMS QtRestClient::RestClass::concatParams
228 
229 // ------------- Generic Implementation -------------
230 
231 #ifndef Q_RESTCLIENT_NO_JSON_SERIALIZER
232 template<typename DT, typename ET>
233 GenericRestReply<DT, ET> *RestClass::call(const QByteArray &verb, const QString &methodPath, const QVariantHash &parameters, const HeaderHash &headers, bool paramsAsBody) const
234 {
235  return std::visit([&](const auto &reply) {
236  return new GenericRestReply<DT, ET>{reply, client(), nullptr};
237  }, create(verb, methodPath, parameters, headers, paramsAsBody));
238 }
239 
240 template<typename DT, typename ET, typename RO>
241 GenericRestReply<DT, ET> *RestClass::call(const QByteArray &verb, const QString &methodPath, const RO &body, const QVariantHash &parameters, const HeaderHash &headers) const
242 {
243  return std::visit([&](auto bodyData) {
244  return std::visit([&](const auto &reply) {
245  return new GenericRestReply<DT, ET>{reply, client(), nullptr};
246  }, create(verb, methodPath, bodyData, parameters, headers));
247  }, client()->serializer()->serializeGeneric(QtJsonSerializer::__private::variant_helper<RO>::toVariant(body)));
248 }
249 
250 template<typename DT, typename ET>
251 GenericRestReply<DT, ET> *RestClass::call(const QByteArray &verb, const QVariantHash &parameters, const HeaderHash &headers, bool paramsAsBody) const
252 {
253  return std::visit([&](const auto &reply) {
254  return new GenericRestReply<DT, ET>{reply, client(), nullptr};
255  }, create(verb, parameters, headers, paramsAsBody));
256 }
257 
258 template<typename DT, typename ET, typename RO>
259 GenericRestReply<DT, ET> *RestClass::call(const QByteArray &verb, const RO &body, const QVariantHash &parameters, const HeaderHash &headers) const
260 {
261  return std::visit([&](auto bodyData) {
262  return std::visit([&](const auto &reply) {
263  return new GenericRestReply<DT, ET>{reply, client(), nullptr};
264  }, create(verb, bodyData, parameters, headers));
265  }, client()->serializer()->serializeGeneric(QtJsonSerializer::__private::variant_helper<RO>::toVariant(body)));
266 }
267 
268 template<typename DT, typename ET>
269 GenericRestReply<DT, ET> *RestClass::call(const QByteArray &verb, const QUrl &relativeUrl, const QVariantHash &parameters, const HeaderHash &headers, bool paramsAsBody) const
270 {
271  return std::visit([&](const auto &reply) {
272  return new GenericRestReply<DT, ET>{reply, client(), nullptr};
273  }, create(verb, relativeUrl, parameters, headers, paramsAsBody));
274 }
275 
276 template<typename DT, typename ET, typename RO>
277 GenericRestReply<DT, ET> *RestClass::call(const QByteArray &verb, const QUrl &relativeUrl, const RO &body, const QVariantHash &parameters, const HeaderHash &headers) const
278 {
279  return std::visit([&](auto bodyData) {
280  return std::visit([&](const auto &reply) {
281  return new GenericRestReply<DT, ET>{reply, client(), nullptr};
282  }, create(verb, relativeUrl, bodyData, parameters, headers));
283  }, client()->serializer()->serializeGeneric(QtJsonSerializer::__private::variant_helper<RO>::toVariant(body)));
284 }
285 #endif
286 
287 template<typename... Args>
288 QVariantHash RestClass::concatParams(const QString &key, const QVariant &value, Args... parameters)
289 {
290  auto hash = concatParams(parameters...);
291  hash.insert(key, value);
292  return hash;
293 }
294 
295 }
296 
297 #endif // QTRESTCLIENT_RESTCLASS_H
298 
QtJsonSerializer::SerializerBase::serializeGeneric
virtual std::variant< QCborValue, QJsonValue > serializeGeneric(const QVariant &value) const=0
QtRestClient::RestClass::patch
GenericRestReply< DT, ET > * patch(const QUrl &relativeUrl, const RO &body, const QVariantHash &parameters={}, const HeaderHash &headers={}) const
Performs PATCH-request with generic objects.
Definition: restclass.h:168
QtRestClient
The Namespace containing all classes of the QtRestClient module.
Definition: genericrestreply.h:14
QUrl
QtRestClient::HeaderHash
QHash< QByteArray, QByteArray > HeaderHash
A typedef for a collection of HTTP request headers.
Definition: qtrestclient_global.h:32
QtRestClient::RestClass::post
GenericRestReply< DT, ET > * post(const QString &methodPath, const RO &body, const HeaderHash &headers={}) const
Performs POST-request with generic objects.
Definition: restclass.h:102
QtRestClient::RestClient::serializer
QtJsonSerializer::SerializerBase * serializer() const
Returns the json serializer used by the restclient.
Definition: restclient.cpp:57
QtRestClient::RestClass::HeadVerb
static const QByteArray HeadVerb
A constant for the HTTP-HEAD verb.
Definition: restclass.h:37
QtRestClient::RestClass::post
GenericRestReply< DT, ET > * post(const QUrl &relativeUrl, const RO &body, const HeaderHash &headers={}) const
Performs POST-request with generic objects.
Definition: restclass.h:120
QtRestClient::RestClass::put
GenericRestReply< DT, ET > * put(const QString &methodPath, const RO &body, const QVariantHash &parameters={}, const HeaderHash &headers={}) const
Performs PUT-request with generic objects.
Definition: restclass.h:128
QtRestClient::RestClass::DeleteVerb
static const QByteArray DeleteVerb
A constant for the HTTP-DELETE verb.
Definition: restclass.h:33
QtRestClient::RestClass::get
GenericRestReply< DT, ET > * get(const QUrl &relativeUrl, const QVariantHash &parameters={}, const HeaderHash &headers={}) const
Performs GET-request with generic objects.
Definition: restclass.h:90
QtRestClient::RestClass
A class to perform requests to an API.
Definition: restclass.h:19
QtRestClient::RestClass::GetVerb
static const QByteArray GetVerb
A constant for the HTTP-GET verb.
Definition: restclass.h:27
QJsonValue
QString
QtRestClient::RestClass::put
GenericRestReply< DT, ET > * put(const QUrl &relativeUrl, const RO &body, const QVariantHash &parameters={}, const HeaderHash &headers={}) const
Performs PUT-request with generic objects.
Definition: restclass.h:136
QtRestClient::GenericRestReply
A class to handle generic replies for generic requests.
Definition: genericrestreply.h:20
QtRestClient::RestClass::PostVerb
static const QByteArray PostVerb
A constant for the HTTP-POST verb.
Definition: restclass.h:29
QtRestClient::RestClass::post
GenericRestReply< DT, ET > * post(const QVariantHash &parameters={}, const HeaderHash &headers={}) const
Performs POST-request with generic objects.
Definition: restclass.h:107
QCborValue
QtRestClient::RestClass::patch
GenericRestReply< DT, ET > * patch(const QString &methodPath, const RO &body, const QVariantHash &parameters={}, const HeaderHash &headers={}) const
Performs PATCH-request with generic objects.
Definition: restclass.h:160
QtRestClient::RestClass::post
GenericRestReply< DT, ET > * post(const RO &body, const HeaderHash &headers={}) const
Performs POST-request with generic objects.
Definition: restclass.h:111
QtRestClient::RestClass::head
GenericRestReply< DT, ET > * head(const QVariantHash &parameters={}, const HeaderHash &headers={}) const
Performs HEAD-request with generic objects.
Definition: restclass.h:180
QtRestClient::RestClass::get
GenericRestReply< DT, ET > * get(const QVariantHash &parameters={}, const HeaderHash &headers={}) const
Performs GET-request with generic objects.
Definition: restclass.h:86
QtRestClient::RestClass::PatchVerb
static const QByteArray PatchVerb
A constant for the HTTP-PATCH verb.
Definition: restclass.h:35
QtRestClient::RestClass::PutVerb
static const QByteArray PutVerb
A constant for the HTTP-PUT verb.
Definition: restclass.h:31
QtRestClient::RestClass::deleteResource
GenericRestReply< DT, ET > * deleteResource(const QString &methodPath, const QVariantHash &parameters={}, const HeaderHash &headers={}) const
Performs DELETE-request with generic objects.
Definition: restclass.h:144
QtRestClient::RestClass::deleteResource
GenericRestReply< DT, ET > * deleteResource(const QVariantHash &parameters={}, const HeaderHash &headers={}) const
Performs DELETE-request with generic objects.
Definition: restclass.h:148
QtRestClient::RestClass::head
GenericRestReply< DT, ET > * head(const QUrl &relativeUrl, const QVariantHash &parameters={}, const HeaderHash &headers={}) const
Performs HEAD-request with generic objects.
Definition: restclass.h:184
QtRestClient::RestClass::client
RestClient * client() const
Returns the rest client this class operates with.
QtRestClient::RestClass::patch
GenericRestReply< DT, ET > * patch(const RO &body, const QVariantHash &parameters={}, const HeaderHash &headers={}) const
Performs PATCH-request with generic objects.
Definition: restclass.h:164
QtRestClient::RestClass::get
GenericRestReply< DT, ET > * get(const QString &methodPath, const QVariantHash &parameters={}, const HeaderHash &headers={}) const
Performs GET-request with generic objects.
Definition: restclass.h:82
QtRestClient::RestClass::head
GenericRestReply< DT, ET > * head(const QString &methodPath, const QVariantHash &parameters={}, const HeaderHash &headers={}) const
Performs HEAD-request with generic objects.
Definition: restclass.h:176
QtRestClient::RestClass::call
GenericRestReply< DT, ET > * call(const QByteArray &verb, const QString &methodPath, const QVariantHash &parameters={}, const HeaderHash &headers={}, bool paramsAsBody=false) const
Performs an API call of the given verb with generic objects.
Definition: restclass.h:233
QVariant
QtRestClient::RestClass::put
GenericRestReply< DT, ET > * put(const RO &body, const QVariantHash &parameters={}, const HeaderHash &headers={}) const
Performs PUT-request with generic objects.
Definition: restclass.h:132
QObject
QtRestClient::RestReply
A class to handle replies for JSON requests.
Definition: restreply.h:23
QtRestClient::RestClient
A class to define access to an API, with general settings.
Definition: restclient.h:30
QtRestClient::RestClass::deleteResource
GenericRestReply< DT, ET > * deleteResource(const QUrl &relativeUrl, const QVariantHash &parameters={}, const HeaderHash &headers={}) const
Performs DELETE-request with generic objects.
Definition: restclass.h:152
QHash
QtRestClient::RestClass::concatParams
static QVariantHash concatParams()
A generic method to concatenate parameters into a QVariantHash.
Definition: restclass.h:195
QtRestClient::RestClass::post
GenericRestReply< DT, ET > * post(const QString &methodPath, const QVariantHash &parameters={}, const HeaderHash &headers={}) const
Performs POST-request with generic objects.
Definition: restclass.h:98
QByteArray
QStringList
QtRestClient::RestClass::post
GenericRestReply< DT, ET > * post(const QUrl &relativeUrl, const QVariantHash &parameters={}, const HeaderHash &headers={}) const
Performs POST-request with generic objects.
Definition: restclass.h:116