QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
|
network access manager for QGIS More...
#include <qgsnetworkaccessmanager.h>
Signals | |
void | authBrowserAborted () |
Emitted when external browser logins are to be aborted. | |
void | cookiesChanged (const QList< QNetworkCookie > &cookies) |
Emitted when the cookies changed. | |
void | downloadProgress (int requestId, qint64 bytesReceived, qint64 bytesTotal) |
Emitted when a network reply receives a progress report. | |
void | finished (QgsNetworkReplyContent reply) |
Emitted whenever a pending network reply is finished. | |
void | requestAboutToBeCreated (QgsNetworkRequestParameters request) |
Emitted when a network request is about to be created. | |
Q_DECL_DEPRECATED void | requestAboutToBeCreated (QNetworkAccessManager::Operation operation, const QNetworkRequest &request, QIODevice *device) |
void | requestAuthDetailsAdded (int requestId, const QString &realm, const QString &user, const QString &password) |
Emitted when network authentication details have been added to a request. | |
void | requestCreated (const QgsNetworkRequestParameters &request) |
Emitted when a network request has been created. | |
Q_DECL_DEPRECATED void | requestCreated (QNetworkReply *reply) |
void | requestEncounteredSslErrors (int requestId, const QList< QSslError > &errors) |
Emitted when a network request encounters SSL errors. | |
void | requestRequiresAuth (int requestId, const QString &realm) |
Emitted when a network request prompts an authentication request. | |
void | requestTimedOut (QgsNetworkRequestParameters request) |
Emitted when a network request has timed out. | |
void | requestTimedOut (QNetworkReply *reply) |
Emitted when a request times out. | |
Public Member Functions | |
QgsNetworkAccessManager (QObject *parent=nullptr) | |
void | abortAuthBrowser () |
Abort any outstanding external browser login request. | |
bool | cacheDisabled () const |
Returns true if all network caching is disabled. | |
QStringList | excludeList () const |
Returns the proxy exclude list. | |
const QNetworkProxy & | fallbackProxy () const |
Returns the fallback proxy used by the manager. | |
void | insertProxyFactory (QNetworkProxyFactory *factory) |
Inserts a factory into the proxy factories list. | |
QStringList | noProxyList () const |
Returns the no proxy list. | |
void | preprocessRequest (QNetworkRequest *req) const |
Preprocesses request. | |
const QList< QNetworkProxyFactory * > | proxyFactories () const |
Returns a list of proxy factories used by the manager. | |
void | removeProxyFactory (QNetworkProxyFactory *factory) |
Removes a factory from the proxy factories list. | |
void | requestAuthCloseBrowser () const |
Forwards an external browser login closure request to the authentication handler. | |
void | requestAuthOpenBrowser (const QUrl &url) const |
Forwards an external browser login url opening request to the authentication handler. | |
void | setAuthHandler (std::unique_ptr< QgsNetworkAuthenticationHandler > handler) |
Sets the application network authentication handler, which is used to respond to network authentication prompts during network requests. | |
void | setCacheDisabled (bool disabled) |
Sets whether all network caching should be disabled. | |
void | setFallbackProxyAndExcludes (const QNetworkProxy &proxy, const QStringList &excludes, const QStringList &noProxyURLs) |
Sets the fallback proxy and URLs which shouldn't use it. | |
void | setSslErrorHandler (std::unique_ptr< QgsSslErrorHandler > handler) |
Sets the application SSL error handler, which is used to respond to SSL errors encountered during network requests. | |
void | setupDefaultProxyAndCache (Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection) |
Setup the QgsNetworkAccessManager (NAM) according to the user's settings. | |
bool | useSystemProxy () const |
Returns whether the system proxy should be used. | |
Static Public Member Functions | |
static QgsNetworkReplyContent | blockingGet (QNetworkRequest &request, const QString &authCfg=QString(), bool forceRefresh=false, QgsFeedback *feedback=nullptr) |
Posts a GET request to obtain the contents of the target request and returns a new QgsNetworkReplyContent object for reading. | |
static QgsNetworkReplyContent | blockingPost (QNetworkRequest &request, const QByteArray &data, const QString &authCfg=QString(), bool forceRefresh=false, QgsFeedback *feedback=nullptr) |
Posts a POST request to obtain the contents of the target request, using the given data, and returns a new QgsNetworkReplyContent object for reading. | |
static QNetworkRequest::CacheLoadControl | cacheLoadControlFromName (const QString &name) |
Returns QNetworkRequest::CacheLoadControl from a name. | |
static QString | cacheLoadControlName (QNetworkRequest::CacheLoadControl control) |
Returns the name for QNetworkRequest::CacheLoadControl. | |
static QgsNetworkAccessManager * | instance (Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection) |
Returns a pointer to the active QgsNetworkAccessManager for the current thread. | |
static bool | removeReplyPreprocessor (const QString &id) |
Removes the custom reply pre-processor function with matching id. | |
static bool | removeRequestPreprocessor (const QString &id) |
Removes the custom request pre-processor function with matching id. | |
static QString | setReplyPreprocessor (const std::function< void(const QNetworkRequest &, QNetworkReply *)> &processor) |
Sets a reply pre-processor function, which allows manipulation of QNetworkReply objects after they are created (but before they are fetched). | |
static QString | setRequestPreprocessor (const std::function< void(QNetworkRequest *request)> &processor) |
Sets a request pre-processor function, which allows manipulation of a network request before it is processed. | |
static void | setTimeout (int time) |
Sets the maximum timeout time for network requests, in milliseconds. | |
static int | timeout () |
Returns the network timeout length, in milliseconds. | |
Static Public Attributes | |
static const QgsSettingsEntryInteger * | settingsNetworkTimeout = new QgsSettingsEntryInteger( QStringLiteral( "network-timeout" ), QgsSettingsTree::sTreeNetwork, 60000, QObject::tr( "Network timeout" ) ) |
Settings entry network timeout. | |
Protected Member Functions | |
QNetworkReply * | createRequest (QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *outgoingData=nullptr) override |
Friends | |
class | TestQgsNetworkAccessManager |
network access manager for QGIS
This class implements the QGIS network access manager. It's a singleton that can be used across QGIS.
Plugins can insert proxy factories and thereby redirect requests to individual proxies.
If no proxy factories are there or none returns a proxy for an URL a fallback proxy can be set. There's also a exclude list that defines URLs that the fallback proxy should not be used for, then no proxy will be used.
Definition at line 256 of file qgsnetworkaccessmanager.h.
QgsNetworkAccessManager::QgsNetworkAccessManager | ( | QObject * | parent = nullptr | ) |
Definition at line 214 of file qgsnetworkaccessmanager.cpp.
void QgsNetworkAccessManager::abortAuthBrowser | ( | ) |
Abort any outstanding external browser login request.
Definition at line 561 of file qgsnetworkaccessmanager.cpp.
|
signal |
Emitted when external browser logins are to be aborted.
|
static |
Posts a GET request to obtain the contents of the target request and returns a new QgsNetworkReplyContent object for reading.
The current thread will be blocked until the request is returned.
This method is safe to call in either the main thread or a worker thread.
If forceRefresh is false
then previously cached replies may be used for the request. If it is set to true
then a new query is always performed.
If an authCfg has been specified, then that authentication configuration required will automatically be applied to request. There is no need to manually apply the authentication to the request prior to calling this method.
The optional feedback argument can be used to abort ongoing requests.
The contents of the reply will be returned after the request is completed or an error occurs.
Definition at line 795 of file qgsnetworkaccessmanager.cpp.
|
static |
Posts a POST request to obtain the contents of the target request, using the given data, and returns a new QgsNetworkReplyContent object for reading.
The current thread will be blocked until the request is returned.
This method is safe to call in either the main thread or a worker thread.
If forceRefresh is false
then previously cached replies may be used for the request. If it is set to true
then a new query is always performed.
If an authCfg has been specified, then that authentication configuration required will automatically be applied to request. There is no need to manually apply the authentication to the request prior to calling this method.
The optional feedback argument can be used to abort ongoing requests.
The contents of the reply will be returned after the request is completed or an error occurs.
Definition at line 803 of file qgsnetworkaccessmanager.cpp.
|
inline |
Returns true
if all network caching is disabled.
Definition at line 426 of file qgsnetworkaccessmanager.h.
|
static |
Returns QNetworkRequest::CacheLoadControl from a name.
Definition at line 596 of file qgsnetworkaccessmanager.cpp.
|
static |
Returns the name for QNetworkRequest::CacheLoadControl.
Definition at line 580 of file qgsnetworkaccessmanager.cpp.
|
signal |
Emitted when the cookies changed.
|
overrideprotected |
Definition at line 299 of file qgsnetworkaccessmanager.cpp.
|
signal |
Emitted when a network reply receives a progress report.
The requestId argument reflects the unique ID identifying the original request which the progress report relates to.
The bytesReceived parameter indicates the number of bytes received, while bytesTotal indicates the total number of bytes expected to be downloaded. If the number of bytes to be downloaded is not known, bytesTotal will be -1.
This signal is propagated to the main thread QgsNetworkAccessManager instance, so it is necessary only to connect to the main thread's signal in order to receive notifications about requests created in any thread.
QStringList QgsNetworkAccessManager::excludeList | ( | ) | const |
Returns the proxy exclude list.
This list consists of the beginning of URL strings which will not use the fallback proxy.
Definition at line 252 of file qgsnetworkaccessmanager.cpp.
const QNetworkProxy & QgsNetworkAccessManager::fallbackProxy | ( | ) | const |
Returns the fallback proxy used by the manager.
The fallback proxy is used for URLs which no other proxy factory returned proxies for.
Definition at line 262 of file qgsnetworkaccessmanager.cpp.
|
signal |
Emitted whenever a pending network reply is finished.
The reply parameter will contain a QgsNetworkReplyContent object, containing all the useful information relating to the reply, including headers and reply content.
This signal is propagated to the main thread QgsNetworkAccessManager instance, so it is necessary only to connect to the main thread's signal in order to receive notifications about requests created in any thread.
void QgsNetworkAccessManager::insertProxyFactory | ( | QNetworkProxyFactory * | factory | ) |
Inserts a factory into the proxy factories list.
Ownership of factory is transferred to the manager.
Definition at line 237 of file qgsnetworkaccessmanager.cpp.
|
static |
Returns a pointer to the active QgsNetworkAccessManager for the current thread.
With the connectionType parameter it is possible to setup the default connection type that is used to handle signals that might require user interaction and therefore need to be handled on the main thread. See in-depth discussion below.
connectionType | In most cases the default of using a Qt::BlockingQueuedConnection is ok, to make a background thread wait for the main thread to answer such a request is fine and anything else is dangerous. However, in case the request was started on the main thread, one should execute a local event loop in a helper thread and freeze the main thread for the duration of the download. In this case, if an authentication request is sent from the background thread network access manager, the background thread should be blocked, the main thread be woken up, processEvents() executed once, the main thread frozen again and the background thread continued. |
Definition at line 197 of file qgsnetworkaccessmanager.cpp.
QStringList QgsNetworkAccessManager::noProxyList | ( | ) | const |
Returns the no proxy list.
This list consists of the beginning of URL strings which will not use any proxy at all
Definition at line 257 of file qgsnetworkaccessmanager.cpp.
void QgsNetworkAccessManager::preprocessRequest | ( | QNetworkRequest * | req | ) | const |
Preprocesses request.
req | the request to preprocess |
Definition at line 845 of file qgsnetworkaccessmanager.cpp.
const QList< QNetworkProxyFactory * > QgsNetworkAccessManager::proxyFactories | ( | ) | const |
Returns a list of proxy factories used by the manager.
Definition at line 247 of file qgsnetworkaccessmanager.cpp.
void QgsNetworkAccessManager::removeProxyFactory | ( | QNetworkProxyFactory * | factory | ) |
Removes a factory from the proxy factories list.
Definition at line 242 of file qgsnetworkaccessmanager.cpp.
|
static |
Removes the custom reply pre-processor function with matching id.
The id must correspond to a pre-processor previously added via a call to setReplyPreprocessor().
Returns true
if processor existed and was removed.
Definition at line 835 of file qgsnetworkaccessmanager.cpp.
|
static |
Removes the custom request pre-processor function with matching id.
The id must correspond to a pre-processor previously added via a call to setRequestPreprocessor().
Returns true
if processor existed and was removed.
Definition at line 818 of file qgsnetworkaccessmanager.cpp.
|
signal |
Emitted when a network request is about to be created.
This signal is propagated to the main thread QgsNetworkAccessManager instance, so it is necessary only to connect to the main thread's signal in order to receive notifications about requests created in any thread.
|
signal |
void QgsNetworkAccessManager::requestAuthCloseBrowser | ( | ) | const |
Forwards an external browser login closure request to the authentication handler.
Definition at line 550 of file qgsnetworkaccessmanager.cpp.
|
signal |
Emitted when network authentication details have been added to a request.
The requestId argument reflects the unique ID identifying the original request which the authentication relates to.
This signal is always sent from the main thread QgsNetworkAccessManager instance, so it is necessary only to connect to the main thread's signal in order to receive notifications about authentication requests from any thread.
This signal is for debugging and logging purposes only, and should not be used to respond to the requests. See QgsNetworkAuthenticationHandler for details on how to handle authentication requests.
void QgsNetworkAccessManager::requestAuthOpenBrowser | ( | const QUrl & | url | ) | const |
Forwards an external browser login url opening request to the authentication handler.
Definition at line 539 of file qgsnetworkaccessmanager.cpp.
|
signal |
Emitted when a network request has been created.
This signal is propagated to the main thread QgsNetworkAccessManager instance, so it is necessary only to connect to the main thread's signal in order to receive notifications about requests created in any thread.
|
signal |
|
signal |
Emitted when a network request encounters SSL errors.
The requestId argument reflects the unique ID identifying the original request which the SSL error relates to.
This signal is propagated to the main thread QgsNetworkAccessManager instance, so it is necessary only to connect to the main thread's signal in order to receive notifications about SSL errors from any thread.
This signal is for debugging and logging purposes only, and cannot be used to respond to the errors. See QgsSslErrorHandler for details on how to handle SSL errors and potentially ignore them.
|
signal |
Emitted when a network request prompts an authentication request.
The requestId argument reflects the unique ID identifying the original request which the authentication relates to.
This signal is propagated to the main thread QgsNetworkAccessManager instance, so it is necessary only to connect to the main thread's signal in order to receive notifications about authentication requests from any thread.
This signal is for debugging and logging purposes only, and cannot be used to respond to the requests. See QgsNetworkAuthenticationHandler for details on how to handle authentication requests.
|
signal |
Emitted when a network request has timed out.
This signal is propagated to the main thread QgsNetworkAccessManager instance, so it is necessary only to connect to the main thread's signal in order to receive notifications about requests created in any thread.
|
signal |
Emitted when a request times out.
void QgsNetworkAccessManager::setAuthHandler | ( | std::unique_ptr< QgsNetworkAuthenticationHandler > | handler | ) |
Sets the application network authentication handler, which is used to respond to network authentication prompts during network requests.
Ownership of handler is transferred to the main thread QgsNetworkAccessManager instance.
This method must ONLY be called on the main thread QgsNetworkAccessManager. It is not necessary to set handlers for background threads – the main thread QgsNetworkAuthenticationHandler will automatically be used in a thread-safe manner for any authentication requests encountered on background threads.
The default QgsNetworkAuthenticationHandler responds to request only by logging the request, but does not provide any username or password resolution.
Definition at line 231 of file qgsnetworkaccessmanager.cpp.
|
inline |
Sets whether all network caching should be disabled.
If disabled is true
then all caching will be disabled, causing all requests to be retrieved from the network regardless of the request's attributes.
Definition at line 438 of file qgsnetworkaccessmanager.h.
void QgsNetworkAccessManager::setFallbackProxyAndExcludes | ( | const QNetworkProxy & | proxy, |
const QStringList & | excludes, | ||
const QStringList & | noProxyURLs | ||
) |
Sets the fallback proxy and URLs which shouldn't use it.
The fallback proxy is used for URLs which no other proxy factory returned proxies for. The excludes list specifies the beginning of URL strings which will not use this fallback proxy. The noProxyURLs list specifies the beginning of URL strings which will not use any proxy at all
Definition at line 267 of file qgsnetworkaccessmanager.cpp.
|
static |
Sets a reply pre-processor function, which allows manipulation of QNetworkReply objects after they are created (but before they are fetched).
The processor function takes a QNetworkRequest request and a QNetworkReply as arguments, and can connect to QNetworkReply signals directly as desired.
Definition at line 828 of file qgsnetworkaccessmanager.cpp.
|
static |
Sets a request pre-processor function, which allows manipulation of a network request before it is processed.
The processor function takes the QNetworkRequest as its argument, and can mutate the request if necessary.
Definition at line 811 of file qgsnetworkaccessmanager.cpp.
void QgsNetworkAccessManager::setSslErrorHandler | ( | std::unique_ptr< QgsSslErrorHandler > | handler | ) |
Sets the application SSL error handler, which is used to respond to SSL errors encountered during network requests.
Ownership of handler is transferred to the main thread QgsNetworkAccessManager instance.
This method must ONLY be called on the main thread QgsNetworkAccessManager. It is not necessary to set handlers for background threads – the main thread QgsSslErrorHandler will automatically be used in a thread-safe manner for any SSL errors encountered on background threads.
The default QgsSslErrorHandler responds to SSL errors only by logging the errors, and uses the default Qt response, which is to abort the request.
Definition at line 225 of file qgsnetworkaccessmanager.cpp.
|
static |
Sets the maximum timeout time for network requests, in milliseconds.
If set to 0, no timeout is set.
Definition at line 790 of file qgsnetworkaccessmanager.cpp.
void QgsNetworkAccessManager::setupDefaultProxyAndCache | ( | Qt::ConnectionType | connectionType = Qt::BlockingQueuedConnection | ) |
Setup the QgsNetworkAccessManager (NAM) according to the user's settings.
The connectionType sets up the default connection type that is used to handle signals that might require user interaction and therefore need to be handled on the main thread. See in-depth discussion in the documentation for the constructor of this class.
Definition at line 617 of file qgsnetworkaccessmanager.cpp.
|
static |
Returns the network timeout length, in milliseconds.
Definition at line 785 of file qgsnetworkaccessmanager.cpp.
|
inline |
Returns whether the system proxy should be used.
Definition at line 444 of file qgsnetworkaccessmanager.h.
|
friend |
Definition at line 889 of file qgsnetworkaccessmanager.h.
|
static |
Settings entry network timeout.
Definition at line 656 of file qgsnetworkaccessmanager.h.