QtRestClient  3.0.0
A library for generic JSON-based REST-APIs, with a mechanism to map JSON to Qt objects
restclient.h
1 #ifndef QTRESTCLIENT_RESTCLIENT_H
2 #define QTRESTCLIENT_RESTCLIENT_H
3 
4 #include "QtRestClient/qtrestclient_global.h"
5 #include "QtRestClient/requestbuilder.h"
6 
7 #include <QtCore/qobject.h>
8 #include <QtCore/qurl.h>
9 #include <QtCore/qurlquery.h>
10 #include <QtCore/qversionnumber.h>
11 #ifdef QT_RESTCLIENT_USE_ASYNC
12 #include <QtCore/qthreadpool.h>
13 #endif
14 
15 #include <QtNetwork/qnetworkrequest.h>
16 
17 #ifndef Q_RESTCLIENT_NO_JSON_SERIALIZER
18 namespace QtJsonSerializer {
19 class SerializerBase;
20 }
21 #endif
22 
23 namespace QtRestClient {
24 
25 class RestClass;
26 class IPagingFactory;
27 
28 class RestClientPrivate;
30 class Q_RESTCLIENT_EXPORT RestClient : public QObject
31 {
32  Q_OBJECT
33  friend class RestClientPrivate;
34 
36  Q_PROPERTY(DataMode dataMode READ dataMode WRITE setDataMode NOTIFY dataModeChanged)
38  Q_PROPERTY(QUrl baseUrl READ baseUrl WRITE setBaseUrl NOTIFY baseUrlChanged)
40  Q_PROPERTY(QVersionNumber apiVersion READ apiVersion WRITE setApiVersion NOTIFY apiVersionChanged)
42  Q_PROPERTY(HeaderHash globalHeaders READ globalHeaders WRITE setGlobalHeaders NOTIFY globalHeadersChanged)
44  Q_PROPERTY(QUrlQuery globalParameters READ globalParameters WRITE setGlobalParameters NOTIFY globalParametersChanged)
46  Q_PROPERTY(QHash<QNetworkRequest::Attribute, QVariant> requestAttributes READ requestAttributes WRITE setRequestAttributes NOTIFY requestAttributesChanged)
48  Q_PROPERTY(bool threaded READ isThreaded WRITE setThreaded NOTIFY threadedChanged)
49 
50 #ifndef QT_NO_SSL
51  Q_PROPERTY(QSslConfiguration sslConfiguration READ sslConfiguration WRITE setSslConfiguration NOTIFY sslConfigurationChanged)
53 #endif
54 
55 #ifdef QT_RESTCLIENT_USE_ASYNC
56  Q_PROPERTY(QThreadPool* asyncPool READ asyncPool WRITE setAsyncPool NOTIFY asyncPoolChanged)
58 #endif
59 
60 public:
62  enum class DataMode {
63  Cbor,
64  Json
65  };
66  Q_ENUM(DataMode)
67 
68 
69  explicit RestClient(QObject *parent = nullptr);
71  explicit RestClient(DataMode dataMode, QObject *parent = nullptr);
72 #ifndef Q_RESTCLIENT_NO_JSON_SERIALIZER
73  explicit RestClient(QtJsonSerializer::SerializerBase *serializer, QObject *parent = nullptr);
75 #endif
76 
78  RestClass *createClass(const QString &path, QObject *parent = nullptr);
80  RestClass *rootClass() const;
81 
83  QNetworkAccessManager *manager() const;
84 #ifndef Q_RESTCLIENT_NO_JSON_SERIALIZER
85  QtJsonSerializer::SerializerBase *serializer() const;
87 #endif
88  IPagingFactory *pagingFactory() const;
90 
92  DataMode dataMode() const;
94  QUrl baseUrl() const;
96  QVersionNumber apiVersion() const;
98  HeaderHash globalHeaders() const;
100  QUrlQuery globalParameters() const;
102  QHash<QNetworkRequest::Attribute, QVariant> requestAttributes() const;
104  bool isThreaded() const;
105 #ifndef QT_NO_SSL
106  QSslConfiguration sslConfiguration() const;
108 #endif
109 #ifdef QT_RESTCLIENT_USE_ASYNC
110  QThreadPool* asyncPool() const;
112 #endif
113 
115  virtual RequestBuilder builder() const;
116 
117 public Q_SLOTS:
119  void setManager(QNetworkAccessManager *manager);
120 #ifndef Q_RESTCLIENT_NO_JSON_SERIALIZER
121  void setSerializer(QtJsonSerializer::SerializerBase *serializer);
123 #endif
124 
126  void setPagingFactory(IPagingFactory *factory);
127 
129  void setDataMode(DataMode dataMode);
131  void setBaseUrl(QUrl baseUrl);
133  void setApiVersion(QVersionNumber apiVersion);
135  void setGlobalHeaders(HeaderHash globalHeaders);
137  void setGlobalParameters(QUrlQuery globalParameters);
139  void setRequestAttributes(QHash<QNetworkRequest::Attribute, QVariant> requestAttributes);
141  void setModernAttributes();
143  void setThreaded(bool threaded);
144 #ifndef QT_NO_SSL
145  void setSslConfiguration(QSslConfiguration sslConfiguration);
147 #endif
148 #ifdef QT_RESTCLIENT_USE_ASYNC
149  void setAsyncPool(QThreadPool* asyncPool);
151 #endif
152 
154  void addGlobalHeader(const QByteArray &name, const QByteArray &value);
156  void removeGlobalHeader(const QByteArray &name);
157 
159  void addGlobalParameter(const QString &name, const QString &value);
161  void removeGlobalParameter(const QString &name);
162 
164  void addRequestAttribute(QNetworkRequest::Attribute attribute, const QVariant &value);
166  void removeRequestAttribute(QNetworkRequest::Attribute attribute);
167 
168 Q_SIGNALS:
170  void dataModeChanged(DataMode dataMode, QPrivateSignal);
172  void baseUrlChanged(QUrl baseUrl, QPrivateSignal);
174  void apiVersionChanged(QVersionNumber apiVersion, QPrivateSignal);
176  void globalHeadersChanged(HeaderHash globalHeaders, QPrivateSignal);
178  void globalParametersChanged(QUrlQuery globalParameters, QPrivateSignal);
180  void requestAttributesChanged(QHash<QNetworkRequest::Attribute, QVariant> requestAttributes, QPrivateSignal);
182  void threadedChanged(bool threaded, QPrivateSignal);
183 #ifndef QT_NO_SSL
184  void sslConfigurationChanged(QSslConfiguration sslConfiguration, QPrivateSignal);
186 #endif
187 #ifdef QT_RESTCLIENT_USE_ASYNC
188  void asyncPoolChanged(QThreadPool* asyncPool, QPrivateSignal);
190 #endif
191 
192 protected:
194  RestClient(RestClientPrivate &dd, QObject *parent = nullptr);
196  void setupNam();
197 
198 private:
199  Q_DECLARE_PRIVATE(RestClient)
200 };
201 
202 }
203 
204 #endif // QTRESTCLIENT_RESTCLIENT_H
QtRestClient
The Namespace containing all classes of the QtRestClient module.
Definition: genericrestreply.h:14
QUrl
QtJsonSerializer::SerializerBase
QtRestClient::RestClass
A class to perform requests to an API.
Definition: restclass.h:19
QtRestClient::RequestBuilder
A helper class to build QUrl and QNetworkRequest objects.
Definition: requestbuilder.h:24
QString
QThreadPool
QVersionNumber
QNetworkRequest
QSslConfiguration
QVariant
QObject
QtRestClient::RestClient::DataMode
DataMode
The different data modes in which the client can operate.
Definition: restclient.h:62
QtRestClient::RestClient
A class to define access to an API, with general settings.
Definition: restclient.h:30
QNetworkAccessManager
QHash
QByteArray
QUrlQuery
QtRestClient::IPagingFactory
A factory interface to create IPaging instances from raw data.
Definition: ipaging.h:77