QtRestClient  3.0.0
A library for generic JSON-based REST-APIs, with a mechanism to map JSON to Qt objects
Classes | Public Member Functions | List of all members
QtRestClient::RequestBuilder Class Reference

A helper class to build QUrl and QNetworkRequest objects. More...

#include <requestbuilder.h>

+ Inheritance diagram for QtRestClient::RequestBuilder:

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.
 
RequestBuilderoperator= (const RequestBuilder &other)
 Copy assignment operator.
 
RequestBuilderoperator= (RequestBuilder &&other) noexcept
 Move assignment operator.
 
RequestBuildersetNetworkAccessManager (QNetworkAccessManager *nam)
 Sets the network access manager to be used for send() More...
 
RequestBuildersetExtender (IExtender *extender)
 Sets the extender to use for extending the build. More...
 
RequestBuildersetCredentials (QString user, QString password={})
 Sets the credentails of the URL. More...
 
RequestBuildersetVersion (QVersionNumber version)
 Sets the version of the API. More...
 
RequestBuilderaddPath (const QString &pathSegment)
 appends a path segment to the builders path More...
 
RequestBuilderaddPath (const QStringList &pathSegment)
 appends a path segment to the builders path More...
 
RequestBuildertrailingSlash (bool enable=true)
 Enables the trailing slash for the generated URL. More...
 
RequestBuilderaddParameter (const QString &name, const QString &value)
 Adds a parameter to the URL. More...
 
RequestBuilderaddParameters (const QUrlQuery &parameters)
 Adds parameters to the URL. More...
 
RequestBuildersetFragment (QString fragment)
 Sets the fragment part of the URL. More...
 
RequestBuilderaddHeader (const QByteArray &name, const QByteArray &value)
 Adds a HTTP header to be added to the network request. More...
 
RequestBuilderaddHeaders (const HeaderHash &headers)
 Adds HTTP headers to be added to the network request. More...
 
RequestBuilderupdateFromRelativeUrl (const QUrl &url, bool mergeQuery=false, bool keepFragment=false)
 Updates the builder from the (relative) URL and includes all of it's elements. More...
 
RequestBuildersetAttribute (QNetworkRequest::Attribute attribute, const QVariant &value)
 Sets the given attribute on the generated network request. More...
 
RequestBuildersetAttributes (const QHash< QNetworkRequest::Attribute, QVariant > &attributes)
 Sets the given attributes on the generated network request. More...
 
RequestBuildersetSslConfig (QSslConfiguration sslConfig)
 Sets the ssl configuration to be used by the network request. More...
 
RequestBuildersetBody (QByteArray body, const QByteArray &contentType, bool setAccept=true)
 Sets the content of the generated network request. More...
 
RequestBuildersetBody (QCborValue body, bool setAccept=true)
 Sets the content of the generated network request. More...
 
RequestBuildersetBody (const QJsonValue &body, bool setAccept=true)
 Sets the content of the generated network request. More...
 
RequestBuildersetVerb (QByteArray verb)
 Sets the HTTP-Verb to be used by the generated network request. More...
 
RequestBuildersetAccept (const QByteArray &mimeType)
 Sets the "Accept" HTTP-header to the given mimetype. More...
 
RequestBuildersetAccept (const QMimeType &mimeType)
 Sets the "Accept" HTTP-header to the given mimetype. More...
 
RequestBuilderaddPostParameter (const QString &name, const QString &value)
 Adds a post parameter to the body. More...
 
RequestBuilderaddPostParameters (const QUrlQuery &parameters)
 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...
 
QNetworkReplysend () 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...
 

Detailed Description

A helper class to build QUrl and QNetworkRequest objects.

The builder is used by RestClient, RestClass and other components to build the API requests.

See also
RestClient::builder, RestClass::builer

Definition at line 24 of file requestbuilder.h.

Constructor & Destructor Documentation

◆ RequestBuilder()

QtRestClient::RequestBuilder::RequestBuilder ( const QUrl baseUrl,
QNetworkAccessManager nam = nullptr 
)

Constructs a builder with the given base url.

Parameters
namThe network access manager to be used. Can be nullptr
baseUrlThe base URL to build ontop of

The base URL is required in order to build. As the name suggests, it's used as base

See also
RestClient::baseUrl, RequestBuilder::send

Member Function Documentation

◆ addHeader()

QtRestClient::RequestBuilder::addHeader ( const QByteArray name,
const QByteArray value 
)

Adds a HTTP header to be added to the network request.

Parameters
nameThe header name
valueThe header value
Returns
A reference to this builder
See also
RestClient::globalHeaders, QNetworkRequest::setRawHeader

◆ addHeaders()

QtRestClient::RequestBuilder::addHeaders ( const HeaderHash headers)

Adds HTTP headers to be added to the network request.

Parameters
headersThe headers to be set
Returns
A reference to this builder
See also
RestClient::globalHeaders, QNetworkRequest::setRawHeader

◆ addParameter()

QtRestClient::RequestBuilder::addParameter ( const QString name,
const QString value 
)

Adds a parameter to the URL.

Parameters
nameThe parameters name
valueThe parameters value
Returns
A reference to this builder
See also
RestClient::globalParameters, QUrl::setQuery(const QUrlQuery &)

◆ addParameters()

QtRestClient::RequestBuilder::addParameters ( const QUrlQuery parameters)

Adds parameters to the URL.

Parameters
parametersThe parameters to be set
Returns
A reference to this builder
See also
RestClient::globalParameters, QUrl::setQuery(const QUrlQuery &)

◆ addPath() [1/2]

QtRestClient::RequestBuilder::addPath ( const QString pathSegment)

appends a path segment to the builders path

Parameters
pathSegmentThe path to be added to the builders URL
Returns
A reference to this builder
See also
RestClient::createClass, RequestBuilder::trailingSlash, QUrl::setPath

◆ addPath() [2/2]

RequestBuilder& QtRestClient::RequestBuilder::addPath ( const QStringList pathSegment)

appends a path segment to the builders path

Parameters
pathSegmentThe path to be added to the builders URL
Returns
A reference to this builder
See also
RestClient::createClass, RequestBuilder::trailingSlash, QUrl::setPath

◆ addPostParameter()

QtRestClient::RequestBuilder::addPostParameter ( const QString name,
const QString value 
)

Adds a post parameter to the body.

Parameters
nameThe parameters name
valueThe parameters value
Returns
A reference to this builder
See also
RestClient::globalParameters, QUrl::setQuery(const QUrlQuery &)

◆ addPostParameters()

QtRestClient::RequestBuilder::addPostParameters ( const QUrlQuery parameters)

Adds post parameters to the body.

Parameters
parametersThe parameters to be set
Returns
A reference to this builder
See also
RestClient::globalParameters, QUrl::setQuery(const QUrlQuery &)

◆ build()

QtRestClient::RequestBuilder::build ( ) const

Creates a network request from the builder settings.

Returns
The generated network request
See also
RequestBuilder::buildUrl, RequestBuilder::send

◆ buildUrl()

QtRestClient::RequestBuilder::buildUrl ( ) const

Creates a URL from the builder settings.

Returns
The generated URL
See also
RequestBuilder::build, RequestBuilder::send

◆ send()

QtRestClient::RequestBuilder::send ( ) const

Creates a network request and sends it with the builder settings.

Returns
The network reply for the sent request
See also
RequestBuilder::sendAsync, RequestBuilder::buildUrl, RequestBuilder::build

◆ sendAsync()

QtRestClient::RequestBuilder::sendAsync ( ) const

Asynchronously creates a network request and sends it with the builder settings.

Returns
A future for the network reply for the sent request

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.

Warning
Please read the Multithreading section of the README before using this method.
See also
RequestBuilder::send, RequestBuilder::buildUrl, RequestBuilder::build

◆ setAccept() [1/2]

QtRestClient::RequestBuilder::setAccept ( const QByteArray mimeType)

Sets the "Accept" HTTP-header to the given mimetype.

Parameters
mimeTypeThe mimeType to be set as the "Accept" header
Returns
A reference to this builder
See also
RequestBuilder::addHeader, RequestBuilder::setBody

◆ setAccept() [2/2]

RequestBuilder& QtRestClient::RequestBuilder::setAccept ( const QMimeType mimeType)

Sets the "Accept" HTTP-header to the given mimetype.

Parameters
mimeTypeThe mimeType to be set as the "Accept" header
Returns
A reference to this builder
See also
RequestBuilder::addHeader, RequestBuilder::setBody

◆ setAttribute()

QtRestClient::RequestBuilder::setAttribute ( QNetworkRequest::Attribute  attribute,
const QVariant value 
)

Sets the given attribute on the generated network request.

Parameters
attributeThe attribute to be set
valueThe attributes value
Returns
A reference to this builder
Note
This property has no effect if using buildUrl()
See also
RestClient::requestAttributes, QNetworkRequest::setAttribute

◆ setAttributes()

QtRestClient::RequestBuilder::setAttributes ( const QHash< QNetworkRequest::Attribute, QVariant > &  attributes)

Sets the given attributes on the generated network request.

Parameters
attributesThe attributes to be set
Returns
A reference to this builder
Note
This property has no effect if using buildUrl()
See also
RestClient::requestAttributes, QNetworkRequest::setAttribute

◆ setBody() [1/3]

QtRestClient::RequestBuilder::setBody ( const QJsonValue body,
bool  setAccept = true 
)

Sets the content of the generated network request.

Parameters
bodyThe JSON data to be sent as Content (sets Content-Type to "application/json")
setAcceptIf set to true, the "Accept" header will be set to "application/json" as well
Returns
A reference to this builder
Note
This property is used by send() only!
See also
QNetworkAccessManager::sendCustomRequest, RequestBuilder::setAccept

◆ setBody() [2/3]

QtRestClient::RequestBuilder::setBody ( QByteArray  body,
const QByteArray contentType,
bool  setAccept = true 
)

Sets the content of the generated network request.

Parameters
bodyThe data to be sent as Content
contentTypeThe content type for the Content
setAcceptIf set to true, the "Accept" header will be set to the contentType
Returns
A reference to this builder
Note
This property is used by send() only!
See also
QNetworkAccessManager::sendCustomRequest, RequestBuilder::setAccept

◆ setBody() [3/3]

QtRestClient::RequestBuilder::setBody ( QCborValue  body,
bool  setAccept = true 
)

Sets the content of the generated network request.

Parameters
bodyThe CBOR data to be sent as Content (sets Content-Type to "application/cbor")
setAcceptIf set to true, the "Accept" header will be set to "application/cbor" as well
Returns
A reference to this builder
Note
This property is used by send() only!
See also
QNetworkAccessManager::sendCustomRequest, RequestBuilder::setAccept

◆ setCredentials()

QtRestClient::RequestBuilder::setCredentials ( QString  user,
QString  password = {} 
)

Sets the credentails of the URL.

Parameters
userThe username to be set on the URL
passwordThe password to be set on the URL
Returns
A reference to this builder
See also
QUrl::setUserInfo

◆ setExtender()

QtRestClient::RequestBuilder::setExtender ( IExtender extender)

Sets the extender to use for extending the build.

Parameters
extenderThe extender to be used for extending when building
Returns
A reference to this builder

The extender can be used to change the generated URL/request as last step of the building process.

See also
RequestBuilder::IExtender

◆ setFragment()

QtRestClient::RequestBuilder::setFragment ( QString  fragment)

Sets the fragment part of the URL.

Parameters
fragmentThe URL fragment to be set
Returns
A reference to this builder
See also
QUrl::setFragment

◆ setNetworkAccessManager()

QtRestClient::RequestBuilder::setNetworkAccessManager ( QNetworkAccessManager nam)

Sets the network access manager to be used for send()

Parameters
namThe network access manager to use for sending requests
Returns
A reference to this builder
See also
RequestBuilder::RequestBuilder, QNetworkAccessManager

◆ setSslConfig()

QtRestClient::RequestBuilder::setSslConfig ( QSslConfiguration  sslConfig)

Sets the ssl configuration to be used by the network request.

Parameters
sslConfigThe ssl configuration to be used
Returns
A reference to this builder

If you don't specify a ssl configuration, the builder will use the default configuration

Note
This property has no effect if using buildUrl()
See also
RestClient::sslConfiguration, QNetworkRequest::setSslConfiguration, QSslConfiguration::defaultConfiguration

◆ setVerb()

QtRestClient::RequestBuilder::setVerb ( QByteArray  verb)

Sets the HTTP-Verb to be used by the generated network request.

Parameters
verbThe HTTP-Verb to be used
Returns
A reference to this builder

If this property is not set, the HTTP-GET will be used as default

Note
This property is used by send() only!
See also
QNetworkAccessManager::sendCustomRequest

◆ setVersion()

QtRestClient::RequestBuilder::setVersion ( QVersionNumber  version)

Sets the version of the API.

Parameters
versionThe API version
Returns
A reference to this builder

See RestClient::apiVersion for details

See also
RestClient::apiVersion

◆ trailingSlash()

QtRestClient::RequestBuilder::trailingSlash ( bool  enable = true)

Enables the trailing slash for the generated URL.

Parameters
enableEnable/disable the trailing slash of the path
Returns
A reference to this builder

Enabled: http://example.com/my/path/?id=42
Disabled: http://example.com/my/path?id=42

See also
RequestBuilder::addPath(const QString &), QUrl::setPath

◆ updateFromRelativeUrl()

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.

Parameters
urlThe (relative) URL to update from
mergeQuerySpecifies, whether the url queries should be merged
keepFragmentSpecifies, whether the fragment should be kept or not
Returns
A reference to this builder

This function does the following:

  • Update the builders url host and path from url
  • Either merges the query of URL with the exiting one or replaces it
  • Either takes the fragment of url or keeps the existing one
Note
If either the query or the fragment of url are emtpy, and you do not keep/merge, then the will be replaced by an empty query/fragment
See also
QUrl::resolved

The documentation for this class was generated from the following files: