QGIS API Documentation 4.1.0-Master (31622b25bb0)
Loading...
Searching...
No Matches
qgsserverquerystringparameter.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsserverquerystringparameter.cpp - QgsServerQueryStringParameter
3
4 ---------------------
5 begin : 10.7.2019
6 copyright : (C) 2019 by Alessandro Pasotti
7 email : elpaso at itopen dot it
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
18
19#include <nlohmann/json.hpp>
20
21#include "qgsserverexception.h"
22#include "qgsserverrequest.h"
23
24#include <QString>
25
26#include "moc_qgsserverquerystringparameter.cpp"
27
28using namespace Qt::StringLiterals;
29
30QgsServerQueryStringParameter::QgsServerQueryStringParameter( const QString name, bool required, QgsServerQueryStringParameter::Type type, const QString &description, const QVariant &defaultValue )
31 : mName( name )
32 , mRequired( required )
33 , mType( type )
34 , mDescription( description )
35 , mDefaultValue( defaultValue )
36{}
37
40
42{
43 // Case insensitive check
44 const QUrlQuery urlQuery( context.request()->url() );
45 const QList<std::pair<QString, QString>> items = urlQuery.queryItems( QUrl::FullyDecoded );
46 QString nameInQueryString;
47 QVariant value;
48 for ( const auto &pair : std::as_const( items ) )
49 {
50 if ( pair.first.compare( mName, Qt::CaseInsensitive ) == 0 )
51 {
52 nameInQueryString = pair.first;
53 value = pair.second;
54 break;
55 }
56 }
57
58 // 1: check required
59 if ( mRequired && nameInQueryString.isEmpty() )
60 {
61 throw QgsServerApiBadRequestException( u"Missing required argument: '%1'"_s.arg( mName ) );
62 }
63
64 // 2: get value from query string or set it to the default
65 if ( nameInQueryString.isEmpty() && mDefaultValue.isValid() )
66 {
67 value = mDefaultValue;
68 }
69
70 if ( value.isValid() )
71 {
72 // 3: check type
73 const QMetaType::Type targetType { static_cast<QMetaType::Type>( mType ) };
74 // Handle csv list type
75 if ( mType == Type::List )
76 {
77 value = value.toString().split( ',' );
78 }
79 if ( value.userType() != targetType )
80 {
81 bool ok = false;
82 if ( value.canConvert( static_cast<int>( targetType ) ) )
83 {
84 ok = true;
85 switch ( mType )
86 {
87 case Type::String:
88 value = value.toString();
89 break;
90 case Type::Boolean:
91 value = value.toBool();
92 break;
93 case Type::Double:
94 value = value.toDouble( &ok );
95 break;
96 case Type::Integer:
97 value = value.toLongLong( &ok );
98 break;
99 case Type::List:
100 // already converted to a string list
101 break;
102 }
103 }
104
105 if ( !ok )
106 {
107 throw QgsServerApiBadRequestException( u"Argument '%1' could not be converted to %2"_s.arg( mName, typeName( mType ) ) );
108 }
109 }
110
111 // 4: check custom validation
112 if ( mCustomValidator && !mCustomValidator( context, value ) )
113 {
114 throw QgsServerApiBadRequestException( u"Argument '%1' is not valid. %2"_s.arg( name(), description() ) );
115 }
116 }
117 return value;
118}
119
120void QgsServerQueryStringParameter::setCustomValidator( const customValidator &customValidator )
121{
122 mCustomValidator = customValidator;
123}
124
126{
127 const auto nameString { name().toStdString() };
128 auto dataType { typeName( mType ).toLower().toStdString() };
129 // Map list to string because it will be serialized
130 if ( dataType == "list" )
131 {
132 dataType = "string";
133 }
134 else if ( dataType == "double" )
135 {
136 dataType = "number";
137 }
138 return {
139 { "name", nameString },
140 { "description", description().toStdString() },
141 { "required", mRequired },
142 { "in", "query" },
143 { "style", "form" },
144 { "explode", false },
145 { "schema", { { "type", dataType } } },
146 // This is unfortunately not in OAS: { "default", mDefaultValue.toString().toStdString() }
147 };
148}
149
151{
152 return mDescription;
153}
154
156{
157 static const QMetaEnum metaEnum = QMetaEnum::fromType<Type>();
158 return metaEnum.valueToKey( static_cast<int>( type ) );
159}
160
162{
163 return mName;
164}
165
167{
168 mDescription = description;
169}
170
172{
173 return mHidden;
174}
175
177{
178 mHidden = hidden;
179}
Bad request error API exception.
Encapsulates the resources for a particular client request.
const QgsServerRequest * request() const
Returns the server request object.
QString description() const
Returns parameter description.
virtual QVariant value(const QgsServerApiContext &context) const
Extracts the value from the request context by validating the parameter value and converting it to it...
QgsServerQueryStringParameter(const QString name, bool required=false, Type type=QgsServerQueryStringParameter::Type::String, const QString &description=QString(), const QVariant &defaultValue=QVariant())
Constructs a QgsServerQueryStringParameter object.
void setHidden(bool hidden)
Set the parameter's hidden status, parameters are not hidden by default.
QString name() const
Returns the name of the parameter.
Type
The Type enum represents the parameter type.
@ List
Parameter is a (comma separated) list of strings, the handler will perform any further required conve...
json data() const
Returns the handler information as a JSON object.
static QString typeName(const Type type)
Returns the name of the type.
void setDescription(const QString &description)
Sets validator description.
void setCustomValidator(const customValidator &customValidator)
Sets the custom validation function to customValidator.
bool hidden() const
Returns true if the parameter is hidden from the schema.
QUrl url() const
Returns the request URL as seen by QGIS server.