QtRestClient
3.0.0
A library for generic JSON-based REST-APIs, with a mechanism to map JSON to Qt objects
|
A helper class to build QUrl and QNetworkRequest objects. More...
#include <requestbuilder.h>
Classes | |
class | IExtender |
A simple interface to add custom extensions to the building process. More... | |
Public Member Functions | |
RequestBuilder (const QUrl &baseUrl, QNetworkAccessManager *nam=nullptr) | |
Constructs a builder with the given base url. More... | |
RequestBuilder (const RequestBuilder &other) | |
Copy constructor. | |
RequestBuilder (RequestBuilder &&other) noexcept | |
Move constructor. | |
RequestBuilder & | operator= (const RequestBuilder &other) |
Copy assignment operator. | |
RequestBuilder & | operator= (RequestBuilder &&other) noexcept |
Move assignment operator. | |
RequestBuilder & | setNetworkAccessManager (QNetworkAccessManager *nam) |
Sets the network access manager to be used for send() More... | |
RequestBuilder & | setExtender (IExtender *extender) |
Sets the extender to use for extending the build. More... | |
RequestBuilder & | setCredentials (QString user, QString password={}) |
Sets the credentails of the URL. More... | |
RequestBuilder & | setVersion (QVersionNumber version) |
Sets the version of the API. More... | |
RequestBuilder & | addPath (const QString &pathSegment) |
appends a path segment to the builders path More... | |
RequestBuilder & | addPath (const QStringList &pathSegment) |
appends a path segment to the builders path More... | |
RequestBuilder & | trailingSlash (bool enable=true) |
Enables the trailing slash for the generated URL. More... | |
RequestBuilder & | addParameter (const QString &name, const QString &value) |
Adds a parameter to the URL. More... | |
RequestBuilder & | addParameters (const QUrlQuery ¶meters) |
Adds parameters to the URL. More... | |
RequestBuilder & | setFragment (QString fragment) |
Sets the fragment part of the URL. More... | |
RequestBuilder & | addHeader (const QByteArray &name, const QByteArray &value) |
Adds a HTTP header to be added to the network request. More... | |
RequestBuilder & | addHeaders (const HeaderHash &headers) |
Adds HTTP headers to be added to the network request. More... | |
RequestBuilder & | updateFromRelativeUrl (const QUrl &url, bool mergeQuery=false, bool keepFragment=false) |
Updates the builder from the (relative) URL and includes all of it's elements. More... | |
RequestBuilder & | setAttribute (QNetworkRequest::Attribute attribute, const QVariant &value) |
Sets the given attribute on the generated network request. More... | |
RequestBuilder & | setAttributes (const QHash< QNetworkRequest::Attribute, QVariant > &attributes) |
Sets the given attributes on the generated network request. More... | |
RequestBuilder & | setSslConfig (QSslConfiguration sslConfig) |
Sets the ssl configuration to be used by the network request. More... | |
RequestBuilder & | setBody (QByteArray body, const QByteArray &contentType, bool setAccept=true) |
Sets the content of the generated network request. More... | |
RequestBuilder & | setBody (QCborValue body, bool setAccept=true) |
Sets the content of the generated network request. More... | |
RequestBuilder & | setBody (const QJsonValue &body, bool setAccept=true) |
Sets the content of the generated network request. More... | |
RequestBuilder & | setVerb (QByteArray verb) |
Sets the HTTP-Verb to be used by the generated network request. More... | |
RequestBuilder & | setAccept (const QByteArray &mimeType) |
Sets the "Accept" HTTP-header to the given mimetype. More... | |
RequestBuilder & | setAccept (const QMimeType &mimeType) |
Sets the "Accept" HTTP-header to the given mimetype. More... | |
RequestBuilder & | addPostParameter (const QString &name, const QString &value) |
Adds a post parameter to the body. More... | |
RequestBuilder & | addPostParameters (const QUrlQuery ¶meters) |
Adds post parameters to the body. More... | |
QUrl | buildUrl () const |
Creates a URL from the builder settings. More... | |
QNetworkRequest | build () const |
Creates a network request from the builder settings. More... | |
QNetworkReply * | send () const |
Creates a network request and sends it with the builder settings. More... | |
QFuture< QNetworkReply * > | sendAsync () const |
Asynchronously creates a network request and sends it with the builder settings. More... | |
A helper class to build QUrl and QNetworkRequest objects.
The builder is used by RestClient, RestClass and other components to build the API requests.
Definition at line 24 of file requestbuilder.h.
QtRestClient::RequestBuilder::RequestBuilder | ( | const QUrl & | baseUrl, |
QNetworkAccessManager * | nam = nullptr |
||
) |
Constructs a builder with the given base url.
nam | The network access manager to be used. Can be nullptr |
baseUrl | The base URL to build ontop of |
The base URL is required in order to build. As the name suggests, it's used as base
QtRestClient::RequestBuilder::addHeader | ( | const QByteArray & | name, |
const QByteArray & | value | ||
) |
Adds a HTTP header to be added to the network request.
name | The header name |
value | The header value |
QtRestClient::RequestBuilder::addHeaders | ( | const HeaderHash & | headers | ) |
Adds HTTP headers to be added to the network request.
headers | The headers to be set |
Adds a parameter to the URL.
name | The parameters name |
value | The parameters value |
QtRestClient::RequestBuilder::addParameters | ( | const QUrlQuery & | parameters | ) |
Adds parameters to the URL.
parameters | The parameters to be set |
QtRestClient::RequestBuilder::addPath | ( | const QString & | pathSegment | ) |
appends a path segment to the builders path
pathSegment | The path to be added to the builders URL |
RequestBuilder& QtRestClient::RequestBuilder::addPath | ( | const QStringList & | pathSegment | ) |
appends a path segment to the builders path
pathSegment | The path to be added to the builders URL |
Adds a post parameter to the body.
name | The parameters name |
value | The parameters value |
QtRestClient::RequestBuilder::addPostParameters | ( | const QUrlQuery & | parameters | ) |
Adds post parameters to the body.
parameters | The parameters to be set |
QtRestClient::RequestBuilder::build | ( | ) | const |
Creates a network request from the builder settings.
QtRestClient::RequestBuilder::buildUrl | ( | ) | const |
Creates a URL from the builder settings.
QtRestClient::RequestBuilder::send | ( | ) | const |
Creates a network request and sends it with the builder settings.
QtRestClient::RequestBuilder::sendAsync | ( | ) | const |
Asynchronously creates a network request and sends it with the builder settings.
This method will dispatch an asynchronous call to the internal QNetworkAccessManager. This call will run on the managers thread and perform the actual sending. The reply generated from the request will then be made available via the returned future.
While the sending itself is run on the primary thread, anything before that, i.e. the URL and request generation, are run on the current thread, to minimize the workload on the managers thread.
QtRestClient::RequestBuilder::setAccept | ( | const QByteArray & | mimeType | ) |
Sets the "Accept" HTTP-header to the given mimetype.
mimeType | The mimeType to be set as the "Accept" header |
RequestBuilder& QtRestClient::RequestBuilder::setAccept | ( | const QMimeType & | mimeType | ) |
Sets the "Accept" HTTP-header to the given mimetype.
mimeType | The mimeType to be set as the "Accept" header |
QtRestClient::RequestBuilder::setAttribute | ( | QNetworkRequest::Attribute | attribute, |
const QVariant & | value | ||
) |
Sets the given attribute on the generated network request.
attribute | The attribute to be set |
value | The attributes value |
QtRestClient::RequestBuilder::setAttributes | ( | const QHash< QNetworkRequest::Attribute, QVariant > & | attributes | ) |
Sets the given attributes on the generated network request.
attributes | The attributes to be set |
QtRestClient::RequestBuilder::setBody | ( | const QJsonValue & | body, |
bool | setAccept = true |
||
) |
Sets the content of the generated network request.
body | The JSON data to be sent as Content (sets Content-Type to "application/json") |
setAccept | If set to true, the "Accept" header will be set to "application/json" as well |
QtRestClient::RequestBuilder::setBody | ( | QByteArray | body, |
const QByteArray & | contentType, | ||
bool | setAccept = true |
||
) |
Sets the content of the generated network request.
body | The data to be sent as Content |
contentType | The content type for the Content |
setAccept | If set to true, the "Accept" header will be set to the contentType |
QtRestClient::RequestBuilder::setBody | ( | QCborValue | body, |
bool | setAccept = true |
||
) |
Sets the content of the generated network request.
body | The CBOR data to be sent as Content (sets Content-Type to "application/cbor") |
setAccept | If set to true, the "Accept" header will be set to "application/cbor" as well |
Sets the credentails of the URL.
user | The username to be set on the URL |
password | The password to be set on the URL |
QtRestClient::RequestBuilder::setExtender | ( | IExtender * | extender | ) |
Sets the extender to use for extending the build.
extender | The extender to be used for extending when building |
The extender can be used to change the generated URL/request as last step of the building process.
QtRestClient::RequestBuilder::setFragment | ( | QString | fragment | ) |
Sets the fragment part of the URL.
fragment | The URL fragment to be set |
QtRestClient::RequestBuilder::setNetworkAccessManager | ( | QNetworkAccessManager * | nam | ) |
Sets the network access manager to be used for send()
nam | The network access manager to use for sending requests |
QtRestClient::RequestBuilder::setSslConfig | ( | QSslConfiguration | sslConfig | ) |
Sets the ssl configuration to be used by the network request.
sslConfig | The ssl configuration to be used |
If you don't specify a ssl configuration, the builder will use the default configuration
QtRestClient::RequestBuilder::setVerb | ( | QByteArray | verb | ) |
Sets the HTTP-Verb to be used by the generated network request.
verb | The HTTP-Verb to be used |
If this property is not set, the HTTP-GET will be used as default
QtRestClient::RequestBuilder::setVersion | ( | QVersionNumber | version | ) |
Sets the version of the API.
version | The API version |
See RestClient::apiVersion for details
QtRestClient::RequestBuilder::trailingSlash | ( | bool | enable = true | ) |
Enables the trailing slash for the generated URL.
enable | Enable/disable the trailing slash of the path |
Enabled: http://example.com/my/path/?id=42
Disabled: http://example.com/my/path?id=42
QtRestClient::RequestBuilder::updateFromRelativeUrl | ( | const QUrl & | url, |
bool | mergeQuery = false , |
||
bool | keepFragment = false |
||
) |
Updates the builder from the (relative) URL and includes all of it's elements.
url | The (relative) URL to update from |
mergeQuery | Specifies, whether the url queries should be merged |
keepFragment | Specifies, whether the fragment should be kept or not |
This function does the following:
url
url
or keeps the existing oneurl
are emtpy, and you do not keep/merge, then the will be replaced by an empty query/fragment