QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
qgsarcgisportalutils.cpp
Go to the documentation of this file.
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 
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 
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 
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 
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 
107 QString QgsArcGisPortalUtils::typeToString( QgsArcGisPortalUtils::ItemType type )
108 {
109  switch ( type )
110  {
112  return QStringLiteral( "Feature Service" );
114  return QStringLiteral( "Map Service" );
116  return QStringLiteral( "Image Service" );
117  }
118  return QString();
119 }
static QVariantMap retrieveUserInfo(const QString &communityUrl, const QString &user, const QString &authcfg, QString &errorTitle, QString &errorText, const QMap< QString, QString > &requestHeaders=QMap< QString, QString >(), QgsFeedback *feedback=nullptr)
Retrieves JSON user info for the specified user name.
static QVariantList retrieveGroupItemsOfType(const QString &contentUrl, const QString &groupId, const QString &authcfg, const QList< int > &itemTypes, QString &errorTitle, QString &errorText, 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 groupId.
static QVariantList retrieveGroupContent(const QString &contentUrl, const QString &groupId, const QString &authcfg, QString &errorTitle, QString &errorText, 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 groupId.
static QVariantList retrieveUserGroups(const QString &communityUrl, const QString &user, const QString &authcfg, QString &errorTitle, QString &errorText, const QMap< QString, QString > &requestHeaders=QMap< QString, QString >(), QgsFeedback *feedback=nullptr)
Retrieves JSON definitions for all groups which the specified user name is a member of.
ItemType
Portal item types (not complete)
@ MapService
ArcGIS Server map service.
@ FeatureService
ArcGIS Server feature service.
@ ImageService
ArcGIS Server image service.
static QVariantMap queryServiceJSON(const QUrl &url, const QString &authcfg, QString &errorTitle, QString &errorText, const QgsStringMap &requestHeaders=QgsStringMap(), QgsFeedback *feedback=nullptr)
Performs a blocking request to a URL and returns the retrieved JSON content.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:54