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