Files
openapi-generator-original/samples/client/petstore/cpp-qt5/client/ServerConfiguration.h
basyskom-dege febd65d3b8 Qt5 parameterized server (#8183)
* first Commit parameterzied Server support

* fixed serverconfig classes

* Defautl constructor f. Config, fixed regex replace

* Polosihed Templates, Added MultiServer support

* Update Readme. Fixed MultiServer. Fixed def. Value

* Passing global Server to mustache. Small fixes

* Updated  samples, fixed mustache for multi server

* added prefixes, removed unused imports

* added newly generated samples

* missing vendorExtension in mustache. Update smaple

* update doc

Co-authored-by: William Cheng <wing328hk@gmail.com>
2020-12-19 11:34:12 +08:00

76 lines
2.4 KiB
C++

/**
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* The version of the OpenAPI document: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
/**
* Representing a Server configuration.
*/
#ifndef SERVERVCONFIGURATION_H
#define SERVERVCONFIGURATION_H
#include <QString>
#include <QMap>
#include <stdexcept>
#include <QRegularExpression>
#include "ServerVariable.h"
class ServerConfiguration {
public:
/**
* @param URL A URL to the target host.
* @param description A description of the host designated by the URL.
* @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template.
*/
ServerConfiguration(const QString& URL, const QString& description, const QMap<QString, ServerVariable>& variables)
: _description(description),
_variables(variables),
_URL(URL){}
ServerConfiguration(){}
~ServerConfiguration(){}
/**
* Format URL template using given variables.
*
* @param variables A map between a variable name and its value.
* @return Formatted URL.
*/
QString URL() {
QString url = _URL;
if(!_variables.empty()){
// go through variables and replace placeholders
for (auto const& v : _variables.keys()) {
QString name = v;
ServerVariable serverVariable = _variables.value(v);
QString value = serverVariable._defaultValue;
if (!serverVariable._enumValues.empty() && !serverVariable._enumValues.contains(value)) {
throw std::runtime_error(QString("The variable " + name + " in the server URL has invalid value " + value + ".").toUtf8());
}
QRegularExpression regex(QString("\\{" + name + "\\}"));
url = url.replace(regex, value);
}
return url;
}
return url;
}
int setDefaultValue(const QString& variable,const QString& value){
if(_variables.contains(variable))
return _variables[variable].setDefaultValue(value);
return -1;
}
QString _description;
QMap<QString, ServerVariable> _variables;
QString _URL;
};
#endif