QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 QgsHttpHeaders &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 QVariantMap QgsArcGisPortalUtils::retrieveUserInfo( const QString &communityUrl, const QString &user, const QString &authcfg, QString &errorTitle, QString &errorText, const QMap< QString, QVariant > &requestHeaders, QgsFeedback *feedback )
42 {
43  return QgsArcGisPortalUtils::retrieveUserInfo( communityUrl, user, authcfg, errorTitle, errorText, QgsHttpHeaders( requestHeaders ), feedback );
44 }
45 
46 QVariantList QgsArcGisPortalUtils::retrieveUserGroups( const QString &communityUrl, const QString &user, const QString &authcfg, QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders, QgsFeedback *feedback )
47 {
48  const QVariantMap info = retrieveUserInfo( communityUrl, user, authcfg, errorTitle, errorText, requestHeaders, feedback );
49  return info.value( QStringLiteral( "groups" ) ).toList();
50 }
51 
52 QVariantList QgsArcGisPortalUtils::retrieveUserGroups( const QString &communityUrl, const QString &user, const QString &authcfg, QString &errorTitle, QString &errorText, const QMap< QString, QVariant > &requestHeaders, QgsFeedback *feedback )
53 {
54  return QgsArcGisPortalUtils::retrieveUserGroups( communityUrl, user, authcfg, errorTitle, errorText, QgsHttpHeaders( requestHeaders ), feedback );
55 }
56 
57 QVariantList QgsArcGisPortalUtils::retrieveGroupContent( const QString &contentUrl, const QString &groupId, const QString &authcfg, QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders, QgsFeedback *feedback, int pageSize )
58 {
59  QString endPoint = contentUrl;
60  if ( endPoint.endsWith( '/' ) )
61  endPoint.chop( 1 );
62 
63  endPoint += QStringLiteral( "/groups/" ) + groupId;
64 
65  int start = 1;
66 
67  QVariantList items;
68  while ( true )
69  {
70  QUrl queryUrl( endPoint );
71  QUrlQuery query( queryUrl );
72  query.addQueryItem( QStringLiteral( "f" ), QStringLiteral( "json" ) );
73  query.addQueryItem( QStringLiteral( "start" ), QString::number( start ) );
74  query.addQueryItem( QStringLiteral( "num" ), QString::number( pageSize ) );
75  queryUrl.setQuery( query );
76 
77  const QVariantMap response = QgsArcGisRestQueryUtils::queryServiceJSON( queryUrl, authcfg, errorTitle, errorText, requestHeaders, feedback );
78  if ( !errorText.isEmpty() )
79  return QVariantList();
80 
81  items.append( response.value( QStringLiteral( "items" ) ).toList() );
82 
83  if ( feedback && feedback->isCanceled() )
84  return items;
85 
86  const int total = response.value( QStringLiteral( "total" ) ).toInt();
87  start += pageSize;
88  if ( total < start )
89  break;
90  }
91  return items;
92 }
93 
94 QVariantList QgsArcGisPortalUtils::retrieveGroupContent( const QString &contentUrl, const QString &groupId, const QString &authcfg, QString &errorTitle, QString &errorText, const QMap< QString, QVariant > &requestHeaders, QgsFeedback *feedback, int pageSize )
95 {
96  return QgsArcGisPortalUtils::retrieveGroupContent( contentUrl, groupId, authcfg, errorTitle, errorText, QgsHttpHeaders( requestHeaders ), feedback, pageSize );
97 }
98 
99 QVariantList QgsArcGisPortalUtils::retrieveGroupItemsOfType( const QString &contentUrl, const QString &groupId, const QString &authcfg, const QList<int> &itemTypes, QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders, QgsFeedback *feedback, int pageSize )
100 {
101  const QVariantList items = retrieveGroupContent( contentUrl, groupId, authcfg, errorTitle, errorText, requestHeaders, feedback, pageSize );
102 
103  // filter results to desired types
104  QVariantList result;
105  for ( const QVariant &item : items )
106  {
107  const QVariantMap itemDef = item.toMap();
108  const QString itemType = itemDef.value( QStringLiteral( "type" ) ).toString();
109 
110  for ( const int filterType : itemTypes )
111  {
112  if ( typeToString( static_cast< ItemType >( filterType ) ).compare( itemType, Qt::CaseInsensitive ) == 0 )
113  {
114  result << item;
115  break;
116  }
117  }
118  }
119  return result;
120 }
121 
122 QVariantList QgsArcGisPortalUtils::retrieveGroupItemsOfType( const QString &contentUrl, const QString &groupId, const QString &authcfg, const QList<int> &itemTypes, QString &errorTitle, QString &errorText, const QMap< QString, QVariant > &requestHeaders, QgsFeedback *feedback, int pageSize )
123 {
124  return QgsArcGisPortalUtils::retrieveGroupItemsOfType( contentUrl, groupId, authcfg, itemTypes, errorTitle, errorText, QgsHttpHeaders( requestHeaders ), feedback, pageSize );
125 }
126 
127 
128 QString QgsArcGisPortalUtils::typeToString( QgsArcGisPortalUtils::ItemType type )
129 {
130  switch ( type )
131  {
133  return QStringLiteral( "Feature Service" );
135  return QStringLiteral( "Map Service" );
137  return QStringLiteral( "Image Service" );
138  }
139  return QString();
140 }
static QVariantMap retrieveUserInfo(const QString &communityUrl, const QString &user, const QString &authcfg, QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders=QgsHttpHeaders(), QgsFeedback *feedback=nullptr)
Retrieves JSON user info for the specified user name.
static QVariantList retrieveGroupContent(const QString &contentUrl, const QString &groupId, const QString &authcfg, QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders=QgsHttpHeaders(), 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 QgsHttpHeaders &requestHeaders=QgsHttpHeaders(), 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 QVariantList retrieveGroupItemsOfType(const QString &contentUrl, const QString &groupId, const QString &authcfg, const QList< int > &itemTypes, QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders=QgsHttpHeaders(), QgsFeedback *feedback=nullptr, int pageSize=100)
Retrieves JSON definitions for all items which belong the the specified groupId.
static QVariantMap queryServiceJSON(const QUrl &url, const QString &authcfg, QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders=QgsHttpHeaders(), 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
This class implements simple http header management.