1 /***************************************************************************
2 qgsarcgisportalutils.h
3 --------------------
4 begin : December 2020
5 copyright : (C) 2020 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15 #include "qgsarcgisportalutils.h"
16 #include "qgsarcgisrestquery.h"
17 #include "qgsfeedback.h"
18
19 #include <QUrl>
20 #include <QUrlQuery>
21
retrieveUserInfo(const QString & communityUrl,const QString & user,const QString & authcfg,QString & errorTitle,QString & errorText,const QMap<QString,QString> & requestHeaders,QgsFeedback * feedback)22 QVariantMap QgsArcGisPortalUtils::retrieveUserInfo( const QString &communityUrl, const QString &user, const QString &authcfg, QString &errorTitle, QString &errorText, const QMap<QString, QString> &requestHeaders, QgsFeedback *feedback )
23 {
24 QString endPoint = communityUrl;
25 if ( endPoint.endsWith( '/' ) )
26 endPoint.chop( 1 );
27
28 if ( user.isEmpty() )
29 endPoint += QLatin1String( "/self" );
30 else
31 endPoint += QStringLiteral( "/users/" ) + user;
32
33 QUrl queryUrl( endPoint );
34 QUrlQuery query( queryUrl );
35 query.addQueryItem( QStringLiteral( "f" ), QStringLiteral( "json" ) );
36 queryUrl.setQuery( query );
37
38 return QgsArcGisRestQueryUtils::queryServiceJSON( queryUrl, authcfg, errorTitle, errorText, requestHeaders, feedback );
39 }
40
retrieveUserGroups(const QString & communityUrl,const QString & user,const QString & authcfg,QString & errorTitle,QString & errorText,const QMap<QString,QString> & requestHeaders,QgsFeedback * feedback)41 QVariantList QgsArcGisPortalUtils::retrieveUserGroups( const QString &communityUrl, const QString &user, const QString &authcfg, QString &errorTitle, QString &errorText, const QMap<QString, QString> &requestHeaders, QgsFeedback *feedback )
42 {
43 const QVariantMap info = retrieveUserInfo( communityUrl, user, authcfg, errorTitle, errorText, requestHeaders, feedback );
44 return info.value( QStringLiteral( "groups" ) ).toList();
45 }
46
retrieveGroupContent(const QString & contentUrl,const QString & groupId,const QString & authcfg,QString & errorTitle,QString & errorText,const QMap<QString,QString> & requestHeaders,QgsFeedback * feedback,int pageSize)47 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 )
48 {
49 QString endPoint = contentUrl;
50 if ( endPoint.endsWith( '/' ) )
51 endPoint.chop( 1 );
52
53 endPoint += QStringLiteral( "/groups/" ) + groupId;
54
55 int start = 1;
56
57 QVariantList items;
58 while ( true )
59 {
60 QUrl queryUrl( endPoint );
61 QUrlQuery query( queryUrl );
62 query.addQueryItem( QStringLiteral( "f" ), QStringLiteral( "json" ) );
63 query.addQueryItem( QStringLiteral( "start" ), QString::number( start ) );
64 query.addQueryItem( QStringLiteral( "num" ), QString::number( pageSize ) );
65 queryUrl.setQuery( query );
66
67 const QVariantMap response = QgsArcGisRestQueryUtils::queryServiceJSON( queryUrl, authcfg, errorTitle, errorText, requestHeaders, feedback );
68 if ( !errorText.isEmpty() )
69 return QVariantList();
70
71 items.append( response.value( QStringLiteral( "items" ) ).toList() );
72
73 if ( feedback && feedback->isCanceled() )
74 return items;
75
76 const int total = response.value( QStringLiteral( "total" ) ).toInt();
77 start += pageSize;
78 if ( total < start )
79 break;
80 }
81 return items;
82 }
83
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)84 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 )
85 {
86 const QVariantList items = retrieveGroupContent( contentUrl, groupId, authcfg, errorTitle, errorText, requestHeaders, feedback, pageSize );
87
88 // filter results to desired types
89 QVariantList result;
90 for ( const QVariant &item : items )
91 {
92 const QVariantMap itemDef = item.toMap();
93 const QString itemType = itemDef.value( QStringLiteral( "type" ) ).toString();
94
95 for ( const int filterType : itemTypes )
96 {
97 if ( typeToString( static_cast< ItemType >( filterType ) ).compare( itemType, Qt::CaseInsensitive ) == 0 )
98 {
99 result << item;
100 break;
101 }
102 }
103 }
104 return result;
105 }
106
typeToString(QgsArcGisPortalUtils::ItemType type)107 QString QgsArcGisPortalUtils::typeToString( QgsArcGisPortalUtils::ItemType type )
108 {
109 switch ( type )
110 {
111 case QgsArcGisPortalUtils::FeatureService:
112 return QStringLiteral( "Feature Service" );
113 case QgsArcGisPortalUtils::MapService:
114 return QStringLiteral( "Map Service" );
115 case QgsArcGisPortalUtils::ImageService:
116 return QStringLiteral( "Image Service" );
117 }
118 return QString();
119 }
120