QGIS API Documentation 3.39.0-Master (3aed037ce22)
Loading...
Searching...
No Matches
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
26class QNetworkRequest;
27class QNetworkReply;
28
46class CORE_EXPORT QgsBlockingNetworkRequest : public QObject
47{
48 Q_OBJECT
49 public:
50
59
61
63
84 ErrorCode get( QNetworkRequest &request, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
85
107 ErrorCode post( QNetworkRequest &request, QIODevice *data, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
108
114 ErrorCode post( QNetworkRequest &request, const QByteArray &data, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
115
136 ErrorCode head( QNetworkRequest &request, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
137
155 ErrorCode put( QNetworkRequest &request, QIODevice *data, QgsFeedback *feedback = nullptr );
156
163 ErrorCode put( QNetworkRequest &request, const QByteArray &data, QgsFeedback *feedback = nullptr );
164
182 ErrorCode deleteResource( QNetworkRequest &request, QgsFeedback *feedback = nullptr );
183
187 QString errorMessage() const { return mErrorMessage; }
188
192 QgsNetworkReplyContent reply() const { return mReplyContent; }
193
198 QString authCfg() const;
199
204 void setAuthCfg( const QString &authCfg );
205
206 public slots:
207
211 void abort();
212
213 signals:
214
218 void downloadProgress( qint64, qint64 );
219
224 Q_DECL_DEPRECATED void downloadFinished() SIP_DEPRECATED;
225
230 void uploadProgress( qint64, qint64 );
231
235 void finished();
236
237 private slots:
238 void replyProgress( qint64, qint64 );
239 void replyFinished();
240 void requestTimedOut( QNetworkReply *reply );
241
242 private :
243
244 enum Method
245 {
246 Get,
247 Post,
248 Head,
249 Put,
250 Delete
251 };
252
254 QNetworkReply *mReply = nullptr;
255
256 Method mMethod = Get;
257
259 QIODevice *mPayloadData;
260
262 QString mAuthCfg;
263
265 QString mErrorMessage;
266
268 ErrorCode mErrorCode = NoError;
269
270 QgsNetworkReplyContent mReplyContent;
271
273 bool mIsAborted = false;
274
276 bool mForceRefresh = false;
277
279 bool mTimedout = false;
280
282 bool mGotNonEmptyResponse = false;
283
284 int mExpirationSec = 30;
285
286 QPointer< QgsFeedback > mFeedback;
287
288 ErrorCode doRequest( Method method, QNetworkRequest &request, bool forceRefresh, QgsFeedback *feedback = nullptr );
289
290 QString errorMessageFailedAuth();
291
292 void sendRequestToNetworkAccessManager( const QNetworkRequest &request );
293
294 void abortIfNotPartialContentReturned();
295};
296
298#ifndef SIP_RUN
299
300class DownloaderThread : public QThread
301{
302 Q_OBJECT
303
304 public:
305 DownloaderThread( const std::function<void()> &function, QObject *parent = nullptr )
306 : QThread( parent )
307 , mFunction( function )
308 {
309 }
310
311 void run() override
312 {
313 mFunction();
314 }
315
316 private:
317 std::function<void()> mFunction;
318};
319
320#endif
322
323#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(), post(), head() or put() 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(), post(), head() or put() request has been mad...
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
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