class QNetworkRequestFactory#

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

New in version 6.7.

Synopsis#

Methods#

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:

otherQNetworkRequestFactory

Creates a copy of other.

__init__(baseUrl)
Parameters:

baseUrlQUrl

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 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"}}
# ...
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. Use setBaseUrl() to set a valid base URL for the requests.

See also

QNetworkRequestFactory(const QUrl &baseUrl) setBaseUrl()

baseUrl()#
Return type:

QUrl

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()

bearerToken()#
Return type:

QByteArray

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 the commonHeaders() also lists Authorization header, it will be overwritten.

clearBearerToken()#

Clears the bearer token.

See also

bearerToken()

clearCommonHeaders()#

Clears current headers.

clearPassword()#

Clears the password set to this factory.

clearQueryParameters()#

Clears the query parameters.

clearUserName()#

Clears the username set to this factory.

commonHeaders()#
Return type:

QHttpHeaders

Returns the currently set headers.

createRequest(path, query)#
Parameters:
Return type:

QNetworkRequest

Returns a QNetworkRequest .

The returned request’s URL is formed by appending the provided path and query to the baseUrl (which may itself 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()

createRequest(query)
Parameters:

queryQUrlQuery

Return type:

QNetworkRequest

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()

createRequest(path)
Parameters:

path – str

Return type:

QNetworkRequest

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()

createRequest()
Return type:

QNetworkRequest

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.

queryParameters()#
Return type:

QUrlQuery

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.

setBaseUrl(url)#
Parameters:

urlQUrl

Sets the base URL used in individual requests to url.

See also

baseUrl()

setBearerToken(token)#
Parameters:

tokenQByteArray

Sets the bearer token to token.

setCommonHeaders(headers)#
Parameters:

headersQHttpHeaders

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. The QRestAccessManager / QNetworkAccessManager will attempt to use these credentials when the server indicates that authentication is required.

setQueryParameters(query)#
Parameters:

queryQUrlQuery

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

setSslConfiguration(configuration)#
Parameters:

configurationQSslConfiguration

Sets the SSL configuration to configuration.

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. The QRestAccessManager / QNetworkAccessManager will attempt to use these credentials when the server indicates that authentication is required.

sslConfiguration()#
Return type:

QSslConfiguration

Returns the SSL configuration set to this factory. The SSL configuration is set to each individual request.

swap(other)#
Parameters:

otherQNetworkRequestFactory

Swaps this factory with other. This operation is very fast and never fails.

userName()#
Return type:

str

Returns the username set to this factory.