Skip to content
Permalink
Browse files
Add method for QgsNetworkContentFetcher to fetch using request instea…
…d of url
  • Loading branch information
nyalldawson committed Apr 1, 2018
1 parent 11c0e56 commit bcf57c3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
@@ -40,6 +40,14 @@ Fetches content from a remote URL and handles redirects. The finished()
signal will be emitted when content has been fetched.

:param url: URL to fetch
%End

void fetchContent( const QNetworkRequest &request );
%Docstring
Fetches content using a network ``request`` and handles redirects. The finished()
signal will be emitted when content has been fetched.

.. versionadded:: 3.2
%End

QNetworkReply *reply();
@@ -38,10 +38,12 @@ QgsNetworkContentFetcher::~QgsNetworkContentFetcher()

void QgsNetworkContentFetcher::fetchContent( const QUrl &url )
{
mContentLoaded = false;
fetchContent( QNetworkRequest( url ) );
}

//get contents
QNetworkRequest request( url );
void QgsNetworkContentFetcher::fetchContent( const QNetworkRequest &request )
{
mContentLoaded = false;

if ( mReply )
{
@@ -54,6 +54,14 @@ class CORE_EXPORT QgsNetworkContentFetcher : public QObject
*/
void fetchContent( const QUrl &url );

/**
* Fetches content using a network \a request and handles redirects. The finished()
* signal will be emitted when content has been fetched.
*
* \since QGIS 3.2
*/
void fetchContent( const QNetworkRequest &request );

/**
* Returns a reference to the network reply
* \returns QNetworkReply for fetched URL content
@@ -22,7 +22,7 @@
from qgis.core import QgsNetworkContentFetcher
from utilities import unitTestDataPath
from qgis.PyQt.QtCore import QUrl
from qgis.PyQt.QtNetwork import QNetworkReply
from qgis.PyQt.QtNetwork import QNetworkReply, QNetworkRequest
import socketserver
import threading
import http.server
@@ -90,6 +90,21 @@ def testFetchUrlContent(self):
html = fetcher.contentAsString()
assert 'QGIS' in html

def testFetchRequestContent(self):
fetcher = QgsNetworkContentFetcher()
self.loaded = False
request = QNetworkRequest(QUrl('http://localhost:' + str(TestQgsNetworkContentFetcher.port) + '/qgis_local_server/index.html'))
fetcher.fetchContent(request)
fetcher.finished.connect(self.contentLoaded)
while not self.loaded:
app.processEvents()

r = fetcher.reply()
assert r.error() == QNetworkReply.NoError, r.error()

html = fetcher.contentAsString()
assert 'QGIS' in html

def testDoubleFetch(self):
fetcher = QgsNetworkContentFetcher()
self.loaded = False

0 comments on commit bcf57c3

Please sign in to comment.