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