Skip to content

Commit

Permalink
Add method for QgsNetworkContentFetcher to fetch using request instea…
Browse files Browse the repository at this point in the history
…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.
8 changes: 8 additions & 0 deletions python/core/qgsnetworkcontentfetcher.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 5 additions & 3 deletions src/core/qgsnetworkcontentfetcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsnetworkcontentfetcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion tests/src/python/test_qgsnetworkcontentfetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bcf57c3

Please sign in to comment.