QGIS API Documentation 4.1.0-Master (60fea48833c)
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:
61
67 enum class RequestFlag : int SIP_ENUM_BASETYPE( IntFlag )
68 {
69 EmptyResponseIsValid = 1 << 0,
70 };
71 Q_ENUM( RequestFlag )
72 Q_DECLARE_FLAGS( RequestFlags, RequestFlag )
73 Q_FLAG( RequestFlags )
74
80 explicit QgsBlockingNetworkRequest( Qgis::NetworkRequestFlags flags = Qgis::NetworkRequestFlags() );
81
82 ~QgsBlockingNetworkRequest() override;
83
89 Qgis::NetworkRequestFlags flags() const { return mFlags; }
90
113 ErrorCode get( QNetworkRequest &request, bool forceRefresh = false, QgsFeedback *feedback = nullptr, RequestFlags requestFlags = QgsBlockingNetworkRequest::RequestFlags() );
114
136 ErrorCode post( QNetworkRequest &request, QIODevice *data, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
137
143 ErrorCode post( QNetworkRequest &request, const QByteArray &data, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
144
165 ErrorCode head( QNetworkRequest &request, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
166
184 ErrorCode put( QNetworkRequest &request, QIODevice *data, QgsFeedback *feedback = nullptr );
185
192 ErrorCode put( QNetworkRequest &request, const QByteArray &data, QgsFeedback *feedback = nullptr );
193
211 ErrorCode deleteResource( QNetworkRequest &request, QgsFeedback *feedback = nullptr );
212
216 QString errorMessage() const { return mErrorMessage; }
217
221 QgsNetworkReplyContent reply() const { return mReplyContent; }
222
227 QString authCfg() const;
228
233 void setAuthCfg( const QString &authCfg );
234
235 public slots:
236
240 void abort();
241
242 signals:
243
247 void downloadProgress( qint64 bytesReceived, qint64 bytesTotal );
248
253 Q_DECL_DEPRECATED void downloadFinished() SIP_DEPRECATED;
254
259 void uploadProgress( qint64 bytesReceived, qint64 bytesTotal );
260
264 void finished();
265
266 private slots:
267 void replyProgress( qint64, qint64 );
268 void replyFinished();
269 void requestTimedOut( QNetworkReply *reply );
270
271 private:
272 Qgis::NetworkRequestFlags mFlags;
273
275 QNetworkReply *mReply = nullptr;
276
277 Qgis::HttpMethod mMethod = Qgis::HttpMethod::Get;
278
280 QIODevice *mPayloadData = nullptr;
281
283 QString mAuthCfg;
284
286 QString mErrorMessage;
287
289 ErrorCode mErrorCode = NoError;
290
291 QgsNetworkReplyContent mReplyContent;
292
294 bool mIsAborted = false;
295
297 bool mForceRefresh = false;
298
300 bool mTimedout = false;
301
303 bool mGotNonEmptyResponse = false;
304
306 RequestFlags mRequestFlags;
307
308 int mExpirationSec = 30;
309
310 QPointer< QgsFeedback > mFeedback;
311
312 ErrorCode doRequest( Qgis::HttpMethod method, QNetworkRequest &request, bool forceRefresh, QgsFeedback *feedback = nullptr, RequestFlags requestFlags = RequestFlags() );
313
314 QString errorMessageFailedAuth();
315
316 void sendRequestToNetworkAccessManager( const QNetworkRequest &request );
317
318 void abortIfNotPartialContentReturned();
319};
320
322#ifndef SIP_RUN
323
324class DownloaderThread : public QThread
325{
326 Q_OBJECT
327
328 public:
329 DownloaderThread( const std::function<void()> &function, QObject *parent = nullptr )
330 : QThread( parent )
331 , mFunction( function )
332 {}
333
334 void run() override { mFunction(); }
335
336 private:
337 std::function<void()> mFunction;
338};
339
340#endif
342
343#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