QGIS API Documentation  3.19.0-Master (c022ae99b5)
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 
107  ErrorCode post( QNetworkRequest &request, const QByteArray &data, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
108 
129  ErrorCode head( QNetworkRequest &request, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
130 
148  ErrorCode put( QNetworkRequest &request, const QByteArray &data, QgsFeedback *feedback = nullptr );
149 
167  ErrorCode deleteResource( QNetworkRequest &request, QgsFeedback *feedback = nullptr );
168 
172  QString errorMessage() const { return mErrorMessage; }
173 
177  QgsNetworkReplyContent reply() const { return mReplyContent; }
178 
183  QString authCfg() const;
184 
189  void setAuthCfg( const QString &authCfg );
190 
191  public slots:
192 
196  void abort();
197 
198  signals:
199 
203  void downloadProgress( qint64, qint64 );
204 
209 
210  private slots:
211  void replyProgress( qint64, qint64 );
212  void replyFinished();
213  void requestTimedOut( QNetworkReply *reply );
214 
215  private :
216 
217  enum Method
218  {
219  Get,
220  Post,
221  Head,
222  Put,
223  Delete
224  };
225 
227  QNetworkReply *mReply = nullptr;
228 
229  Method mMethod = Get;
230  QByteArray mPayloadData;
231 
233  QString mAuthCfg;
234 
236  QString mErrorMessage;
237 
239  ErrorCode mErrorCode = NoError;
240 
241  QgsNetworkReplyContent mReplyContent;
242 
244  bool mIsAborted = false;
245 
247  bool mForceRefresh = false;
248 
250  bool mTimedout = false;
251 
253  bool mGotNonEmptyResponse = false;
254 
255  int mExpirationSec = 30;
256 
257  QPointer< QgsFeedback > mFeedback;
258 
259  ErrorCode doRequest( Method method, QNetworkRequest &request, bool forceRefresh, QgsFeedback *feedback = nullptr );
260 
261  QString errorMessageFailedAuth();
262 
263  void sendRequestToNetworkAccessManager( const QNetworkRequest &request );
264 };
265 
267 #ifndef SIP_RUN
268 
269 class DownloaderThread : public QThread
270 {
271  Q_OBJECT
272 
273  public:
274  DownloaderThread( const std::function<void()> &function, QObject *parent = nullptr )
275  : QThread( parent )
276  , mFunction( function )
277  {
278  }
279 
280  void run() override
281  {
282  mFunction();
283  }
284 
285  private:
286  std::function<void()> mFunction;
287 };
288 
289 #endif
291 
292 #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.
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...