Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QNetworkRequestFactory Class Reference

Convenience class for grouping remote server endpoints that share common network request properties. More...

#include <qnetworkrequestfactory.h>

+ Collaboration diagram for QNetworkRequestFactory:

Public Member Functions

Q_NETWORK_EXPORT QNetworkRequestFactory ()
 Creates a new QNetworkRequestFactory object.
 
Q_NETWORK_EXPORT QNetworkRequestFactory (const QUrl &baseUrl)
 Creates a new QNetworkRequestFactory object, initializing the base URL to baseUrl.
 
Q_NETWORK_EXPORT ~QNetworkRequestFactory ()
 Destroys this QNetworkRequestFactory object.
 
Q_NETWORK_EXPORT QNetworkRequestFactory (const QNetworkRequestFactory &other)
 Creates a copy of other.
 
 QNetworkRequestFactory (QNetworkRequestFactory &&other) noexcept=default
 Move-constructs the factory from other.
 
Q_NETWORK_EXPORT QNetworkRequestFactoryoperator= (const QNetworkRequestFactory &other)
 Creates a copy of other and returns a reference to this factory.
 
void swap (QNetworkRequestFactory &other) noexcept
 \memberswap{factory}
 
Q_NETWORK_EXPORT QUrl baseUrl () const
 Returns the base URL used for the individual requests.
 
Q_NETWORK_EXPORT void setBaseUrl (const QUrl &url)
 Sets the base URL used in individual requests to url.
 
Q_NETWORK_EXPORT QNetworkRequest createRequest () const
 Returns a QNetworkRequest.
 
Q_NETWORK_EXPORT QNetworkRequest createRequest (const QUrlQuery &query) const
 Returns a QNetworkRequest.
 
Q_NETWORK_EXPORT QNetworkRequest createRequest (const QString &path) const
 Returns a QNetworkRequest.
 
Q_NETWORK_EXPORT QNetworkRequest createRequest (const QString &path, const QUrlQuery &query) const
 Returns a QNetworkRequest.
 
Q_NETWORK_EXPORT void setCommonHeaders (const QHttpHeaders &headers)
 Sets headers that are common to all requests.
 
Q_NETWORK_EXPORT QHttpHeaders commonHeaders () const
 Returns the currently set headers.
 
Q_NETWORK_EXPORT void clearCommonHeaders ()
 Clears current headers.
 
Q_NETWORK_EXPORT QByteArray bearerToken () const
 Returns the bearer token that has been set.
 
Q_NETWORK_EXPORT void setBearerToken (const QByteArray &token)
 Sets the bearer token to token.
 
Q_NETWORK_EXPORT void clearBearerToken ()
 Clears the bearer token.
 
Q_NETWORK_EXPORT QString userName () const
 Returns the username set to this factory.
 
Q_NETWORK_EXPORT void setUserName (const QString &userName)
 Sets the username of this factory to userName.
 
Q_NETWORK_EXPORT void clearUserName ()
 Clears the username set to this factory.
 
Q_NETWORK_EXPORT QString password () const
 Returns the password set to this factory.
 
Q_NETWORK_EXPORT void setPassword (const QString &password)
 Sets the password of this factory to password.
 
Q_NETWORK_EXPORT void clearPassword ()
 Clears the password set to this factory.
 
Q_NETWORK_EXPORT void setTransferTimeout (std::chrono::milliseconds timeout)
 Sets timeout used for transfers.
 
Q_NETWORK_EXPORT std::chrono::milliseconds transferTimeout () const
 Returns the timeout used for transfers.
 
Q_NETWORK_EXPORT QUrlQuery queryParameters () const
 Returns query parameters that are added to individual requests' query parameters.
 
Q_NETWORK_EXPORT void setQueryParameters (const QUrlQuery &query)
 Sets query parameters that are added to individual requests' query parameters.
 
Q_NETWORK_EXPORT void clearQueryParameters ()
 Clears the query parameters.
 
Q_NETWORK_EXPORT void setPriority (QNetworkRequest::Priority priority)
 
Q_NETWORK_EXPORT QNetworkRequest::Priority priority () const
 
Q_NETWORK_EXPORT QVariant attribute (QNetworkRequest::Attribute attribute) const
 
Q_NETWORK_EXPORT QVariant attribute (QNetworkRequest::Attribute attribute, const QVariant &defaultValue) const
 
Q_NETWORK_EXPORT void setAttribute (QNetworkRequest::Attribute attribute, const QVariant &value)
 
Q_NETWORK_EXPORT void clearAttribute (QNetworkRequest::Attribute attribute)
 
Q_NETWORK_EXPORT void clearAttributes ()
 

Friends

Q_NETWORK_EXPORT QDebug operator<< (QDebug debug, const QNetworkRequestFactory &reply)
 Writes factory into debug stream.
 

Detailed Description

Convenience class for grouping remote server endpoints that share common network request properties.

Since
6.7

\inmodule QtNetwork

REST servers often have endpoints that require the same headers and other data. Grouping such endpoints with a QNetworkRequestFactory makes it more convenient to issue requests to these endpoints; only the typically varying parts such as path and query parameters are provided when creating a new request.

Basic usage steps of QNetworkRequestFactory are as follows: \list

  • Instantiation
  • Setting the data common to all requests
  • Issuing requests \endlist

An example of usage:

// Instantiate a factory somewhere suitable in the application
QNetworkRequestFactory api{{"https://example.com/v1"_L1}};
// Set bearer token
api.setBearerToken("my_token");
// Issue requests (reply handling omitted for brevity)
manager.get(api.createRequest("models"_L1)); // https://example.com/v1/models
// The conventional leading '/' for the path can be used as well
manager.get(api.createRequest("/models"_L1)); // https://example.com/v1/models

Definition at line 28 of file qnetworkrequestfactory.h.

Constructor & Destructor Documentation

◆ QNetworkRequestFactory() [1/4]

QNetworkRequestFactory::QNetworkRequestFactory ( )

Creates a new QNetworkRequestFactory object.

Use setBaseUrl() to set a valid base URL for the requests.

See also
QNetworkRequestFactory(const QUrl &baseUrl), setBaseUrl()

Definition at line 56 of file qnetworkrequestfactory.cpp.

◆ QNetworkRequestFactory() [2/4]

QNetworkRequestFactory::QNetworkRequestFactory ( const QUrl & baseUrl)
explicit

Creates a new QNetworkRequestFactory object, initializing the base URL to baseUrl.

The base URL is used to populate subsequent network requests.

If the URL contains a path component, it will be extracted and used as a base path in subsequent network requests. This means that any paths provided when requesting individual requests will be appended to this base path, as illustrated below:

// Here the API version v2 is used as the base path:
QNetworkRequestFactory api{{"https://example.com/v2"_L1}};
// ...
manager.get(api.createRequest("models"_L1)); // https://example.com/v2/models
// Equivalent with a leading '/'
manager.get(api.createRequest("/models"_L1)); // https://example.com/v2/models

Definition at line 73 of file qnetworkrequestfactory.cpp.

◆ ~QNetworkRequestFactory()

QNetworkRequestFactory::~QNetworkRequestFactory ( )
default

Destroys this QNetworkRequestFactory object.

◆ QNetworkRequestFactory() [3/4]

QNetworkRequestFactory::QNetworkRequestFactory ( const QNetworkRequestFactory & other)
default

Creates a copy of other.

◆ QNetworkRequestFactory() [4/4]

QNetworkRequestFactory::QNetworkRequestFactory ( QNetworkRequestFactory && other)
defaultnoexcept

Move-constructs the factory from other.

Note
The moved-from object other is placed in a partially-formed state, in which the only valid operations are destruction and assignment of a new value.

Member Function Documentation

◆ attribute() [1/2]

QVariant QNetworkRequestFactory::attribute ( QNetworkRequest::Attribute attribute) const
Since
6.8

Returns the value associated with attribute. If the attribute has not been set, returns a default-constructed \l QVariant.

See also
attribute(QNetworkRequest::Attribute, const QVariant &), setAttribute(), clearAttributes(), QNetworkRequest::Attribute

Definition at line 545 of file qnetworkrequestfactory.cpp.

◆ attribute() [2/2]

QVariant QNetworkRequestFactory::attribute ( QNetworkRequest::Attribute attribute,
const QVariant & defaultValue ) const
Since
6.8

Returns the value associated with attribute. If the attribute has not been set, returns defaultValue.

See also
attribute(), setAttribute(), clearAttributes(), QNetworkRequest::Attribute

Definition at line 559 of file qnetworkrequestfactory.cpp.

◆ baseUrl()

QUrl QNetworkRequestFactory::baseUrl ( ) const

Returns the base URL used for the individual requests.

The base URL may contain a path component. This path is used as path "prefix" for the paths that are provided when generating individual requests.

See also
setBaseUrl()

Definition at line 130 of file qnetworkrequestfactory.cpp.

◆ bearerToken()

QByteArray QNetworkRequestFactory::bearerToken ( ) const

Returns the bearer token that has been set.

The bearer token, if present, is used to set the {Authorization: Bearer my_token} header for requests. This is a common authorization convention and is provided as an additional convenience.

The means to acquire the bearer token vary. Standard methods include OAuth2 and the service provider's website/dashboard. It is expected that the bearer token changes over time. For example, when updated with a refresh token, always setting the new token again ensures that subsequent requests have the latest, valid token.

The presence of the bearer token does not impact the \l commonHeaders() listing. If the \l commonHeaders() also lists Authorization header, it will be overwritten.

See also
setBearerToken(), commonHeaders()

Definition at line 289 of file qnetworkrequestfactory.cpp.

◆ clearAttribute()

void QNetworkRequestFactory::clearAttribute ( QNetworkRequest::Attribute attribute)
Since
6.8

Clears attribute set to this factory.

See also
attribute(), setAttribute()

Definition at line 572 of file qnetworkrequestfactory.cpp.

◆ clearAttributes()

void QNetworkRequestFactory::clearAttributes ( )
Since
6.8

Clears any attributes set to this factory.

See also
attribute(), setAttribute()

Definition at line 587 of file qnetworkrequestfactory.cpp.

◆ clearBearerToken()

void QNetworkRequestFactory::clearBearerToken ( )

Clears the bearer token.

See also
bearerToken()

Definition at line 313 of file qnetworkrequestfactory.cpp.

◆ clearCommonHeaders()

void QNetworkRequestFactory::clearCommonHeaders ( )

Clears current headers.

See also
commonHeaders(), setCommonHeaders()

Definition at line 262 of file qnetworkrequestfactory.cpp.

◆ clearPassword()

void QNetworkRequestFactory::clearPassword ( )

Clears the password set to this factory.

See also
password(), setPassword(), userName()

Definition at line 394 of file qnetworkrequestfactory.cpp.

◆ clearQueryParameters()

void QNetworkRequestFactory::clearQueryParameters ( )

Clears the query parameters.

See also
queryParameters()

Definition at line 465 of file qnetworkrequestfactory.cpp.

◆ clearUserName()

void QNetworkRequestFactory::clearUserName ( )

Clears the username set to this factory.

Definition at line 353 of file qnetworkrequestfactory.cpp.

◆ commonHeaders()

QHttpHeaders QNetworkRequestFactory::commonHeaders ( ) const

Returns the currently set headers.

See also
setCommonHeaders(), clearCommonHeaders()

Definition at line 252 of file qnetworkrequestfactory.cpp.

◆ createRequest() [1/4]

QNetworkRequest QNetworkRequestFactory::createRequest ( ) const

Returns a QNetworkRequest.

The returned request is filled with the data that this factory has been configured with.

See also
createRequest(const QUrlQuery&), createRequest(const QString&, const QUrlQuery&)

Definition at line 185 of file qnetworkrequestfactory.cpp.

◆ createRequest() [2/4]

QNetworkRequest QNetworkRequestFactory::createRequest ( const QString & path) const

Returns a QNetworkRequest.

The returned request's URL is formed by appending the provided path to the baseUrl (which may itself have a path component).

See also
createRequest(const QString &, const QUrlQuery &), createRequest(), baseUrl()

Definition at line 198 of file qnetworkrequestfactory.cpp.

◆ createRequest() [3/4]

QNetworkRequest QNetworkRequestFactory::createRequest ( const QString & path,
const QUrlQuery & query ) const

Returns a QNetworkRequest.

The returned requests URL is formed by appending the provided path and query to the baseUrl (which may have a path component).

If the provided path contains query items, they will be combined with the items in query.

See also
createRequest(const QUrlQuery&), createRequest(), baseUrl()

Definition at line 227 of file qnetworkrequestfactory.cpp.

◆ createRequest() [4/4]

QNetworkRequest QNetworkRequestFactory::createRequest ( const QUrlQuery & query) const

Returns a QNetworkRequest.

The returned request's URL is formed by appending the provided query to the baseUrl.

See also
createRequest(const QString &, const QUrlQuery &), createRequest(), baseUrl()

Definition at line 211 of file qnetworkrequestfactory.cpp.

◆ operator=()

QNetworkRequestFactory & QNetworkRequestFactory::operator= ( const QNetworkRequestFactory & other)
default

Creates a copy of other and returns a reference to this factory.

Move-assigns other and returns a reference to this factory.

Note
The moved-from object other is placed in a partially-formed state, in which the only valid operations are destruction and assignment of a new value.

◆ password()

QString QNetworkRequestFactory::password ( ) const

Returns the password set to this factory.

See also
password(), clearPassword(), userName()

Definition at line 366 of file qnetworkrequestfactory.cpp.

◆ priority()

QNetworkRequest::Priority QNetworkRequestFactory::priority ( ) const
Since
6.8

Returns the priority assigned to any future requests created by this factory.

See also
setPriority(), QNetworkRequest::priority()

Definition at line 500 of file qnetworkrequestfactory.cpp.

◆ queryParameters()

QUrlQuery QNetworkRequestFactory::queryParameters ( ) const

Returns query parameters that are added to individual requests' query parameters.

The query parameters are added to any potential query parameters provided with the individual \l createRequest() calls.

Use cases for using repeating query parameters are server dependent, but typical examples include language setting {?lang=en}, format specification {?format=json}, API version specification {?version=1.0} and API key authentication.

See also
setQueryParameters(), clearQueryParameters(), createRequest()

Definition at line 440 of file qnetworkrequestfactory.cpp.

◆ setAttribute()

void QNetworkRequestFactory::setAttribute ( QNetworkRequest::Attribute attribute,
const QVariant & value )
Since
6.8

Sets the value associated with attribute to value. If the attribute is already set, the previous value is replaced. The attributes are set to any future requests created by this factory.

See also
attribute(), clearAttribute(), clearAttributes(), QNetworkRequest::Attribute

Definition at line 516 of file qnetworkrequestfactory.cpp.

◆ setBaseUrl()

void QNetworkRequestFactory::setBaseUrl ( const QUrl & url)

Sets the base URL used in individual requests to url.

See also
baseUrl()

Definition at line 140 of file qnetworkrequestfactory.cpp.

◆ setBearerToken()

void QNetworkRequestFactory::setBearerToken ( const QByteArray & token)

Sets the bearer token to token.

See also
bearerToken(), clearBearerToken()

Definition at line 299 of file qnetworkrequestfactory.cpp.

◆ setCommonHeaders()

void QNetworkRequestFactory::setCommonHeaders ( const QHttpHeaders & headers)

Sets headers that are common to all requests.

These headers are added to individual requests' headers. This is a convenience mechanism for setting headers that repeat across requests.

See also
commonHeaders(), clearCommonHeaders(), createRequest()

Definition at line 241 of file qnetworkrequestfactory.cpp.

◆ setPassword()

void QNetworkRequestFactory::setPassword ( const QString & password)

Sets the password of this factory to password.

The password is set in the request URL when \l createRequest() is called. The QRestAccessManager / QNetworkAccessManager will attempt to use these credentials when the server indicates that authentication is required.

See also
password(), clearPassword(), userName()

Definition at line 381 of file qnetworkrequestfactory.cpp.

◆ setPriority()

void QNetworkRequestFactory::setPriority ( QNetworkRequest::Priority priority)
Since
6.8

Sets the priority for any future requests created by this factory to priority.

The default priority is \l QNetworkRequest::NormalPriority.

See also
priority(), QNetworkRequest::setPriority()

Definition at line 484 of file qnetworkrequestfactory.cpp.

◆ setQueryParameters()

void QNetworkRequestFactory::setQueryParameters ( const QUrlQuery & query)

Sets query parameters that are added to individual requests' query parameters.

See also
queryParameters(), clearQueryParameters()

Definition at line 451 of file qnetworkrequestfactory.cpp.

◆ setTransferTimeout()

void QNetworkRequestFactory::setTransferTimeout ( std::chrono::milliseconds timeout)

Sets timeout used for transfers.

See also
transferTimeout(), QNetworkRequest::setTransferTimeout(), QNetworkAccessManager::setTransferTimeout()

Definition at line 408 of file qnetworkrequestfactory.cpp.

◆ setUserName()

void QNetworkRequestFactory::setUserName ( const QString & userName)

Sets the username of this factory to userName.

The username is set in the request URL when \l createRequest() is called. The QRestAccessManager / QNetworkAccessManager will attempt to use these credentials when the server indicates that authentication is required.

See also
userName(), clearUserName(), password()

Definition at line 342 of file qnetworkrequestfactory.cpp.

◆ swap()

void QNetworkRequestFactory::swap ( QNetworkRequestFactory & other)
inlinenoexcept

\memberswap{factory}

Definition at line 40 of file qnetworkrequestfactory.h.

◆ transferTimeout()

std::chrono::milliseconds QNetworkRequestFactory::transferTimeout ( ) const

Returns the timeout used for transfers.

See also
setTransferTimeout(), QNetworkRequest::transferTimeout(), QNetworkAccessManager::transferTimeout()

Definition at line 423 of file qnetworkrequestfactory.cpp.

◆ userName()

QString QNetworkRequestFactory::userName ( ) const

Returns the username set to this factory.

See also
setUserName(), clearUserName(), password()

Definition at line 327 of file qnetworkrequestfactory.cpp.

Friends And Related Symbol Documentation

◆ operator<<

Q_NETWORK_EXPORT QDebug operator<< ( QDebug debug,
const QNetworkRequestFactory & factory )
friend

Writes factory into debug stream.

See also
{Debugging Techniques}

Definition at line 691 of file qnetworkrequestfactory.cpp.


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