Skip to content
Permalink
Browse files

Method to retrieve all group content

  • Loading branch information
nyalldawson committed Dec 21, 2020
1 parent 23b1533 commit 8b313f84437b61f60a127b6a41adae14f8efb853
@@ -59,6 +59,22 @@ If ``user`` is blank then the user associated with the current logon details wil
- errorText: error text of any encountered errors
%End

static QVariantList retrieveGroupContent( const QString &contentUrl, const QString &groupId, const QString &authcfg, 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 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

};

/************************************************************************
@@ -14,6 +14,8 @@
***************************************************************************/
#include "qgsarcgisportalutils.h"
#include "qgsarcgisrestquery.h"
#include "qgsfeedback.h"

#include <QUrl>
#include <QUrlQuery>

@@ -41,3 +43,40 @@ QVariantList QgsArcGisPortalUtils::retrieveUserGroups( const QString &communityU
const QVariantMap info = retrieveUserInfo( communityUrl, user, authcfg, errorTitle, errorText, requestHeaders, feedback );
return info.value( QStringLiteral( "groups" ) ).toList();
}

QVariantList QgsArcGisPortalUtils::retrieveGroupContent( const QString &contentUrl, const QString &groupId, const QString &authcfg, QString &errorTitle, QString &errorText, const QMap<QString, QString> &requestHeaders, QgsFeedback *feedback, int pageSize )
{
QString endPoint = contentUrl;
if ( endPoint.endsWith( '/' ) )
endPoint.chop( 1 );

endPoint += QStringLiteral( "/groups/" ) + groupId;

int start = 1;

QVariantList items;
while ( true )
{
QUrl queryUrl( endPoint );
QUrlQuery query( queryUrl );
query.addQueryItem( QStringLiteral( "f" ), QStringLiteral( "json" ) );
query.addQueryItem( QStringLiteral( "start" ), QString::number( start ) );
query.addQueryItem( QStringLiteral( "num" ), QString::number( pageSize ) );
queryUrl.setQuery( query );

const QVariantMap response = QgsArcGisRestQueryUtils::queryServiceJSON( queryUrl, authcfg, errorTitle, errorText, requestHeaders, feedback );
if ( !errorText.isEmpty() )
return QVariantList();

items.append( response.value( QStringLiteral( "items" ) ).toList() );

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

const int total = response.value( QStringLiteral( "total" ) ).toInt();
start += pageSize;
if ( total < start )
break;
}
return items;
}
@@ -70,6 +70,22 @@ class CORE_EXPORT QgsArcGisPortalUtils
*/
static QVariantList retrieveUserGroups( const QString &communityUrl, const QString &user, const QString &authcfg, QString &errorTitle SIP_OUT, QString &errorText SIP_OUT, const QMap< QString, QString > &requestHeaders = QMap< QString, QString >(), QgsFeedback *feedback = nullptr );

/**
* 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 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 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 );

};

#endif // QGSARCGISPORTALUTILS_H
@@ -173,6 +173,51 @@ def test_retrieve_groups(self):
self.assertFalse(res[2])
self.assertEqual(res[0], [{'id': 'c4', 'title': 'A Group'}, {'id': 'd4', 'title': 'Another Group'}])

def test_retrieve_group_items(self):
"""
Test retrieving group content
"""
print(self.basetestpath)
endpoint = self.basetestpath + '/group_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"
},
{
"id": "20",
"title": "Item 2"
}
]
}""".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"
}
]
}""".encode('UTF-8'))
res = QgsArcGisPortalUtils.retrieveGroupContent('http://' + endpoint, 'ab1', '', pageSize=2)
# no errors
self.assertFalse(res[1])
self.assertFalse(res[2])
self.assertEqual(res[0], [{'id': '74', 'title': 'Item 1'}, {'id': '20', 'title': 'Item 2'},
{'id': '75', 'title': 'Item 3'}])


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

0 comments on commit 8b313f8

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