Convenience class for grouping remote server endpoints that share common network request properties. 更多...
| 頭: |
#include <QNetworkRequestFactory>
|
| CMake: |
find_package(Qt6 REQUIRED COMPONENTS Network)
target_link_libraries(mytarget PRIVATE Qt6::Network)
|
| qmake: |
QT += network
|
| Since: | Qt 6.7 |
| QNetworkRequestFactory () | |
| QNetworkRequestFactory (const QUrl & baseUrl ) | |
| QNetworkRequestFactory (const QNetworkRequestFactory & other ) | |
| QNetworkRequestFactory (QNetworkRequestFactory && other ) | |
| ~QNetworkRequestFactory () | |
(從 6.8 起)
QVariant
|
attribute (QNetworkRequest::Attribute attribute ) const |
(從 6.8 起)
QVariant
|
attribute (QNetworkRequest::Attribute attribute , const QVariant & defaultValue ) const |
| QUrl | baseUrl () const |
| QByteArray | bearerToken () const |
(從 6.8 起)
void
|
clearAttribute (QNetworkRequest::Attribute attribute ) |
(從 6.8 起)
void
|
clearAttributes () |
| void | clearBearerToken () |
| void | clearCommonHeaders () |
| void | clearPassword () |
| void | clearQueryParameters () |
| void | clearUserName () |
| QHttpHeaders | commonHeaders () const |
| QNetworkRequest | createRequest () const |
| QNetworkRequest | createRequest (const QString & path ) const |
| QNetworkRequest | createRequest (const QUrlQuery & query ) const |
| QNetworkRequest | createRequest (const QString & path , const QUrlQuery & query ) const |
| QString | password () const |
(從 6.8 起)
QNetworkRequest::Priority
|
priority () const |
| QUrlQuery | queryParameters () const |
(從 6.8 起)
void
|
setAttribute (QNetworkRequest::Attribute attribute , const QVariant & value ) |
| void | setBaseUrl (const QUrl & url ) |
| void | setBearerToken (const QByteArray & token ) |
| void | setCommonHeaders (const QHttpHeaders & headers ) |
| void | setPassword (const QString & password ) |
(從 6.8 起)
void
|
setPriority (QNetworkRequest::Priority priority ) |
| void | setQueryParameters (const QUrlQuery & query ) |
| void | setSslConfiguration (const QSslConfiguration & configuration ) |
| void | setTransferTimeout (std::chrono::milliseconds timeout ) |
| void | setUserName (const QString & userName ) |
| QSslConfiguration | sslConfiguration () const |
| void | swap (QNetworkRequestFactory & other ) |
| std::chrono::milliseconds | transferTimeout () const |
| QString | userName () const |
| QNetworkRequestFactory & | operator= (QNetworkRequestFactory && other ) |
| QNetworkRequestFactory & | operator= (const QNetworkRequestFactory & other ) |
| QDebug | operator<< (QDebug debug , const QNetworkRequestFactory & factory ) |
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:
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
Creates a new QNetworkRequestFactory object. Use setBaseUrl () to set a valid base URL for the requests.
另請參閱 QNetworkRequestFactory (const QUrl &baseUrl) and setBaseUrl ().
[explicit]
QNetworkRequestFactory::
QNetworkRequestFactory
(const
QUrl
&
baseUrl
)
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
創建副本為 other .
[noexcept]
QNetworkRequestFactory::
QNetworkRequestFactory
(
QNetworkRequestFactory
&&
other
)
Move-constructs the factory from other .
注意: 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.
[noexcept]
QNetworkRequestFactory::
~QNetworkRequestFactory
()
銷毀此 QNetworkRequestFactory 對象。
[since 6.8]
QVariant
QNetworkRequestFactory::
屬性
(
QNetworkRequest::Attribute
attribute
) const
Returns the value associated with attribute . If the attribute has not been set, returns a default-constructed QVariant .
該函數在 Qt 6.8 引入。
另請參閱 attribute (QNetworkRequest::Attribute, const QVariant &), setAttribute (), clearAttributes (),和 QNetworkRequest::Attribute .
[since 6.8]
QVariant
QNetworkRequestFactory::
屬性
(
QNetworkRequest::Attribute
attribute
, const
QVariant
&
defaultValue
) const
Returns the value associated with attribute . If the attribute has not been set, returns defaultValue .
該函數在 Qt 6.8 引入。
另請參閱 attribute (), setAttribute (), clearAttributes (),和 QNetworkRequest::Attribute .
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.
另請參閱 setBaseUrl ().
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
commonHeaders
() listing. If the
commonHeaders
() also lists
Authorization
header, it will be overwritten.
另請參閱 setBearerToken () 和 commonHeaders ().
[since 6.8]
void
QNetworkRequestFactory::
clearAttribute
(
QNetworkRequest::Attribute
attribute
)
Clears attribute set to this factory.
該函數在 Qt 6.8 引入。
另請參閱 attribute () 和 setAttribute ().
[since 6.8]
void
QNetworkRequestFactory::
clearAttributes
()
Clears any attributes set to this factory.
該函數在 Qt 6.8 引入。
另請參閱 attribute () 和 setAttribute ().
Clears the bearer token.
另請參閱 bearerToken ().
Clears current headers.
另請參閱 commonHeaders () 和 setCommonHeaders ().
Clears the password set to this factory.
另請參閱 password (), setPassword (),和 userName ().
Clears the query parameters.
另請參閱 queryParameters ().
Clears the username set to this factory.
Returns the currently set headers.
另請參閱 setCommonHeaders () 和 clearCommonHeaders ().
返迴 QNetworkRequest .
The returned request is filled with the data that this factory has been configured with.
另請參閱 createRequest (const QUrlQuery&) and createRequest (const QString&, const QUrlQuery&).
返迴 QNetworkRequest .
The returned request's URL is formed by appending the provided path 到 baseUrl (which may itself have a path component).
另請參閱 createRequest (const QString &, const QUrlQuery &), createRequest (),和 baseUrl ().
返迴 QNetworkRequest .
The returned request's URL is formed by appending the provided query 到 baseUrl .
另請參閱 createRequest (const QString &, const QUrlQuery &), createRequest (),和 baseUrl ().
返迴 QNetworkRequest .
The returned requests URL is formed by appending the provided path and query 到 baseUrl (which may have a path component).
If the provided path contains query items, they will be combined with the items in query .
另請參閱 createRequest (const QUrlQuery&), createRequest (),和 baseUrl ().
Returns the password set to this factory.
另請參閱 setPassword (), clearPassword (),和 userName ().
[since 6.8]
QNetworkRequest::Priority
QNetworkRequestFactory::
priority
() const
Returns the priority assigned to any future requests created by this factory.
該函數在 Qt 6.8 引入。
另請參閱 setPriority () 和 QNetworkRequest::priority ().
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 createRequest () 調用。
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.
另請參閱 setQueryParameters (), clearQueryParameters (),和 createRequest ().
[since 6.8]
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.
該函數在 Qt 6.8 引入。
另請參閱 attribute (), clearAttribute (), clearAttributes (),和 QNetworkRequest::Attribute .
Sets the base URL used in individual requests to url .
另請參閱 baseUrl ().
Sets the bearer token to token .
另請參閱 bearerToken () 和 clearBearerToken ().
設置 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.
另請參閱 commonHeaders (), clearCommonHeaders (),和 createRequest ().
Sets the password of this factory to password .
The password is set in the request URL when createRequest () is called. The QRestAccessManager / QNetworkAccessManager will attempt to use these credentials when the server indicates that authentication is required.
另請參閱 password (), clearPassword (),和 userName ().
[since 6.8]
void
QNetworkRequestFactory::
setPriority
(
QNetworkRequest::Priority
priority
)
Sets the priority for any future requests created by this factory to priority .
The default priority is QNetworkRequest::NormalPriority .
該函數在 Qt 6.8 引入。
另請參閱 priority () 和 QNetworkRequest::setPriority ().
設置 query parameters that are added to individual requests' query parameters.
另請參閱 queryParameters () 和 clearQueryParameters ().
Sets the SSL configuration to configuration .
另請參閱 sslConfiguration ().
設置 timeout used for transfers.
另請參閱 transferTimeout (), QNetworkRequest::setTransferTimeout (),和 QNetworkAccessManager::setTransferTimeout ().
Sets the username of this factory to userName .
The username is set in the request URL when createRequest () is called. The QRestAccessManager / QNetworkAccessManager will attempt to use these credentials when the server indicates that authentication is required.
另請參閱 userName (), clearUserName (),和 password ().
Returns the SSL configuration set to this factory. The SSL configuration is set to each individual request.
另請參閱 setSslConfiguration ().
[noexcept]
void
QNetworkRequestFactory::
swap
(
QNetworkRequestFactory
&
other
)
Swaps this factory with other 。此操作很快且從不失敗。
Returns the timeout used for transfers.
另請參閱 setTransferTimeout (), QNetworkRequest::transferTimeout (),和 QNetworkAccessManager::transferTimeout ().
Returns the username set to this factory.
另請參閱 setUserName (), clearUserName (),和 password ().
[noexcept]
QNetworkRequestFactory
&QNetworkRequestFactory::
operator=
(
QNetworkRequestFactory
&&
other
)
移動賦值 other and returns a reference to this factory.
注意: 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.
創建副本為 other and returns a reference to this factory.
寫入 factory into debug stream.
另請參閱 調試技術 .