Simplify getLayers method for geonode client.

This commit is contained in:
Ismail Sunni 2017-08-25 21:48:39 +07:00 committed by Nyall Dawson
parent bdc3d36601
commit 500c5c1108
5 changed files with 24 additions and 30 deletions

View File

@ -39,12 +39,7 @@ class QgsGeoNodeRequest : QObject
:rtype: bool :rtype: bool
%End %End
bool getLayers(); QList<QgsServiceLayerDetail> getLayers();
%Docstring
:rtype: bool
%End
QList<QgsServiceLayerDetail> parseLayers( QByteArray layerResponse );
%Docstring %Docstring
:rtype: list of QgsServiceLayerDetail :rtype: list of QgsServiceLayerDetail
%End %End

View File

@ -59,9 +59,15 @@ void QgsGeoNodeRequest::abort()
} }
} }
bool QgsGeoNodeRequest::getLayers() QList<QgsServiceLayerDetail> QgsGeoNodeRequest::getLayers()
{ {
return request( QStringLiteral( "/api/layers/" ) ); QList<QgsServiceLayerDetail> layers;
bool success = request( QStringLiteral( "/api/layers/" ) );
if ( !success )
{
return layers;
}
return parseLayers( this->response() );
} }
void QgsGeoNodeRequest::replyProgress( qint64 bytesReceived, qint64 bytesTotal ) void QgsGeoNodeRequest::replyProgress( qint64 bytesReceived, qint64 bytesTotal )
@ -83,7 +89,6 @@ void QgsGeoNodeRequest::setProtocol( const QString &protocol )
mProtocol = protocol; mProtocol = protocol;
} }
void QgsGeoNodeRequest::replyFinished() void QgsGeoNodeRequest::replyFinished()
{ {
QgsMessageLog::logMessage( QStringLiteral( "Reply finished" ), tr( "GeoNode" ) ); QgsMessageLog::logMessage( QStringLiteral( "Reply finished" ), tr( "GeoNode" ) );
@ -293,19 +298,17 @@ QList<QgsServiceLayerDetail> QgsGeoNodeRequest::parseLayers( QByteArray layerRes
return layers; return layers;
} }
QStringList QgsGeoNodeRequest::serviceUrls( QString serviceType ) QStringList QgsGeoNodeRequest::serviceUrls( QString serviceType )
{ {
QStringList urls; QStringList urls;
bool success = getLayers();
if ( !success ) QList<QgsServiceLayerDetail> layers = getLayers();
if ( layers.empty() )
{ {
return urls; return urls;
} }
QList<QgsServiceLayerDetail> layers = parseLayers( this->response() );
Q_FOREACH ( QgsServiceLayerDetail layer, layers ) Q_FOREACH ( QgsServiceLayerDetail layer, layers )
{ {
QString url; QString url;
@ -339,17 +342,16 @@ QStringList QgsGeoNodeRequest::serviceUrls( QString serviceType )
return urls; return urls;
} }
QgsStringMap QgsGeoNodeRequest::serviceUrlData( QString serviceType ) QgsStringMap QgsGeoNodeRequest::serviceUrlData( QString serviceType )
{ {
QgsStringMap urls; QgsStringMap urls;
bool success = getLayers();
if ( !success ) QList<QgsServiceLayerDetail> layers = getLayers();
if ( layers.empty() )
{ {
return urls; return urls;
} }
QList<QgsServiceLayerDetail> layers = parseLayers( this->response() );
Q_FOREACH ( QgsServiceLayerDetail layer, layers ) Q_FOREACH ( QgsServiceLayerDetail layer, layers )
{ {
@ -386,7 +388,6 @@ QgsStringMap QgsGeoNodeRequest::serviceUrlData( QString serviceType )
return urls; return urls;
} }
bool QgsGeoNodeRequest::request( QString endPoint ) bool QgsGeoNodeRequest::request( QString endPoint )
{ {
abort(); abort();

View File

@ -49,9 +49,7 @@ class CORE_EXPORT QgsGeoNodeRequest : public QObject
bool request( QString endPoint ); bool request( QString endPoint );
bool getLayers(); QList<QgsServiceLayerDetail> getLayers();
QList<QgsServiceLayerDetail> parseLayers( QByteArray layerResponse );
// Obtain list of unique URL in the geonode // Obtain list of unique URL in the geonode
QStringList serviceUrls( QString serviceType ); QStringList serviceUrls( QString serviceType );
@ -71,6 +69,9 @@ class CORE_EXPORT QgsGeoNodeRequest : public QObject
QString getProtocol() const; QString getProtocol() const;
void setProtocol( const QString &protocol ); void setProtocol( const QString &protocol );
private:
QList<QgsServiceLayerDetail> parseLayers( QByteArray layerResponse );
signals: signals:
//! \brief emit a signal to be caught by qgisapp and display a statusQString on status bar //! \brief emit a signal to be caught by qgisapp and display a statusQString on status bar
void statusChanged( const QString &statusQString ); void statusChanged( const QString &statusQString );

View File

@ -248,10 +248,11 @@ void QgsGeoNodeNewConnection::testConnection()
QApplication::setOverrideCursor( Qt::BusyCursor ); QApplication::setOverrideCursor( Qt::BusyCursor );
QString url = txtUrl->text(); QString url = txtUrl->text();
QgsGeoNodeRequest geonodeRequest( url, true ); QgsGeoNodeRequest geonodeRequest( url, true );
bool success = geonodeRequest.getLayers();
QList<QgsServiceLayerDetail> layers = geonodeRequest.getLayers();
QApplication::restoreOverrideCursor(); QApplication::restoreOverrideCursor();
if ( success ) if ( !layers.empty() )
{ {
QMessageBox::information( this, QMessageBox::information( this,
tr( "Test connection" ), tr( "Test connection" ),

View File

@ -194,10 +194,10 @@ void QgsGeoNodeSourceSelect::connectToGeonodeConnection()
QgsGeoNodeRequest geonodeRequest( url, true ); QgsGeoNodeRequest geonodeRequest( url, true );
QApplication::setOverrideCursor( Qt::WaitCursor ); QApplication::setOverrideCursor( Qt::WaitCursor );
bool success = geonodeRequest.getLayers(); QList<QgsServiceLayerDetail> layers = geonodeRequest.getLayers();
QApplication::restoreOverrideCursor(); QApplication::restoreOverrideCursor();
if ( success ) if ( !layers.empty() )
{ {
QgsMessageLog::logMessage( QStringLiteral( "Success" ), tr( "GeoNode" ) ); QgsMessageLog::logMessage( QStringLiteral( "Success" ), tr( "GeoNode" ) );
} }
@ -206,10 +206,6 @@ void QgsGeoNodeSourceSelect::connectToGeonodeConnection()
QgsMessageLog::logMessage( QStringLiteral( "Failed" ), tr( "GeoNode" ) ); QgsMessageLog::logMessage( QStringLiteral( "Failed" ), tr( "GeoNode" ) );
} }
QByteArray ba = geonodeRequest.response();
QList<QgsServiceLayerDetail> layers = geonodeRequest.parseLayers( ba );
if ( mModel ) if ( mModel )
{ {
mModel->removeRows( 0, mModel->rowCount() ); mModel->removeRows( 0, mModel->rowCount() );