Skip to content

Commit

Permalink
[C++][Qt5] Added function to add a new Server. Removed unused variabl…
Browse files Browse the repository at this point in the history
…es (OpenAPITools#8725)

* cleanup unused variables,added addServerConfiguration() function

* added setNewServer functions

* using QUrl instead of QString
  • Loading branch information
basyskom-dege authored Mar 1, 2021
1 parent be91db5 commit 16e7408
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ namespace {{this}} {
class {{prefix}}ServerConfiguration {
public:
/**
* @param URL A URL to the target host.
* @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.
*/
{{prefix}}ServerConfiguration(const QString& URL, const QString& description, const QMap<QString, {{prefix}}ServerVariable>& variables)
{{prefix}}ServerConfiguration(const QUrl &url, const QString &description, const QMap<QString, {{prefix}}ServerVariable> &variables)
: _description(description),
_variables(variables),
_URL(URL){}
_url(url){}
{{prefix}}ServerConfiguration(){}
~{{prefix}}ServerConfiguration(){}
Expand All @@ -35,7 +35,7 @@ public:
* @return Formatted URL.
*/
QString URL() {
QString url = _URL;
QString url = _url.toString();
if(!_variables.empty()){
// go through variables and replace placeholders
for (auto const& v : _variables.keys()) {
Expand All @@ -55,15 +55,15 @@ public:
return url;
}
int setDefaultValue(const QString& variable,const QString& value){
int setDefaultValue(const QString &variable,const QString &value){
if(_variables.contains(variable))
return _variables[variable].setDefaultValue(value);
return -1;
}
QString _description;
QMap<QString, {{prefix}}ServerVariable> _variables;
QString _URL;
QUrl _url;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@
namespace {{this}} {
{{/cppNamespaceDeclarations}}

{{classname}}::{{classname}}(const QString &scheme, const QString &host, int port, const QString &basePath, const int timeOut)
: _scheme(scheme),
_host(host),
_port(port),
_basePath(basePath),
_timeOut(timeOut),
{{classname}}::{{classname}}(const int timeOut)
: _timeOut(timeOut),
_manager(nullptr),
isResponseCompressionEnabled(false),
isRequestCompressionEnabled(false) {
Expand All @@ -32,7 +28,7 @@ QList<{{prefix}}ServerConfiguration> defaultConf = QList<{{prefix}}ServerConfigu
QList<{{prefix}}ServerConfiguration> serverConf = QList<{{prefix}}ServerConfiguration>();
{{#vendorExtensions.x-cpp-global-server-list}}
defaultConf.append({{prefix}}ServerConfiguration(
"{{{url}}}",
QUrl("{{{url}}}"),
"{{{description}}}{{^description}}No description provided{{/description}}",
{{#variables}}{{#-first}}QMap<QString, {{prefix}}ServerVariable>{ {{/-first}}
{"{{{name}}}", {{prefix}}ServerVariable("{{{description}}}{{^description}}No description provided{{/description}}","{{{defaultValue}}}",
Expand All @@ -48,7 +44,7 @@ _serverIndices.insert("{{nickname}}",0);
{{/servers}}
{{#servers}}
serverConf.append({{prefix}}ServerConfiguration(
"{{{url}}}",
QUrl("{{{url}}}"),
"{{{description}}}{{^description}}No description provided{{/description}}",
{{#variables}}{{#-first}}QMap<QString, {{prefix}}ServerVariable>{ {{/-first}}
{"{{{name}}}", {{prefix}}ServerVariable("{{{description}}}{{^description}}No description provided{{/description}}","{{{defaultValue}}}",
Expand Down Expand Up @@ -79,18 +75,6 @@ void {{classname}}::setServerIndex(const QString &operation, int serverIndex){
_serverIndices[operation] = serverIndex;
}

void {{classname}}::setScheme(const QString &scheme) {
_scheme = scheme;
}

void {{classname}}::setHost(const QString &host) {
_host = host;
}

void {{classname}}::setPort(int port) {
_port = port;
}

void {{classname}}::setApiKey(const QString &apiKeyName, const QString &apiKey){
_apiKeys.insert(apiKeyName,apiKey);
}
Expand All @@ -107,9 +91,6 @@ void {{classname}}::setPassword(const QString &password) {
_password = password;
}

void {{classname}}::setBasePath(const QString &basePath) {
_basePath = basePath;
}

void {{classname}}::setTimeOut(const int timeOut) {
_timeOut = timeOut;
Expand All @@ -123,6 +104,49 @@ void {{classname}}::setNetworkAccessManager(QNetworkAccessManager* manager) {
_manager = manager;
}

/**
* Appends a new ServerConfiguration to the config map for a specific operation.
* @param operation The id to the target operation.
* @param url A string that contains the URL of the server
* @param description A String that describes the server
* @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template.
* returns the index of the new server config on success and -1 if the operation is not found
*/
int {{classname}}::addServerConfiguration(const QString &operation, const QUrl &url, const QString &description, const QMap<QString, {{prefix}}ServerVariable> &variables){
if(_serverConfigs.contains(operation)){
_serverConfigs[operation].append({{prefix}}ServerConfiguration(
url,
description,
variables));
return _serverConfigs[operation].size()-1;
}else{
return -1;
}
}

/**
* Appends a new ServerConfiguration to the config map for a all operations and sets the index to that server.
* @param url A string that contains the URL of the server
* @param description A String that describes the server
* @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template.
*/
void {{classname}}::setNewServerForAllOperations(const QUrl &url, const QString &description, const QMap<QString, {{prefix}}ServerVariable> &variables){
for(auto e : _serverIndices.keys()){
setServerIndex(e, addServerConfiguration(e, url, description, variables));
}
}
/**
* Appends a new ServerConfiguration to the config map for an operations and sets the index to that server.
* @param URL A string that contains the URL of the server
* @param description A String that describes the server
* @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template.
*/
void {{classname}}::setNewServer(const QString &operation, const QUrl &url, const QString &description, const QMap<QString, {{prefix}}ServerVariable> &variables){
setServerIndex(operation, addServerConfiguration(operation, url, description, variables));
}

void {{classname}}::addHeaders(const QString &key, const QString &value) {
defaultHeaders.insert(key, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,22 @@ class {{classname}} : public QObject {
Q_OBJECT
public:
{{classname}}(const QString &scheme = "{{scheme}}", const QString &host = "{{serverHost}}", int port = {{#serverPort}}{{serverPort}}{{/serverPort}}{{^serverPort}}0{{/serverPort}}, const QString &basePath = "{{basePathWithoutHost}}", const int timeOut = 0);
{{classname}}(const int timeOut = 0);
~{{classname}}();

void initializeServerConfigs();
int setDefaultServerValue(int serverIndex,const QString &operation, const QString &variable,const QString &val);
void setServerIndex(const QString &operation, int serverIndex);
void setScheme(const QString &scheme);
void setHost(const QString &host);
void setPort(int port);
void setApiKey(const QString &apiKeyName, const QString &apiKey);
void setBearerToken(const QString &token);
void setUsername(const QString &username);
void setPassword(const QString &password);
void setBasePath(const QString &basePath);
void setTimeOut(const int timeOut);
void setWorkingDirectory(const QString &path);
void setNetworkAccessManager(QNetworkAccessManager* manager);
int addServerConfiguration(const QString &operation, const QUrl &url, const QString &description = "", const QMap<QString, {{prefix}}ServerVariable> &variables = QMap<QString, {{prefix}}ServerVariable>());
void setNewServerForAllOperations(const QUrl &url, const QString &description = "", const QMap<QString, {{prefix}}ServerVariable> &variables = QMap<QString, {{prefix}}ServerVariable>());
void setNewServer(const QString &operation, const QUrl &url, const QString &description = "", const QMap<QString, {{prefix}}ServerVariable> &variables = QMap<QString, {{prefix}}ServerVariable>());
void addHeaders(const QString &key, const QString &value);
void enableRequestCompression();
void enableResponseCompression();
Expand All @@ -50,9 +49,6 @@ public:
{{#isDeprecated}}Q_DECL_DEPRECATED {{/isDeprecated}}void {{nickname}}({{#allParams}}const {{{dataType}}} &{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});{{/operation}}{{/operations}}

private:
QString _scheme, _host;
int _port;
QString _basePath;
QMap<QString,int> _serverIndices;
QMap<QString,QList<{{prefix}}ServerConfiguration>> _serverConfigs;
QMap<QString, QString> _apiKeys;
Expand Down
68 changes: 46 additions & 22 deletions samples/client/petstore/cpp-qt5/client/PFXPetApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@

namespace test_namespace {

PFXPetApi::PFXPetApi(const QString &scheme, const QString &host, int port, const QString &basePath, const int timeOut)
: _scheme(scheme),
_host(host),
_port(port),
_basePath(basePath),
_timeOut(timeOut),
PFXPetApi::PFXPetApi(const int timeOut)
: _timeOut(timeOut),
_manager(nullptr),
isResponseCompressionEnabled(false),
isRequestCompressionEnabled(false) {
Expand All @@ -39,7 +35,7 @@ QList<PFXServerConfiguration> defaultConf = QList<PFXServerConfiguration>();
//varying endpoint server
QList<PFXServerConfiguration> serverConf = QList<PFXServerConfiguration>();
defaultConf.append(PFXServerConfiguration(
"http://petstore.swagger.io/v2",
QUrl("http://petstore.swagger.io/v2"),
"No description provided",
QMap<QString, PFXServerVariable>()));
_serverConfigs.insert("addPet",defaultConf);
Expand Down Expand Up @@ -85,18 +81,6 @@ void PFXPetApi::setServerIndex(const QString &operation, int serverIndex){
_serverIndices[operation] = serverIndex;
}

void PFXPetApi::setScheme(const QString &scheme) {
_scheme = scheme;
}

void PFXPetApi::setHost(const QString &host) {
_host = host;
}

void PFXPetApi::setPort(int port) {
_port = port;
}

void PFXPetApi::setApiKey(const QString &apiKeyName, const QString &apiKey){
_apiKeys.insert(apiKeyName,apiKey);
}
Expand All @@ -113,9 +97,6 @@ void PFXPetApi::setPassword(const QString &password) {
_password = password;
}

void PFXPetApi::setBasePath(const QString &basePath) {
_basePath = basePath;
}

void PFXPetApi::setTimeOut(const int timeOut) {
_timeOut = timeOut;
Expand All @@ -129,6 +110,49 @@ void PFXPetApi::setNetworkAccessManager(QNetworkAccessManager* manager) {
_manager = manager;
}

/**
* Appends a new ServerConfiguration to the config map for a specific operation.
* @param operation The id to the target operation.
* @param url A string that contains the URL of the server
* @param description A String that describes the server
* @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template.
* returns the index of the new server config on success and -1 if the operation is not found
*/
int PFXPetApi::addServerConfiguration(const QString &operation, const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables){
if(_serverConfigs.contains(operation)){
_serverConfigs[operation].append(PFXServerConfiguration(
url,
description,
variables));
return _serverConfigs[operation].size()-1;
}else{
return -1;
}
}

/**
* Appends a new ServerConfiguration to the config map for a all operations and sets the index to that server.
* @param url A string that contains the URL of the server
* @param description A String that describes the server
* @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template.
*/
void PFXPetApi::setNewServerForAllOperations(const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables){
for(auto e : _serverIndices.keys()){
setServerIndex(e, addServerConfiguration(e, url, description, variables));
}
}
/**
* Appends a new ServerConfiguration to the config map for an operations and sets the index to that server.
* @param URL A string that contains the URL of the server
* @param description A String that describes the server
* @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template.
*/
void PFXPetApi::setNewServer(const QString &operation, const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables){

setServerIndex(operation, addServerConfiguration(operation, url, description, variables));

}

void PFXPetApi::addHeaders(const QString &key, const QString &value) {
defaultHeaders.insert(key, value);
}
Expand Down
12 changes: 4 additions & 8 deletions samples/client/petstore/cpp-qt5/client/PFXPetApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,22 @@ class PFXPetApi : public QObject {
Q_OBJECT

public:
PFXPetApi(const QString &scheme = "http", const QString &host = "petstore.swagger.io", int port = 0, const QString &basePath = "/v2", const int timeOut = 0);
PFXPetApi(const int timeOut = 0);
~PFXPetApi();

void initializeServerConfigs();
int setDefaultServerValue(int serverIndex,const QString &operation, const QString &variable,const QString &val);
void setServerIndex(const QString &operation, int serverIndex);
void setScheme(const QString &scheme);
void setHost(const QString &host);
void setPort(int port);
void setApiKey(const QString &apiKeyName, const QString &apiKey);
void setBearerToken(const QString &token);
void setUsername(const QString &username);
void setPassword(const QString &password);
void setBasePath(const QString &basePath);
void setTimeOut(const int timeOut);
void setWorkingDirectory(const QString &path);
void setNetworkAccessManager(QNetworkAccessManager* manager);
int addServerConfiguration(const QString &operation, const QUrl &url, const QString &description = "", const QMap<QString, PFXServerVariable> &variables = QMap<QString, PFXServerVariable>());
void setNewServerForAllOperations(const QUrl &url, const QString &description = "", const QMap<QString, PFXServerVariable> &variables = QMap<QString, PFXServerVariable>());
void setNewServer(const QString &operation, const QUrl &url, const QString &description = "", const QMap<QString, PFXServerVariable> &variables = QMap<QString, PFXServerVariable>());
void addHeaders(const QString &key, const QString &value);
void enableRequestCompression();
void enableResponseCompression();
Expand All @@ -67,9 +66,6 @@ class PFXPetApi : public QObject {
void uploadFile(const qint64 &pet_id, const QString &additional_metadata, const PFXHttpFileElement &file);

private:
QString _scheme, _host;
int _port;
QString _basePath;
QMap<QString,int> _serverIndices;
QMap<QString,QList<PFXServerConfiguration>> _serverConfigs;
QMap<QString, QString> _apiKeys;
Expand Down
12 changes: 6 additions & 6 deletions samples/client/petstore/cpp-qt5/client/PFXServerConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ namespace test_namespace {
class PFXServerConfiguration {
public:
/**
* @param URL A URL to the target host.
* @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.
*/
PFXServerConfiguration(const QString& URL, const QString& description, const QMap<QString, PFXServerVariable>& variables)
PFXServerConfiguration(const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables)
: _description(description),
_variables(variables),
_URL(URL){}
_url(url){}
PFXServerConfiguration(){}
~PFXServerConfiguration(){}

Expand All @@ -43,7 +43,7 @@ class PFXServerConfiguration {
* @return Formatted URL.
*/
QString URL() {
QString url = _URL;
QString url = _url.toString();
if(!_variables.empty()){
// go through variables and replace placeholders
for (auto const& v : _variables.keys()) {
Expand All @@ -63,15 +63,15 @@ class PFXServerConfiguration {
return url;
}

int setDefaultValue(const QString& variable,const QString& value){
int setDefaultValue(const QString &variable,const QString &value){
if(_variables.contains(variable))
return _variables[variable].setDefaultValue(value);
return -1;
}

QString _description;
QMap<QString, PFXServerVariable> _variables;
QString _URL;
QUrl _url;

};

Expand Down
Loading

0 comments on commit 16e7408

Please sign in to comment.