QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 && ! 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 ( 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 )
99  .arg( 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() ).arg( description() ) );
107  }
108  }
109  return value;
110 }
111 
112 void 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  return
127  {
128  { "name", nameString },
129  { "description", "Filter the collection by '" + nameString + "'" },
130  { "required", mRequired },
131  { "in", "query"},
132  { "style", "form"},
133  { "explode", false },
134  { "schema", {{ "type", dataType }}},
135  // This is unfortunately not in OAS: { "default", mDefaultValue.toString().toStdString() }
136  };
137 }
138 
140 {
141  return mDescription;
142 }
143 
145 {
146  static QMetaEnum metaEnum = QMetaEnum::fromType<Type>();
147  return metaEnum.valueToKey( static_cast<int>( type ) );
148 }
149 
151 {
152  return mName;
153 }
154 
156 {
157  mDescription = description;
158 }
void setDescription(const QString &description)
Sets validator description.
QString name() const
Returns the name of the parameter.
static QString typeName(const Type type)
Returns the name of the type.
void setCustomValidator(const customValidator &customValidator)
Sets the custom validation function to customValidator.
The QgsServerApiContext class encapsulates the resources for a particular client request: the request...
QString description() const
Returns parameter description.
QgsServerQueryStringParameter(const QString name, bool required=false, Type type=QgsServerQueryStringParameter::Type::String, const QString &description=QString(), const QVariant &defaultValue=QVariant())
Constructs a QgsServerQueryStringParameter object.
Bad request error API exception.
json data() const
Returns the handler information as a JSON object.
virtual QVariant value(const QgsServerApiContext &context) const
Extracts the value from the request context by validating the parameter value and converting it to it...
Type
The Type enum represents the parameter type.
const QgsServerRequest * request() const
Returns the server request object.