Skip to content

Commit

Permalink
Add method to retrieve filtered group content
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 21, 2020
1 parent 8b313f8 commit d878460
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 0 deletions.
Expand Up @@ -25,6 +25,13 @@ Utility functions for working with ArcGIS REST services.
%End
public:

enum ItemType
{
FeatureService,
MapService,
ImageService,
};

static QVariantMap retrieveUserInfo( const QString &communityUrl, const QString &user, const QString &authcfg, QString &errorTitle /Out/, QString &errorText /Out/, const QMap< QString, QString > &requestHeaders = QMap< QString, QString >(), QgsFeedback *feedback = 0 );
%Docstring
Retrieves JSON user info for the specified user name.
Expand Down Expand Up @@ -75,6 +82,26 @@ Retrieves JSON definitions for all items which belong the the specified ``groupI
- errorText: error text of any encountered errors
%End

static QVariantList retrieveGroupItemsOfType( const QString &contentUrl, const QString &groupId, const QString &authcfg,
const QList< int > &itemTypes,
QString &errorTitle /Out/, QString &errorText /Out/, const QMap< QString, QString > &requestHeaders = QMap< QString, QString >(), QgsFeedback *feedback = 0, int pageSize = 100 );
%Docstring
Retrieves JSON definitions for all items which belong the the specified ``groupId``.

:param contentUrl: should be set to the Portal's content URL, e.g. https://mysite.com/portal/sharing/rest/content
:param groupId: ID of group to query
:param authcfg: authentification configuration ID
:param itemTypes: list of desired item types (using QgsArcGisPortalUtils.ItemType values)
:param requestHeaders: optional additional request headers
:param feedback: optional feedback argument for cancelation support
:param pageSize: number of results to retrieve for each request. Maximum value is 100.

:return: - a list of JSON item info for all items within the group
- errorTitle: title summary of any encountered errrors
- errorText: error text of any encountered errors
%End


};

/************************************************************************
Expand Down
37 changes: 37 additions & 0 deletions src/core/providers/arcgis/qgsarcgisportalutils.cpp
Expand Up @@ -80,3 +80,40 @@ QVariantList QgsArcGisPortalUtils::retrieveGroupContent( const QString &contentU
}
return items;
}

QVariantList QgsArcGisPortalUtils::retrieveGroupItemsOfType( const QString &contentUrl, const QString &groupId, const QString &authcfg, const QList<int> &itemTypes, QString &errorTitle, QString &errorText, const QMap<QString, QString> &requestHeaders, QgsFeedback *feedback, int pageSize )
{
const QVariantList items = retrieveGroupContent( contentUrl, groupId, authcfg, errorTitle, errorText, requestHeaders, feedback, pageSize );

// filter results to desired types
QVariantList result;
for ( const QVariant &item : items )
{
const QVariantMap itemDef = item.toMap();
const QString itemType = itemDef.value( QStringLiteral( "type" ) ).toString();

for ( int filterType : itemTypes )
{
if ( typeToString( static_cast< ItemType >( filterType ) ).compare( itemType, Qt::CaseInsensitive ) == 0 )
{
result << item;
break;
}
}
}
return result;
}

QString QgsArcGisPortalUtils::typeToString( QgsArcGisPortalUtils::ItemType type )
{
switch ( type )
{
case QgsArcGisPortalUtils::FeatureService:
return QStringLiteral( "Feature Service" );
case QgsArcGisPortalUtils::MapService:
return QStringLiteral( "Map Service" );
case QgsArcGisPortalUtils::ImageService:
return QStringLiteral( "Image Service" );
}
return QString();
}
34 changes: 34 additions & 0 deletions src/core/providers/arcgis/qgsarcgisportalutils.h
Expand Up @@ -36,6 +36,16 @@ class CORE_EXPORT QgsArcGisPortalUtils
{
public:

/**
* Portal item types (not complete)
*/
enum ItemType
{
FeatureService, //!< ArcGIS Server feature service
MapService, //!< ArcGIS Server map service
ImageService, //!< ArcGIS Server image service
};

/**
* Retrieves JSON user info for the specified user name.
*
Expand Down Expand Up @@ -86,6 +96,30 @@ class CORE_EXPORT QgsArcGisPortalUtils
*/
static QVariantList retrieveGroupContent( const QString &contentUrl, const QString &groupId, const QString &authcfg, QString &errorTitle SIP_OUT, QString &errorText SIP_OUT, const QMap< QString, QString > &requestHeaders = QMap< QString, QString >(), QgsFeedback *feedback = nullptr, int pageSize = 100 );

/**
* Retrieves JSON definitions for all items which belong the the specified \a groupId.
*
* \param contentUrl should be set to the Portal's content URL, e.g. https://mysite.com/portal/sharing/rest/content/
* \param groupId ID of group to query
* \param authcfg authentification configuration ID
* \param itemTypes list of desired item types (using QgsArcGisPortalUtils.ItemType values)
* \param errorTitle title summary of any encountered errrors
* \param errorText error text of any encountered errors
* \param requestHeaders optional additional request headers
* \param feedback optional feedback argument for cancelation support
* \param pageSize number of results to retrieve for each request. Maximum value is 100.
*
* \returns a list of JSON item info for all items within the group
*/
static QVariantList retrieveGroupItemsOfType( const QString &contentUrl, const QString &groupId, const QString &authcfg,
const QList< int > &itemTypes,
QString &errorTitle SIP_OUT, QString &errorText SIP_OUT, const QMap< QString, QString > &requestHeaders = QMap< QString, QString >(), QgsFeedback *feedback = nullptr, int pageSize = 100 );


private:

static QString typeToString( ItemType type );

};

#endif // QGSARCGISPORTALUTILS_H
68 changes: 68 additions & 0 deletions tests/src/python/test_qgsarcgisportalutils.py
Expand Up @@ -218,6 +218,74 @@ def test_retrieve_group_items(self):
self.assertEqual(res[0], [{'id': '74', 'title': 'Item 1'}, {'id': '20', 'title': 'Item 2'},
{'id': '75', 'title': 'Item 3'}])

def test_retrieve_group_items_filtered(self):
"""
Test retrieving group content
"""
print(self.basetestpath)
endpoint = self.basetestpath + '/groupf_items_fake_qgis_http_endpoint'

with open(sanitize(endpoint + '_groups/', 'ab1?f=json&start=1&num=2'), 'wb') as f:
f.write("""{
"total": 3,
"start": 1,
"num": 2,
"nextStart": 3,
"items": [
{
"id": "74",
"title": "Item 1",
"type":"Feature Service"
},
{
"id": "20",
"title": "Item 2",
"type":"Map Service"
}
]
}""".encode('UTF-8'))

with open(sanitize(endpoint + '_groups/', 'ab1?f=json&start=3&num=2'), 'wb') as f:
f.write("""{
"total": 3,
"start": 3,
"num": 1,
"nextStart": 3,
"items": [
{
"id": "75",
"title": "Item 3",
"type":"Image Service"
}
]
}""".encode('UTF-8'))
res = QgsArcGisPortalUtils.retrieveGroupItemsOfType('http://' + endpoint, 'ab1', '',
[QgsArcGisPortalUtils.FeatureService], pageSize=2)
# no errors
self.assertFalse(res[1])
self.assertFalse(res[2])
self.assertEqual(res[0], [{'id': '74', 'title': 'Item 1', 'type': 'Feature Service'}])
res = QgsArcGisPortalUtils.retrieveGroupItemsOfType('http://' + endpoint, 'ab1', '',
[QgsArcGisPortalUtils.MapService], pageSize=2)
# no errors
self.assertFalse(res[1])
self.assertFalse(res[2])
self.assertEqual(res[0], [{'id': '20', 'title': 'Item 2', 'type': 'Map Service'}])
res = QgsArcGisPortalUtils.retrieveGroupItemsOfType('http://' + endpoint, 'ab1', '',
[QgsArcGisPortalUtils.ImageService], pageSize=2)
# no errors
self.assertFalse(res[1])
self.assertFalse(res[2])
self.assertEqual(res[0], [{'id': '75', 'title': 'Item 3', 'type': 'Image Service'}])
res = QgsArcGisPortalUtils.retrieveGroupItemsOfType('http://' + endpoint, 'ab1', '',
[QgsArcGisPortalUtils.FeatureService,
QgsArcGisPortalUtils.MapService], pageSize=2)
# no errors
self.assertFalse(res[1])
self.assertFalse(res[2])
self.assertEqual(res[0], [{'id': '74', 'title': 'Item 1', 'type': 'Feature Service'},
{'id': '20', 'title': 'Item 2', 'type': 'Map Service'}])


if __name__ == '__main__':
unittest.main()

0 comments on commit d878460

Please sign in to comment.