QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
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 ***************************************************************************/
16#include "qgsarcgisrestquery.h"
17#include "qgsfeedback.h"
18
19#include <QUrl>
20#include <QUrlQuery>
21
22QVariantMap QgsArcGisPortalUtils::retrieveUserInfo( const QString &communityUrl, const QString &user, const QString &authcfg, QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders, QgsFeedback *feedback, const QString &urlPrefix )
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, urlPrefix );
39}
40
41QVariantMap QgsArcGisPortalUtils::retrieveUserInfo( const QString &communityUrl, const QString &user, const QString &authcfg, QString &errorTitle, QString &errorText, const QMap< QString, QVariant > &requestHeaders, QgsFeedback *feedback, const QString &urlPrefix )
42{
43 return QgsArcGisPortalUtils::retrieveUserInfo( communityUrl, user, authcfg, errorTitle, errorText, QgsHttpHeaders( requestHeaders ), feedback, urlPrefix );
44}
45
46QVariantList QgsArcGisPortalUtils::retrieveUserGroups( const QString &communityUrl, const QString &user, const QString &authcfg, QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders, QgsFeedback *feedback, const QString &urlPrefix )
47{
48 const QVariantMap info = retrieveUserInfo( communityUrl, user, authcfg, errorTitle, errorText, requestHeaders, feedback, urlPrefix );
49 return info.value( QStringLiteral( "groups" ) ).toList();
50}
51
52QVariantList QgsArcGisPortalUtils::retrieveUserGroups( const QString &communityUrl, const QString &user, const QString &authcfg, QString &errorTitle, QString &errorText, const QMap< QString, QVariant > &requestHeaders, QgsFeedback *feedback, const QString &urlPrefix )
53{
54 return QgsArcGisPortalUtils::retrieveUserGroups( communityUrl, user, authcfg, errorTitle, errorText, QgsHttpHeaders( requestHeaders ), feedback, urlPrefix );
55}
56
57QVariantList QgsArcGisPortalUtils::retrieveGroupContent( const QString &contentUrl, const QString &groupId, const QString &authcfg, QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders, QgsFeedback *feedback, int pageSize, const QString &urlPrefix )
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, urlPrefix );
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
94QVariantList QgsArcGisPortalUtils::retrieveGroupContent( const QString &contentUrl, const QString &groupId, const QString &authcfg, QString &errorTitle, QString &errorText, const QMap< QString, QVariant > &requestHeaders, QgsFeedback *feedback, int pageSize, const QString &urlPrefix )
95{
96 return QgsArcGisPortalUtils::retrieveGroupContent( contentUrl, groupId, authcfg, errorTitle, errorText, QgsHttpHeaders( requestHeaders ), feedback, pageSize, urlPrefix );
97}
98
99QVariantList 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, const QString &urlPrefix )
100{
101 const QVariantList items = retrieveGroupContent( contentUrl, groupId, authcfg, errorTitle, errorText, requestHeaders, feedback, pageSize, urlPrefix );
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< Qgis::ArcGisRestServiceType >( filterType ) ).compare( itemType, Qt::CaseInsensitive ) == 0 )
113 {
114 result << item;
115 break;
116 }
117 }
118 }
119 return result;
120}
121
122QVariantList 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, const QString &urlPrefix )
123{
124 return QgsArcGisPortalUtils::retrieveGroupItemsOfType( contentUrl, groupId, authcfg, itemTypes, errorTitle, errorText, QgsHttpHeaders( requestHeaders ), feedback, pageSize, urlPrefix );
125}
126
127
128QString QgsArcGisPortalUtils::typeToString( Qgis::ArcGisRestServiceType type )
129{
130 // note -- these values are different in the Portal REST responses vs the standard ArcGIS REST responses!
131 switch ( type )
132 {
134 return QStringLiteral( "Feature Service" );
136 return QStringLiteral( "Map Service" );
138 return QStringLiteral( "Image Service" );
139
144 return QString();
145 }
147}
ArcGisRestServiceType
Available ArcGIS REST service types.
Definition qgis.h:4075
@ GeocodeServer
GeocodeServer.
@ Unknown
Other unknown/unsupported type.
@ FeatureServer
FeatureServer.
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, const QString &urlPrefix=QString())
Retrieves JSON definitions for all items which belong the the specified groupId.
static QVariantMap retrieveUserInfo(const QString &communityUrl, const QString &user, const QString &authcfg, QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders=QgsHttpHeaders(), QgsFeedback *feedback=nullptr, const QString &urlPrefix=QString())
Retrieves JSON user info for the specified user name.
static QVariantList retrieveUserGroups(const QString &communityUrl, const QString &user, const QString &authcfg, QString &errorTitle, QString &errorText, const QgsHttpHeaders &requestHeaders=QgsHttpHeaders(), QgsFeedback *feedback=nullptr, const QString &urlPrefix=QString())
Retrieves JSON definitions for all groups which the specified user name is a member of.
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, const QString &urlPrefix=QString())
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, const QString &urlPrefix=QString())
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:44
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
This class implements simple http header management.
#define BUILTIN_UNREACHABLE
Definition qgis.h:6612