Skip to content

Commit bcf57c3

Browse files
committed
Add method for QgsNetworkContentFetcher to fetch using request instead of url
1 parent 11c0e56 commit bcf57c3

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

python/core/qgsnetworkcontentfetcher.sip.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ Fetches content from a remote URL and handles redirects. The finished()
4040
signal will be emitted when content has been fetched.
4141

4242
:param url: URL to fetch
43+
%End
44+
45+
void fetchContent( const QNetworkRequest &request );
46+
%Docstring
47+
Fetches content using a network ``request`` and handles redirects. The finished()
48+
signal will be emitted when content has been fetched.
49+
50+
.. versionadded:: 3.2
4351
%End
4452

4553
QNetworkReply *reply();

src/core/qgsnetworkcontentfetcher.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ QgsNetworkContentFetcher::~QgsNetworkContentFetcher()
3838

3939
void QgsNetworkContentFetcher::fetchContent( const QUrl &url )
4040
{
41-
mContentLoaded = false;
41+
fetchContent( QNetworkRequest( url ) );
42+
}
4243

43-
//get contents
44-
QNetworkRequest request( url );
44+
void QgsNetworkContentFetcher::fetchContent( const QNetworkRequest &request )
45+
{
46+
mContentLoaded = false;
4547

4648
if ( mReply )
4749
{

src/core/qgsnetworkcontentfetcher.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ class CORE_EXPORT QgsNetworkContentFetcher : public QObject
5454
*/
5555
void fetchContent( const QUrl &url );
5656

57+
/**
58+
* Fetches content using a network \a request and handles redirects. The finished()
59+
* signal will be emitted when content has been fetched.
60+
*
61+
* \since QGIS 3.2
62+
*/
63+
void fetchContent( const QNetworkRequest &request );
64+
5765
/**
5866
* Returns a reference to the network reply
5967
* \returns QNetworkReply for fetched URL content

tests/src/python/test_qgsnetworkcontentfetcher.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from qgis.core import QgsNetworkContentFetcher
2323
from utilities import unitTestDataPath
2424
from qgis.PyQt.QtCore import QUrl
25-
from qgis.PyQt.QtNetwork import QNetworkReply
25+
from qgis.PyQt.QtNetwork import QNetworkReply, QNetworkRequest
2626
import socketserver
2727
import threading
2828
import http.server
@@ -90,6 +90,21 @@ def testFetchUrlContent(self):
9090
html = fetcher.contentAsString()
9191
assert 'QGIS' in html
9292

93+
def testFetchRequestContent(self):
94+
fetcher = QgsNetworkContentFetcher()
95+
self.loaded = False
96+
request = QNetworkRequest(QUrl('http://localhost:' + str(TestQgsNetworkContentFetcher.port) + '/qgis_local_server/index.html'))
97+
fetcher.fetchContent(request)
98+
fetcher.finished.connect(self.contentLoaded)
99+
while not self.loaded:
100+
app.processEvents()
101+
102+
r = fetcher.reply()
103+
assert r.error() == QNetworkReply.NoError, r.error()
104+
105+
html = fetcher.contentAsString()
106+
assert 'QGIS' in html
107+
93108
def testDoubleFetch(self):
94109
fetcher = QgsNetworkContentFetcher()
95110
self.loaded = False

0 commit comments

Comments
 (0)