Skip to content
Permalink
Browse files

[afs] Use QgsBlockingNetworkRequest instead of event loop

Instantly a more stable provider!
  • Loading branch information
nyalldawson committed Nov 13, 2018
1 parent d1a1d75 commit df2d6601b8ecffec7b8924c4a7404f6d1683444f
Showing with 15 additions and 55 deletions.
  1. +0 −8 src/providers/arcgisrest/qgsafsprovider.cpp
  2. +15 −47 src/providers/arcgisrest/qgsarcgisrestutils.cpp
@@ -21,21 +21,13 @@
#include "qgsdatasourceuri.h"
#include "qgsafsdataitems.h"
#include "qgslogger.h"
#include "geometry/qgsgeometry.h"
#include "qgsnetworkaccessmanager.h"
#include "qgsdataitemprovider.h"

#ifdef HAVE_GUI
#include "qgsafssourceselect.h"
#include "qgssourceselectprovider.h"
#endif

#include <QEventLoop>
#include <QMessageBox>
#include <QNetworkRequest>
#include <QNetworkReply>


static const QString TEXT_PROVIDER_KEY = QStringLiteral( "arcgisfeatureserver" );
static const QString TEXT_PROVIDER_DESCRIPTION = QStringLiteral( "ArcGIS Feature Server data provider" );

@@ -42,10 +42,8 @@
#include "qgscategorizedsymbolrenderer.h"
#include "qgsvectorlayerlabeling.h"

#include <QEventLoop>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QThread>
#include "qgsblockingnetworkrequest.h"
#include <QJsonDocument>
#include <QJsonObject>

@@ -471,57 +469,27 @@ QList<quint32> QgsArcGisRestUtils::getObjectIdsByExtent( const QString &layerurl

QByteArray QgsArcGisRestUtils::queryService( const QUrl &u, const QString &authcfg, QString &errorTitle, QString &errorText, QgsFeedback *feedback )
{
QEventLoop loop;
QUrl url = parseUrl( u );

QNetworkRequest request( url );
QgsBlockingNetworkRequest networkRequest;
networkRequest.setAuthCfg( authcfg );
const QgsBlockingNetworkRequest::ErrorCode error = networkRequest.get( request, false, feedback );

if ( !authcfg.isEmpty() )
{
QgsApplication::authManager()->updateNetworkRequest( request, authcfg );
}

QNetworkReply *reply = nullptr;
QgsNetworkAccessManager *nam = QgsNetworkAccessManager::instance();
if ( feedback && feedback->isCanceled() )
return QByteArray();

// Request data, handling redirects
while ( true )
// Handle network errors
if ( error != QgsBlockingNetworkRequest::NoError )
{
reply = nam->get( request );
QObject::connect( reply, &QNetworkReply::finished, &loop, &QEventLoop::quit );
if ( feedback )
{
QObject::connect( feedback, &QgsFeedback::canceled, reply, &QNetworkReply::abort );
}

loop.exec( QEventLoop::ExcludeUserInputEvents );

reply->deleteLater();

if ( feedback && feedback->isCanceled() )
return QByteArray();

// Handle network errors
if ( reply->error() != QNetworkReply::NoError )
{
QgsDebugMsg( QStringLiteral( "Network error: %1" ).arg( reply->errorString() ) );
errorTitle = QStringLiteral( "Network error" );
errorText = reply->errorString();
return QByteArray();
}

// Handle HTTP redirects
QVariant redirect = reply->attribute( QNetworkRequest::RedirectionTargetAttribute );
if ( redirect.isNull() )
{
break;
}

QgsDebugMsg( "redirecting to " + redirect.toUrl().toString() );
request.setUrl( redirect.toUrl() );
QgsDebugMsg( QStringLiteral( "Network error: %1" ).arg( networkRequest.errorMessage() ) );
errorTitle = QStringLiteral( "Network error" );
errorText = networkRequest.errorMessage();
return QByteArray();
}
QByteArray result = reply->readAll();
return result;

const QgsNetworkReplyContent content = networkRequest.reply();
return content.content();
}

QVariantMap QgsArcGisRestUtils::queryServiceJSON( const QUrl &url, const QString &authcfg, QString &errorTitle, QString &errorText, QgsFeedback *feedback )

0 comments on commit df2d660

Please sign in to comment.
You can’t perform that action at this time.