QGIS API Documentation 3.43.0-Master (3ee7834ace6)
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 "qgis.h"
22#include <QThread>
23#include <QObject>
24#include <functional>
25#include <QPointer>
26
27class QNetworkRequest;
28class QNetworkReply;
29
47class CORE_EXPORT QgsBlockingNetworkRequest : public QObject
48{
49 Q_OBJECT
50 public:
51
60
66 enum class RequestFlag : int SIP_ENUM_BASETYPE( IntFlag )
67 {
68 EmptyResponseIsValid = 1 << 0,
69 };
70 Q_ENUM( RequestFlag )
71 Q_DECLARE_FLAGS( RequestFlags, RequestFlag )
72 Q_FLAG( RequestFlags )
73
76
77 ~QgsBlockingNetworkRequest() override;
78
101 ErrorCode get( QNetworkRequest &request, bool forceRefresh = false, QgsFeedback *feedback = nullptr, RequestFlags requestFlags = QgsBlockingNetworkRequest::RequestFlags() );
102
124 ErrorCode post( QNetworkRequest &request, QIODevice *data, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
125
131 ErrorCode post( QNetworkRequest &request, const QByteArray &data, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
132
153 ErrorCode head( QNetworkRequest &request, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
154
172 ErrorCode put( QNetworkRequest &request, QIODevice *data, QgsFeedback *feedback = nullptr );
173
180 ErrorCode put( QNetworkRequest &request, const QByteArray &data, QgsFeedback *feedback = nullptr );
181
199 ErrorCode deleteResource( QNetworkRequest &request, QgsFeedback *feedback = nullptr );
200
204 QString errorMessage() const { return mErrorMessage; }
205
209 QgsNetworkReplyContent reply() const { return mReplyContent; }
210
215 QString authCfg() const;
216
221 void setAuthCfg( const QString &authCfg );
222
223 public slots:
224
228 void abort();
229
230 signals:
231
235 void downloadProgress( qint64 bytesReceived, qint64 bytesTotal );
236
241 Q_DECL_DEPRECATED void downloadFinished() SIP_DEPRECATED;
242
247 void uploadProgress( qint64 bytesReceived, qint64 bytesTotal );
248
252 void finished();
253
254 private slots:
255 void replyProgress( qint64, qint64 );
256 void replyFinished();
257 void requestTimedOut( QNetworkReply *reply );
258
259 private :
260
262 QNetworkReply *mReply = nullptr;
263
264 Qgis::HttpMethod mMethod = Qgis::HttpMethod::Get;
265
267 QIODevice *mPayloadData;
268
270 QString mAuthCfg;
271
273 QString mErrorMessage;
274
276 ErrorCode mErrorCode = NoError;
277
278 QgsNetworkReplyContent mReplyContent;
279
281 bool mIsAborted = false;
282
284 bool mForceRefresh = false;
285
287 bool mTimedout = false;
288
290 bool mGotNonEmptyResponse = false;
291
293 RequestFlags mRequestFlags;
294
295 int mExpirationSec = 30;
296
297 QPointer< QgsFeedback > mFeedback;
298
299 ErrorCode doRequest( Qgis::HttpMethod method, QNetworkRequest &request, bool forceRefresh, QgsFeedback *feedback = nullptr, RequestFlags requestFlags = RequestFlags() );
300
301 QString errorMessageFailedAuth();
302
303 void sendRequestToNetworkAccessManager( const QNetworkRequest &request );
304
305 void abortIfNotPartialContentReturned();
306};
307
309#ifndef SIP_RUN
310
311class DownloaderThread : public QThread
312{
313 Q_OBJECT
314
315 public:
316 DownloaderThread( const std::function<void()> &function, QObject *parent = nullptr )
317 : QThread( parent )
318 , mFunction( function )
319 {
320 }
321
322 void run() override
323 {
324 mFunction();
325 }
326
327 private:
328 std::function<void()> mFunction;
329};
330
331#endif
333
334#endif // QGSBLOCKINGNETWORKREQUEST_H
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...
Q_DECL_DEPRECATED void downloadFinished()
Emitted once a request has finished downloading.
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
Emitted when when data arrives during a request.
@ 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
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:278