QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
112  QString errorMessage() const { return mErrorMessage; }
113 
117  QgsNetworkReplyContent reply() const { return mReplyContent; }
118 
123  QString authCfg() const;
124 
129  void setAuthCfg( const QString &authCfg );
130 
131  public slots:
132 
136  void abort();
137 
138  signals:
139 
143  void downloadProgress( qint64, qint64 );
144 
148  void downloadFinished();
149 
150  private slots:
151  void replyProgress( qint64, qint64 );
152  void replyFinished();
153  void requestTimedOut( QNetworkReply *reply );
154 
155  private :
156 
157  enum Method
158  {
159  Get,
160  Post
161  };
162 
164  QNetworkReply *mReply = nullptr;
165 
166  Method mMethod = Get;
167  QByteArray mPostData;
168 
170  QString mAuthCfg;
171 
173  QString mErrorMessage;
174 
176  ErrorCode mErrorCode = NoError;
177 
178  QgsNetworkReplyContent mReplyContent;
179 
181  bool mIsAborted = false;
182 
184  bool mForceRefresh = false;
185 
187  bool mTimedout = false;
188 
190  bool mGotNonEmptyResponse = false;
191 
192  int mExpirationSec = 30;
193 
194  QPointer< QgsFeedback > mFeedback;
195 
196  ErrorCode doRequest( Method method, QNetworkRequest &request, bool forceRefresh, QgsFeedback *feedback = nullptr );
197 
198  QString errorMessageFailedAuth();
199 
200 };
201 
203 #ifndef SIP_RUN
204 
205 class DownloaderThread : public QThread
206 {
207  Q_OBJECT
208 
209  public:
210  DownloaderThread( const std::function<void()> &function, QObject *parent = nullptr )
211  : QThread( parent )
212  , mFunction( function )
213  {
214  }
215 
216  void run() override
217  {
218  mFunction();
219  }
220 
221  private:
222  std::function<void()> mFunction;
223 };
224 
225 #endif
226 
228 #endif // QGSBLOCKINGNETWORKREQUEST_H
Timeout was reached before a reply was received.
Base class for feedback objects to be used for cancellation of something running in a worker thread...
Definition: qgsfeedback.h:44
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...
QString errorMessage() const
Returns the error message string, after a get() or post() request has been made.
QgsNetworkReplyContent reply() const
Returns the content of the network reply, after a get() or post() request has been made...
Encapsulates a network reply within a container which is inexpensive to copy and safe to pass between...