QGIS API Documentation 4.1.0-Master (467af3bbe65)
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 <QHttpMultiPart>
26#include <QObject>
27#include <QPointer>
28#include <QThread>
29
30class QNetworkRequest;
31class QNetworkReply;
32
50class CORE_EXPORT QgsBlockingNetworkRequest : public QObject
51{
52 Q_OBJECT
53 public:
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
152 ErrorCode post( QNetworkRequest &request, QHttpMultiPart *data, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
153
174 ErrorCode head( QNetworkRequest &request, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
175
193 ErrorCode put( QNetworkRequest &request, QIODevice *data, QgsFeedback *feedback = nullptr );
194
201 ErrorCode put( QNetworkRequest &request, const QByteArray &data, QgsFeedback *feedback = nullptr );
202
220 ErrorCode deleteResource( QNetworkRequest &request, QgsFeedback *feedback = nullptr );
221
225 QString errorMessage() const { return mErrorMessage; }
226
230 QgsNetworkReplyContent reply() const { return mReplyContent; }
231
236 QString authCfg() const;
237
242 void setAuthCfg( const QString &authCfg );
243
244 public slots:
245
249 void abort();
250
251 signals:
252
256 void downloadProgress( qint64 bytesReceived, qint64 bytesTotal );
257
262 Q_DECL_DEPRECATED void downloadFinished() SIP_DEPRECATED;
263
268 void uploadProgress( qint64 bytesReceived, qint64 bytesTotal );
269
273 void finished();
274
275 private slots:
276 void replyProgress( qint64, qint64 );
277 void replyFinished();
278 void requestTimedOut( QNetworkReply *reply );
279
280 private:
281 Qgis::NetworkRequestFlags mFlags;
282
284 QNetworkReply *mReply = nullptr;
285
286 Qgis::HttpMethod mMethod = Qgis::HttpMethod::Get;
287
289 std::variant<QIODevice *, QHttpMultiPart *> mPayloadDataVariant;
290
292 QString mAuthCfg;
293
295 QString mErrorMessage;
296
298 ErrorCode mErrorCode = NoError;
299
300 QgsNetworkReplyContent mReplyContent;
301
303 bool mIsAborted = false;
304
306 bool mForceRefresh = false;
307
309 bool mTimedout = false;
310
312 bool mGotNonEmptyResponse = false;
313
315 RequestFlags mRequestFlags;
316
317 int mExpirationSec = 30;
318
319 QPointer< QgsFeedback > mFeedback;
320
321 ErrorCode doRequest( Qgis::HttpMethod method, QNetworkRequest &request, bool forceRefresh, QgsFeedback *feedback = nullptr, RequestFlags requestFlags = RequestFlags() );
322
323 QString errorMessageFailedAuth();
324
325 void sendRequestToNetworkAccessManager( const QNetworkRequest &request );
326
327 void abortIfNotPartialContentReturned();
328};
329
331#ifndef SIP_RUN
332
333class DownloaderThread : public QThread
334{
335 Q_OBJECT
336
337 public:
338 DownloaderThread( const std::function<void()> &function, QObject *parent = nullptr )
339 : QThread( parent )
340 , mFunction( function )
341 {}
342
343 void run() override { mFunction(); }
344
345 private:
346 std::function<void()> mFunction;
347};
348
349#endif
351
352#endif // QGSBLOCKINGNETWORKREQUEST_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
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:113
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:274