QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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#include "moc_qgsserverquerystringparameter.cpp"
19#include "qgsserverrequest.h"
20#include "qgsserverexception.h"
21#include "nlohmann/json.hpp"
22
24 bool required,
26 const QString &description,
27 const QVariant &defaultValue ):
28 mName( name ),
29 mRequired( required ),
30 mType( type ),
31 mDescription( description ),
32 mDefaultValue( defaultValue )
33{
34}
35
40
42{
43
44 // 1: check required
45 if ( mRequired && !QUrlQuery( context.request()->url() ).hasQueryItem( mName ) )
46 {
47 throw QgsServerApiBadRequestException( QStringLiteral( "Missing required argument: '%1'" ).arg( mName ) );
48 }
49
50 // 2: get value from query string or set it to the default
51 QVariant value;
52 if ( QUrlQuery( context.request()->url() ).hasQueryItem( mName ) )
53 {
54 value = QUrlQuery( context.request()->url() ).queryItemValue( mName, QUrl::FullyDecoded );
55 }
56 else if ( mDefaultValue.isValid() )
57 {
58 value = mDefaultValue;
59 }
60
61 if ( value.isValid() )
62 {
63
64 // 3: check type
65 const QMetaType::Type targetType { static_cast< QMetaType::Type >( mType )};
66 // Handle csv list type
67 if ( mType == Type::List )
68 {
69 value = value.toString().split( ',' );
70 }
71 if ( value.userType() != targetType )
72 {
73 bool ok = false;
74 if ( value.canConvert( static_cast<int>( targetType ) ) )
75 {
76 ok = true;
77 switch ( mType )
78 {
79 case Type::String:
80 value = value.toString( );
81 break;
82 case Type::Boolean:
83 value = value.toBool( );
84 break;
85 case Type::Double:
86 value = value.toDouble( &ok );
87 break;
88 case Type::Integer:
89 value = value.toLongLong( &ok );
90 break;
91 case Type::List:
92 // already converted to a string list
93 break;
94 }
95 }
96
97 if ( ! ok )
98 {
99 throw QgsServerApiBadRequestException( QStringLiteral( "Argument '%1' could not be converted to %2" ).arg( mName, typeName( mType ) ) );
100 }
101 }
102
103 // 4: check custom validation
104 if ( mCustomValidator && ! mCustomValidator( context, value ) )
105 {
106 throw QgsServerApiBadRequestException( QStringLiteral( "Argument '%1' is not valid. %2" ).arg( name(), description() ) );
107 }
108 }
109 return value;
110}
111
112void QgsServerQueryStringParameter::setCustomValidator( const customValidator &customValidator )
113{
114 mCustomValidator = customValidator;
115}
116
118{
119 const auto nameString { name().toStdString() };
120 auto dataType { typeName( mType ).toLower().toStdString() };
121 // Map list to string because it will be serialized
122 if ( dataType == "list" )
123 {
124 dataType = "string";
125 }
126 else if ( dataType == "double" )
127 {
128 dataType = "number";
129 }
130 return
131 {
132 { "name", nameString },
133 { "description", description().toStdString() },
134 { "required", mRequired },
135 { "in", "query"},
136 { "style", "form"},
137 { "explode", false },
138 { "schema", {{ "type", dataType }}},
139 // This is unfortunately not in OAS: { "default", mDefaultValue.toString().toStdString() }
140 };
141}
142
144{
145 return mDescription;
146}
147
149{
150 static const QMetaEnum metaEnum = QMetaEnum::fromType<Type>();
151 return metaEnum.valueToKey( static_cast<int>( type ) );
152}
153
155{
156 return mName;
157}
158
159void QgsServerQueryStringParameter::setDescription( const QString &description )
160{
161 mDescription = description;
162}
163
165{
166 return mHidden;
167}
168
170{
171 mHidden = hidden;
172}
Bad request error API exception.
The QgsServerApiContext class encapsulates the resources for a particular client request: the 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.
@ String
Parameter is a string.
@ Boolean
Parameter is a boolean.
@ List
Parameter is a (comma separated) list of strings, the handler will perform any further required conve...
@ Integer
Parameter is an integer.
@ Double
Parameter is a double.
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.
const QString & typeName