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 (), password(), 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.
另请参阅 调试技术 .