QGIS API Documentation 3.99.0-Master (d270888f95f)
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 ), mRequired( required ), mType( type ), mDescription( description ), mDefaultValue( defaultValue )
32{
33}
34
38
40{
41 // 1: check required
42 if ( mRequired && !QUrlQuery( context.request()->url() ).hasQueryItem( mName ) )
43 {
44 throw QgsServerApiBadRequestException( u"Missing required argument: '%1'"_s.arg( mName ) );
45 }
46
47 // 2: get value from query string or set it to the default
48 QVariant value;
49 if ( QUrlQuery( context.request()->url() ).hasQueryItem( mName ) )
50 {
51 value = QUrlQuery( context.request()->url() ).queryItemValue( mName, QUrl::FullyDecoded );
52 }
53 else if ( mDefaultValue.isValid() )
54 {
55 value = mDefaultValue;
56 }
57
58 if ( value.isValid() )
59 {
60 // 3: check type
61 const QMetaType::Type targetType { static_cast<QMetaType::Type>( mType ) };
62 // Handle csv list type
63 if ( mType == Type::List )
64 {
65 value = value.toString().split( ',' );
66 }
67 if ( value.userType() != targetType )
68 {
69 bool ok = false;
70 if ( value.canConvert( static_cast<int>( targetType ) ) )
71 {
72 ok = true;
73 switch ( mType )
74 {
75 case Type::String:
76 value = value.toString();
77 break;
78 case Type::Boolean:
79 value = value.toBool();
80 break;
81 case Type::Double:
82 value = value.toDouble( &ok );
83 break;
84 case Type::Integer:
85 value = value.toLongLong( &ok );
86 break;
87 case Type::List:
88 // already converted to a string list
89 break;
90 }
91 }
92
93 if ( !ok )
94 {
95 throw QgsServerApiBadRequestException( u"Argument '%1' could not be converted to %2"_s.arg( mName, typeName( mType ) ) );
96 }
97 }
98
99 // 4: check custom validation
100 if ( mCustomValidator && !mCustomValidator( context, value ) )
101 {
102 throw QgsServerApiBadRequestException( u"Argument '%1' is not valid. %2"_s.arg( name(), description() ) );
103 }
104 }
105 return value;
106}
107
108void QgsServerQueryStringParameter::setCustomValidator( const customValidator &customValidator )
109{
110 mCustomValidator = customValidator;
111}
112
114{
115 const auto nameString { name().toStdString() };
116 auto dataType { typeName( mType ).toLower().toStdString() };
117 // Map list to string because it will be serialized
118 if ( dataType == "list" )
119 {
120 dataType = "string";
121 }
122 else if ( dataType == "double" )
123 {
124 dataType = "number";
125 }
126 return {
127 { "name", nameString },
128 { "description", description().toStdString() },
129 { "required", mRequired },
130 { "in", "query" },
131 { "style", "form" },
132 { "explode", false },
133 { "schema", { { "type", dataType } } },
134 // This is unfortunately not in OAS: { "default", mDefaultValue.toString().toStdString() }
135 };
136}
137
139{
140 return mDescription;
141}
142
144{
145 static const QMetaEnum metaEnum = QMetaEnum::fromType<Type>();
146 return metaEnum.valueToKey( static_cast<int>( type ) );
147}
148
150{
151 return mName;
152}
153
155{
156 mDescription = description;
157}
158
160{
161 return mHidden;
162}
163
165{
166 mHidden = hidden;
167}
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.