Revision control
/**
* Qt Quick Demo - RESTful API client
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* Do not edit the class manually.
*/
#include "OAIDefaultApi.h"
#include "OAIServerConfiguration.h"
#include <QtCore/qanystringview.h>
#include <QtCore/qjsonarray.h>
#include <QtCore/qjsondocument.h>
#include <QtNetwork/qhttpmultipart.h>
#include <QtNetwork/qnetworkaccessmanager.h>
#include <QtNetwork/qnetworkreply.h>
#include <QtNetwork/qnetworkrequestfactory.h>
#include <QtNetwork/qrestaccessmanager.h>
using namespace Qt::StringLiterals;
namespace OpenAPI {
OAIDefaultApi::OAIDefaultApi(QObject *parent)
: OAIBaseApi(parent)
{
initializeServerConfigs();
}
OAIDefaultApi::~OAIDefaultApi()
{
}
void OAIDefaultApi::initializeServerConfigs()
{
//Default server
QList<OAIServerConfiguration> defaultConf = QList<OAIServerConfiguration>();
//varying endpoint server
defaultConf.append(OAIServerConfiguration(
QUrl("https://reqres.in/api"),
"Public REST API test service",
QMap<QString, OAIServerVariable>()));
defaultConf.append(OAIServerConfiguration(
QUrl("http://127.0.0.1:49425/api"),
"Qt-based REST API server",
QMap<QString, OAIServerVariable>()));
m_serverConfigs.insert("testOperation", defaultConf);
m_serverIndices.insert("testOperation", 0);
}
/**
* \fn virtual void OAIDefaultApi::testOperation(const qint32 &pathParam, const ::OpenAPI::OptionalParam<bool> &queryParam0 = ::OpenAPI::OptionalParam<bool>(), const ::OpenAPI::OptionalParam<OAITestObject> &queryParam1 = ::OpenAPI::OptionalParam<OAITestObject>(), const ::OpenAPI::OptionalParam<QList<qint32>> &queryParam2 = ::OpenAPI::OptionalParam<QList<qint32>>(), const ::OpenAPI::OptionalParam<OAITestOperation_request> &oAITestOperationRequest = ::OpenAPI::OptionalParam<OAITestOperation_request>())
* 'testOperation' operation sends the request to a server.
* The request parameters are defined by a specification file.
* @param[in] pathParam qint32 [required]
* @param[in] queryParam0 bool [optional]
* @param[in] queryParam1 OAITestObject [optional]
* @param[in] queryParam2 QList<qint32> [optional]
* @param[in] oAITestOperationRequest OAITestOperation_request [optional]
*/
/**
* \fn template < Functor, > void OAIDefaultApi::testOperation(const qint32 &pathParam, const ::OpenAPI::OptionalParam<bool> &queryParam0 = ::OpenAPI::OptionalParam<bool>(), const ::OpenAPI::OptionalParam<OAITestObject> &queryParam1 = ::OpenAPI::OptionalParam<OAITestObject>(), const ::OpenAPI::OptionalParam<QList<qint32>> &queryParam2 = ::OpenAPI::OptionalParam<QList<qint32>>(), const ::OpenAPI::OptionalParam<OAITestOperation_request> &oAITestOperationRequest = ::OpenAPI::OptionalParam<OAITestOperation_request>(), const ContextTypeForFunctor< Functor > *context = nullptr, Functor &&callback = (){})
* 'testOperation' operation sends the request to a server.
* The request parameters are defined by a specification file.
*
* \attention Use the operation with following parameters in the callback:
* \code {c++}
* testOperation(pathParam, queryParam0, queryParam1, queryParam2, oAITestOperationRequest, this, [&](const QRestReply &reply) { if (reply.isSuccess()) ... });
* \endcode
* \note The template function can not be virtual in C++17.
* If you want to use 'makeOperationsVirtual' option for mocking API,
* please override virtual testOperationWithDataImpl() in derived class.
* The virtual testOperationWithDataImpl() is being called by the template
* function.
* @param[in] pathParam qint32 [required]
* @param[in] queryParam0 bool [optional]
* @param[in] queryParam1 OAITestObject [optional]
* @param[in] queryParam2 QList<qint32> [optional]
* @param[in] oAITestOperationRequest OAITestOperation_request [optional]
* @param[in] context const ContextTypeForFunctor< Functor > * [optional]
* @param[in] callback Functor && [optional]
*/
/**
* \fn void OAIDefaultApi::testOperationCallback(const QRestReply &reply)
* Processes a \a reply response from a server.
* The result of processed data is emitted by testOperationFinished() or
* being returned as a callback parameter of testOperation() request.
* @param[in] reply const QRestReply &
*/
/**
* \fn virtual void OAIDefaultApi::testOperationWithDataImpl(const qint32 &pathParam, const ::OpenAPI::OptionalParam<bool> &queryParam0, const ::OpenAPI::OptionalParam<OAITestObject> &queryParam1, const ::OpenAPI::OptionalParam<QList<qint32>> &queryParam2, const ::OpenAPI::OptionalParam<OAITestOperation_request> &oAITestOperationRequest, const QObject *context, QtPrivate::QSlotObjectBase *slot)
* Implements the testOperation() operation request.
* \note If 'makeOperationsVirtual' option is true, this function is declared as virtual
* and can be overloaded for mocking testOperation() operation calls.
* @param[in] pathParam qint32 [required]
* @param[in] queryParam0 bool [optional]
* @param[in] queryParam1 OAITestObject [optional]
* @param[in] queryParam2 QList<qint32> [optional]
* @param[in] oAITestOperationRequest OAITestOperation_request [optional]
* @param[in] context const QObject * [optional]
* @param[in] slot QtPrivate::QSlotObjectBase * [optional]
*/
void OAIDefaultApi::testOperationWithDataImpl(const qint32 &pathParam, const ::OpenAPI::OptionalParam<bool> &queryParam0, const ::OpenAPI::OptionalParam<OAITestObject> &queryParam1, const ::OpenAPI::OptionalParam<QList<qint32>> &queryParam2, const ::OpenAPI::OptionalParam<OAITestOperation_request> &oAITestOperationRequest, const QObject *context, QtPrivate::QSlotObjectBase *slot)
{
const QUrl serverUrl = m_serverConfigs["testOperation"][m_serverIndices.value("testOperation")].serverUrl();
QString fullPath = "/test/{path_param}";
m_networkFactory->setBaseUrl(serverUrl);
{
QString pathParamPathParam = QString("{%1}").arg("path_param");
QString pathStyle = "simple";
if (pathStyle.isEmpty())
pathStyle = "simple";
const QString pathPrefix = getParamStylePrefix(pathStyle);
const QString pathDelimiter = getParamStyleDelimiter(pathStyle, false);
const bool isObject = false || false;
[[maybe_unused]] const QString assignOperator = getParamStyleAssignOperator(pathStyle, false, isObject);
- const QString pathSuffix = getParamStyleSuffix(pathStyle, u"path_param"_s, false, isObject);
+ const QString pathSuffix = getParamStyleSuffix(pathStyle, QUrl::toPercentEncoding(u"path_param"_s), false, isObject);
QString paramString = pathPrefix + pathSuffix;
paramString += QUrl::toPercentEncoding(::OpenAPI::toStringValue(pathParam));
// In case style=matrix and paramString is empty due to any reasons,
// we serialize it like undefined value and delete '='.
// Described here: https://spec.openapis.org/oas/v3.1.1.html#style-values
if ((paramString == pathPrefix + pathSuffix) && QString("simple") == "matrix"_L1)
paramString.chop(1);
fullPath.replace(pathParamPathParam, paramString);
}
int queryParamCounter = 0;
{
[[maybe_unused]] QString paramString;
QString queryStyle = "form";
if (queryStyle.isEmpty())
queryStyle = "form";
const QString queryPrefix = getParamStylePrefix(queryStyle);
[[maybe_unused]] const QString queryDelimiter = getParamStyleDelimiter(queryStyle, true);
const bool isObject = false || false;
- const QString querySuffix = getParamStyleSuffix(queryStyle, u"query_param0"_s, true, isObject);
+ const QString querySuffix = getParamStyleSuffix(queryStyle, QUrl::toPercentEncoding(u"query_param0"_s), true, isObject);
[[maybe_unused]] const QString queryAssignOperator = getParamStyleAssignOperator(queryStyle, true, isObject);
paramString = querySuffix;
if ((fullPath.indexOf("?") != fullPath.size() - 1) && (queryParamCounter == 0))
fullPath.append(queryPrefix);
if (queryParam0.hasValue()) {
if (queryParamCounter > 0)
fullPath.append("&");
fullPath.append(querySuffix + QUrl::toPercentEncoding(::OpenAPI::toStringValue(queryParam0.value())));
queryParamCounter++;
}
}
{
[[maybe_unused]] QString paramString;
QString queryStyle = "form";
if (queryStyle.isEmpty())
queryStyle = "form";
const QString queryPrefix = getParamStylePrefix(queryStyle);
[[maybe_unused]] const QString queryDelimiter = getParamStyleDelimiter(queryStyle, true);
const bool isObject = true || false;
- const QString querySuffix = getParamStyleSuffix(queryStyle, u"query_param1"_s, true, isObject);
+ const QString querySuffix = getParamStyleSuffix(queryStyle, QUrl::toPercentEncoding(u"query_param1"_s), true, isObject);
[[maybe_unused]] const QString queryAssignOperator = getParamStyleAssignOperator(queryStyle, true, isObject);
paramString = querySuffix;
if ((fullPath.indexOf("?") != fullPath.size() - 1) && (queryParamCounter == 0))
fullPath.append(queryPrefix);
if (queryParam1.hasValue()) {
if (queryParamCounter > 0)
fullPath.append("&");
const QJsonObject parameter = queryParam1.value().asJsonObject();
paramString = serializeJsonValue(QJsonValue(parameter), queryStyle, true,
querySuffix, queryAssignOperator, queryDelimiter,
true);
// style=form && explode=true && non-object => 'query_param1' isn't used in serialization
// style=form && explode=true && empty object => need to be 'query_param1='
if (paramString.isEmpty() && queryStyle == "form")
paramString = u"query_param1="_s;
fullPath.append(paramString);
queryParamCounter++;
} else if (queryParam1.isNull()) {
if (queryParamCounter > 0)
fullPath.append("&");
// style=form && explode=true && non-object => 'query_param1' isn't used in serialization
// style=form && explode=true && empty object => need to be 'query_param1='
if (queryStyle == "form")
fullPath.append(u"query_param1="_s);
else
fullPath.append(querySuffix);
queryParamCounter++;
}
}
{
[[maybe_unused]] QString paramString;
QString queryStyle = "form";
if (queryStyle.isEmpty())
queryStyle = "form";
const QString queryPrefix = getParamStylePrefix(queryStyle);
[[maybe_unused]] const QString queryDelimiter = getParamStyleDelimiter(queryStyle, true);
const bool isObject = false || false;
- const QString querySuffix = getParamStyleSuffix(queryStyle, u"query_param2"_s, true, isObject);
+ const QString querySuffix = getParamStyleSuffix(queryStyle, QUrl::toPercentEncoding(u"query_param2"_s), true, isObject);
[[maybe_unused]] const QString queryAssignOperator = getParamStyleAssignOperator(queryStyle, true, isObject);
if ((fullPath.indexOf("?") != fullPath.size() - 1) && (queryParamCounter == 0))
fullPath.append(queryPrefix);
if (queryParam2.hasValue()) {
if (queryParamCounter > 0)
fullPath.append("&");
fullPath.append(serializeArrayValue(queryParam2.value(), queryStyle, true, querySuffix, queryDelimiter, true));
queryParamCounter++;
}
}
// set m_testOperationPath for serialization tests
m_testOperationPath = fullPath;
OAIHttpRequestInput input(fullPath, "POST");
input.m_headers.replaceOrAppend(QHttpHeaders::WellKnownHeader::ContentType, "application/json"_L1);
if (oAITestOperationRequest.hasValue()) {
QByteArray output = oAITestOperationRequest.value().asJson().toUtf8();
input.m_requestBody.append(output);
} else if (oAITestOperationRequest.isNull()) {
input.m_requestBody.append("null");
}
QNetworkRequest request
= OAIHttpRequestWorker::getNetworkRequest(input, m_requestContent, m_networkFactory,
m_isResponseCompressionEnabled, m_isRequestCompressionEnabled);
QNetworkReply *reply = execute(input, request, m_requestContent);
if (reply != nullptr) {
reply->setParent(this);
m_callerData.insert(reply, OAICallerInfo{context, slot});
connect(reply, &QNetworkReply::finished, this, [this, reply] {
testOperationCallback(QRestReply(reply));
});
connect(reply, &QNetworkReply::errorOccurred, this, [this, reply] {
if (reply) {
emit testOperationErrorOccurred(reply->error(), reply->errorString());
OAICallerInfo callerInfo = m_callerData.take(reply);
if (callerInfo.slot) {
QRestReply restRepl(reply);
void *argv[] = { nullptr, &restRepl };
QObject *context = callerInfo.contextObject ? const_cast<QObject*>(callerInfo.contextObject) : nullptr;
callerInfo.slot->call(context, argv);
}
}
});
}
}
void OAIDefaultApi::testOperationCallback(const QRestReply &reply)
{
auto netReply = reply.networkReply();
if (netReply)
netReply->disconnect(this);
if (!reply.isSuccess())
return;
// Check if callback is provided
OAICallerInfo callerInfo = m_callerData.take(netReply);
if (callerInfo.slot) {
void *argv[] = { nullptr, const_cast<QRestReply*>(&reply) };
QObject *context = callerInfo.contextObject
? const_cast<QObject*>(callerInfo.contextObject) : nullptr;
callerInfo.slot->call(context, argv);
}
emit testOperationFinished();
}
} // namespace OpenAPI