QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 "qgsserverrequest.h"
19 #include "qgsserverexception.h"
20 #include "nlohmann/json.hpp"
21 
23  bool required,
25  const QString &description,
26  const QVariant &defaultValue ):
27  mName( name ),
28  mRequired( required ),
29  mType( type ),
30  mDescription( description ),
31  mDefaultValue( defaultValue )
32 {
33 }
34 
36 {
37 
38 }
39 
41 {
42 
43  // 1: check required
44  if ( mRequired && !QUrlQuery( context.request()->url() ).hasQueryItem( mName ) )
45  {
46  throw QgsServerApiBadRequestException( QStringLiteral( "Missing required argument: '%1'" ).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 
63  // 3: check type
64  const QVariant::Type targetType { static_cast< QVariant::Type >( mType )};
65  // Handle csv list type
66  if ( mType == Type::List )
67  {
68  value = value.toString().split( ',' );
69  }
70  if ( value.type() != targetType )
71  {
72  bool ok = false;
73  if ( value.canConvert( static_cast<int>( targetType ) ) )
74  {
75  ok = true;
76  switch ( mType )
77  {
78  case Type::String:
79  value = value.toString( );
80  break;
81  case Type::Boolean:
82  value = value.toBool( );
83  break;
84  case Type::Double:
85  value = value.toDouble( &ok );
86  break;
87  case Type::Integer:
88  value = value.toLongLong( &ok );
89  break;
90  case Type::List:
91  // already converted to a string list
92  break;
93  }
94  }
95 
96  if ( ! ok )
97  {
98  throw QgsServerApiBadRequestException( QStringLiteral( "Argument '%1' could not be converted to %2" ).arg( mName, typeName( mType ) ) );
99  }
100  }
101 
102  // 4: check custom validation
103  if ( mCustomValidator && ! mCustomValidator( context, value ) )
104  {
105  throw QgsServerApiBadRequestException( QStringLiteral( "Argument '%1' is not valid. %2" ).arg( name(), description() ) );
106  }
107  }
108  return value;
109 }
110 
111 void QgsServerQueryStringParameter::setCustomValidator( const customValidator &customValidator )
112 {
113  mCustomValidator = customValidator;
114 }
115 
117 {
118  const auto nameString { name().toStdString() };
119  auto dataType { typeName( mType ).toLower().toStdString() };
120  // Map list to string because it will be serialized
121  if ( dataType == "list" )
122  {
123  dataType = "string";
124  }
125  else if ( dataType == "double" )
126  {
127  dataType = "number";
128  }
129  return
130  {
131  { "name", nameString },
132  { "description", description().toStdString() },
133  { "required", mRequired },
134  { "in", "query"},
135  { "style", "form"},
136  { "explode", false },
137  { "schema", {{ "type", dataType }}},
138  // This is unfortunately not in OAS: { "default", mDefaultValue.toString().toStdString() }
139  };
140 }
141 
143 {
144  return mDescription;
145 }
146 
148 {
149  static QMetaEnum metaEnum = QMetaEnum::fromType<Type>();
150  return metaEnum.valueToKey( static_cast<int>( type ) );
151 }
152 
154 {
155  return mName;
156 }
157 
158 void QgsServerQueryStringParameter::setDescription( const QString &description )
159 {
160  mDescription = description;
161 }
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.
QString name() const
Returns the name of the parameter.
Type
The Type enum represents the parameter type.
@ Boolean
parameter is a double
@ List
parameter is a boolean
@ Integer
parameter is a string
@ Double
parameter is an integer
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.