cpp-qt-client: Fix cppcheck warnings (#10322)

* Fix cppcheck warnings

* Improve coding style

* Add 4 spaces
This commit is contained in:
Martin Delille 2021-09-08 10:10:28 +02:00 committed by GitHub
parent 86ead27a40
commit 2239ca36fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 398 additions and 437 deletions

View File

@ -11,19 +11,21 @@ namespace {{this}} {
{{classname}}::{{classname}}(const int timeOut) {{classname}}::{{classname}}(const int timeOut)
: _timeOut(timeOut), : _timeOut(timeOut),
_manager(nullptr), _manager(nullptr),
isResponseCompressionEnabled(false), _isResponseCompressionEnabled(false),
isRequestCompressionEnabled(false) { _isRequestCompressionEnabled(false) {
initializeServerConfigs(); initializeServerConfigs();
} }
{{classname}}::~{{classname}}() { {{classname}}::~{{classname}}() {
} }
void {{classname}}::initializeServerConfigs(){ void {{classname}}::initializeServerConfigs() {
//Default server //Default server
QList<{{prefix}}ServerConfiguration> defaultConf = QList<{{prefix}}ServerConfiguration>(); QList<{{prefix}}ServerConfiguration> defaultConf = QList<{{prefix}}ServerConfiguration>();
//varying endpoint server //varying endpoint server
{{#servers}}
QList<{{prefix}}ServerConfiguration> serverConf = QList<{{prefix}}ServerConfiguration>(); QList<{{prefix}}ServerConfiguration> serverConf = QList<{{prefix}}ServerConfiguration>();
{{/servers}}
{{#vendorExtensions.x-cpp-global-server-list}} {{#vendorExtensions.x-cpp-global-server-list}}
defaultConf.append({{prefix}}ServerConfiguration( defaultConf.append({{prefix}}ServerConfiguration(
QUrl("{{{url}}}"), QUrl("{{{url}}}"),
@ -58,23 +60,24 @@ void {{classname}}::initializeServerConfigs(){
* returns 0 on success and -1, -2 or -3 on failure. * 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 * -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 {{classname}}::setDefaultServerValue(int serverIndex, const QString &operation, const QString &variable, const QString &value){ int {{classname}}::setDefaultServerValue(int serverIndex, const QString &operation, const QString &variable, const QString &value) {
auto it = _serverConfigs.find(operation); auto it = _serverConfigs.find(operation);
if(it != _serverConfigs.end() && serverIndex < it.value().size() ){ if (it != _serverConfigs.end() && serverIndex < it.value().size()) {
return _serverConfigs[operation][serverIndex].setDefaultValue(variable,value); return _serverConfigs[operation][serverIndex].setDefaultValue(variable,value);
} }
return -3; return -3;
} }
void {{classname}}::setServerIndex(const QString &operation, int serverIndex){ void {{classname}}::setServerIndex(const QString &operation, int serverIndex) {
if(_serverIndices.contains(operation) && serverIndex < _serverConfigs.find(operation).value().size() ) if (_serverIndices.contains(operation) && serverIndex < _serverConfigs.find(operation).value().size()) {
_serverIndices[operation] = serverIndex; _serverIndices[operation] = serverIndex;
}
} }
void {{classname}}::setApiKey(const QString &apiKeyName, const QString &apiKey){ void {{classname}}::setApiKey(const QString &apiKeyName, const QString &apiKey) {
_apiKeys.insert(apiKeyName,apiKey); _apiKeys.insert(apiKeyName,apiKey);
} }
void {{classname}}::setBearerToken(const QString &token){ void {{classname}}::setBearerToken(const QString &token) {
_bearerToken = token; _bearerToken = token;
} }
@ -107,14 +110,14 @@ void {{classname}}::setNetworkAccessManager(QNetworkAccessManager* manager) {
* @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template. * @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 * 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){ int {{classname}}::addServerConfiguration(const QString &operation, const QUrl &url, const QString &description, const QMap<QString, {{prefix}}ServerVariable> &variables) {
if(_serverConfigs.contains(operation)){ if (_serverConfigs.contains(operation)) {
_serverConfigs[operation].append({{prefix}}ServerConfiguration( _serverConfigs[operation].append({{prefix}}ServerConfiguration(
url, url,
description, description,
variables)); variables));
return _serverConfigs[operation].size()-1; return _serverConfigs[operation].size()-1;
}else{ } else {
return -1; return -1;
} }
} }
@ -125,9 +128,9 @@ int {{classname}}::addServerConfiguration(const QString &operation, const QUrl &
* @param description A String that describes 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. * @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){ void {{classname}}::setNewServerForAllOperations(const QUrl &url, const QString &description, const QMap<QString, {{prefix}}ServerVariable> &variables) {
for(auto e : _serverIndices.keys()){ for (auto keyIt = _serverIndices.keyBegin(); keyIt != _serverIndices.keyEnd(); keyIt++) {
setServerIndex(e, addServerConfiguration(e, url, description, variables)); setServerIndex(*keyIt, addServerConfiguration(*keyIt, url, description, variables));
} }
} }
@ -137,85 +140,85 @@ void {{classname}}::setNewServerForAllOperations(const QUrl &url, const QString
* @param description A String that describes 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. * @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){ 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)); setServerIndex(operation, addServerConfiguration(operation, url, description, variables));
} }
void {{classname}}::addHeaders(const QString &key, const QString &value) { void {{classname}}::addHeaders(const QString &key, const QString &value) {
defaultHeaders.insert(key, value); _defaultHeaders.insert(key, value);
} }
void {{classname}}::enableRequestCompression() { void {{classname}}::enableRequestCompression() {
isRequestCompressionEnabled = true; _isRequestCompressionEnabled = true;
} }
void {{classname}}::enableResponseCompression() { void {{classname}}::enableResponseCompression() {
isResponseCompressionEnabled = true; _isResponseCompressionEnabled = true;
} }
void {{classname}}::abortRequests(){ void {{classname}}::abortRequests() {
emit abortRequestsSignal(); emit abortRequestsSignal();
} }
QString {{classname}}::getParamStylePrefix(QString style){ QString {{classname}}::getParamStylePrefix(const QString &style) {
if(style == "matrix"){ if (style == "matrix") {
return ";"; return ";";
}else if(style == "label"){ } else if (style == "label") {
return "."; return ".";
}else if(style == "form"){ } else if (style == "form") {
return "&"; return "&";
}else if(style == "simple"){ } else if (style == "simple") {
return ""; return "";
}else if(style == "spaceDelimited"){ } else if (style == "spaceDelimited") {
return "&"; return "&";
}else if(style == "pipeDelimited"){ } else if (style == "pipeDelimited") {
return "&"; return "&";
}else{ } else {
return "none"; return "none";
} }
} }
QString {{classname}}::getParamStyleSuffix(QString style){ QString {{classname}}::getParamStyleSuffix(const QString &style) {
if(style == "matrix"){ if (style == "matrix") {
return "="; return "=";
}else if(style == "label"){ } else if (style == "label") {
return ""; return "";
}else if(style == "form"){ } else if (style == "form") {
return "="; return "=";
}else if(style == "simple"){ } else if (style == "simple") {
return ""; return "";
}else if(style == "spaceDelimited"){ } else if (style == "spaceDelimited") {
return "="; return "=";
}else if(style == "pipeDelimited"){ } else if (style == "pipeDelimited") {
return "="; return "=";
}else{ } else {
return "none"; return "none";
} }
} }
QString {{classname}}::getParamStyleDelimiter(QString style, QString name, bool isExplode){ QString {{classname}}::getParamStyleDelimiter(const QString &style, const QString &name, bool isExplode) {
if(style == "matrix"){ if (style == "matrix") {
return (isExplode) ? ";" + name + "=" : ","; return (isExplode) ? ";" + name + "=" : ",";
}else if(style == "label"){ } else if (style == "label") {
return (isExplode) ? "." : ","; return (isExplode) ? "." : ",";
}else if(style == "form"){ } else if (style == "form") {
return (isExplode) ? "&" + name + "=" : ","; return (isExplode) ? "&" + name + "=" : ",";
}else if(style == "simple"){ } else if (style == "simple") {
return ","; return ",";
}else if(style == "spaceDelimited"){ } else if (style == "spaceDelimited") {
return (isExplode) ? "&" + name + "=" : " "; return (isExplode) ? "&" + name + "=" : " ";
}else if(style == "pipeDelimited"){ } else if (style == "pipeDelimited") {
return (isExplode) ? "&" + name + "=" : "|"; return (isExplode) ? "&" + name + "=" : "|";
}else if(style == "deepObject"){ } else if (style == "deepObject") {
return (isExplode) ? "&" : "none"; return (isExplode) ? "&" : "none";
}else { } else {
return "none"; return "none";
} }
} }
@ -225,11 +228,11 @@ QString {{classname}}::getParamStyleDelimiter(QString style, QString name, bool
void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}} &{{/required}}{{^required}}const ::{{cppNamespace}}::OptionalParam<{{{dataType}}}> &{{/required}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) { void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}} &{{/required}}{{^required}}const ::{{cppNamespace}}::OptionalParam<{{{dataType}}}> &{{/required}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) {
QString fullPath = QString(_serverConfigs["{{nickname}}"][_serverIndices.value("{{nickname}}")].URL()+"{{{path}}}"); QString fullPath = QString(_serverConfigs["{{nickname}}"][_serverIndices.value("{{nickname}}")].URL()+"{{{path}}}");
{{#authMethods}}{{#isApiKey}}{{#isKeyInHeader}} {{#authMethods}}{{#isApiKey}}{{#isKeyInHeader}}
if(_apiKeys.contains("{{name}}")){ if (_apiKeys.contains("{{name}}")) {
addHeaders("{{name}}",_apiKeys.find("{{name}}").value()); addHeaders("{{name}}",_apiKeys.find("{{name}}").value());
} }
{{/isKeyInHeader}}{{#isKeyInQuery}} {{/isKeyInHeader}}{{#isKeyInQuery}}
if(_apiKeys.contains("{{name}}")){ if (_apiKeys.contains("{{name}}")) {
if (fullPath.indexOf("?") > 0) if (fullPath.indexOf("?") > 0)
fullPath.append("&"); fullPath.append("&");
else else
@ -237,22 +240,22 @@ void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}}
fullPath.append("{{{name}}}=").append(_apiKeys.find("{{name}}").value()); fullPath.append("{{{name}}}=").append(_apiKeys.find("{{name}}").value());
} }
{{/isKeyInQuery}}{{/isApiKey}}{{#isBasicBearer}} {{/isKeyInQuery}}{{/isApiKey}}{{#isBasicBearer}}
if(!_bearerToken.isEmpty()) if (!_bearerToken.isEmpty())
addHeaders("Authorization", "Bearer " + _bearerToken); addHeaders("Authorization", "Bearer " + _bearerToken);
{{/isBasicBearer}}{{#isBasicBasic}} {{/isBasicBearer}}{{#isBasicBasic}}
if(!_username.isEmpty() && !_password.isEmpty()){ if (!_username.isEmpty() && !_password.isEmpty()) {
QByteArray b64; QByteArray b64;
b64.append(_username.toUtf8() + ":" + _password.toUtf8()); b64.append(_username.toUtf8() + ":" + _password.toUtf8());
addHeaders("Authorization","Basic " + b64.toBase64()); addHeaders("Authorization","Basic " + b64.toBase64());
}{{/isBasicBasic}}{{/authMethods}} }{{/isBasicBasic}}{{/authMethods}}
{{#pathParams}} {{#pathParams}}
{{^required}}if({{paramName}}.hasValue()){{/required}} {{^required}}if ({{paramName}}.hasValue()) {{/required}}
{ {
QString {{paramName}}PathParam("{"); QString {{paramName}}PathParam("{");
{{paramName}}PathParam.append("{{baseName}}").append("}"); {{paramName}}PathParam.append("{{baseName}}").append("}");
QString pathPrefix, pathSuffix, pathDelimiter; QString pathPrefix, pathSuffix, pathDelimiter;
QString pathStyle = "{{style}}"; QString pathStyle = "{{style}}";
if(pathStyle == "") if (pathStyle == "")
pathStyle = "simple"; pathStyle = "simple";
pathPrefix = getParamStylePrefix(pathStyle); pathPrefix = getParamStylePrefix(pathStyle);
pathSuffix = getParamStyleSuffix(pathStyle); pathSuffix = getParamStyleSuffix(pathStyle);
@ -308,7 +311,7 @@ void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}}
{{/isPrimitiveType}} {{/isPrimitiveType}}
{{/collectionFormat}} {{/collectionFormat}}
{{#collectionFormat}} {{#collectionFormat}}
if({{{paramName}}}{{^required}}.value(){{/required}}.size() > 0) { if ({{{paramName}}}{{^required}}.value(){{/required}}.size() > 0) {
QString paramString = (pathStyle == "matrix") ? pathPrefix+"{{baseName}}"+pathSuffix : pathPrefix; QString paramString = (pathStyle == "matrix") ? pathPrefix+"{{baseName}}"+pathSuffix : pathPrefix;
qint32 count = 0; qint32 count = 0;
foreach ({{{baseType}}} t, {{paramName}}{{^required}}.value(){{/required}}) { foreach ({{{baseType}}} t, {{paramName}}{{^required}}.value(){{/required}}) {
@ -327,10 +330,10 @@ void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}}
QString queryPrefix, querySuffix, queryDelimiter, queryStyle; QString queryPrefix, querySuffix, queryDelimiter, queryStyle;
{{/hasQueryParams}} {{/hasQueryParams}}
{{#queryParams}} {{#queryParams}}
{{^required}}if({{paramName}}.hasValue()){{/required}} {{^required}}if ({{paramName}}.hasValue()){{/required}}
{ {
queryStyle = "{{style}}"; queryStyle = "{{style}}";
if(queryStyle == "") if (queryStyle == "")
queryStyle = "form"; queryStyle = "form";
queryPrefix = getParamStylePrefix(queryStyle); queryPrefix = getParamStylePrefix(queryStyle);
querySuffix = getParamStyleSuffix(queryStyle); querySuffix = getParamStyleSuffix(queryStyle);
@ -392,7 +395,7 @@ void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}}
{{/isPrimitiveType}} {{/isPrimitiveType}}
{{/collectionFormat}} {{/collectionFormat}}
{{#collectionFormat}} {{#collectionFormat}}
if({{{paramName}}}{{^required}}.value(){{/required}}.size() > 0) { if ({{{paramName}}}{{^required}}.value(){{/required}}.size() > 0) {
if (QString("{{collectionFormat}}").indexOf("multi") == 0) { if (QString("{{collectionFormat}}").indexOf("multi") == 0) {
foreach ({{{baseType}}} t, {{paramName}}{{^required}}.value(){{/required}}) { foreach ({{{baseType}}} t, {{paramName}}{{^required}}.value(){{/required}}) {
if (fullPath.indexOf("?") > 0) if (fullPath.indexOf("?") > 0)
@ -474,21 +477,21 @@ void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}}
{{prefix}}HttpRequestWorker *worker = new {{prefix}}HttpRequestWorker(this, _manager); {{prefix}}HttpRequestWorker *worker = new {{prefix}}HttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut); worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);{{#contentCompression}} worker->setWorkingDirectory(_workingDirectory);{{#contentCompression}}
worker->setResponseCompressionEnabled(isResponseCompressionEnabled); worker->setResponseCompressionEnabled(_isResponseCompressionEnabled);
worker->setRequestCompressionEnabled(isRequestCompressionEnabled);{{/contentCompression}} worker->setRequestCompressionEnabled(_isRequestCompressionEnabled);{{/contentCompression}}
{{prefix}}HttpRequestInput input(fullPath, "{{httpMethod}}"); {{prefix}}HttpRequestInput input(fullPath, "{{httpMethod}}");
{{#formParams}} {{#formParams}}
{{#first}} {{#first}}
QString formPrefix,formSuffix, formDelimiter; QString formPrefix,formSuffix, formDelimiter;
QString formStyle = "{{style}}"; QString formStyle = "{{style}}";
if(formStyle == "") if (formStyle == "")
formStyle = "form"; formStyle = "form";
formPrefix = getParamStylePrefix(formStyle); formPrefix = getParamStylePrefix(formStyle);
formSuffix = getParamStyleSuffix(formStyle); formSuffix = getParamStyleSuffix(formStyle);
formDelimiter = getParamStyleDelimiter(formStyle, "{{baseName}}", {{isExplode}}); formDelimiter = getParamStyleDelimiter(formStyle, "{{baseName}}", {{isExplode}});
{{/first}} {{/first}}
{{^required}}if({{paramName}}.hasValue()){{/required}} {{^required}}if ({{paramName}}.hasValue()){{/required}}
{ {
{{^isFile}} {{^isFile}}
input.add_var("{{baseName}}", ::{{cppNamespace}}::toStringValue({{paramName}}{{^required}}.value(){{/required}})); input.add_var("{{baseName}}", ::{{cppNamespace}}::toStringValue({{paramName}}{{^required}}.value(){{/required}}));
@ -499,7 +502,7 @@ void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}}
} }
{{/formParams}} {{/formParams}}
{{#bodyParams}} {{#bodyParams}}
{{^required}}if({{paramName}}.hasValue()){{/required}}{ {{^required}}if ({{paramName}}.hasValue()){{/required}}{
{{#isContainer}} {{#isContainer}}
{{#isArray}} {{#isArray}}
QJsonDocument doc(::{{cppNamespace}}::toJsonValue({{paramName}}{{^required}}.value(){{/required}}).toArray());{{/isArray}}{{#isMap}} QJsonDocument doc(::{{cppNamespace}}::toJsonValue({{paramName}}{{^required}}.value(){{/required}}).toArray());{{/isArray}}{{#isMap}}
@ -514,7 +517,7 @@ void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}}
{{/isContainer}} {{/isContainer}}
}{{/bodyParams}} }{{/bodyParams}}
{{#headerParams}} {{#headerParams}}
{{^required}}if({{paramName}}.hasValue()){{/required}} {{^required}}if ({{paramName}}.hasValue()){{/required}}
{ {
{{^collectionFormat}} {{^collectionFormat}}
{{^isPrimitiveType}} {{^isPrimitiveType}}
@ -581,9 +584,9 @@ void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}}
} }
{{/headerParams}} {{/headerParams}}
{{#cookieParams}} {{#cookieParams}}
{{^required}}if({{paramName}}.hasValue()){{/required}} {{^required}}if ({{paramName}}.hasValue()){{/required}}
{ {
if(QString("{{style}}").indexOf("form") == 0){ if (QString("{{style}}").indexOf("form") == 0) {
{{^collectionFormat}} {{^collectionFormat}}
{{^isPrimitiveType}} {{^isPrimitiveType}}
{{^isExplode}} {{^isExplode}}
@ -630,7 +633,7 @@ void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}}
{{/isExplode}} {{/isExplode}}
{{/isPrimitiveType}} {{/isPrimitiveType}}
{{#isPrimitiveType}} {{#isPrimitiveType}}
if(!::{{cppNamespace}}::toStringValue({{paramName}}{{^required}}.value(){{/required}}).isEmpty()) { if (!::{{cppNamespace}}::toStringValue({{paramName}}{{^required}}.value(){{/required}}).isEmpty()) {
input.headers.insert("Cookie", "{{baseName}}="+::{{cppNamespace}}::toStringValue({{paramName}}{{^required}}.value(){{/required}})); input.headers.insert("Cookie", "{{baseName}}="+::{{cppNamespace}}::toStringValue({{paramName}}{{^required}}.value(){{/required}}));
} }
{{/isPrimitiveType}} {{/isPrimitiveType}}
@ -653,12 +656,14 @@ void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}}
} }
} }
{{/cookieParams}} {{/cookieParams}}
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); } for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
connect(worker, &{{prefix}}HttpRequestWorker::on_execution_finished, this, &{{classname}}::{{nickname}}Callback); connect(worker, &{{prefix}}HttpRequestWorker::on_execution_finished, this, &{{classname}}::{{nickname}}Callback);
connect(this, &{{classname}}::abortRequestsSignal, worker, &QObject::deleteLater); connect(this, &{{classname}}::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, [this](){ connect(worker, &QObject::destroyed, this, [this]() {
if(findChildren<{{prefix}}HttpRequestWorker*>().count() == 0){ if (findChildren<{{prefix}}HttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted(); emit allPendingRequestsCompleted();
} }
}); });
@ -667,15 +672,11 @@ void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}}
} }
void {{classname}}::{{nickname}}Callback({{prefix}}HttpRequestWorker *worker) { void {{classname}}::{{nickname}}Callback({{prefix}}HttpRequestWorker *worker) {
QString msg;
QString error_str = worker->error_str; QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type; QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type == QNetworkReply::NoError) { if (worker->error_type != QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length()); error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
} else {
msg = "Error: " + worker->error_str;
error_str = QString("%1, %2").arg(worker->error_str).arg(QString(worker->response));
} }
{{#returnType}} {{#returnType}}
{{#isArray}} {{#isArray}}

View File

@ -43,9 +43,9 @@ public:
void enableRequestCompression(); void enableRequestCompression();
void enableResponseCompression(); void enableResponseCompression();
void abortRequests(); void abortRequests();
QString getParamStylePrefix(QString style); QString getParamStylePrefix(const QString &style);
QString getParamStyleSuffix(QString style); QString getParamStyleSuffix(const QString &style);
QString getParamStyleDelimiter(QString style, QString name, bool isExplode); QString getParamStyleDelimiter(const QString &style, const QString &name, bool isExplode);
{{#operations}}{{#operation}} {{#operations}}{{#operation}}
{{#hasParams}} /** {{#hasParams}} /**
{{#allParams}} {{#allParams}}
@ -70,9 +70,9 @@ private:
int _timeOut; int _timeOut;
QString _workingDirectory; QString _workingDirectory;
QNetworkAccessManager* _manager; QNetworkAccessManager* _manager;
QMap<QString, QString> defaultHeaders; QMap<QString, QString> _defaultHeaders;
bool isResponseCompressionEnabled; bool _isResponseCompressionEnabled;
bool isRequestCompressionEnabled; bool _isRequestCompressionEnabled;
{{#operations}}{{#operation}} {{#operations}}{{#operation}}
void {{nickname}}Callback({{prefix}}HttpRequestWorker *worker);{{/operation}}{{/operations}} void {{nickname}}Callback({{prefix}}HttpRequestWorker *worker);{{/operation}}{{/operations}}

View File

@ -19,19 +19,18 @@ namespace test_namespace {
PFXPetApi::PFXPetApi(const int timeOut) PFXPetApi::PFXPetApi(const int timeOut)
: _timeOut(timeOut), : _timeOut(timeOut),
_manager(nullptr), _manager(nullptr),
isResponseCompressionEnabled(false), _isResponseCompressionEnabled(false),
isRequestCompressionEnabled(false) { _isRequestCompressionEnabled(false) {
initializeServerConfigs(); initializeServerConfigs();
} }
PFXPetApi::~PFXPetApi() { PFXPetApi::~PFXPetApi() {
} }
void PFXPetApi::initializeServerConfigs(){ void PFXPetApi::initializeServerConfigs() {
//Default server //Default server
QList<PFXServerConfiguration> defaultConf = QList<PFXServerConfiguration>(); QList<PFXServerConfiguration> defaultConf = QList<PFXServerConfiguration>();
//varying endpoint server //varying endpoint server
QList<PFXServerConfiguration> serverConf = QList<PFXServerConfiguration>();
defaultConf.append(PFXServerConfiguration( defaultConf.append(PFXServerConfiguration(
QUrl("http://petstore.swagger.io/v2"), QUrl("http://petstore.swagger.io/v2"),
"No description provided", "No description provided",
@ -58,23 +57,24 @@ void PFXPetApi::initializeServerConfigs(){
* returns 0 on success and -1, -2 or -3 on failure. * 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 * -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 PFXPetApi::setDefaultServerValue(int serverIndex, const QString &operation, const QString &variable, const QString &value){ int PFXPetApi::setDefaultServerValue(int serverIndex, const QString &operation, const QString &variable, const QString &value) {
auto it = _serverConfigs.find(operation); auto it = _serverConfigs.find(operation);
if(it != _serverConfigs.end() && serverIndex < it.value().size() ){ if (it != _serverConfigs.end() && serverIndex < it.value().size()) {
return _serverConfigs[operation][serverIndex].setDefaultValue(variable,value); return _serverConfigs[operation][serverIndex].setDefaultValue(variable,value);
} }
return -3; return -3;
} }
void PFXPetApi::setServerIndex(const QString &operation, int serverIndex){ void PFXPetApi::setServerIndex(const QString &operation, int serverIndex) {
if(_serverIndices.contains(operation) && serverIndex < _serverConfigs.find(operation).value().size() ) if (_serverIndices.contains(operation) && serverIndex < _serverConfigs.find(operation).value().size()) {
_serverIndices[operation] = serverIndex; _serverIndices[operation] = serverIndex;
}
} }
void PFXPetApi::setApiKey(const QString &apiKeyName, const QString &apiKey){ void PFXPetApi::setApiKey(const QString &apiKeyName, const QString &apiKey) {
_apiKeys.insert(apiKeyName,apiKey); _apiKeys.insert(apiKeyName,apiKey);
} }
void PFXPetApi::setBearerToken(const QString &token){ void PFXPetApi::setBearerToken(const QString &token) {
_bearerToken = token; _bearerToken = token;
} }
@ -107,14 +107,14 @@ void PFXPetApi::setNetworkAccessManager(QNetworkAccessManager* manager) {
* @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template. * @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 * 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){ int PFXPetApi::addServerConfiguration(const QString &operation, const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables) {
if(_serverConfigs.contains(operation)){ if (_serverConfigs.contains(operation)) {
_serverConfigs[operation].append(PFXServerConfiguration( _serverConfigs[operation].append(PFXServerConfiguration(
url, url,
description, description,
variables)); variables));
return _serverConfigs[operation].size()-1; return _serverConfigs[operation].size()-1;
}else{ } else {
return -1; return -1;
} }
} }
@ -125,9 +125,9 @@ int PFXPetApi::addServerConfiguration(const QString &operation, const QUrl &url,
* @param description A String that describes 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. * @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){ void PFXPetApi::setNewServerForAllOperations(const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables) {
for(auto e : _serverIndices.keys()){ for (auto keyIt = _serverIndices.keyBegin(); keyIt != _serverIndices.keyEnd(); keyIt++) {
setServerIndex(e, addServerConfiguration(e, url, description, variables)); setServerIndex(*keyIt, addServerConfiguration(*keyIt, url, description, variables));
} }
} }
@ -137,85 +137,85 @@ void PFXPetApi::setNewServerForAllOperations(const QUrl &url, const QString &des
* @param description A String that describes 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. * @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){ void PFXPetApi::setNewServer(const QString &operation, const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables) {
setServerIndex(operation, addServerConfiguration(operation, url, description, variables)); setServerIndex(operation, addServerConfiguration(operation, url, description, variables));
} }
void PFXPetApi::addHeaders(const QString &key, const QString &value) { void PFXPetApi::addHeaders(const QString &key, const QString &value) {
defaultHeaders.insert(key, value); _defaultHeaders.insert(key, value);
} }
void PFXPetApi::enableRequestCompression() { void PFXPetApi::enableRequestCompression() {
isRequestCompressionEnabled = true; _isRequestCompressionEnabled = true;
} }
void PFXPetApi::enableResponseCompression() { void PFXPetApi::enableResponseCompression() {
isResponseCompressionEnabled = true; _isResponseCompressionEnabled = true;
} }
void PFXPetApi::abortRequests(){ void PFXPetApi::abortRequests() {
emit abortRequestsSignal(); emit abortRequestsSignal();
} }
QString PFXPetApi::getParamStylePrefix(QString style){ QString PFXPetApi::getParamStylePrefix(const QString &style) {
if(style == "matrix"){ if (style == "matrix") {
return ";"; return ";";
}else if(style == "label"){ } else if (style == "label") {
return "."; return ".";
}else if(style == "form"){ } else if (style == "form") {
return "&"; return "&";
}else if(style == "simple"){ } else if (style == "simple") {
return ""; return "";
}else if(style == "spaceDelimited"){ } else if (style == "spaceDelimited") {
return "&"; return "&";
}else if(style == "pipeDelimited"){ } else if (style == "pipeDelimited") {
return "&"; return "&";
}else{ } else {
return "none"; return "none";
} }
} }
QString PFXPetApi::getParamStyleSuffix(QString style){ QString PFXPetApi::getParamStyleSuffix(const QString &style) {
if(style == "matrix"){ if (style == "matrix") {
return "="; return "=";
}else if(style == "label"){ } else if (style == "label") {
return ""; return "";
}else if(style == "form"){ } else if (style == "form") {
return "="; return "=";
}else if(style == "simple"){ } else if (style == "simple") {
return ""; return "";
}else if(style == "spaceDelimited"){ } else if (style == "spaceDelimited") {
return "="; return "=";
}else if(style == "pipeDelimited"){ } else if (style == "pipeDelimited") {
return "="; return "=";
}else{ } else {
return "none"; return "none";
} }
} }
QString PFXPetApi::getParamStyleDelimiter(QString style, QString name, bool isExplode){ QString PFXPetApi::getParamStyleDelimiter(const QString &style, const QString &name, bool isExplode) {
if(style == "matrix"){ if (style == "matrix") {
return (isExplode) ? ";" + name + "=" : ","; return (isExplode) ? ";" + name + "=" : ",";
}else if(style == "label"){ } else if (style == "label") {
return (isExplode) ? "." : ","; return (isExplode) ? "." : ",";
}else if(style == "form"){ } else if (style == "form") {
return (isExplode) ? "&" + name + "=" : ","; return (isExplode) ? "&" + name + "=" : ",";
}else if(style == "simple"){ } else if (style == "simple") {
return ","; return ",";
}else if(style == "spaceDelimited"){ } else if (style == "spaceDelimited") {
return (isExplode) ? "&" + name + "=" : " "; return (isExplode) ? "&" + name + "=" : " ";
}else if(style == "pipeDelimited"){ } else if (style == "pipeDelimited") {
return (isExplode) ? "&" + name + "=" : "|"; return (isExplode) ? "&" + name + "=" : "|";
}else if(style == "deepObject"){ } else if (style == "deepObject") {
return (isExplode) ? "&" : "none"; return (isExplode) ? "&" : "none";
}else { } else {
return "none"; return "none";
} }
} }
@ -233,12 +233,14 @@ void PFXPetApi::addPet(const PFXPet &body) {
QByteArray output = body.asJson().toUtf8(); QByteArray output = body.asJson().toUtf8();
input.request_body.append(output); input.request_body.append(output);
} }
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); } for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXPetApi::addPetCallback); connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXPetApi::addPetCallback);
connect(this, &PFXPetApi::abortRequestsSignal, worker, &QObject::deleteLater); connect(this, &PFXPetApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, [this](){ connect(worker, &QObject::destroyed, this, [this]() {
if(findChildren<PFXHttpRequestWorker*>().count() == 0){ if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted(); emit allPendingRequestsCompleted();
} }
}); });
@ -247,15 +249,11 @@ void PFXPetApi::addPet(const PFXPet &body) {
} }
void PFXPetApi::addPetCallback(PFXHttpRequestWorker *worker) { void PFXPetApi::addPetCallback(PFXHttpRequestWorker *worker) {
QString msg;
QString error_str = worker->error_str; QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type; QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type == QNetworkReply::NoError) { if (worker->error_type != QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length()); error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
} else {
msg = "Error: " + worker->error_str;
error_str = QString("%1, %2").arg(worker->error_str).arg(QString(worker->response));
} }
worker->deleteLater(); worker->deleteLater();
@ -277,7 +275,7 @@ void PFXPetApi::deletePet(const qint64 &pet_id, const ::test_namespace::Optional
pet_idPathParam.append("petId").append("}"); pet_idPathParam.append("petId").append("}");
QString pathPrefix, pathSuffix, pathDelimiter; QString pathPrefix, pathSuffix, pathDelimiter;
QString pathStyle = ""; QString pathStyle = "";
if(pathStyle == "") if (pathStyle == "")
pathStyle = "simple"; pathStyle = "simple";
pathPrefix = getParamStylePrefix(pathStyle); pathPrefix = getParamStylePrefix(pathStyle);
pathSuffix = getParamStyleSuffix(pathStyle); pathSuffix = getParamStyleSuffix(pathStyle);
@ -291,18 +289,20 @@ void PFXPetApi::deletePet(const qint64 &pet_id, const ::test_namespace::Optional
PFXHttpRequestInput input(fullPath, "DELETE"); PFXHttpRequestInput input(fullPath, "DELETE");
if(api_key.hasValue()) if (api_key.hasValue())
{ {
if (!::test_namespace::toStringValue(api_key.value()).isEmpty()) { if (!::test_namespace::toStringValue(api_key.value()).isEmpty()) {
input.headers.insert("api_key", ::test_namespace::toStringValue(api_key.value())); input.headers.insert("api_key", ::test_namespace::toStringValue(api_key.value()));
} }
} }
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); } for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXPetApi::deletePetCallback); connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXPetApi::deletePetCallback);
connect(this, &PFXPetApi::abortRequestsSignal, worker, &QObject::deleteLater); connect(this, &PFXPetApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, [this](){ connect(worker, &QObject::destroyed, this, [this]() {
if(findChildren<PFXHttpRequestWorker*>().count() == 0){ if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted(); emit allPendingRequestsCompleted();
} }
}); });
@ -311,15 +311,11 @@ void PFXPetApi::deletePet(const qint64 &pet_id, const ::test_namespace::Optional
} }
void PFXPetApi::deletePetCallback(PFXHttpRequestWorker *worker) { void PFXPetApi::deletePetCallback(PFXHttpRequestWorker *worker) {
QString msg;
QString error_str = worker->error_str; QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type; QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type == QNetworkReply::NoError) { if (worker->error_type != QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length()); error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
} else {
msg = "Error: " + worker->error_str;
error_str = QString("%1, %2").arg(worker->error_str).arg(QString(worker->response));
} }
worker->deleteLater(); worker->deleteLater();
@ -339,12 +335,12 @@ void PFXPetApi::findPetsByStatus(const QList<QString> &status) {
{ {
queryStyle = "form"; queryStyle = "form";
if(queryStyle == "") if (queryStyle == "")
queryStyle = "form"; queryStyle = "form";
queryPrefix = getParamStylePrefix(queryStyle); queryPrefix = getParamStylePrefix(queryStyle);
querySuffix = getParamStyleSuffix(queryStyle); querySuffix = getParamStyleSuffix(queryStyle);
queryDelimiter = getParamStyleDelimiter(queryStyle, "status", false); queryDelimiter = getParamStyleDelimiter(queryStyle, "status", false);
if(status.size() > 0) { if (status.size() > 0) {
if (QString("csv").indexOf("multi") == 0) { if (QString("csv").indexOf("multi") == 0) {
foreach (QString t, status) { foreach (QString t, status) {
if (fullPath.indexOf("?") > 0) if (fullPath.indexOf("?") > 0)
@ -427,12 +423,14 @@ void PFXPetApi::findPetsByStatus(const QList<QString> &status) {
PFXHttpRequestInput input(fullPath, "GET"); PFXHttpRequestInput input(fullPath, "GET");
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); } for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXPetApi::findPetsByStatusCallback); connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXPetApi::findPetsByStatusCallback);
connect(this, &PFXPetApi::abortRequestsSignal, worker, &QObject::deleteLater); connect(this, &PFXPetApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, [this](){ connect(worker, &QObject::destroyed, this, [this]() {
if(findChildren<PFXHttpRequestWorker*>().count() == 0){ if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted(); emit allPendingRequestsCompleted();
} }
}); });
@ -441,15 +439,11 @@ void PFXPetApi::findPetsByStatus(const QList<QString> &status) {
} }
void PFXPetApi::findPetsByStatusCallback(PFXHttpRequestWorker *worker) { void PFXPetApi::findPetsByStatusCallback(PFXHttpRequestWorker *worker) {
QString msg;
QString error_str = worker->error_str; QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type; QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type == QNetworkReply::NoError) { if (worker->error_type != QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length()); error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
} else {
msg = "Error: " + worker->error_str;
error_str = QString("%1, %2").arg(worker->error_str).arg(QString(worker->response));
} }
QList<PFXPet> output; QList<PFXPet> output;
QString json(worker->response); QString json(worker->response);
@ -479,12 +473,12 @@ void PFXPetApi::findPetsByTags(const QList<QString> &tags) {
{ {
queryStyle = "form"; queryStyle = "form";
if(queryStyle == "") if (queryStyle == "")
queryStyle = "form"; queryStyle = "form";
queryPrefix = getParamStylePrefix(queryStyle); queryPrefix = getParamStylePrefix(queryStyle);
querySuffix = getParamStyleSuffix(queryStyle); querySuffix = getParamStyleSuffix(queryStyle);
queryDelimiter = getParamStyleDelimiter(queryStyle, "tags", false); queryDelimiter = getParamStyleDelimiter(queryStyle, "tags", false);
if(tags.size() > 0) { if (tags.size() > 0) {
if (QString("csv").indexOf("multi") == 0) { if (QString("csv").indexOf("multi") == 0) {
foreach (QString t, tags) { foreach (QString t, tags) {
if (fullPath.indexOf("?") > 0) if (fullPath.indexOf("?") > 0)
@ -567,12 +561,14 @@ void PFXPetApi::findPetsByTags(const QList<QString> &tags) {
PFXHttpRequestInput input(fullPath, "GET"); PFXHttpRequestInput input(fullPath, "GET");
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); } for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXPetApi::findPetsByTagsCallback); connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXPetApi::findPetsByTagsCallback);
connect(this, &PFXPetApi::abortRequestsSignal, worker, &QObject::deleteLater); connect(this, &PFXPetApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, [this](){ connect(worker, &QObject::destroyed, this, [this]() {
if(findChildren<PFXHttpRequestWorker*>().count() == 0){ if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted(); emit allPendingRequestsCompleted();
} }
}); });
@ -581,15 +577,11 @@ void PFXPetApi::findPetsByTags(const QList<QString> &tags) {
} }
void PFXPetApi::findPetsByTagsCallback(PFXHttpRequestWorker *worker) { void PFXPetApi::findPetsByTagsCallback(PFXHttpRequestWorker *worker) {
QString msg;
QString error_str = worker->error_str; QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type; QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type == QNetworkReply::NoError) { if (worker->error_type != QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length()); error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
} else {
msg = "Error: " + worker->error_str;
error_str = QString("%1, %2").arg(worker->error_str).arg(QString(worker->response));
} }
QList<PFXPet> output; QList<PFXPet> output;
QString json(worker->response); QString json(worker->response);
@ -615,7 +607,7 @@ void PFXPetApi::findPetsByTagsCallback(PFXHttpRequestWorker *worker) {
void PFXPetApi::getPetById(const qint64 &pet_id) { void PFXPetApi::getPetById(const qint64 &pet_id) {
QString fullPath = QString(_serverConfigs["getPetById"][_serverIndices.value("getPetById")].URL()+"/pet/{petId}"); QString fullPath = QString(_serverConfigs["getPetById"][_serverIndices.value("getPetById")].URL()+"/pet/{petId}");
if(_apiKeys.contains("api_key")){ if (_apiKeys.contains("api_key")) {
addHeaders("api_key",_apiKeys.find("api_key").value()); addHeaders("api_key",_apiKeys.find("api_key").value());
} }
@ -625,7 +617,7 @@ void PFXPetApi::getPetById(const qint64 &pet_id) {
pet_idPathParam.append("petId").append("}"); pet_idPathParam.append("petId").append("}");
QString pathPrefix, pathSuffix, pathDelimiter; QString pathPrefix, pathSuffix, pathDelimiter;
QString pathStyle = ""; QString pathStyle = "";
if(pathStyle == "") if (pathStyle == "")
pathStyle = "simple"; pathStyle = "simple";
pathPrefix = getParamStylePrefix(pathStyle); pathPrefix = getParamStylePrefix(pathStyle);
pathSuffix = getParamStyleSuffix(pathStyle); pathSuffix = getParamStyleSuffix(pathStyle);
@ -639,12 +631,14 @@ void PFXPetApi::getPetById(const qint64 &pet_id) {
PFXHttpRequestInput input(fullPath, "GET"); PFXHttpRequestInput input(fullPath, "GET");
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); } for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXPetApi::getPetByIdCallback); connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXPetApi::getPetByIdCallback);
connect(this, &PFXPetApi::abortRequestsSignal, worker, &QObject::deleteLater); connect(this, &PFXPetApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, [this](){ connect(worker, &QObject::destroyed, this, [this]() {
if(findChildren<PFXHttpRequestWorker*>().count() == 0){ if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted(); emit allPendingRequestsCompleted();
} }
}); });
@ -653,15 +647,11 @@ void PFXPetApi::getPetById(const qint64 &pet_id) {
} }
void PFXPetApi::getPetByIdCallback(PFXHttpRequestWorker *worker) { void PFXPetApi::getPetByIdCallback(PFXHttpRequestWorker *worker) {
QString msg;
QString error_str = worker->error_str; QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type; QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type == QNetworkReply::NoError) { if (worker->error_type != QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length()); error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
} else {
msg = "Error: " + worker->error_str;
error_str = QString("%1, %2").arg(worker->error_str).arg(QString(worker->response));
} }
PFXPet output(QString(worker->response)); PFXPet output(QString(worker->response));
worker->deleteLater(); worker->deleteLater();
@ -688,12 +678,14 @@ void PFXPetApi::updatePet(const PFXPet &body) {
QByteArray output = body.asJson().toUtf8(); QByteArray output = body.asJson().toUtf8();
input.request_body.append(output); input.request_body.append(output);
} }
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); } for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXPetApi::updatePetCallback); connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXPetApi::updatePetCallback);
connect(this, &PFXPetApi::abortRequestsSignal, worker, &QObject::deleteLater); connect(this, &PFXPetApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, [this](){ connect(worker, &QObject::destroyed, this, [this]() {
if(findChildren<PFXHttpRequestWorker*>().count() == 0){ if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted(); emit allPendingRequestsCompleted();
} }
}); });
@ -702,15 +694,11 @@ void PFXPetApi::updatePet(const PFXPet &body) {
} }
void PFXPetApi::updatePetCallback(PFXHttpRequestWorker *worker) { void PFXPetApi::updatePetCallback(PFXHttpRequestWorker *worker) {
QString msg;
QString error_str = worker->error_str; QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type; QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type == QNetworkReply::NoError) { if (worker->error_type != QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length()); error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
} else {
msg = "Error: " + worker->error_str;
error_str = QString("%1, %2").arg(worker->error_str).arg(QString(worker->response));
} }
worker->deleteLater(); worker->deleteLater();
@ -732,7 +720,7 @@ void PFXPetApi::updatePetWithForm(const qint64 &pet_id, const ::test_namespace::
pet_idPathParam.append("petId").append("}"); pet_idPathParam.append("petId").append("}");
QString pathPrefix, pathSuffix, pathDelimiter; QString pathPrefix, pathSuffix, pathDelimiter;
QString pathStyle = ""; QString pathStyle = "";
if(pathStyle == "") if (pathStyle == "")
pathStyle = "simple"; pathStyle = "simple";
pathPrefix = getParamStylePrefix(pathStyle); pathPrefix = getParamStylePrefix(pathStyle);
pathSuffix = getParamStyleSuffix(pathStyle); pathSuffix = getParamStyleSuffix(pathStyle);
@ -745,21 +733,23 @@ void PFXPetApi::updatePetWithForm(const qint64 &pet_id, const ::test_namespace::
worker->setWorkingDirectory(_workingDirectory); worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "POST"); PFXHttpRequestInput input(fullPath, "POST");
if(name.hasValue()) if (name.hasValue())
{ {
input.add_var("name", ::test_namespace::toStringValue(name.value())); input.add_var("name", ::test_namespace::toStringValue(name.value()));
} }
if(status.hasValue()) if (status.hasValue())
{ {
input.add_var("status", ::test_namespace::toStringValue(status.value())); input.add_var("status", ::test_namespace::toStringValue(status.value()));
} }
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); } for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXPetApi::updatePetWithFormCallback); connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXPetApi::updatePetWithFormCallback);
connect(this, &PFXPetApi::abortRequestsSignal, worker, &QObject::deleteLater); connect(this, &PFXPetApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, [this](){ connect(worker, &QObject::destroyed, this, [this]() {
if(findChildren<PFXHttpRequestWorker*>().count() == 0){ if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted(); emit allPendingRequestsCompleted();
} }
}); });
@ -768,15 +758,11 @@ void PFXPetApi::updatePetWithForm(const qint64 &pet_id, const ::test_namespace::
} }
void PFXPetApi::updatePetWithFormCallback(PFXHttpRequestWorker *worker) { void PFXPetApi::updatePetWithFormCallback(PFXHttpRequestWorker *worker) {
QString msg;
QString error_str = worker->error_str; QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type; QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type == QNetworkReply::NoError) { if (worker->error_type != QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length()); error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
} else {
msg = "Error: " + worker->error_str;
error_str = QString("%1, %2").arg(worker->error_str).arg(QString(worker->response));
} }
worker->deleteLater(); worker->deleteLater();
@ -798,7 +784,7 @@ void PFXPetApi::uploadFile(const qint64 &pet_id, const ::test_namespace::Optiona
pet_idPathParam.append("petId").append("}"); pet_idPathParam.append("petId").append("}");
QString pathPrefix, pathSuffix, pathDelimiter; QString pathPrefix, pathSuffix, pathDelimiter;
QString pathStyle = ""; QString pathStyle = "";
if(pathStyle == "") if (pathStyle == "")
pathStyle = "simple"; pathStyle = "simple";
pathPrefix = getParamStylePrefix(pathStyle); pathPrefix = getParamStylePrefix(pathStyle);
pathSuffix = getParamStyleSuffix(pathStyle); pathSuffix = getParamStyleSuffix(pathStyle);
@ -811,21 +797,23 @@ void PFXPetApi::uploadFile(const qint64 &pet_id, const ::test_namespace::Optiona
worker->setWorkingDirectory(_workingDirectory); worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "POST"); PFXHttpRequestInput input(fullPath, "POST");
if(additional_metadata.hasValue()) if (additional_metadata.hasValue())
{ {
input.add_var("additionalMetadata", ::test_namespace::toStringValue(additional_metadata.value())); input.add_var("additionalMetadata", ::test_namespace::toStringValue(additional_metadata.value()));
} }
if(file.hasValue()) if (file.hasValue())
{ {
input.add_file("file", file.value().local_filename, file.value().request_filename, file.value().mime_type); input.add_file("file", file.value().local_filename, file.value().request_filename, file.value().mime_type);
} }
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); } for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXPetApi::uploadFileCallback); connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXPetApi::uploadFileCallback);
connect(this, &PFXPetApi::abortRequestsSignal, worker, &QObject::deleteLater); connect(this, &PFXPetApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, [this](){ connect(worker, &QObject::destroyed, this, [this]() {
if(findChildren<PFXHttpRequestWorker*>().count() == 0){ if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted(); emit allPendingRequestsCompleted();
} }
}); });
@ -834,15 +822,11 @@ void PFXPetApi::uploadFile(const qint64 &pet_id, const ::test_namespace::Optiona
} }
void PFXPetApi::uploadFileCallback(PFXHttpRequestWorker *worker) { void PFXPetApi::uploadFileCallback(PFXHttpRequestWorker *worker) {
QString msg;
QString error_str = worker->error_str; QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type; QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type == QNetworkReply::NoError) { if (worker->error_type != QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length()); error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
} else {
msg = "Error: " + worker->error_str;
error_str = QString("%1, %2").arg(worker->error_str).arg(QString(worker->response));
} }
PFXApiResponse output(QString(worker->response)); PFXApiResponse output(QString(worker->response));
worker->deleteLater(); worker->deleteLater();

View File

@ -53,9 +53,9 @@ public:
void enableRequestCompression(); void enableRequestCompression();
void enableResponseCompression(); void enableResponseCompression();
void abortRequests(); void abortRequests();
QString getParamStylePrefix(QString style); QString getParamStylePrefix(const QString &style);
QString getParamStyleSuffix(QString style); QString getParamStyleSuffix(const QString &style);
QString getParamStyleDelimiter(QString style, QString name, bool isExplode); QString getParamStyleDelimiter(const QString &style, const QString &name, bool isExplode);
/** /**
* @param[in] body PFXPet [required] * @param[in] body PFXPet [required]
@ -113,9 +113,9 @@ private:
int _timeOut; int _timeOut;
QString _workingDirectory; QString _workingDirectory;
QNetworkAccessManager* _manager; QNetworkAccessManager* _manager;
QMap<QString, QString> defaultHeaders; QMap<QString, QString> _defaultHeaders;
bool isResponseCompressionEnabled; bool _isResponseCompressionEnabled;
bool isRequestCompressionEnabled; bool _isRequestCompressionEnabled;
void addPetCallback(PFXHttpRequestWorker *worker); void addPetCallback(PFXHttpRequestWorker *worker);
void deletePetCallback(PFXHttpRequestWorker *worker); void deletePetCallback(PFXHttpRequestWorker *worker);

View File

@ -19,19 +19,18 @@ namespace test_namespace {
PFXStoreApi::PFXStoreApi(const int timeOut) PFXStoreApi::PFXStoreApi(const int timeOut)
: _timeOut(timeOut), : _timeOut(timeOut),
_manager(nullptr), _manager(nullptr),
isResponseCompressionEnabled(false), _isResponseCompressionEnabled(false),
isRequestCompressionEnabled(false) { _isRequestCompressionEnabled(false) {
initializeServerConfigs(); initializeServerConfigs();
} }
PFXStoreApi::~PFXStoreApi() { PFXStoreApi::~PFXStoreApi() {
} }
void PFXStoreApi::initializeServerConfigs(){ void PFXStoreApi::initializeServerConfigs() {
//Default server //Default server
QList<PFXServerConfiguration> defaultConf = QList<PFXServerConfiguration>(); QList<PFXServerConfiguration> defaultConf = QList<PFXServerConfiguration>();
//varying endpoint server //varying endpoint server
QList<PFXServerConfiguration> serverConf = QList<PFXServerConfiguration>();
defaultConf.append(PFXServerConfiguration( defaultConf.append(PFXServerConfiguration(
QUrl("http://petstore.swagger.io/v2"), QUrl("http://petstore.swagger.io/v2"),
"No description provided", "No description provided",
@ -50,23 +49,24 @@ void PFXStoreApi::initializeServerConfigs(){
* returns 0 on success and -1, -2 or -3 on failure. * 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 * -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 PFXStoreApi::setDefaultServerValue(int serverIndex, const QString &operation, const QString &variable, const QString &value){ int PFXStoreApi::setDefaultServerValue(int serverIndex, const QString &operation, const QString &variable, const QString &value) {
auto it = _serverConfigs.find(operation); auto it = _serverConfigs.find(operation);
if(it != _serverConfigs.end() && serverIndex < it.value().size() ){ if (it != _serverConfigs.end() && serverIndex < it.value().size()) {
return _serverConfigs[operation][serverIndex].setDefaultValue(variable,value); return _serverConfigs[operation][serverIndex].setDefaultValue(variable,value);
} }
return -3; return -3;
} }
void PFXStoreApi::setServerIndex(const QString &operation, int serverIndex){ void PFXStoreApi::setServerIndex(const QString &operation, int serverIndex) {
if(_serverIndices.contains(operation) && serverIndex < _serverConfigs.find(operation).value().size() ) if (_serverIndices.contains(operation) && serverIndex < _serverConfigs.find(operation).value().size()) {
_serverIndices[operation] = serverIndex; _serverIndices[operation] = serverIndex;
}
} }
void PFXStoreApi::setApiKey(const QString &apiKeyName, const QString &apiKey){ void PFXStoreApi::setApiKey(const QString &apiKeyName, const QString &apiKey) {
_apiKeys.insert(apiKeyName,apiKey); _apiKeys.insert(apiKeyName,apiKey);
} }
void PFXStoreApi::setBearerToken(const QString &token){ void PFXStoreApi::setBearerToken(const QString &token) {
_bearerToken = token; _bearerToken = token;
} }
@ -99,14 +99,14 @@ void PFXStoreApi::setNetworkAccessManager(QNetworkAccessManager* manager) {
* @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template. * @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 * returns the index of the new server config on success and -1 if the operation is not found
*/ */
int PFXStoreApi::addServerConfiguration(const QString &operation, const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables){ int PFXStoreApi::addServerConfiguration(const QString &operation, const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables) {
if(_serverConfigs.contains(operation)){ if (_serverConfigs.contains(operation)) {
_serverConfigs[operation].append(PFXServerConfiguration( _serverConfigs[operation].append(PFXServerConfiguration(
url, url,
description, description,
variables)); variables));
return _serverConfigs[operation].size()-1; return _serverConfigs[operation].size()-1;
}else{ } else {
return -1; return -1;
} }
} }
@ -117,9 +117,9 @@ int PFXStoreApi::addServerConfiguration(const QString &operation, const QUrl &ur
* @param description A String that describes 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. * @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template.
*/ */
void PFXStoreApi::setNewServerForAllOperations(const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables){ void PFXStoreApi::setNewServerForAllOperations(const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables) {
for(auto e : _serverIndices.keys()){ for (auto keyIt = _serverIndices.keyBegin(); keyIt != _serverIndices.keyEnd(); keyIt++) {
setServerIndex(e, addServerConfiguration(e, url, description, variables)); setServerIndex(*keyIt, addServerConfiguration(*keyIt, url, description, variables));
} }
} }
@ -129,85 +129,85 @@ void PFXStoreApi::setNewServerForAllOperations(const QUrl &url, const QString &d
* @param description A String that describes 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. * @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template.
*/ */
void PFXStoreApi::setNewServer(const QString &operation, const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables){ void PFXStoreApi::setNewServer(const QString &operation, const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables) {
setServerIndex(operation, addServerConfiguration(operation, url, description, variables)); setServerIndex(operation, addServerConfiguration(operation, url, description, variables));
} }
void PFXStoreApi::addHeaders(const QString &key, const QString &value) { void PFXStoreApi::addHeaders(const QString &key, const QString &value) {
defaultHeaders.insert(key, value); _defaultHeaders.insert(key, value);
} }
void PFXStoreApi::enableRequestCompression() { void PFXStoreApi::enableRequestCompression() {
isRequestCompressionEnabled = true; _isRequestCompressionEnabled = true;
} }
void PFXStoreApi::enableResponseCompression() { void PFXStoreApi::enableResponseCompression() {
isResponseCompressionEnabled = true; _isResponseCompressionEnabled = true;
} }
void PFXStoreApi::abortRequests(){ void PFXStoreApi::abortRequests() {
emit abortRequestsSignal(); emit abortRequestsSignal();
} }
QString PFXStoreApi::getParamStylePrefix(QString style){ QString PFXStoreApi::getParamStylePrefix(const QString &style) {
if(style == "matrix"){ if (style == "matrix") {
return ";"; return ";";
}else if(style == "label"){ } else if (style == "label") {
return "."; return ".";
}else if(style == "form"){ } else if (style == "form") {
return "&"; return "&";
}else if(style == "simple"){ } else if (style == "simple") {
return ""; return "";
}else if(style == "spaceDelimited"){ } else if (style == "spaceDelimited") {
return "&"; return "&";
}else if(style == "pipeDelimited"){ } else if (style == "pipeDelimited") {
return "&"; return "&";
}else{ } else {
return "none"; return "none";
} }
} }
QString PFXStoreApi::getParamStyleSuffix(QString style){ QString PFXStoreApi::getParamStyleSuffix(const QString &style) {
if(style == "matrix"){ if (style == "matrix") {
return "="; return "=";
}else if(style == "label"){ } else if (style == "label") {
return ""; return "";
}else if(style == "form"){ } else if (style == "form") {
return "="; return "=";
}else if(style == "simple"){ } else if (style == "simple") {
return ""; return "";
}else if(style == "spaceDelimited"){ } else if (style == "spaceDelimited") {
return "="; return "=";
}else if(style == "pipeDelimited"){ } else if (style == "pipeDelimited") {
return "="; return "=";
}else{ } else {
return "none"; return "none";
} }
} }
QString PFXStoreApi::getParamStyleDelimiter(QString style, QString name, bool isExplode){ QString PFXStoreApi::getParamStyleDelimiter(const QString &style, const QString &name, bool isExplode) {
if(style == "matrix"){ if (style == "matrix") {
return (isExplode) ? ";" + name + "=" : ","; return (isExplode) ? ";" + name + "=" : ",";
}else if(style == "label"){ } else if (style == "label") {
return (isExplode) ? "." : ","; return (isExplode) ? "." : ",";
}else if(style == "form"){ } else if (style == "form") {
return (isExplode) ? "&" + name + "=" : ","; return (isExplode) ? "&" + name + "=" : ",";
}else if(style == "simple"){ } else if (style == "simple") {
return ","; return ",";
}else if(style == "spaceDelimited"){ } else if (style == "spaceDelimited") {
return (isExplode) ? "&" + name + "=" : " "; return (isExplode) ? "&" + name + "=" : " ";
}else if(style == "pipeDelimited"){ } else if (style == "pipeDelimited") {
return (isExplode) ? "&" + name + "=" : "|"; return (isExplode) ? "&" + name + "=" : "|";
}else if(style == "deepObject"){ } else if (style == "deepObject") {
return (isExplode) ? "&" : "none"; return (isExplode) ? "&" : "none";
}else { } else {
return "none"; return "none";
} }
} }
@ -221,7 +221,7 @@ void PFXStoreApi::deleteOrder(const QString &order_id) {
order_idPathParam.append("orderId").append("}"); order_idPathParam.append("orderId").append("}");
QString pathPrefix, pathSuffix, pathDelimiter; QString pathPrefix, pathSuffix, pathDelimiter;
QString pathStyle = ""; QString pathStyle = "";
if(pathStyle == "") if (pathStyle == "")
pathStyle = "simple"; pathStyle = "simple";
pathPrefix = getParamStylePrefix(pathStyle); pathPrefix = getParamStylePrefix(pathStyle);
pathSuffix = getParamStyleSuffix(pathStyle); pathSuffix = getParamStyleSuffix(pathStyle);
@ -235,12 +235,14 @@ void PFXStoreApi::deleteOrder(const QString &order_id) {
PFXHttpRequestInput input(fullPath, "DELETE"); PFXHttpRequestInput input(fullPath, "DELETE");
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); } for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXStoreApi::deleteOrderCallback); connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXStoreApi::deleteOrderCallback);
connect(this, &PFXStoreApi::abortRequestsSignal, worker, &QObject::deleteLater); connect(this, &PFXStoreApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, [this](){ connect(worker, &QObject::destroyed, this, [this]() {
if(findChildren<PFXHttpRequestWorker*>().count() == 0){ if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted(); emit allPendingRequestsCompleted();
} }
}); });
@ -249,15 +251,11 @@ void PFXStoreApi::deleteOrder(const QString &order_id) {
} }
void PFXStoreApi::deleteOrderCallback(PFXHttpRequestWorker *worker) { void PFXStoreApi::deleteOrderCallback(PFXHttpRequestWorker *worker) {
QString msg;
QString error_str = worker->error_str; QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type; QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type == QNetworkReply::NoError) { if (worker->error_type != QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length()); error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
} else {
msg = "Error: " + worker->error_str;
error_str = QString("%1, %2").arg(worker->error_str).arg(QString(worker->response));
} }
worker->deleteLater(); worker->deleteLater();
@ -273,7 +271,7 @@ void PFXStoreApi::deleteOrderCallback(PFXHttpRequestWorker *worker) {
void PFXStoreApi::getInventory() { void PFXStoreApi::getInventory() {
QString fullPath = QString(_serverConfigs["getInventory"][_serverIndices.value("getInventory")].URL()+"/store/inventory"); QString fullPath = QString(_serverConfigs["getInventory"][_serverIndices.value("getInventory")].URL()+"/store/inventory");
if(_apiKeys.contains("api_key")){ if (_apiKeys.contains("api_key")) {
addHeaders("api_key",_apiKeys.find("api_key").value()); addHeaders("api_key",_apiKeys.find("api_key").value());
} }
@ -283,12 +281,14 @@ void PFXStoreApi::getInventory() {
PFXHttpRequestInput input(fullPath, "GET"); PFXHttpRequestInput input(fullPath, "GET");
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); } for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXStoreApi::getInventoryCallback); connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXStoreApi::getInventoryCallback);
connect(this, &PFXStoreApi::abortRequestsSignal, worker, &QObject::deleteLater); connect(this, &PFXStoreApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, [this](){ connect(worker, &QObject::destroyed, this, [this]() {
if(findChildren<PFXHttpRequestWorker*>().count() == 0){ if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted(); emit allPendingRequestsCompleted();
} }
}); });
@ -297,15 +297,11 @@ void PFXStoreApi::getInventory() {
} }
void PFXStoreApi::getInventoryCallback(PFXHttpRequestWorker *worker) { void PFXStoreApi::getInventoryCallback(PFXHttpRequestWorker *worker) {
QString msg;
QString error_str = worker->error_str; QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type; QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type == QNetworkReply::NoError) { if (worker->error_type != QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length()); error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
} else {
msg = "Error: " + worker->error_str;
error_str = QString("%1, %2").arg(worker->error_str).arg(QString(worker->response));
} }
QMap<QString, qint32> output; QMap<QString, qint32> output;
QString json(worker->response); QString json(worker->response);
@ -337,7 +333,7 @@ void PFXStoreApi::getOrderById(const qint64 &order_id) {
order_idPathParam.append("orderId").append("}"); order_idPathParam.append("orderId").append("}");
QString pathPrefix, pathSuffix, pathDelimiter; QString pathPrefix, pathSuffix, pathDelimiter;
QString pathStyle = ""; QString pathStyle = "";
if(pathStyle == "") if (pathStyle == "")
pathStyle = "simple"; pathStyle = "simple";
pathPrefix = getParamStylePrefix(pathStyle); pathPrefix = getParamStylePrefix(pathStyle);
pathSuffix = getParamStyleSuffix(pathStyle); pathSuffix = getParamStyleSuffix(pathStyle);
@ -351,12 +347,14 @@ void PFXStoreApi::getOrderById(const qint64 &order_id) {
PFXHttpRequestInput input(fullPath, "GET"); PFXHttpRequestInput input(fullPath, "GET");
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); } for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXStoreApi::getOrderByIdCallback); connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXStoreApi::getOrderByIdCallback);
connect(this, &PFXStoreApi::abortRequestsSignal, worker, &QObject::deleteLater); connect(this, &PFXStoreApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, [this](){ connect(worker, &QObject::destroyed, this, [this]() {
if(findChildren<PFXHttpRequestWorker*>().count() == 0){ if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted(); emit allPendingRequestsCompleted();
} }
}); });
@ -365,15 +363,11 @@ void PFXStoreApi::getOrderById(const qint64 &order_id) {
} }
void PFXStoreApi::getOrderByIdCallback(PFXHttpRequestWorker *worker) { void PFXStoreApi::getOrderByIdCallback(PFXHttpRequestWorker *worker) {
QString msg;
QString error_str = worker->error_str; QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type; QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type == QNetworkReply::NoError) { if (worker->error_type != QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length()); error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
} else {
msg = "Error: " + worker->error_str;
error_str = QString("%1, %2").arg(worker->error_str).arg(QString(worker->response));
} }
PFXOrder output(QString(worker->response)); PFXOrder output(QString(worker->response));
worker->deleteLater(); worker->deleteLater();
@ -400,12 +394,14 @@ void PFXStoreApi::placeOrder(const PFXOrder &body) {
QByteArray output = body.asJson().toUtf8(); QByteArray output = body.asJson().toUtf8();
input.request_body.append(output); input.request_body.append(output);
} }
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); } for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXStoreApi::placeOrderCallback); connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXStoreApi::placeOrderCallback);
connect(this, &PFXStoreApi::abortRequestsSignal, worker, &QObject::deleteLater); connect(this, &PFXStoreApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, [this](){ connect(worker, &QObject::destroyed, this, [this]() {
if(findChildren<PFXHttpRequestWorker*>().count() == 0){ if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted(); emit allPendingRequestsCompleted();
} }
}); });
@ -414,15 +410,11 @@ void PFXStoreApi::placeOrder(const PFXOrder &body) {
} }
void PFXStoreApi::placeOrderCallback(PFXHttpRequestWorker *worker) { void PFXStoreApi::placeOrderCallback(PFXHttpRequestWorker *worker) {
QString msg;
QString error_str = worker->error_str; QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type; QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type == QNetworkReply::NoError) { if (worker->error_type != QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length()); error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
} else {
msg = "Error: " + worker->error_str;
error_str = QString("%1, %2").arg(worker->error_str).arg(QString(worker->response));
} }
PFXOrder output(QString(worker->response)); PFXOrder output(QString(worker->response));
worker->deleteLater(); worker->deleteLater();

View File

@ -52,9 +52,9 @@ public:
void enableRequestCompression(); void enableRequestCompression();
void enableResponseCompression(); void enableResponseCompression();
void abortRequests(); void abortRequests();
QString getParamStylePrefix(QString style); QString getParamStylePrefix(const QString &style);
QString getParamStyleSuffix(QString style); QString getParamStyleSuffix(const QString &style);
QString getParamStyleDelimiter(QString style, QString name, bool isExplode); QString getParamStyleDelimiter(const QString &style, const QString &name, bool isExplode);
/** /**
* @param[in] order_id QString [required] * @param[in] order_id QString [required]
@ -85,9 +85,9 @@ private:
int _timeOut; int _timeOut;
QString _workingDirectory; QString _workingDirectory;
QNetworkAccessManager* _manager; QNetworkAccessManager* _manager;
QMap<QString, QString> defaultHeaders; QMap<QString, QString> _defaultHeaders;
bool isResponseCompressionEnabled; bool _isResponseCompressionEnabled;
bool isRequestCompressionEnabled; bool _isRequestCompressionEnabled;
void deleteOrderCallback(PFXHttpRequestWorker *worker); void deleteOrderCallback(PFXHttpRequestWorker *worker);
void getInventoryCallback(PFXHttpRequestWorker *worker); void getInventoryCallback(PFXHttpRequestWorker *worker);

View File

@ -19,19 +19,18 @@ namespace test_namespace {
PFXUserApi::PFXUserApi(const int timeOut) PFXUserApi::PFXUserApi(const int timeOut)
: _timeOut(timeOut), : _timeOut(timeOut),
_manager(nullptr), _manager(nullptr),
isResponseCompressionEnabled(false), _isResponseCompressionEnabled(false),
isRequestCompressionEnabled(false) { _isRequestCompressionEnabled(false) {
initializeServerConfigs(); initializeServerConfigs();
} }
PFXUserApi::~PFXUserApi() { PFXUserApi::~PFXUserApi() {
} }
void PFXUserApi::initializeServerConfigs(){ void PFXUserApi::initializeServerConfigs() {
//Default server //Default server
QList<PFXServerConfiguration> defaultConf = QList<PFXServerConfiguration>(); QList<PFXServerConfiguration> defaultConf = QList<PFXServerConfiguration>();
//varying endpoint server //varying endpoint server
QList<PFXServerConfiguration> serverConf = QList<PFXServerConfiguration>();
defaultConf.append(PFXServerConfiguration( defaultConf.append(PFXServerConfiguration(
QUrl("http://petstore.swagger.io/v2"), QUrl("http://petstore.swagger.io/v2"),
"No description provided", "No description provided",
@ -58,23 +57,24 @@ void PFXUserApi::initializeServerConfigs(){
* returns 0 on success and -1, -2 or -3 on failure. * 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 * -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 PFXUserApi::setDefaultServerValue(int serverIndex, const QString &operation, const QString &variable, const QString &value){ int PFXUserApi::setDefaultServerValue(int serverIndex, const QString &operation, const QString &variable, const QString &value) {
auto it = _serverConfigs.find(operation); auto it = _serverConfigs.find(operation);
if(it != _serverConfigs.end() && serverIndex < it.value().size() ){ if (it != _serverConfigs.end() && serverIndex < it.value().size()) {
return _serverConfigs[operation][serverIndex].setDefaultValue(variable,value); return _serverConfigs[operation][serverIndex].setDefaultValue(variable,value);
} }
return -3; return -3;
} }
void PFXUserApi::setServerIndex(const QString &operation, int serverIndex){ void PFXUserApi::setServerIndex(const QString &operation, int serverIndex) {
if(_serverIndices.contains(operation) && serverIndex < _serverConfigs.find(operation).value().size() ) if (_serverIndices.contains(operation) && serverIndex < _serverConfigs.find(operation).value().size()) {
_serverIndices[operation] = serverIndex; _serverIndices[operation] = serverIndex;
}
} }
void PFXUserApi::setApiKey(const QString &apiKeyName, const QString &apiKey){ void PFXUserApi::setApiKey(const QString &apiKeyName, const QString &apiKey) {
_apiKeys.insert(apiKeyName,apiKey); _apiKeys.insert(apiKeyName,apiKey);
} }
void PFXUserApi::setBearerToken(const QString &token){ void PFXUserApi::setBearerToken(const QString &token) {
_bearerToken = token; _bearerToken = token;
} }
@ -107,14 +107,14 @@ void PFXUserApi::setNetworkAccessManager(QNetworkAccessManager* manager) {
* @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template. * @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 * returns the index of the new server config on success and -1 if the operation is not found
*/ */
int PFXUserApi::addServerConfiguration(const QString &operation, const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables){ int PFXUserApi::addServerConfiguration(const QString &operation, const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables) {
if(_serverConfigs.contains(operation)){ if (_serverConfigs.contains(operation)) {
_serverConfigs[operation].append(PFXServerConfiguration( _serverConfigs[operation].append(PFXServerConfiguration(
url, url,
description, description,
variables)); variables));
return _serverConfigs[operation].size()-1; return _serverConfigs[operation].size()-1;
}else{ } else {
return -1; return -1;
} }
} }
@ -125,9 +125,9 @@ int PFXUserApi::addServerConfiguration(const QString &operation, const QUrl &url
* @param description A String that describes 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. * @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template.
*/ */
void PFXUserApi::setNewServerForAllOperations(const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables){ void PFXUserApi::setNewServerForAllOperations(const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables) {
for(auto e : _serverIndices.keys()){ for (auto keyIt = _serverIndices.keyBegin(); keyIt != _serverIndices.keyEnd(); keyIt++) {
setServerIndex(e, addServerConfiguration(e, url, description, variables)); setServerIndex(*keyIt, addServerConfiguration(*keyIt, url, description, variables));
} }
} }
@ -137,85 +137,85 @@ void PFXUserApi::setNewServerForAllOperations(const QUrl &url, const QString &de
* @param description A String that describes 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. * @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template.
*/ */
void PFXUserApi::setNewServer(const QString &operation, const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables){ void PFXUserApi::setNewServer(const QString &operation, const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables) {
setServerIndex(operation, addServerConfiguration(operation, url, description, variables)); setServerIndex(operation, addServerConfiguration(operation, url, description, variables));
} }
void PFXUserApi::addHeaders(const QString &key, const QString &value) { void PFXUserApi::addHeaders(const QString &key, const QString &value) {
defaultHeaders.insert(key, value); _defaultHeaders.insert(key, value);
} }
void PFXUserApi::enableRequestCompression() { void PFXUserApi::enableRequestCompression() {
isRequestCompressionEnabled = true; _isRequestCompressionEnabled = true;
} }
void PFXUserApi::enableResponseCompression() { void PFXUserApi::enableResponseCompression() {
isResponseCompressionEnabled = true; _isResponseCompressionEnabled = true;
} }
void PFXUserApi::abortRequests(){ void PFXUserApi::abortRequests() {
emit abortRequestsSignal(); emit abortRequestsSignal();
} }
QString PFXUserApi::getParamStylePrefix(QString style){ QString PFXUserApi::getParamStylePrefix(const QString &style) {
if(style == "matrix"){ if (style == "matrix") {
return ";"; return ";";
}else if(style == "label"){ } else if (style == "label") {
return "."; return ".";
}else if(style == "form"){ } else if (style == "form") {
return "&"; return "&";
}else if(style == "simple"){ } else if (style == "simple") {
return ""; return "";
}else if(style == "spaceDelimited"){ } else if (style == "spaceDelimited") {
return "&"; return "&";
}else if(style == "pipeDelimited"){ } else if (style == "pipeDelimited") {
return "&"; return "&";
}else{ } else {
return "none"; return "none";
} }
} }
QString PFXUserApi::getParamStyleSuffix(QString style){ QString PFXUserApi::getParamStyleSuffix(const QString &style) {
if(style == "matrix"){ if (style == "matrix") {
return "="; return "=";
}else if(style == "label"){ } else if (style == "label") {
return ""; return "";
}else if(style == "form"){ } else if (style == "form") {
return "="; return "=";
}else if(style == "simple"){ } else if (style == "simple") {
return ""; return "";
}else if(style == "spaceDelimited"){ } else if (style == "spaceDelimited") {
return "="; return "=";
}else if(style == "pipeDelimited"){ } else if (style == "pipeDelimited") {
return "="; return "=";
}else{ } else {
return "none"; return "none";
} }
} }
QString PFXUserApi::getParamStyleDelimiter(QString style, QString name, bool isExplode){ QString PFXUserApi::getParamStyleDelimiter(const QString &style, const QString &name, bool isExplode) {
if(style == "matrix"){ if (style == "matrix") {
return (isExplode) ? ";" + name + "=" : ","; return (isExplode) ? ";" + name + "=" : ",";
}else if(style == "label"){ } else if (style == "label") {
return (isExplode) ? "." : ","; return (isExplode) ? "." : ",";
}else if(style == "form"){ } else if (style == "form") {
return (isExplode) ? "&" + name + "=" : ","; return (isExplode) ? "&" + name + "=" : ",";
}else if(style == "simple"){ } else if (style == "simple") {
return ","; return ",";
}else if(style == "spaceDelimited"){ } else if (style == "spaceDelimited") {
return (isExplode) ? "&" + name + "=" : " "; return (isExplode) ? "&" + name + "=" : " ";
}else if(style == "pipeDelimited"){ } else if (style == "pipeDelimited") {
return (isExplode) ? "&" + name + "=" : "|"; return (isExplode) ? "&" + name + "=" : "|";
}else if(style == "deepObject"){ } else if (style == "deepObject") {
return (isExplode) ? "&" : "none"; return (isExplode) ? "&" : "none";
}else { } else {
return "none"; return "none";
} }
} }
@ -233,12 +233,14 @@ void PFXUserApi::createUser(const PFXUser &body) {
QByteArray output = body.asJson().toUtf8(); QByteArray output = body.asJson().toUtf8();
input.request_body.append(output); input.request_body.append(output);
} }
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); } for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXUserApi::createUserCallback); connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXUserApi::createUserCallback);
connect(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater); connect(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, [this](){ connect(worker, &QObject::destroyed, this, [this]() {
if(findChildren<PFXHttpRequestWorker*>().count() == 0){ if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted(); emit allPendingRequestsCompleted();
} }
}); });
@ -247,15 +249,11 @@ void PFXUserApi::createUser(const PFXUser &body) {
} }
void PFXUserApi::createUserCallback(PFXHttpRequestWorker *worker) { void PFXUserApi::createUserCallback(PFXHttpRequestWorker *worker) {
QString msg;
QString error_str = worker->error_str; QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type; QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type == QNetworkReply::NoError) { if (worker->error_type != QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length()); error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
} else {
msg = "Error: " + worker->error_str;
error_str = QString("%1, %2").arg(worker->error_str).arg(QString(worker->response));
} }
worker->deleteLater(); worker->deleteLater();
@ -281,12 +279,14 @@ void PFXUserApi::createUsersWithArrayInput(const QList<PFXUser> &body) {
QByteArray bytes = doc.toJson(); QByteArray bytes = doc.toJson();
input.request_body.append(bytes); input.request_body.append(bytes);
} }
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); } for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXUserApi::createUsersWithArrayInputCallback); connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXUserApi::createUsersWithArrayInputCallback);
connect(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater); connect(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, [this](){ connect(worker, &QObject::destroyed, this, [this]() {
if(findChildren<PFXHttpRequestWorker*>().count() == 0){ if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted(); emit allPendingRequestsCompleted();
} }
}); });
@ -295,15 +295,11 @@ void PFXUserApi::createUsersWithArrayInput(const QList<PFXUser> &body) {
} }
void PFXUserApi::createUsersWithArrayInputCallback(PFXHttpRequestWorker *worker) { void PFXUserApi::createUsersWithArrayInputCallback(PFXHttpRequestWorker *worker) {
QString msg;
QString error_str = worker->error_str; QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type; QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type == QNetworkReply::NoError) { if (worker->error_type != QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length()); error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
} else {
msg = "Error: " + worker->error_str;
error_str = QString("%1, %2").arg(worker->error_str).arg(QString(worker->response));
} }
worker->deleteLater(); worker->deleteLater();
@ -329,12 +325,14 @@ void PFXUserApi::createUsersWithListInput(const QList<PFXUser> &body) {
QByteArray bytes = doc.toJson(); QByteArray bytes = doc.toJson();
input.request_body.append(bytes); input.request_body.append(bytes);
} }
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); } for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXUserApi::createUsersWithListInputCallback); connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXUserApi::createUsersWithListInputCallback);
connect(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater); connect(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, [this](){ connect(worker, &QObject::destroyed, this, [this]() {
if(findChildren<PFXHttpRequestWorker*>().count() == 0){ if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted(); emit allPendingRequestsCompleted();
} }
}); });
@ -343,15 +341,11 @@ void PFXUserApi::createUsersWithListInput(const QList<PFXUser> &body) {
} }
void PFXUserApi::createUsersWithListInputCallback(PFXHttpRequestWorker *worker) { void PFXUserApi::createUsersWithListInputCallback(PFXHttpRequestWorker *worker) {
QString msg;
QString error_str = worker->error_str; QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type; QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type == QNetworkReply::NoError) { if (worker->error_type != QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length()); error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
} else {
msg = "Error: " + worker->error_str;
error_str = QString("%1, %2").arg(worker->error_str).arg(QString(worker->response));
} }
worker->deleteLater(); worker->deleteLater();
@ -373,7 +367,7 @@ void PFXUserApi::deleteUser(const QString &username) {
usernamePathParam.append("username").append("}"); usernamePathParam.append("username").append("}");
QString pathPrefix, pathSuffix, pathDelimiter; QString pathPrefix, pathSuffix, pathDelimiter;
QString pathStyle = ""; QString pathStyle = "";
if(pathStyle == "") if (pathStyle == "")
pathStyle = "simple"; pathStyle = "simple";
pathPrefix = getParamStylePrefix(pathStyle); pathPrefix = getParamStylePrefix(pathStyle);
pathSuffix = getParamStyleSuffix(pathStyle); pathSuffix = getParamStyleSuffix(pathStyle);
@ -387,12 +381,14 @@ void PFXUserApi::deleteUser(const QString &username) {
PFXHttpRequestInput input(fullPath, "DELETE"); PFXHttpRequestInput input(fullPath, "DELETE");
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); } for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXUserApi::deleteUserCallback); connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXUserApi::deleteUserCallback);
connect(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater); connect(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, [this](){ connect(worker, &QObject::destroyed, this, [this]() {
if(findChildren<PFXHttpRequestWorker*>().count() == 0){ if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted(); emit allPendingRequestsCompleted();
} }
}); });
@ -401,15 +397,11 @@ void PFXUserApi::deleteUser(const QString &username) {
} }
void PFXUserApi::deleteUserCallback(PFXHttpRequestWorker *worker) { void PFXUserApi::deleteUserCallback(PFXHttpRequestWorker *worker) {
QString msg;
QString error_str = worker->error_str; QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type; QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type == QNetworkReply::NoError) { if (worker->error_type != QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length()); error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
} else {
msg = "Error: " + worker->error_str;
error_str = QString("%1, %2").arg(worker->error_str).arg(QString(worker->response));
} }
worker->deleteLater(); worker->deleteLater();
@ -431,7 +423,7 @@ void PFXUserApi::getUserByName(const QString &username) {
usernamePathParam.append("username").append("}"); usernamePathParam.append("username").append("}");
QString pathPrefix, pathSuffix, pathDelimiter; QString pathPrefix, pathSuffix, pathDelimiter;
QString pathStyle = ""; QString pathStyle = "";
if(pathStyle == "") if (pathStyle == "")
pathStyle = "simple"; pathStyle = "simple";
pathPrefix = getParamStylePrefix(pathStyle); pathPrefix = getParamStylePrefix(pathStyle);
pathSuffix = getParamStyleSuffix(pathStyle); pathSuffix = getParamStyleSuffix(pathStyle);
@ -445,12 +437,14 @@ void PFXUserApi::getUserByName(const QString &username) {
PFXHttpRequestInput input(fullPath, "GET"); PFXHttpRequestInput input(fullPath, "GET");
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); } for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXUserApi::getUserByNameCallback); connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXUserApi::getUserByNameCallback);
connect(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater); connect(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, [this](){ connect(worker, &QObject::destroyed, this, [this]() {
if(findChildren<PFXHttpRequestWorker*>().count() == 0){ if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted(); emit allPendingRequestsCompleted();
} }
}); });
@ -459,15 +453,11 @@ void PFXUserApi::getUserByName(const QString &username) {
} }
void PFXUserApi::getUserByNameCallback(PFXHttpRequestWorker *worker) { void PFXUserApi::getUserByNameCallback(PFXHttpRequestWorker *worker) {
QString msg;
QString error_str = worker->error_str; QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type; QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type == QNetworkReply::NoError) { if (worker->error_type != QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length()); error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
} else {
msg = "Error: " + worker->error_str;
error_str = QString("%1, %2").arg(worker->error_str).arg(QString(worker->response));
} }
PFXUser output(QString(worker->response)); PFXUser output(QString(worker->response));
worker->deleteLater(); worker->deleteLater();
@ -488,7 +478,7 @@ void PFXUserApi::loginUser(const QString &username, const QString &password) {
{ {
queryStyle = ""; queryStyle = "";
if(queryStyle == "") if (queryStyle == "")
queryStyle = "form"; queryStyle = "form";
queryPrefix = getParamStylePrefix(queryStyle); queryPrefix = getParamStylePrefix(queryStyle);
querySuffix = getParamStyleSuffix(queryStyle); querySuffix = getParamStyleSuffix(queryStyle);
@ -503,7 +493,7 @@ void PFXUserApi::loginUser(const QString &username, const QString &password) {
{ {
queryStyle = ""; queryStyle = "";
if(queryStyle == "") if (queryStyle == "")
queryStyle = "form"; queryStyle = "form";
queryPrefix = getParamStylePrefix(queryStyle); queryPrefix = getParamStylePrefix(queryStyle);
querySuffix = getParamStyleSuffix(queryStyle); querySuffix = getParamStyleSuffix(queryStyle);
@ -521,12 +511,14 @@ void PFXUserApi::loginUser(const QString &username, const QString &password) {
PFXHttpRequestInput input(fullPath, "GET"); PFXHttpRequestInput input(fullPath, "GET");
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); } for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXUserApi::loginUserCallback); connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXUserApi::loginUserCallback);
connect(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater); connect(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, [this](){ connect(worker, &QObject::destroyed, this, [this]() {
if(findChildren<PFXHttpRequestWorker*>().count() == 0){ if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted(); emit allPendingRequestsCompleted();
} }
}); });
@ -535,15 +527,11 @@ void PFXUserApi::loginUser(const QString &username, const QString &password) {
} }
void PFXUserApi::loginUserCallback(PFXHttpRequestWorker *worker) { void PFXUserApi::loginUserCallback(PFXHttpRequestWorker *worker) {
QString msg;
QString error_str = worker->error_str; QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type; QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type == QNetworkReply::NoError) { if (worker->error_type != QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length()); error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
} else {
msg = "Error: " + worker->error_str;
error_str = QString("%1, %2").arg(worker->error_str).arg(QString(worker->response));
} }
QString output; QString output;
::test_namespace::fromStringValue(QString(worker->response), output); ::test_namespace::fromStringValue(QString(worker->response), output);
@ -567,12 +555,14 @@ void PFXUserApi::logoutUser() {
PFXHttpRequestInput input(fullPath, "GET"); PFXHttpRequestInput input(fullPath, "GET");
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); } for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXUserApi::logoutUserCallback); connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXUserApi::logoutUserCallback);
connect(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater); connect(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, [this](){ connect(worker, &QObject::destroyed, this, [this]() {
if(findChildren<PFXHttpRequestWorker*>().count() == 0){ if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted(); emit allPendingRequestsCompleted();
} }
}); });
@ -581,15 +571,11 @@ void PFXUserApi::logoutUser() {
} }
void PFXUserApi::logoutUserCallback(PFXHttpRequestWorker *worker) { void PFXUserApi::logoutUserCallback(PFXHttpRequestWorker *worker) {
QString msg;
QString error_str = worker->error_str; QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type; QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type == QNetworkReply::NoError) { if (worker->error_type != QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length()); error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
} else {
msg = "Error: " + worker->error_str;
error_str = QString("%1, %2").arg(worker->error_str).arg(QString(worker->response));
} }
worker->deleteLater(); worker->deleteLater();
@ -611,7 +597,7 @@ void PFXUserApi::updateUser(const QString &username, const PFXUser &body) {
usernamePathParam.append("username").append("}"); usernamePathParam.append("username").append("}");
QString pathPrefix, pathSuffix, pathDelimiter; QString pathPrefix, pathSuffix, pathDelimiter;
QString pathStyle = ""; QString pathStyle = "";
if(pathStyle == "") if (pathStyle == "")
pathStyle = "simple"; pathStyle = "simple";
pathPrefix = getParamStylePrefix(pathStyle); pathPrefix = getParamStylePrefix(pathStyle);
pathSuffix = getParamStyleSuffix(pathStyle); pathSuffix = getParamStyleSuffix(pathStyle);
@ -629,12 +615,14 @@ void PFXUserApi::updateUser(const QString &username, const PFXUser &body) {
QByteArray output = body.asJson().toUtf8(); QByteArray output = body.asJson().toUtf8();
input.request_body.append(output); input.request_body.append(output);
} }
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); } for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXUserApi::updateUserCallback); connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXUserApi::updateUserCallback);
connect(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater); connect(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, [this](){ connect(worker, &QObject::destroyed, this, [this]() {
if(findChildren<PFXHttpRequestWorker*>().count() == 0){ if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted(); emit allPendingRequestsCompleted();
} }
}); });
@ -643,15 +631,11 @@ void PFXUserApi::updateUser(const QString &username, const PFXUser &body) {
} }
void PFXUserApi::updateUserCallback(PFXHttpRequestWorker *worker) { void PFXUserApi::updateUserCallback(PFXHttpRequestWorker *worker) {
QString msg;
QString error_str = worker->error_str; QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type; QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type == QNetworkReply::NoError) { if (worker->error_type != QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length()); error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
} else {
msg = "Error: " + worker->error_str;
error_str = QString("%1, %2").arg(worker->error_str).arg(QString(worker->response));
} }
worker->deleteLater(); worker->deleteLater();

View File

@ -52,9 +52,9 @@ public:
void enableRequestCompression(); void enableRequestCompression();
void enableResponseCompression(); void enableResponseCompression();
void abortRequests(); void abortRequests();
QString getParamStylePrefix(QString style); QString getParamStylePrefix(const QString &style);
QString getParamStyleSuffix(QString style); QString getParamStyleSuffix(const QString &style);
QString getParamStyleDelimiter(QString style, QString name, bool isExplode); QString getParamStyleDelimiter(const QString &style, const QString &name, bool isExplode);
/** /**
* @param[in] body PFXUser [required] * @param[in] body PFXUser [required]
@ -107,9 +107,9 @@ private:
int _timeOut; int _timeOut;
QString _workingDirectory; QString _workingDirectory;
QNetworkAccessManager* _manager; QNetworkAccessManager* _manager;
QMap<QString, QString> defaultHeaders; QMap<QString, QString> _defaultHeaders;
bool isResponseCompressionEnabled; bool _isResponseCompressionEnabled;
bool isRequestCompressionEnabled; bool _isRequestCompressionEnabled;
void createUserCallback(PFXHttpRequestWorker *worker); void createUserCallback(PFXHttpRequestWorker *worker);
void createUsersWithArrayInputCallback(PFXHttpRequestWorker *worker); void createUsersWithArrayInputCallback(PFXHttpRequestWorker *worker);