Qt cpp client primitives types body (#12301)

* Fixxed issue with numeric primitive in body

* Aded primitives to cpp-qt/petstore.yaml (test ressource)

* generated  samples via  ./bin/generate-samples.sh
This commit is contained in:
Thi
2022-05-15 11:48:19 +02:00
committed by GitHub
parent d23e95a3df
commit 559a2618df
10 changed files with 534 additions and 3 deletions
@@ -515,9 +515,9 @@ void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}}
QJsonDocument doc(::{{cppNamespace}}::toJsonValue({{paramName}}{{^required}}.value(){{/required}}).toObject());{{/isMap}}
QByteArray bytes = doc.toJson();
input.request_body.append(bytes);
{{/isContainer}}{{^isContainer}}{{#isString}}
QByteArray output = {{paramName}}{{^required}}.value(){{/required}}.toUtf8();{{/isString}}{{#isByteArray}}QByteArray output({{paramName}}{{^required}}.value(){{/required}});{{/isByteArray}}{{^isString}}{{^isByteArray}}{{^isFile}}
QByteArray output = {{paramName}}{{^required}}.value(){{/required}}.asJson().toUtf8();{{/isFile}}{{/isByteArray}}{{/isString}}{{#isFile}}{{#hasConsumes}}input.headers.insert("Content-Type", {{#consumes}}{{^-first}}, {{/-first}}"{{mediaType}}"{{/consumes}});{{/hasConsumes}}
{{/isContainer}}{{^isContainer}}
{{#isString}}QByteArray output = {{paramName}}{{^required}}.value(){{/required}}.toUtf8();{{/isString}}{{#isByteArray}}QByteArray output({{paramName}}{{^required}}.value(){{/required}});{{/isByteArray}}{{#isNumeric}}QByteArray output = QByteArray::number({{paramName}}{{^required}}.value(){{/required}});{{/isNumeric}}{{^isString}}{{^isByteArray}}{{^isFile}}{{^isNumeric}}
QByteArray output = {{paramName}}{{^required}}.value(){{/required}}.asJson().toUtf8();{{/isNumeric}}{{/isFile}}{{/isByteArray}}{{/isString}}{{#isFile}}{{#hasConsumes}}input.headers.insert("Content-Type", {{#consumes}}{{^-first}}, {{/-first}}"{{mediaType}}"{{/consumes}});{{/hasConsumes}}
QByteArray output = {{paramName}}{{^required}}.value(){{/required}}.asByteArray();{{/isFile}}
input.request_body.append(output);
{{/isContainer}}
@@ -371,6 +371,40 @@ paths:
description: Invalid ID supplied
"404":
description: Order not found
/primitives/integer:
post:
tags:
- primitives
summary: Post an integer
responses:
'200':
description: OK
requestBody:
content:
text/plain:
schema:
type: integer
minimum: 0
maximum: 4294967295
examples:
example-1:
value: 123
/primitives/number:
put:
tags:
- primitives
summary: Put a number
responses:
'200':
description: OK
requestBody:
content:
text/plain:
schema:
type: number
examples:
example-1:
value: 123.192318
/user:
post:
tags:
@@ -20,6 +20,8 @@ client/PFXPet.cpp
client/PFXPet.h
client/PFXPetApi.cpp
client/PFXPetApi.h
client/PFXPrimitivesApi.cpp
client/PFXPrimitivesApi.h
client/PFXServerConfiguration.h
client/PFXServerVariable.h
client/PFXStoreApi.cpp
@@ -23,6 +23,7 @@ add_library(${PROJECT_NAME}
PFXTag.cpp
PFXUser.cpp
PFXPetApi.cpp
PFXPrimitivesApi.cpp
PFXStoreApi.cpp
PFXUserApi.cpp
PFXHelpers.cpp
@@ -238,6 +238,7 @@ void PFXPetApi::addPet(const PFXPet &pfx_pet) {
{
QByteArray output = pfx_pet.asJson().toUtf8();
input.request_body.append(output);
}
@@ -931,6 +932,7 @@ void PFXPetApi::updatePet(const PFXPet &pfx_pet) {
{
QByteArray output = pfx_pet.asJson().toUtf8();
input.request_body.append(output);
}
@@ -0,0 +1,370 @@
/**
* 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.
*/
#include "PFXPrimitivesApi.h"
#include "PFXServerConfiguration.h"
#include <QJsonArray>
#include <QJsonDocument>
namespace test_namespace {
PFXPrimitivesApi::PFXPrimitivesApi(const int timeOut)
: _timeOut(timeOut),
_manager(nullptr),
_isResponseCompressionEnabled(false),
_isRequestCompressionEnabled(false) {
initializeServerConfigs();
}
PFXPrimitivesApi::~PFXPrimitivesApi() {
}
void PFXPrimitivesApi::initializeServerConfigs() {
//Default server
QList<PFXServerConfiguration> defaultConf = QList<PFXServerConfiguration>();
//varying endpoint server
defaultConf.append(PFXServerConfiguration(
QUrl("http://petstore.swagger.io/v2"),
"No description provided",
QMap<QString, PFXServerVariable>()));
_serverConfigs.insert("primitivesIntegerPost", defaultConf);
_serverIndices.insert("primitivesIntegerPost", 0);
_serverConfigs.insert("primitivesNumberPut", defaultConf);
_serverIndices.insert("primitivesNumberPut", 0);
}
/**
* returns 0 on success and -1, -2 or -3 on failure.
* -1 when the variable does not exist and -2 if the value is not defined in the enum and -3 if the operation or server index is not found
*/
int PFXPrimitivesApi::setDefaultServerValue(int serverIndex, const QString &operation, const QString &variable, const QString &value) {
auto it = _serverConfigs.find(operation);
if (it != _serverConfigs.end() && serverIndex < it.value().size()) {
return _serverConfigs[operation][serverIndex].setDefaultValue(variable,value);
}
return -3;
}
void PFXPrimitivesApi::setServerIndex(const QString &operation, int serverIndex) {
if (_serverIndices.contains(operation) && serverIndex < _serverConfigs.find(operation).value().size()) {
_serverIndices[operation] = serverIndex;
}
}
void PFXPrimitivesApi::setApiKey(const QString &apiKeyName, const QString &apiKey) {
_apiKeys.insert(apiKeyName,apiKey);
}
void PFXPrimitivesApi::setBearerToken(const QString &token) {
_bearerToken = token;
}
void PFXPrimitivesApi::setUsername(const QString &username) {
_username = username;
}
void PFXPrimitivesApi::setPassword(const QString &password) {
_password = password;
}
void PFXPrimitivesApi::setTimeOut(const int timeOut) {
_timeOut = timeOut;
}
void PFXPrimitivesApi::setWorkingDirectory(const QString &path) {
_workingDirectory = path;
}
void PFXPrimitivesApi::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 PFXPrimitivesApi::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 PFXPrimitivesApi::setNewServerForAllOperations(const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables) {
#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
for (auto keyIt = _serverIndices.keyBegin(); keyIt != _serverIndices.keyEnd(); keyIt++) {
setServerIndex(*keyIt, addServerConfiguration(*keyIt, url, description, variables));
}
#else
for (auto &e : _serverIndices.keys()) {
setServerIndex(e, addServerConfiguration(e, url, description, variables));
}
#endif
}
/**
* 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 PFXPrimitivesApi::setNewServer(const QString &operation, const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables) {
setServerIndex(operation, addServerConfiguration(operation, url, description, variables));
}
void PFXPrimitivesApi::addHeaders(const QString &key, const QString &value) {
_defaultHeaders.insert(key, value);
}
void PFXPrimitivesApi::enableRequestCompression() {
_isRequestCompressionEnabled = true;
}
void PFXPrimitivesApi::enableResponseCompression() {
_isResponseCompressionEnabled = true;
}
void PFXPrimitivesApi::abortRequests() {
emit abortRequestsSignal();
}
QString PFXPrimitivesApi::getParamStylePrefix(const QString &style) {
if (style == "matrix") {
return ";";
} else if (style == "label") {
return ".";
} else if (style == "form") {
return "&";
} else if (style == "simple") {
return "";
} else if (style == "spaceDelimited") {
return "&";
} else if (style == "pipeDelimited") {
return "&";
} else {
return "none";
}
}
QString PFXPrimitivesApi::getParamStyleSuffix(const QString &style) {
if (style == "matrix") {
return "=";
} else if (style == "label") {
return "";
} else if (style == "form") {
return "=";
} else if (style == "simple") {
return "";
} else if (style == "spaceDelimited") {
return "=";
} else if (style == "pipeDelimited") {
return "=";
} else {
return "none";
}
}
QString PFXPrimitivesApi::getParamStyleDelimiter(const QString &style, const QString &name, bool isExplode) {
if (style == "matrix") {
return (isExplode) ? ";" + name + "=" : ",";
} else if (style == "label") {
return (isExplode) ? "." : ",";
} else if (style == "form") {
return (isExplode) ? "&" + name + "=" : ",";
} else if (style == "simple") {
return ",";
} else if (style == "spaceDelimited") {
return (isExplode) ? "&" + name + "=" : " ";
} else if (style == "pipeDelimited") {
return (isExplode) ? "&" + name + "=" : "|";
} else if (style == "deepObject") {
return (isExplode) ? "&" : "none";
} else {
return "none";
}
}
void PFXPrimitivesApi::primitivesIntegerPost(const ::test_namespace::OptionalParam<qint32> &body) {
QString fullPath = QString(_serverConfigs["primitivesIntegerPost"][_serverIndices.value("primitivesIntegerPost")].URL()+"/primitives/integer");
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "POST");
if (body.hasValue()){
QByteArray output = QByteArray::number(body.value());
input.request_body.append(output);
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
#else
for (auto key : _defaultHeaders.keys()) {
input.headers.insert(key, _defaultHeaders[key]);
}
#endif
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXPrimitivesApi::primitivesIntegerPostCallback);
connect(this, &PFXPrimitivesApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, this, [this]() {
if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted();
}
});
worker->execute(&input);
}
void PFXPrimitivesApi::primitivesIntegerPostCallback(PFXHttpRequestWorker *worker) {
QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type != QNetworkReply::NoError) {
error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
}
worker->deleteLater();
if (worker->error_type == QNetworkReply::NoError) {
emit primitivesIntegerPostSignal();
emit primitivesIntegerPostSignalFull(worker);
} else {
emit primitivesIntegerPostSignalE(error_type, error_str);
emit primitivesIntegerPostSignalEFull(worker, error_type, error_str);
}
}
void PFXPrimitivesApi::primitivesNumberPut(const ::test_namespace::OptionalParam<double> &body) {
QString fullPath = QString(_serverConfigs["primitivesNumberPut"][_serverIndices.value("primitivesNumberPut")].URL()+"/primitives/number");
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "PUT");
if (body.hasValue()){
QByteArray output = QByteArray::number(body.value());
input.request_body.append(output);
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
#else
for (auto key : _defaultHeaders.keys()) {
input.headers.insert(key, _defaultHeaders[key]);
}
#endif
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXPrimitivesApi::primitivesNumberPutCallback);
connect(this, &PFXPrimitivesApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, this, [this]() {
if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted();
}
});
worker->execute(&input);
}
void PFXPrimitivesApi::primitivesNumberPutCallback(PFXHttpRequestWorker *worker) {
QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type != QNetworkReply::NoError) {
error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
}
worker->deleteLater();
if (worker->error_type == QNetworkReply::NoError) {
emit primitivesNumberPutSignal();
emit primitivesNumberPutSignalFull(worker);
} else {
emit primitivesNumberPutSignalE(error_type, error_str);
emit primitivesNumberPutSignalEFull(worker, error_type, error_str);
}
}
void PFXPrimitivesApi::tokenAvailable(){
oauthToken token;
switch (_OauthMethod) {
case 1: //implicit flow
token = _implicitFlow.getToken(_latestScope.join(" "));
if(token.isValid()){
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
_latestWorker->execute(&_latestInput);
}else{
_implicitFlow.removeToken(_latestScope.join(" "));
qDebug() << "Could not retrieve a valid token";
}
break;
case 2: //authorization flow
token = _authFlow.getToken(_latestScope.join(" "));
if(token.isValid()){
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
_latestWorker->execute(&_latestInput);
}else{
_authFlow.removeToken(_latestScope.join(" "));
qDebug() << "Could not retrieve a valid token";
}
break;
case 3: //client credentials flow
token = _credentialFlow.getToken(_latestScope.join(" "));
if(token.isValid()){
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
_latestWorker->execute(&_latestInput);
}else{
_credentialFlow.removeToken(_latestScope.join(" "));
qDebug() << "Could not retrieve a valid token";
}
break;
case 4: //resource owner password flow
token = _passwordFlow.getToken(_latestScope.join(" "));
if(token.isValid()){
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
_latestWorker->execute(&_latestInput);
}else{
_credentialFlow.removeToken(_latestScope.join(" "));
qDebug() << "Could not retrieve a valid token";
}
break;
default:
qDebug() << "No Oauth method set!";
break;
}
}
} // namespace test_namespace
@@ -0,0 +1,117 @@
/**
* 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.
*/
#ifndef PFX_PFXPrimitivesApi_H
#define PFX_PFXPrimitivesApi_H
#include "PFXHelpers.h"
#include "PFXHttpRequest.h"
#include "PFXServerConfiguration.h"
#include "PFXOauth.h"
#include <QString>
#include <QObject>
#include <QByteArray>
#include <QStringList>
#include <QList>
#include <QNetworkAccessManager>
namespace test_namespace {
class PFXPrimitivesApi : public QObject {
Q_OBJECT
public:
PFXPrimitivesApi(const int timeOut = 0);
~PFXPrimitivesApi();
void initializeServerConfigs();
int setDefaultServerValue(int serverIndex,const QString &operation, const QString &variable,const QString &val);
void setServerIndex(const QString &operation, int serverIndex);
void setApiKey(const QString &apiKeyName, const QString &apiKey);
void setBearerToken(const QString &token);
void setUsername(const QString &username);
void setPassword(const QString &password);
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();
void abortRequests();
QString getParamStylePrefix(const QString &style);
QString getParamStyleSuffix(const QString &style);
QString getParamStyleDelimiter(const QString &style, const QString &name, bool isExplode);
/**
* @param[in] body qint32 [optional]
*/
void primitivesIntegerPost(const ::test_namespace::OptionalParam<qint32> &body = ::test_namespace::OptionalParam<qint32>());
/**
* @param[in] body double [optional]
*/
void primitivesNumberPut(const ::test_namespace::OptionalParam<double> &body = ::test_namespace::OptionalParam<double>());
private:
QMap<QString,int> _serverIndices;
QMap<QString,QList<PFXServerConfiguration>> _serverConfigs;
QMap<QString, QString> _apiKeys;
QString _bearerToken;
QString _username;
QString _password;
int _timeOut;
QString _workingDirectory;
QNetworkAccessManager* _manager;
QMap<QString, QString> _defaultHeaders;
bool _isResponseCompressionEnabled;
bool _isRequestCompressionEnabled;
PFXHttpRequestInput _latestInput;
PFXHttpRequestWorker *_latestWorker;
QStringList _latestScope;
OauthCode _authFlow;
OauthImplicit _implicitFlow;
OauthCredentials _credentialFlow;
OauthPassword _passwordFlow;
int _OauthMethod = 0;
void primitivesIntegerPostCallback(PFXHttpRequestWorker *worker);
void primitivesNumberPutCallback(PFXHttpRequestWorker *worker);
signals:
void primitivesIntegerPostSignal();
void primitivesNumberPutSignal();
void primitivesIntegerPostSignalFull(PFXHttpRequestWorker *worker);
void primitivesNumberPutSignalFull(PFXHttpRequestWorker *worker);
void primitivesIntegerPostSignalE(QNetworkReply::NetworkError error_type, QString error_str);
void primitivesNumberPutSignalE(QNetworkReply::NetworkError error_type, QString error_str);
void primitivesIntegerPostSignalEFull(PFXHttpRequestWorker *worker, QNetworkReply::NetworkError error_type, QString error_str);
void primitivesNumberPutSignalEFull(PFXHttpRequestWorker *worker, QNetworkReply::NetworkError error_type, QString error_str);
void abortRequestsSignal();
void allPendingRequestsCompleted();
public slots:
void tokenAvailable();
};
} // namespace test_namespace
#endif
@@ -415,6 +415,7 @@ void PFXStoreApi::placeOrder(const PFXOrder &pfx_order) {
{
QByteArray output = pfx_order.asJson().toUtf8();
input.request_body.append(output);
}
@@ -236,6 +236,7 @@ void PFXUserApi::createUser(const PFXUser &pfx_user) {
{
QByteArray output = pfx_user.asJson().toUtf8();
input.request_body.append(output);
}
@@ -660,6 +661,7 @@ void PFXUserApi::updateUser(const QString &username, const PFXUser &pfx_user) {
{
QByteArray output = pfx_user.asJson().toUtf8();
input.request_body.append(output);
}
@@ -10,6 +10,7 @@ HEADERS += \
$${PWD}/PFXUser.h \
# APIs
$${PWD}/PFXPetApi.h \
$${PWD}/PFXPrimitivesApi.h \
$${PWD}/PFXStoreApi.h \
$${PWD}/PFXUserApi.h \
# Others
@@ -32,6 +33,7 @@ SOURCES += \
$${PWD}/PFXUser.cpp \
# APIs
$${PWD}/PFXPetApi.cpp \
$${PWD}/PFXPrimitivesApi.cpp \
$${PWD}/PFXStoreApi.cpp \
$${PWD}/PFXUserApi.cpp \
# Others