Skip to content

Commit

Permalink
[afs] Use QgsBlockingNetworkRequest instead of event loop
Browse files Browse the repository at this point in the history
Instantly a more stable provider!
  • Loading branch information
nyalldawson committed Dec 20, 2018
1 parent d1a1d75 commit df2d660
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 55 deletions.
8 changes: 0 additions & 8 deletions src/providers/arcgisrest/qgsafsprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" );

Expand Down
62 changes: 15 additions & 47 deletions src/providers/arcgisrest/qgsarcgisrestutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -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 )
Expand Down

0 comments on commit df2d660

Please sign in to comment.