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