- class QNetworkRequestFactory#
Convenience class for grouping remote server endpoints that share common network request properties. More…
New in version 6.7.
Synopsis#
Methods#
def
__init__()
def
baseUrl()
def
bearerToken()
def
clearPassword()
def
clearUserName()
def
commonHeaders()
def
createRequest()
def
password()
def
setBaseUrl()
def
setBearerToken()
def
setPassword()
def
setUserName()
def
swap()
def
userName()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description#
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
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:Instantiation
Setting the data common to all requests
Issuing requests
An example of usage:
# Instantiate a factory somewhere suitable in the application QNetworkRequestFactory api{{"https://example.com/v1"}} # Set bearer token api.setBearerToken("my_token") # Issue requests (reply handling omitted for brevity) manager.get(api.createRequest("models")) # https://example.com/v1/models # The conventional leading '/' for the path can be used as well manager.get(api.createRequest("/models")) # https://example.com/v1/models
- __init__(other)#
- Parameters:
other –
QNetworkRequestFactory
Creates a copy of
other
.- __init__(baseUrl)
- Parameters:
baseUrl –
QUrl
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Creates a new
QNetworkRequestFactory
object, initializing the base URL tobaseUrl
. 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"}} # ... manager.get(api.createRequest("models")) # https://example.com/v2/models # Equivalent with a leading '/' manager.get(api.createRequest("/models")) # https://example.com/v2/models
- __init__()
Creates a new
QNetworkRequestFactory
object. UsesetBaseUrl()
to set a valid base URL for the requests.See also
QNetworkRequestFactory(const QUrl &baseUrl)
setBaseUrl()
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
- bearerToken()#
- Return type:
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 provided as an additional convenience.Means to acquire the bearer token varies. Common methods include
OAuth2
and the service provider’s website/dashboard. It’s common that the bearer token changes over time, for example when updated with a refresh token. By always re-setting the new token ensures that subsequent requests will always have the latest, valid, token.The presence of the bearer token does not impact the
commonHeaders()
listing. If thecommonHeaders()
also listsAuthorization
header, it will be overwritten.See also
- clearBearerToken()#
Clears the bearer token.
See also
- clearCommonHeaders()#
Clears current headers.
See also
- clearPassword()#
Clears the password set to this factory.
See also
- clearQueryParameters()#
Clears the query parameters.
See also
- clearUserName()#
Clears the username set to this factory.
- commonHeaders()#
- Return type:
Returns the currently set headers.
See also
Returns a
QNetworkRequest
.The returned request’s URL is formed by appending the provided
path
andquery
to thebaseUrl
(which may itself have a path component).If the provided
path
contains query items, they will be combined with the items inquery
.See also
createRequest(const QUrlQuery&)
createRequest()
baseUrl()
- createRequest(query)
- Parameters:
query –
QUrlQuery
- Return type:
Returns a
QNetworkRequest
.The returned request’s URL is formed by appending the provided
query
to thebaseUrl
.See also
createRequest(const QString &, const QUrlQuery &)
createRequest()
baseUrl()
- createRequest(path)
- Parameters:
path – str
- Return type:
Returns a
QNetworkRequest
.The returned request’s URL is formed by appending the provided
path
to thebaseUrl
(which may itself have a path component).See also
createRequest(const QString &, const QUrlQuery &)
createRequest()
baseUrl()
- createRequest()
- Return type:
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&)
- password()#
- Return type:
str
Returns the password set to this factory.
See also
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()
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.Sets the base URL used in individual requests to
url
.See also
- setBearerToken(token)#
- Parameters:
token –
QByteArray
Sets the bearer token to
token
.See also
- setCommonHeaders(headers)#
- Parameters:
headers –
QHttpHeaders
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.
- setPassword(password)#
- Parameters:
password – str
Sets the password of this factory to
password
.The password is set in the request URL when
createRequest()
is called. TheQRestAccessManager
/QNetworkAccessManager
will attempt to use these credentials when the server indicates that authentication is required.See also
Sets
query
parameters that are added to individual requests’ query parameters.See also
- setSslConfiguration(configuration)#
- Parameters:
configuration –
QSslConfiguration
Sets the SSL configuration to
configuration
.See also
- setUserName(userName)#
- Parameters:
userName – str
Sets the username of this factory to
userName
.The username is set in the request URL when
createRequest()
is called. TheQRestAccessManager
/QNetworkAccessManager
will attempt to use these credentials when the server indicates that authentication is required.See also
- sslConfiguration()#
- Return type:
Returns the SSL configuration set to this factory. The SSL configuration is set to each individual request.
See also
- swap(other)#
- Parameters:
other –
QNetworkRequestFactory
Swaps this factory with
other
. This operation is very fast and never fails.- userName()#
- Return type:
str
Returns the username set to this factory.
See also