QtRestClient  3.0.0
A library for generic JSON-based REST-APIs, with a mechanism to map JSON to Qt objects
requestbuilder.h
1 #ifndef QTRESTCLIENT_REQUESTBUILDER_H
2 #define QTRESTCLIENT_REQUESTBUILDER_H
3 
4 #include "QtRestClient/qtrestclient_global.h"
5 
6 #include <QtCore/qcborvalue.h>
7 #include <QtCore/qjsonvalue.h>
8 #include <QtCore/qurl.h>
9 #include <QtCore/qurlquery.h>
10 #include <QtCore/qversionnumber.h>
11 #include <QtCore/qshareddata.h>
12 #include <QtCore/qmimetype.h>
13 #ifdef QT_RESTCLIENT_USE_ASYNC
14 #include <QtCore/qfuture.h>
15 #endif
16 
17 #include <QtNetwork/qnetworkrequest.h>
18 #include <QtNetwork/qnetworkreply.h>
19 
20 namespace QtRestClient {
21 
22 struct RequestBuilderPrivate;
24 class Q_RESTCLIENT_EXPORT RequestBuilder
25 {
26 public:
28  class Q_RESTCLIENT_EXPORT IExtender
29  {
30  Q_DISABLE_COPY(IExtender)
31  public:
32  IExtender();
33  virtual ~IExtender();
34 
36  virtual void extendUrl(QUrl &url) const;
38  virtual bool requiresBody() const;
40  virtual void extendRequest(QNetworkRequest &request, QByteArray &verb, QByteArray *body) const;
41  };
42 
44  RequestBuilder(const QUrl &baseUrl, QNetworkAccessManager *nam = nullptr);
46  RequestBuilder(const RequestBuilder &other);
48  RequestBuilder(RequestBuilder &&other) noexcept;
50  RequestBuilder &operator=(const RequestBuilder &other);
52  RequestBuilder &operator=(RequestBuilder &&other) noexcept;
53  ~RequestBuilder();
54 
56  RequestBuilder &setNetworkAccessManager(QNetworkAccessManager *nam);
58  RequestBuilder &setExtender(IExtender *extender);
59 
61  RequestBuilder &setCredentials(QString user, QString password = {});
63  RequestBuilder &setVersion(QVersionNumber version);
65  RequestBuilder &addPath(const QString &pathSegment);
67  RequestBuilder &addPath(const QStringList &pathSegment);
69  RequestBuilder &trailingSlash(bool enable = true);
71  RequestBuilder &addParameter(const QString &name, const QString &value);
73  RequestBuilder &addParameters(const QUrlQuery &parameters);
75  RequestBuilder &setFragment(QString fragment);
77  RequestBuilder &addHeader(const QByteArray &name, const QByteArray &value);
79  RequestBuilder &addHeaders(const HeaderHash &headers);
80 
82  RequestBuilder &updateFromRelativeUrl(const QUrl &url, bool mergeQuery = false, bool keepFragment = false);
83 
85  RequestBuilder &setAttribute(QNetworkRequest::Attribute attribute, const QVariant &value);
87  RequestBuilder &setAttributes(const QHash<QNetworkRequest::Attribute, QVariant> &attributes);
88 #ifndef QT_NO_SSL
89  RequestBuilder &setSslConfig(QSslConfiguration sslConfig);
91 #endif
92 
94  RequestBuilder &setBody(QByteArray body, const QByteArray &contentType, bool setAccept = true);
96  RequestBuilder &setBody(QCborValue body, bool setAccept = true);
98  RequestBuilder &setBody(const QJsonValue &body, bool setAccept = true);
100  RequestBuilder &setVerb(QByteArray verb);
102  RequestBuilder &setAccept(const QByteArray &mimeType);
104  RequestBuilder &setAccept(const QMimeType &mimeType);
105 
107  RequestBuilder &addPostParameter(const QString &name, const QString &value);
109  RequestBuilder &addPostParameters(const QUrlQuery &parameters);
110 
112  QUrl buildUrl() const;
114  QNetworkRequest build() const;
116  QNetworkReply *send() const;
117 #ifdef QT_RESTCLIENT_USE_ASYNC
118  QFuture<QNetworkReply*> sendAsync() const;
120 #endif
121 
122 private:
124 };
125 
126 }
127 
128 #endif // QTRESTCLIENT_REQUESTBUILDER_H
QtRestClient
The Namespace containing all classes of the QtRestClient module.
Definition: genericrestreply.h:14
QUrl
QNetworkReply
QSharedDataPointer< RequestBuilderPrivate >
QJsonValue
QtRestClient::RequestBuilder
A helper class to build QUrl and QNetworkRequest objects.
Definition: requestbuilder.h:24
QString
QMimeType
QVersionNumber
QNetworkRequest
QCborValue
QFuture
QSslConfiguration
QVariant
QNetworkAccessManager
QHash
QByteArray
QStringList
QtRestClient::RequestBuilder::IExtender
A simple interface to add custom extensions to the building process.
Definition: requestbuilder.h:28
QUrlQuery