Skip to content

Commit 4908ef1

Browse files
committed
Revert "Revert QgsBlockingNetworkRequest"
With recent changes in QgsNetworkAccessManager this should be safe to resurrect (fingers crossed!). Also simplify code a lot, because now QgsNetworkAccessManager handles waking the worker thread after the auth request is handled. Add a lot more tests
1 parent c03ce93 commit 4908ef1

16 files changed

+1371
-62
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/qgsblockingnetworkrequest.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
class QgsBlockingNetworkRequest : QObject
12+
{
13+
%Docstring
14+
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy
15+
and authentication settings.
16+
17+
This class should be used whenever a blocking network request is required. Unlike implementations
18+
which rely on QApplication.processEvents() or creation of a QEventLoop, this class is completely
19+
thread safe and can be used on either the main thread or background threads without issue.
20+
21+
Redirects are automatically handled by the class.
22+
23+
After completion of a request, the reply content should be retrieved by calling getReplyContent().
24+
This method returns a QgsNetworkReplyContent container, which is safe and cheap to copy and pass
25+
between threads without issue.
26+
27+
.. versionadded:: 3.6
28+
%End
29+
30+
%TypeHeaderCode
31+
#include "qgsblockingnetworkrequest.h"
32+
%End
33+
public:
34+
35+
enum ErrorCode
36+
{
37+
NoError,
38+
NetworkError,
39+
TimeoutError,
40+
ServerExceptionError,
41+
};
42+
43+
explicit QgsBlockingNetworkRequest();
44+
%Docstring
45+
Constructor for QgsBlockingNetworkRequest
46+
%End
47+
48+
~QgsBlockingNetworkRequest();
49+
50+
ErrorCode get( QNetworkRequest &request, bool forceRefresh = false, QgsFeedback *feedback = 0 );
51+
%Docstring
52+
Performs a "get" operation on the specified ``request``.
53+
54+
If ``forceRefresh`` is false then previously cached replies may be used for the request. If
55+
it is set to true then a new query is always performed.
56+
57+
If an authCfg() has been set, then any authentication configuration required will automatically be applied to
58+
``request``. There is no need to manually apply the authentication to the request prior to calling
59+
this method.
60+
61+
The optional ``feedback`` argument can be used to abort ongoing requests.
62+
63+
The method will return NoError if the get operation was successful. The contents of the reply can be retrieved
64+
by calling reply().
65+
66+
If an error was encountered then a specific ErrorCode will be returned, and a detailed error message
67+
can be retrieved by calling errorMessage().
68+
69+
.. seealso:: :py:func:`post`
70+
%End
71+
72+
ErrorCode post( QNetworkRequest &request, const QByteArray &data, bool forceRefresh = false, QgsFeedback *feedback = 0 );
73+
%Docstring
74+
Performs a "post" operation on the specified ``request``, using the given ``data``.
75+
76+
If ``forceRefresh`` is false then previously cached replies may be used for the request. If
77+
it is set to true then a new query is always performed.
78+
79+
If an authCfg() has been set, then any authentication configuration required will automatically be applied to
80+
``request``. There is no need to manually apply the authentication to the request prior to calling
81+
this method.
82+
83+
The optional ``feedback`` argument can be used to abort ongoing requests.
84+
85+
The method will return NoError if the get operation was successful. The contents of the reply can be retrieved
86+
by calling reply().
87+
88+
If an error was encountered then a specific ErrorCode will be returned, and a detailed error message
89+
can be retrieved by calling errorMessage().
90+
91+
.. seealso:: :py:func:`get`
92+
%End
93+
94+
QString errorMessage() const;
95+
%Docstring
96+
Returns the error message string, after a get() or post() request has been made.\
97+
%End
98+
99+
QgsNetworkReplyContent reply() const;
100+
%Docstring
101+
Returns the content of the network reply, after a get() or post() request has been made.
102+
%End
103+
104+
QString authCfg() const;
105+
%Docstring
106+
Returns the authentication config id which will be used during the request.
107+
108+
.. seealso:: :py:func:`setAuthCfg`
109+
%End
110+
111+
void setAuthCfg( const QString &authCfg );
112+
%Docstring
113+
Sets the authentication config id which should be used during the request.
114+
115+
.. seealso:: :py:func:`authCfg`
116+
%End
117+
118+
public slots:
119+
120+
void abort();
121+
%Docstring
122+
Aborts the network request immediately.
123+
%End
124+
125+
signals:
126+
127+
void downloadProgress( qint64, qint64 );
128+
%Docstring
129+
Emitted when when data arrives during a request.
130+
%End
131+
132+
void downloadFinished();
133+
%Docstring
134+
Emitted once a request has finished downloading.
135+
%End
136+
137+
};
138+
139+
140+
/************************************************************************
141+
* This file has been generated automatically from *
142+
* *
143+
* src/core/qgsblockingnetworkrequest.h *
144+
* *
145+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
146+
************************************************************************/

python/core/auto_generated/qgsnetworkaccessmanager.sip.in

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212

1313

14+
1415
class QgsNetworkRequestParameters
1516
{
1617
%Docstring
@@ -251,6 +252,52 @@ Sets the maximum timeout ``time`` for network requests, in milliseconds.
251252

252253
.. seealso:: :py:func:`timeout`
253254

255+
.. versionadded:: 3.6
256+
%End
257+
258+
static QgsNetworkReplyContent blockingGet( QNetworkRequest &request, const QString &authCfg = QString(), bool forceRefresh = false, QgsFeedback *feedback = 0 );
259+
%Docstring
260+
Posts a GET request to obtain the contents of the target request and returns a new QgsNetworkReplyContent object for reading.
261+
The current thread will be blocked until the request is returned.
262+
263+
This method is safe to call in either the main thread or a worker thread.
264+
265+
If ``forceRefresh`` is false then previously cached replies may be used for the request. If
266+
it is set to true then a new query is always performed.
267+
268+
If an ``authCfg`` has been specified, then that authentication configuration required will automatically be applied to
269+
``request``. There is no need to manually apply the authentication to the request prior to calling
270+
this method.
271+
272+
The optional ``feedback`` argument can be used to abort ongoing requests.
273+
274+
The contents of the reply will be returned after the request is completed or an error occurs.
275+
276+
.. seealso:: :py:func:`blockingPost`
277+
278+
.. versionadded:: 3.6
279+
%End
280+
281+
static QgsNetworkReplyContent blockingPost( QNetworkRequest &request, const QByteArray &data, const QString &authCfg = QString(), bool forceRefresh = false, QgsFeedback *feedback = 0 );
282+
%Docstring
283+
Posts a POST request to obtain the contents of the target ``request``, using the given ``data``, and returns a new
284+
QgsNetworkReplyContent object for reading. The current thread will be blocked until the request is returned.
285+
286+
This method is safe to call in either the main thread or a worker thread.
287+
288+
If ``forceRefresh`` is false then previously cached replies may be used for the request. If
289+
it is set to true then a new query is always performed.
290+
291+
If an ``authCfg`` has been specified, then that authentication configuration required will automatically be applied to
292+
``request``. There is no need to manually apply the authentication to the request prior to calling
293+
this method.
294+
295+
The optional ``feedback`` argument can be used to abort ongoing requests.
296+
297+
The contents of the reply will be returned after the request is completed or an error occurs.
298+
299+
.. seealso:: :py:func:`blockingGet`
300+
254301
.. versionadded:: 3.6
255302
%End
256303

python/core/auto_generated/qgsnetworkreply.sip.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ Returns the unique ID identifying the original request which this response was f
109109
Returns the original network request.
110110
%End
111111

112+
void setContent( const QByteArray &content );
113+
114+
QByteArray content() const;
115+
112116
};
113117

114118
/************************************************************************

python/core/core_auto.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@
318318
%Include auto_generated/qgsactionscoperegistry.sip
319319
%Include auto_generated/qgsanimatedicon.sip
320320
%Include auto_generated/qgsauxiliarystorage.sip
321+
%Include auto_generated/qgsblockingnetworkrequest.sip
321322
%Include auto_generated/qgsbrowsermodel.sip
322323
%Include auto_generated/qgsbrowserproxymodel.sip
323324
%Include auto_generated/qgscoordinatereferencesystem.sip

src/app/qgisapp.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,10 @@ QgisApp::~QgisApp()
15891589
qDeleteAll( mCustomDropHandlers );
15901590
qDeleteAll( mCustomLayoutDropHandlers );
15911591

1592+
// replace gui based network access managers with failing only ones
1593+
QgsNetworkAccessManager::instance()->setSslErrorHandler( qgis::make_unique< QgsSslErrorHandler >() );
1594+
QgsNetworkAccessManager::instance()->setAuthHandler( qgis::make_unique< QgsNetworkAuthenticationHandler >() );
1595+
15921596
const QList<QgsMapCanvas *> canvases = mapCanvases();
15931597
for ( QgsMapCanvas *canvas : canvases )
15941598
{

src/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ SET(QGIS_CORE_SRCS
153153
qgsattributeeditorelement.cpp
154154
qgsauxiliarystorage.cpp
155155
qgsbearingutils.cpp
156+
qgsblockingnetworkrequest.cpp
156157
qgsbrowsermodel.cpp
157158
qgsbrowserproxymodel.cpp
158159
qgscachedfeatureiterator.cpp
@@ -602,6 +603,7 @@ SET(QGIS_CORE_MOC_HDRS
602603
qgsactionscoperegistry.h
603604
qgsanimatedicon.h
604605
qgsauxiliarystorage.h
606+
qgsblockingnetworkrequest.h
605607
qgsbrowsermodel.h
606608
qgsbrowserproxymodel.h
607609
qgscoordinatereferencesystem.h

0 commit comments

Comments
 (0)