QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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
65 enum class RequestFlag : int SIP_ENUM_BASETYPE( IntFlag )
66 {
67 EmptyResponseIsValid = 1 << 0,
68 };
69 Q_ENUM( RequestFlag )
70 Q_DECLARE_FLAGS( RequestFlags, RequestFlag )
71 Q_FLAG( RequestFlags )
72
75
76 ~QgsBlockingNetworkRequest() override;
77
100 ErrorCode get( QNetworkRequest &request, bool forceRefresh = false, QgsFeedback *feedback = nullptr, RequestFlags requestFlags = QgsBlockingNetworkRequest::RequestFlags() );
101
123 ErrorCode post( QNetworkRequest &request, QIODevice *data, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
124
130 ErrorCode post( QNetworkRequest &request, const QByteArray &data, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
131
152 ErrorCode head( QNetworkRequest &request, bool forceRefresh = false, QgsFeedback *feedback = nullptr );
153
171 ErrorCode put( QNetworkRequest &request, QIODevice *data, QgsFeedback *feedback = nullptr );
172
179 ErrorCode put( QNetworkRequest &request, const QByteArray &data, QgsFeedback *feedback = nullptr );
180
198 ErrorCode deleteResource( QNetworkRequest &request, QgsFeedback *feedback = nullptr );
199
203 QString errorMessage() const { return mErrorMessage; }
204
208 QgsNetworkReplyContent reply() const { return mReplyContent; }
209
214 QString authCfg() const;
215
220 void setAuthCfg( const QString &authCfg );
221
222 public slots:
223
227 void abort();
228
229 signals:
230
234 void downloadProgress( qint64 bytesReceived, qint64 bytesTotal );
235
240 Q_DECL_DEPRECATED void downloadFinished() SIP_DEPRECATED;
241
246 void uploadProgress( qint64 bytesReceived, qint64 bytesTotal );
247
251 void finished();
252
253 private slots:
254 void replyProgress( qint64, qint64 );
255 void replyFinished();
256 void requestTimedOut( QNetworkReply *reply );
257
258 private :
259
260 enum Method
261 {
262 Get,
263 Post,
264 Head,
265 Put,
266 Delete
267 };
268
270 QNetworkReply *mReply = nullptr;
271
272 Method mMethod = Get;
273
275 QIODevice *mPayloadData;
276
278 QString mAuthCfg;
279
281 QString mErrorMessage;
282
284 ErrorCode mErrorCode = NoError;
285
286 QgsNetworkReplyContent mReplyContent;
287
289 bool mIsAborted = false;
290
292 bool mForceRefresh = false;
293
295 bool mTimedout = false;
296
298 bool mGotNonEmptyResponse = false;
299
301 RequestFlags mRequestFlags;
302
303 int mExpirationSec = 30;
304
305 QPointer< QgsFeedback > mFeedback;
306
307 ErrorCode doRequest( Method method, QNetworkRequest &request, bool forceRefresh, QgsFeedback *feedback = nullptr, RequestFlags requestFlags = RequestFlags() );
308
309 QString errorMessageFailedAuth();
310
311 void sendRequestToNetworkAccessManager( const QNetworkRequest &request );
312
313 void abortIfNotPartialContentReturned();
314};
315
317#ifndef SIP_RUN
318
319class DownloaderThread : public QThread
320{
321 Q_OBJECT
322
323 public:
324 DownloaderThread( const std::function<void()> &function, QObject *parent = nullptr )
325 : QThread( parent )
326 , mFunction( function )
327 {
328 }
329
330 void run() override
331 {
332 mFunction();
333 }
334
335 private:
336 std::function<void()> mFunction;
337};
338
339#endif
341
342#endif // QGSBLOCKINGNETWORKREQUEST_H
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