QGIS API Documentation 3.99.0-Master (26c88405ac0)
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 <functional>
19
20#include "qgis.h"
21#include "qgis_core.h"
22#include "qgsfeedback.h"
23#include "qgsnetworkreply.h"
24
25#include <QObject>
26#include <QPointer>
27#include <QThread>
28
29class QNetworkRequest;
30class QNetworkReply;
31
49class CORE_EXPORT QgsBlockingNetworkRequest : public QObject
50{
51 Q_OBJECT
52 public:
53
62
68 enum class RequestFlag : int SIP_ENUM_BASETYPE( IntFlag )
69 {
70 EmptyResponseIsValid = 1 << 0,
71 };
72 Q_ENUM( RequestFlag )
73 Q_DECLARE_FLAGS( RequestFlags, RequestFlag )
74 Q_FLAG( RequestFlags )
75
81 explicit QgsBlockingNetworkRequest( Qgis::NetworkRequestFlags flags = Qgis::NetworkRequestFlags() );
82
83 ~QgsBlockingNetworkRequest() override;
84
90 Qgis::NetworkRequestFlags flags() const { return mFlags; }
91
114 ErrorCode get( QNetworkRequest &request, bool forceRefresh = false, QgsFeedback *feedback = nullptr, RequestFlags requestFlags = QgsBlockingNetworkRequest::RequestFlags() );
115
137 ErrorCode post( QNetworkRequest &request, QIODevice *data, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
138
144 ErrorCode post( QNetworkRequest &request, const QByteArray &data, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
145
166 ErrorCode head( QNetworkRequest &request, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
167
185 ErrorCode put( QNetworkRequest &request, QIODevice *data, QgsFeedback *feedback = nullptr );
186
193 ErrorCode put( QNetworkRequest &request, const QByteArray &data, QgsFeedback *feedback = nullptr );
194
212 ErrorCode deleteResource( QNetworkRequest &request, QgsFeedback *feedback = nullptr );
213
217 QString errorMessage() const { return mErrorMessage; }
218
222 QgsNetworkReplyContent reply() const { return mReplyContent; }
223
228 QString authCfg() const;
229
234 void setAuthCfg( const QString &authCfg );
235
236 public slots:
237
241 void abort();
242
243 signals:
244
248 void downloadProgress( qint64 bytesReceived, qint64 bytesTotal );
249
254 Q_DECL_DEPRECATED void downloadFinished() SIP_DEPRECATED;
255
260 void uploadProgress( qint64 bytesReceived, qint64 bytesTotal );
261
265 void finished();
266
267 private slots:
268 void replyProgress( qint64, qint64 );
269 void replyFinished();
270 void requestTimedOut( QNetworkReply *reply );
271
272 private :
273
274 Qgis::NetworkRequestFlags mFlags;
275
277 QNetworkReply *mReply = nullptr;
278
279 Qgis::HttpMethod mMethod = Qgis::HttpMethod::Get;
280
282 QIODevice *mPayloadData = nullptr;
283
285 QString mAuthCfg;
286
288 QString mErrorMessage;
289
291 ErrorCode mErrorCode = NoError;
292
293 QgsNetworkReplyContent mReplyContent;
294
296 bool mIsAborted = false;
297
299 bool mForceRefresh = false;
300
302 bool mTimedout = false;
303
305 bool mGotNonEmptyResponse = false;
306
308 RequestFlags mRequestFlags;
309
310 int mExpirationSec = 30;
311
312 QPointer< QgsFeedback > mFeedback;
313
314 ErrorCode doRequest( Qgis::HttpMethod method, QNetworkRequest &request, bool forceRefresh, QgsFeedback *feedback = nullptr, RequestFlags requestFlags = RequestFlags() );
315
316 QString errorMessageFailedAuth();
317
318 void sendRequestToNetworkAccessManager( const QNetworkRequest &request );
319
320 void abortIfNotPartialContentReturned();
321};
322
324#ifndef SIP_RUN
325
326class DownloaderThread : public QThread
327{
328 Q_OBJECT
329
330 public:
331 DownloaderThread( const std::function<void()> &function, QObject *parent = nullptr )
332 : QThread( parent )
333 , mFunction( function )
334 {
335 }
336
337 void run() override
338 {
339 mFunction();
340 }
341
342 private:
343 std::function<void()> mFunction;
344};
345
346#endif
348
349#endif // QGSBLOCKINGNETWORKREQUEST_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:56
QgsBlockingNetworkRequest(Qgis::NetworkRequestFlags flags=Qgis::NetworkRequestFlags())
Constructor for QgsBlockingNetworkRequest.
void uploadProgress(qint64 bytesReceived, qint64 bytesTotal)
Emitted when when data are sent during a request.
Qgis::NetworkRequestFlags flags() const
Returns the network request flags.
Q_DECL_DEPRECATED void downloadFinished()
Emitted once a request has finished downloading.
void finished()
Emitted once a request has finished.
QString errorMessage() const
Returns the error message string, after a get(), post(), head() or put() request has been made.
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:114
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:275