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/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, [&](QRestReply &reply) { if (reply.isSuccess()) ... });
+ * 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("{");
pathParamPathParam.append("path_param").append("}");
QString pathPrefix, pathSuffix, pathDelimiter;
QString pathStyle = "simple";
if (pathStyle == "")
pathStyle = "simple";
pathPrefix = getParamStylePrefix(pathStyle);
pathSuffix = getParamStyleSuffix(pathStyle);
pathDelimiter = getParamStyleDelimiter(pathStyle, "path_param", false);
QString paramString = (pathStyle == "matrix") ? pathPrefix + "path_param" + pathSuffix : pathPrefix;
fullPath.replace(pathParamPathParam, QUrl::toPercentEncoding(paramString + ::OpenAPI::toStringValue(pathParam)));
QString queryPrefix, querySuffix, queryDelimiter, queryStyle;
queryStyle = "form";
if (queryStyle == "")
queryStyle = "form";
queryPrefix = getParamStylePrefix(queryStyle);
querySuffix = getParamStyleSuffix(queryStyle);
if (queryParam0.hasValue()) {
if (fullPath.indexOf("?") > 0)
fullPath.append(queryPrefix);
else
fullPath.append("?");
fullPath.append(QUrl::toPercentEncoding("query_param0") + querySuffix + QUrl::toPercentEncoding(::OpenAPI::toStringValue(queryParam0.value())));
}
queryStyle = "form";
if (queryStyle == "")
queryStyle = "form";
queryPrefix = getParamStylePrefix(queryStyle);
querySuffix = getParamStyleSuffix(queryStyle);
if (queryParam1.hasValue()) {
if (fullPath.indexOf("?") > 0)
fullPath.append(queryPrefix);
else
fullPath.append("?");
QString paramString = (queryStyle == "form" && true) ? "" : "query_param1" + querySuffix;
QString assignOperator;
if (queryStyle == "form")
assignOperator = (true) ? "=" : ",";
else if (queryStyle == "deepObject")
assignOperator = (true) ? "=" : "none";
queryDelimiter = ((queryStyle == "form" || queryStyle == "deepObject") && true) ? "&" : ",";
const QJsonObject parameter = queryParam1.value().asJsonObject();
qint32 count = 0;
for(const QString& key : parameter.keys()) {
if (count > 0)
paramString.append(queryDelimiter);
paramString.append(::OpenAPI::optionParameterToString(((queryStyle == "form") ? key
: QString("query_param1") + QString("[") + key + QString("]")), assignOperator, parameter.value(key)));
count++;
}
fullPath.append(QUrl::toPercentEncoding(paramString));
} else if (queryParam1.isNull()) {
if (fullPath.indexOf("?") > 0)
fullPath.append(queryPrefix);
else
fullPath.append("?");
fullPath.append(QUrl::toPercentEncoding("query_param1")).append(querySuffix).append(QUrl::toPercentEncoding(QString("null")));
}
queryStyle = "form";
if (queryStyle == "")
queryStyle = "form";
queryPrefix = getParamStylePrefix(queryStyle);
querySuffix = getParamStyleSuffix(queryStyle);
if (queryParam2.hasValue()) {
queryDelimiter = getParamStyleDelimiter(queryStyle, "query_param2", true);
if (queryParam2.value().size() > 0) {
if (QString("multi").indexOf("multi") == 0) {
for (qint32 t : queryParam2.value()) {
if (fullPath.indexOf("?") > 0)
fullPath.append(queryPrefix);
else
fullPath.append("?");
fullPath.append("query_param2=").append(::OpenAPI::toStringValue(t));
}
} else if (QString("multi").indexOf("ssv") == 0) {
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?").append(queryPrefix).append("query_param2").append(querySuffix);
qint32 count = 0;
for (qint32 t : queryParam2.value()) {
if (count > 0) {
fullPath.append((true)? queryDelimiter : QUrl::toPercentEncoding(queryDelimiter));
}
fullPath.append(::OpenAPI::toStringValue(t));
count++;
}
} else if (QString("multi").indexOf("tsv") == 0) {
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?").append(queryPrefix).append("query_param2").append(querySuffix);
qint32 count = 0;
for (qint32 t : queryParam2.value()) {
if (count > 0) {
fullPath.append("\t");
}
fullPath.append(::OpenAPI::toStringValue(t));
count++;
}
} else if (QString("multi").indexOf("csv") == 0) {
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?").append(queryPrefix).append("query_param2").append(querySuffix);
qint32 count = 0;
for (qint32 t : queryParam2.value()) {
if (count > 0) {
fullPath.append(queryDelimiter);
}
fullPath.append(::OpenAPI::toStringValue(t));
count++;
}
} else if (QString("multi").indexOf("pipes") == 0) {
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?").append(queryPrefix).append("query_param2").append(querySuffix);
qint32 count = 0;
for (qint32 t : queryParam2.value()) {
if (count > 0) {
fullPath.append(queryDelimiter);
}
fullPath.append(::OpenAPI::toStringValue(t));
count++;
}
} else if (QString("multi").indexOf("deepObject") == 0) {
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?").append(queryPrefix).append("query_param2").append(querySuffix);
qint32 count = 0;
for (qint32 t : queryParam2.value()) {
if (count > 0) {
fullPath.append(queryDelimiter);
}
fullPath.append(::OpenAPI::toStringValue(t));
count++;
}
}
}
}
OAIHttpRequestInput input(fullPath, "POST");
if (oAITestOperationRequest.hasValue()) {
QByteArray output = oAITestOperationRequest.value().asJson().toUtf8();
input.m_requestBody.append(output);
} else if (oAITestOperationRequest.isNull()) {
input.m_requestBody.append("null");
}
QByteArray requestContent;
QNetworkRequest request
= OAIHttpRequestWorker::getNetworkRequest(input, requestContent, m_networkFactory,
m_isResponseCompressionEnabled, m_isRequestCompressionEnabled);
QNetworkReply *reply = execute(input, request, 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)
disconnect(netReply, nullptr, nullptr, nullptr);
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