![]() |
Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
|
Convenience class for grouping remote server endpoints that share common network request properties. More...
#include <qnetworkrequestfactory.h>
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 QNetworkRequestFactory & | operator= (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. | |
Convenience class for grouping remote server endpoints that share common network request properties.
\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
An example of usage:
Definition at line 28 of file qnetworkrequestfactory.h.
QNetworkRequestFactory::QNetworkRequestFactory | ( | ) |
Creates a new QNetworkRequestFactory object.
Use setBaseUrl() to set a valid base URL for the requests.
Definition at line 56 of file qnetworkrequestfactory.cpp.
|
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:
Definition at line 73 of file qnetworkrequestfactory.cpp.
|
default |
Destroys this QNetworkRequestFactory object.
|
default |
Creates a copy of other.
|
defaultnoexcept |
Move-constructs the factory from other.
QVariant QNetworkRequestFactory::attribute | ( | QNetworkRequest::Attribute | attribute | ) | const |
Returns the value associated with attribute. If the attribute has not been set, returns a default-constructed \l QVariant.
Definition at line 545 of file qnetworkrequestfactory.cpp.
QVariant QNetworkRequestFactory::attribute | ( | QNetworkRequest::Attribute | attribute, |
const QVariant & | defaultValue ) const |
Returns the value associated with attribute. If the attribute has not been set, returns defaultValue.
Definition at line 559 of file qnetworkrequestfactory.cpp.
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.
Definition at line 130 of file qnetworkrequestfactory.cpp.
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.
Definition at line 289 of file qnetworkrequestfactory.cpp.
void QNetworkRequestFactory::clearAttribute | ( | QNetworkRequest::Attribute | attribute | ) |
Clears attribute set to this factory.
Definition at line 572 of file qnetworkrequestfactory.cpp.
void QNetworkRequestFactory::clearAttributes | ( | ) |
Clears any attributes set to this factory.
Definition at line 587 of file qnetworkrequestfactory.cpp.
void QNetworkRequestFactory::clearBearerToken | ( | ) |
Clears the bearer token.
Definition at line 313 of file qnetworkrequestfactory.cpp.
void QNetworkRequestFactory::clearCommonHeaders | ( | ) |
Clears current headers.
Definition at line 262 of file qnetworkrequestfactory.cpp.
void QNetworkRequestFactory::clearPassword | ( | ) |
Clears the password set to this factory.
Definition at line 394 of file qnetworkrequestfactory.cpp.
void QNetworkRequestFactory::clearQueryParameters | ( | ) |
Clears the query parameters.
Definition at line 465 of file qnetworkrequestfactory.cpp.
void QNetworkRequestFactory::clearUserName | ( | ) |
Clears the username set to this factory.
Definition at line 353 of file qnetworkrequestfactory.cpp.
QHttpHeaders QNetworkRequestFactory::commonHeaders | ( | ) | const |
Returns the currently set headers.
Definition at line 252 of file qnetworkrequestfactory.cpp.
QNetworkRequest QNetworkRequestFactory::createRequest | ( | ) | const |
Returns a QNetworkRequest.
The returned request is filled with the data that this factory has been configured with.
Definition at line 185 of file qnetworkrequestfactory.cpp.
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).
Definition at line 198 of file qnetworkrequestfactory.cpp.
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.
Definition at line 227 of file qnetworkrequestfactory.cpp.
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.
Definition at line 211 of file qnetworkrequestfactory.cpp.
|
default |
Creates a copy of other and returns a reference to this factory.
Move-assigns other and returns a reference to this factory.
QString QNetworkRequestFactory::password | ( | ) | const |
Returns the password set to this factory.
Definition at line 366 of file qnetworkrequestfactory.cpp.
QNetworkRequest::Priority QNetworkRequestFactory::priority | ( | ) | const |
Returns the priority assigned to any future requests created by this factory.
Definition at line 500 of file qnetworkrequestfactory.cpp.
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.
Definition at line 440 of file qnetworkrequestfactory.cpp.
void QNetworkRequestFactory::setAttribute | ( | QNetworkRequest::Attribute | attribute, |
const QVariant & | value ) |
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.
Definition at line 516 of file qnetworkrequestfactory.cpp.
Sets the base URL used in individual requests to url.
Definition at line 140 of file qnetworkrequestfactory.cpp.
void QNetworkRequestFactory::setBearerToken | ( | const QByteArray & | token | ) |
Sets the bearer token to token.
Definition at line 299 of file qnetworkrequestfactory.cpp.
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.
Definition at line 241 of file qnetworkrequestfactory.cpp.
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.
Definition at line 381 of file qnetworkrequestfactory.cpp.
void QNetworkRequestFactory::setPriority | ( | QNetworkRequest::Priority | priority | ) |
Sets the priority for any future requests created by this factory to priority.
The default priority is \l QNetworkRequest::NormalPriority.
Definition at line 484 of file qnetworkrequestfactory.cpp.
Sets query parameters that are added to individual requests' query parameters.
Definition at line 451 of file qnetworkrequestfactory.cpp.
void QNetworkRequestFactory::setTransferTimeout | ( | std::chrono::milliseconds | timeout | ) |
Sets timeout used for transfers.
Definition at line 408 of file qnetworkrequestfactory.cpp.
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.
Definition at line 342 of file qnetworkrequestfactory.cpp.
|
inlinenoexcept |
\memberswap{factory}
Definition at line 40 of file qnetworkrequestfactory.h.
std::chrono::milliseconds QNetworkRequestFactory::transferTimeout | ( | ) | const |
Returns the timeout used for transfers.
Definition at line 423 of file qnetworkrequestfactory.cpp.
QString QNetworkRequestFactory::userName | ( | ) | const |
Returns the username set to this factory.
Definition at line 327 of file qnetworkrequestfactory.cpp.
|
friend |
Writes factory into debug stream.
Definition at line 691 of file qnetworkrequestfactory.cpp.