Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
QgsBlockingNetworkRequest: add head(), put() and deleteResource() met…
…hods
  • Loading branch information
rouault authored and nyalldawson committed Oct 24, 2020
1 parent d8f4c7b commit 3ba4cb4
Show file tree
Hide file tree
Showing 5 changed files with 491 additions and 42 deletions.
60 changes: 60 additions & 0 deletions python/core/auto_generated/qgsblockingnetworkrequest.sip.in
Expand Up @@ -89,6 +89,66 @@ If an error was encountered then a specific ErrorCode will be returned, and a de
can be retrieved by calling :py:func:`~QgsBlockingNetworkRequest.errorMessage`.

.. seealso:: :py:func:`get`
%End

ErrorCode head( QNetworkRequest &request, bool forceRefresh = false, QgsFeedback *feedback = 0 );
%Docstring
Performs a "head" operation on the specified ``request``.

If ``forceRefresh`` is ``False`` then previously cached replies may be used for the request. If
it is set to ``True`` then a new query is always performed.

If an :py:func:`~QgsBlockingNetworkRequest.authCfg` has been set, then any authentication configuration required will automatically be applied to
``request``. There is no need to manually apply the authentication to the request prior to calling
this method.

The optional ``feedback`` argument can be used to abort ongoing requests.

The method will return NoError if the get operation was successful. The contents of the reply can be retrieved
by calling :py:func:`~QgsBlockingNetworkRequest.reply`.

If an error was encountered then a specific ErrorCode will be returned, and a detailed error message
can be retrieved by calling :py:func:`~QgsBlockingNetworkRequest.errorMessage`.

.. versionadded:: 3.18
%End

ErrorCode put( QNetworkRequest &request, const QByteArray &data, QgsFeedback *feedback = 0 );
%Docstring
Performs a "put" operation on the specified ``request``, using the given ``data``.

If an :py:func:`~QgsBlockingNetworkRequest.authCfg` has been set, then any authentication configuration required will automatically be applied to
``request``. There is no need to manually apply the authentication to the request prior to calling
this method.

The optional ``feedback`` argument can be used to abort ongoing requests.

The method will return NoError if the get operation was successful. The contents of the reply can be retrieved
by calling :py:func:`~QgsBlockingNetworkRequest.reply`.

If an error was encountered then a specific ErrorCode will be returned, and a detailed error message
can be retrieved by calling :py:func:`~QgsBlockingNetworkRequest.errorMessage`.

.. versionadded:: 3.18
%End

ErrorCode deleteResource( QNetworkRequest &request, QgsFeedback *feedback = 0 );
%Docstring
Performs a "delete" operation on the specified ``request``.

If an :py:func:`~QgsBlockingNetworkRequest.authCfg` has been set, then any authentication configuration required will automatically be applied to
``request``. There is no need to manually apply the authentication to the request prior to calling
this method.

The optional ``feedback`` argument can be used to abort ongoing requests.

The method will return NoError if the get operation was successful. The contents of the reply can be retrieved
by calling :py:func:`~QgsBlockingNetworkRequest.reply`.

If an error was encountered then a specific ErrorCode will be returned, and a detailed error message
can be retrieved by calling :py:func:`~QgsBlockingNetworkRequest.errorMessage`.

.. versionadded:: 3.18
%End

QString errorMessage() const;
Expand Down
67 changes: 46 additions & 21 deletions src/core/qgsblockingnetworkrequest.cpp
Expand Up @@ -61,10 +61,52 @@ QgsBlockingNetworkRequest::ErrorCode QgsBlockingNetworkRequest::get( QNetworkReq

QgsBlockingNetworkRequest::ErrorCode QgsBlockingNetworkRequest::post( QNetworkRequest &request, const QByteArray &data, bool forceRefresh, QgsFeedback *feedback )
{
mPostData = data;
mPayloadData = data;
return doRequest( Post, request, forceRefresh, feedback );
}

QgsBlockingNetworkRequest::ErrorCode QgsBlockingNetworkRequest::head( QNetworkRequest &request, bool forceRefresh, QgsFeedback *feedback )
{
return doRequest( Head, request, forceRefresh, feedback );
}

QgsBlockingNetworkRequest::ErrorCode QgsBlockingNetworkRequest::put( QNetworkRequest &request, const QByteArray &data, QgsFeedback *feedback )
{
mPayloadData = data;
return doRequest( Put, request, true, feedback );
}

QgsBlockingNetworkRequest::ErrorCode QgsBlockingNetworkRequest::deleteResource( QNetworkRequest &request, QgsFeedback *feedback )
{
return doRequest( Delete, request, true, feedback );
}

void QgsBlockingNetworkRequest::sendRequestToNetworkAccessManager( const QNetworkRequest &request )
{
switch ( mMethod )
{
case Get:
mReply = QgsNetworkAccessManager::instance()->get( request );
break;

case Post:
mReply = QgsNetworkAccessManager::instance()->post( request, mPayloadData );
break;

case Head:
mReply = QgsNetworkAccessManager::instance()->head( request );
break;

case Put:
mReply = QgsNetworkAccessManager::instance()->put( request, mPayloadData );
break;

case Delete:
mReply = QgsNetworkAccessManager::instance()->deleteResource( request );
break;
};
}

QgsBlockingNetworkRequest::ErrorCode QgsBlockingNetworkRequest::doRequest( QgsBlockingNetworkRequest::Method method, QNetworkRequest &request, bool forceRefresh, QgsFeedback *feedback )
{
mMethod = method;
Expand Down Expand Up @@ -114,16 +156,7 @@ QgsBlockingNetworkRequest::ErrorCode QgsBlockingNetworkRequest::doRequest( QgsBl

success = true;

switch ( mMethod )
{
case Get:
mReply = QgsNetworkAccessManager::instance()->get( request );
break;

case Post:
mReply = QgsNetworkAccessManager::instance()->post( request, mPostData );
break;
};
sendRequestToNetworkAccessManager( request );

if ( mFeedback )
connect( mFeedback, &QgsFeedback::canceled, mReply, &QNetworkReply::abort );
Expand Down Expand Up @@ -295,16 +328,8 @@ void QgsBlockingNetworkRequest::replyFinished()
mReply = nullptr;

QgsDebugMsgLevel( QStringLiteral( "redirected: %1 forceRefresh=%2" ).arg( redirect.toString() ).arg( mForceRefresh ), 2 );
switch ( mMethod )
{
case Get:
mReply = QgsNetworkAccessManager::instance()->get( request );
break;

case Post:
mReply = QgsNetworkAccessManager::instance()->post( request, mPostData );
break;
};
sendRequestToNetworkAccessManager( request );

if ( mFeedback )
connect( mFeedback, &QgsFeedback::canceled, mReply, &QNetworkReply::abort );
Expand Down Expand Up @@ -361,7 +386,7 @@ void QgsBlockingNetworkRequest::replyFinished()

mReplyContent = QgsNetworkReplyContent( mReply );
const QByteArray content = mReply->readAll();
if ( content.isEmpty() && !mGotNonEmptyResponse )
if ( content.isEmpty() && !mGotNonEmptyResponse && mMethod == Get )
{
mErrorMessage = tr( "empty response: %1" ).arg( mReply->errorString() );
mErrorCode = ServerExceptionError;
Expand Down
68 changes: 66 additions & 2 deletions src/core/qgsblockingnetworkrequest.h
Expand Up @@ -106,6 +106,66 @@ class CORE_EXPORT QgsBlockingNetworkRequest : public QObject
*/
ErrorCode post( QNetworkRequest &request, const QByteArray &data, bool forceRefresh = false, QgsFeedback *feedback = nullptr );

/**
* Performs a "head" operation on the specified \a request.
*
* If \a forceRefresh is FALSE then previously cached replies may be used for the request. If
* it is set to TRUE then a new query is always performed.
*
* If an authCfg() has been set, then any authentication configuration required will automatically be applied to
* \a request. There is no need to manually apply the authentication to the request prior to calling
* this method.
*
* The optional \a feedback argument can be used to abort ongoing requests.
*
* The method will return NoError if the get operation was successful. The contents of the reply can be retrieved
* by calling reply().
*
* If an error was encountered then a specific ErrorCode will be returned, and a detailed error message
* can be retrieved by calling errorMessage().
*
* \since 3.18
*/
ErrorCode head( QNetworkRequest &request, bool forceRefresh = false, QgsFeedback *feedback = nullptr );

/**
* Performs a "put" operation on the specified \a request, using the given \a data.
*
* If an authCfg() has been set, then any authentication configuration required will automatically be applied to
* \a request. There is no need to manually apply the authentication to the request prior to calling
* this method.
*
* The optional \a feedback argument can be used to abort ongoing requests.
*
* The method will return NoError if the get operation was successful. The contents of the reply can be retrieved
* by calling reply().
*
* If an error was encountered then a specific ErrorCode will be returned, and a detailed error message
* can be retrieved by calling errorMessage().
*
* \since 3.18
*/
ErrorCode put( QNetworkRequest &request, const QByteArray &data, QgsFeedback *feedback = nullptr );

/**
* Performs a "delete" operation on the specified \a request.
*
* If an authCfg() has been set, then any authentication configuration required will automatically be applied to
* \a request. There is no need to manually apply the authentication to the request prior to calling
* this method.
*
* The optional \a feedback argument can be used to abort ongoing requests.
*
* The method will return NoError if the get operation was successful. The contents of the reply can be retrieved
* by calling reply().
*
* If an error was encountered then a specific ErrorCode will be returned, and a detailed error message
* can be retrieved by calling errorMessage().
*
* \since 3.18
*/
ErrorCode deleteResource( QNetworkRequest &request, QgsFeedback *feedback = nullptr );

/**
* Returns the error message string, after a get() or post() request has been made.\
*/
Expand Down Expand Up @@ -157,14 +217,17 @@ class CORE_EXPORT QgsBlockingNetworkRequest : public QObject
enum Method
{
Get,
Post
Post,
Head,
Put,
Delete
};

//! The reply to the request
QNetworkReply *mReply = nullptr;

Method mMethod = Get;
QByteArray mPostData;
QByteArray mPayloadData;

//! Authentication configuration ID
QString mAuthCfg;
Expand Down Expand Up @@ -197,6 +260,7 @@ class CORE_EXPORT QgsBlockingNetworkRequest : public QObject

QString errorMessageFailedAuth();

void sendRequestToNetworkAccessManager( const QNetworkRequest &request );
};

///@cond PRIVATE
Expand Down

0 comments on commit 3ba4cb4

Please sign in to comment.