QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 )
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  else if ( dataType == "double" )
127  {
128  dataType = "number";
129  }
130  return
131  {
132  { "name", nameString },
133  { "description", description().toStdString() },
134  { "required", mRequired },
135  { "in", "query"},
136  { "style", "form"},
137  { "explode", false },
138  { "schema", {{ "type", dataType }}},
139  // This is unfortunately not in OAS: { "default", mDefaultValue.toString().toStdString() }
140  };
141 }
142 
144 {
145  return mDescription;
146 }
147 
149 {
150  static QMetaEnum metaEnum = QMetaEnum::fromType<Type>();
151  return metaEnum.valueToKey( static_cast<int>( type ) );
152 }
153 
155 {
156  return mName;
157 }
158 
159 void QgsServerQueryStringParameter::setDescription( const QString &description )
160 {
161  mDescription = description;
162 }
QgsServerApiContext
The QgsServerApiContext class encapsulates the resources for a particular client request: the request...
Definition: qgsserverapicontext.h:39
QgsServerQueryStringParameter::Type::Double
@ Double
parameter is an integer
QgsServerQueryStringParameter::typeName
static QString typeName(const Type type)
Returns the name of the type.
Definition: qgsserverquerystringparameter.cpp:148
QgsServerQueryStringParameter::~QgsServerQueryStringParameter
virtual ~QgsServerQueryStringParameter()
Definition: qgsserverquerystringparameter.cpp:35
QgsServerQueryStringParameter::setDescription
void setDescription(const QString &description)
Sets validator description.
Definition: qgsserverquerystringparameter.cpp:159
QgsServerQueryStringParameter::Type::Integer
@ Integer
parameter is a string
QgsServerQueryStringParameter::Type::List
@ List
parameter is a boolean
qgsserverquerystringparameter.h
QgsServerQueryStringParameter::name
QString name() const
Returns the name of the parameter.
Definition: qgsserverquerystringparameter.cpp:154
qgsserverrequest.h
QgsServerQueryStringParameter::setCustomValidator
void setCustomValidator(const customValidator &customValidator)
Sets the custom validation function to customValidator.
Definition: qgsserverquerystringparameter.cpp:112
QgsServerQueryStringParameter::description
QString description() const
Returns parameter description.
Definition: qgsserverquerystringparameter.cpp:143
QgsServerQueryStringParameter::data
json data() const
Returns the handler information as a JSON object.
Definition: qgsserverquerystringparameter.cpp:117
QgsServerQueryStringParameter::value
virtual QVariant value(const QgsServerApiContext &context) const
Extracts the value from the request context by validating the parameter value and converting it to it...
Definition: qgsserverquerystringparameter.cpp:40
QgsServerQueryStringParameter::Type::Boolean
@ Boolean
parameter is a double
QgsServerRequest::url
QUrl url() const
Definition: qgsserverrequest.cpp:65
QgsServerApiContext::request
const QgsServerRequest * request() const
Returns the server request object.
Definition: qgsserverapicontext.cpp:33
QgsServerQueryStringParameter::QgsServerQueryStringParameter
QgsServerQueryStringParameter(const QString name, bool required=false, Type type=QgsServerQueryStringParameter::Type::String, const QString &description=QString(), const QVariant &defaultValue=QVariant())
Constructs a QgsServerQueryStringParameter object.
Definition: qgsserverquerystringparameter.cpp:22
QgsServerQueryStringParameter::Type::String
@ String
QgsServerApiBadRequestException
Bad request error API exception.
Definition: qgsserverexception.h:245
qgsserverexception.h
QgsServerQueryStringParameter::Type
Type
The Type enum represents the parameter type.
Definition: qgsserverquerystringparameter.h:61