QGIS API Documentation  3.23.0-Master (eb871beae0)
qgsblockingnetworkrequest.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsblockingnetworkrequest.h
3  ---------------------------
4  begin : November 2018
5  copyright : (C) 2018 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #ifndef QGSBLOCKINGNETWORKREQUEST_H
16 #define QGSBLOCKINGNETWORKREQUEST_H
17 
18 #include "qgis_core.h"
19 #include "qgsnetworkreply.h"
20 #include "qgsfeedback.h"
21 #include <QThread>
22 #include <QObject>
23 #include <functional>
24 #include <QPointer>
25 
26 class QNetworkRequest;
27 class QNetworkReply;
28 
46 class CORE_EXPORT QgsBlockingNetworkRequest : public QObject
47 {
48  Q_OBJECT
49  public:
50 
52  enum ErrorCode
53  {
58  };
59 
61  explicit QgsBlockingNetworkRequest();
62 
63  ~QgsBlockingNetworkRequest() override;
64 
85  ErrorCode get( QNetworkRequest &request, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
86 
108  ErrorCode post( QNetworkRequest &request, QIODevice *data, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
109 
115  ErrorCode post( QNetworkRequest &request, const QByteArray &data, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
116 
137  ErrorCode head( QNetworkRequest &request, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
138 
156  ErrorCode put( QNetworkRequest &request, QIODevice *data, QgsFeedback *feedback = nullptr );
157 
164  ErrorCode put( QNetworkRequest &request, const QByteArray &data, QgsFeedback *feedback = nullptr );
165 
183  ErrorCode deleteResource( QNetworkRequest &request, QgsFeedback *feedback = nullptr );
184 
188  QString errorMessage() const { return mErrorMessage; }
189 
193  QgsNetworkReplyContent reply() const { return mReplyContent; }
194 
199  QString authCfg() const;
200 
205  void setAuthCfg( const QString &authCfg );
206 
207  public slots:
208 
212  void abort();
213 
214  signals:
215 
219  void downloadProgress( qint64, qint64 );
220 
225  Q_DECL_DEPRECATED void downloadFinished() SIP_DEPRECATED;
226 
231  void uploadProgress( qint64, qint64 );
232 
236  void finished();
237 
238  private slots:
239  void replyProgress( qint64, qint64 );
240  void replyFinished();
241  void requestTimedOut( QNetworkReply *reply );
242 
243  private :
244 
245  enum Method
246  {
247  Get,
248  Post,
249  Head,
250  Put,
251  Delete
252  };
253 
255  QNetworkReply *mReply = nullptr;
256 
257  Method mMethod = Get;
258 
260  QIODevice *mPayloadData;
261 
263  QString mAuthCfg;
264 
266  QString mErrorMessage;
267 
269  ErrorCode mErrorCode = NoError;
270 
271  QgsNetworkReplyContent mReplyContent;
272 
274  bool mIsAborted = false;
275 
277  bool mForceRefresh = false;
278 
280  bool mTimedout = false;
281 
283  bool mGotNonEmptyResponse = false;
284 
285  int mExpirationSec = 30;
286 
287  QPointer< QgsFeedback > mFeedback;
288 
289  ErrorCode doRequest( Method method, QNetworkRequest &request, bool forceRefresh, QgsFeedback *feedback = nullptr );
290 
291  QString errorMessageFailedAuth();
292 
293  void sendRequestToNetworkAccessManager( const QNetworkRequest &request );
294 };
295 
297 #ifndef SIP_RUN
298 
299 class DownloaderThread : public QThread
300 {
301  Q_OBJECT
302 
303  public:
304  DownloaderThread( const std::function<void()> &function, QObject *parent = nullptr )
305  : QThread( parent )
306  , mFunction( function )
307  {
308  }
309 
310  void run() override
311  {
312  mFunction();
313  }
314 
315  private:
316  std::function<void()> mFunction;
317 };
318 
319 #endif
321 
322 #endif // QGSBLOCKINGNETWORKREQUEST_H
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...
void downloadProgress(qint64, qint64)
Emitted when when data arrives during a request.
Q_DECL_DEPRECATED void downloadFinished()
Emitted once a request has finished downloading.
QString errorMessage() const
Returns the error message string, after a get() or post() request has been made.
@ NetworkError
A network error occurred.
@ ServerExceptionError
An exception was raised by the server.
@ NoError
No error was encountered.
@ TimeoutError
Timeout was reached before a reply was received.
QgsNetworkReplyContent reply() const
Returns the content of the network reply, after a get() or post() request has been made.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
Encapsulates a network reply within a container which is inexpensive to copy and safe to pass between...
#define SIP_DEPRECATED
Definition: qgis_sip.h:106