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 "OAIBaseApi.h"
#include "OAIHttpRequest.h"
#include <QtCore/QDir>
#include <QtCore/qjsonarray.h>
#include <QtCore/qjsondocument.h>
#include <QtNetwork/qhttpmultipart.h>
#include <QtNetwork/qnetworkaccessmanager.h>
#include <QtNetwork/qnetworkrequestfactory.h>
#include <QtNetwork/qrestaccessmanager.h>
using namespace Qt::StringLiterals;
namespace OpenAPI {
OAIBaseApi::OAIBaseApi(QObject *parent)
: QObject(parent),
m_workingDirectory(QDir::currentPath()),
m_manager(nullptr),
m_restManager(nullptr),
m_networkFactory(nullptr),
m_isResponseCompressionEnabled(false),
m_isRequestCompressionEnabled(false)
{
if (!m_manager) {
m_manager = std::make_shared<QNetworkAccessManager>();
}
if (!m_restManager) {
m_restManager = std::make_shared<QRestAccessManager>(m_manager.get(), this);
}
if (!m_networkFactory) {
m_networkFactory = std::make_shared<QNetworkRequestFactory>();
}
}
OAIBaseApi::~OAIBaseApi()
{
}
OAIBaseApi::ServerError OAIBaseApi::setDefaultServerValue(int serverIndex, const QString &operation, const QString &variable, const QString &value)
{
auto it = m_serverConfigs.find(operation);
if (it == m_serverConfigs.end()) {
return ServerError::OperationNotFound;
} else if (serverIndex >= it.value().size()) {
return ServerError::ServerIndexNotFound;
}
return m_serverConfigs[operation][serverIndex].setDefaultValue(variable,value);
}
OAIBaseApi::ServerError OAIBaseApi::setServerIndex(const QString &operation, int serverIndex)
{
if (!m_serverIndices.contains(operation))
return ServerError::OperationNotFound;
if (serverIndex >= m_serverConfigs.find(operation).value().size())
return ServerError::ServerIndexNotFound;
m_serverIndices[operation] = serverIndex;
return ServerError::NoError;
}
void OAIBaseApi::setApiKey(const QString &apiKeyName, const QString &apiKey)
{
m_apiKeys.insert(apiKeyName, apiKey);
}
void OAIBaseApi::setBearerToken(const QString &token)
{
m_bearerToken = token;
}
void OAIBaseApi::setUsername(const QString &username)
{
m_username = username;
}
void OAIBaseApi::setPassword(const QString &password)
{
m_password = password;
}
void OAIBaseApi::setTimeOut(std::chrono::milliseconds timeOut)
{
m_networkFactory->setTransferTimeout(timeOut);
}
#if QT_CONFIG(ssl)
void OAIBaseApi::setSslConfiguration(QSslConfiguration config)
{
m_networkFactory->setSslConfiguration(config);
}
#endif // ssl
void OAIBaseApi::setWorkingDirectory(const QString &path)
{
m_workingDirectory = path;
}
void OAIBaseApi::setNetworkAccessResources(std::shared_ptr<QNetworkAccessManager> manager, std::shared_ptr<QRestAccessManager> ram)
{
if (manager == m_manager)
return;
if (!manager || !ram || (manager.get() != ram->networkAccessManager()))
qCritical() << "Network resources are not valid.";
m_manager.reset();
m_restManager.reset();
m_manager = manager;
m_restManager = ram;
}
void OAIBaseApi::setNetworkRequestFactory(std::shared_ptr<QNetworkRequestFactory> factory)
{
if (m_networkFactory == factory)
return;
if (!factory)
qCritical() << "New network factory cannot be null.";
m_networkFactory.reset();
m_networkFactory = factory;
}
/**
* Appends a new ServerConfiguration to the config map for a specific operation.
* @param operation The id to the target operation.
* @param url A string that contains the URL of the server
* @param description A String that describes the server
* @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template.
* returns the index of the new server config on success and std::nullopt if the operation is not found
*/
std::optional<int> OAIBaseApi::addServerConfiguration(const QString &operation, const QUrl &url, const QString &description, const QMap<QString, OAIServerVariable> &variables)
{
if (m_serverConfigs.contains(operation)) {
m_serverConfigs[operation].append(OAIServerConfiguration(
url,
description,
variables));
return m_serverConfigs[operation].size()-1;
}
return std::nullopt; // ServerError::OperationNotFound
}
/**
* Appends a new ServerConfiguration to the config map for all operations and sets the index to that server.
* @param url A string that contains the URL of the server
* @param description A String that describes the server
* @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template.
*/
OAIBaseApi::ServerError OAIBaseApi::setNewServerForAllOperations(const QUrl &url, const QString &description, const QMap<QString, OAIServerVariable> &variables)
{
OAIBaseApi::ServerError err;
for (auto keyIt = m_serverIndices.keyBegin(); keyIt != m_serverIndices.keyEnd(); keyIt++) {
err = setNewServer(*keyIt, url, description, variables);
if (err != ServerError::NoError) // Mark failure but continue with other operations
qWarning() <<"Failed to set server configuration for " << *keyIt << " operation. Error:" << errorString(err);
}
return err;
}
/**
* Appends a new ServerConfiguration to the config map for an operation and sets the index to that server.
* @param operation A name of user operation
* @param url A string that contains the URL of the server
* @param description A String that describes the server
* @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template.
*/
OAIBaseApi::ServerError OAIBaseApi::setNewServer(const QString &operation, const QUrl &url, const QString &description, const QMap<QString, OAIServerVariable> &variables)
{
std::optional<int> serverIndex = addServerConfiguration(operation, url, description, variables);
if (!serverIndex.has_value()) // operation was not found in m_serverConfigs
return ServerError::OperationNotFound;
return setServerIndex(operation, serverIndex.value());
}
void OAIBaseApi::setHeader(QHttpHeaders::WellKnownHeader key, QAnyStringView value)
{
if (m_networkFactory->commonHeaders().values(key).contains(value))
return;
QHttpHeaders headers = m_networkFactory->commonHeaders();
headers.replaceOrAppend(key, value);
m_networkFactory->setCommonHeaders(std::move(headers));
}
void OAIBaseApi::setHeader(QAnyStringView key, QAnyStringView value)
{
if (m_networkFactory->commonHeaders().values(key).contains(value))
return;
QHttpHeaders headers = m_networkFactory->commonHeaders();
headers.replaceOrAppend(key, value);
m_networkFactory->setCommonHeaders(std::move(headers));
}
void OAIBaseApi::enableRequestCompression()
{
m_isRequestCompressionEnabled = true;
}
void OAIBaseApi::enableResponseCompression()
{
m_isResponseCompressionEnabled = true;
}
// Param prefix - the first parameter related symbol in a serialization string.
QString OAIBaseApi::getParamStylePrefix(const QString &style)
{
if (style == "matrix") {
return ";";
} else if (style == "label") {
return ".";
} else if (style == "form") {
return "?";
} else if (style == "simple") {
return "";
} else if (style == "spaceDelimited") {
return "?";
} else if (style == "pipeDelimited") {
return "?";
} else if (style == "deepObject") {
return "?";
}
Q_UNREACHABLE_RETURN(QString());
}
- QString OAIBaseApi::getParamStyleSuffix(const QString &style, const QString &name, bool isExplode, bool isObject)
+ QString OAIBaseApi::getParamStyleSuffix(const QString &style, const QString &name, SerializationFlags flags)
{
+ const bool isExplode = flags.testFlag(SerializationFlag::Explode);
+ const bool isObject = flags.testFlag(SerializationFlag::Object);
if (style == "matrix") {
// for undefined cases "=" will be deleted on serialization.
return (isExplode && isObject) ? "" : name + "=";
} else if (style == "label") {
return "";
} else if (style == "form") {
return (isExplode && isObject) ? "" : name + "=";
} else if (style == "simple") {
return "";
} else if (style == "spaceDelimited") {
return name + "=";
} else if (style == "pipeDelimited") {
return name + "=";
} else if (style == "deepObject") {
return name;
}
Q_UNREACHABLE_RETURN(QString());
}
- QString OAIBaseApi::getParamStyleDelimiter(const QString &style, bool isExplode)
+ QString OAIBaseApi::getParamStyleDelimiter(const QString &style, SerializationFlags flags)
{
+ const bool isExplode = flags.testFlag(SerializationFlag::Explode);
if (style == "matrix") {
return (isExplode) ? ";" : ",";
} else if (style == "label") {
return (isExplode) ? "." : ",";
} else if (style == "form") {
return (isExplode) ? "&" : ",";
} else if (style == "simple") {
return ",";
} else if (style == "spaceDelimited") {
return QUrl::toPercentEncoding(" ");
} else if (style == "pipeDelimited") {
return QUrl::toPercentEncoding("|");
} else if (style == "deepObject") {
return "&";
}
Q_UNREACHABLE_RETURN(QString());
}
- QString OAIBaseApi::getParamStyleAssignOperator(const QString &style, bool isExplode, bool isObject)
+ QString OAIBaseApi::getParamStyleAssignOperator(const QString &style, SerializationFlags flags)
{
+ const bool isExplode = flags.testFlag(SerializationFlag::Explode);
+ const bool isObject = flags.testFlag(SerializationFlag::Object);
if (!isObject)
return "";
if (style == "matrix") {
return (isExplode) ? "=" : ",";
} else if (style == "label") {
return (isExplode) ? "=" : ",";
} else if (style == "form") {
return (isExplode) ? "=" : ",";
} else if (style == "simple") {
return (isExplode) ? "=" : ",";
} else if (style == "spaceDelimited") {
return QUrl::toPercentEncoding(" ");
} else if (style == "pipeDelimited") {
return QUrl::toPercentEncoding("|");
} else if (style == "deepObject") {
return "=";
}
Q_UNREACHABLE_RETURN(QString());
}
QString OAIBaseApi::errorString(ServerError error) const
{
switch (error) {
case ServerError::NoError:
return "No Error"_L1;
case ServerError::ServerVariableNotFound:
return "Server Variable Not Found"_L1;
case ServerError::EnumValueNotFound:
return "Enum Value Not Found"_L1;
case ServerError::OperationNotFound:
return "Operation Not Found"_L1;
case ServerError::ServerIndexNotFound:
return "Server Index Not Found"_L1;
}
Q_UNREACHABLE_RETURN(QString());
}
QNetworkReply *OAIBaseApi::execute(OAIHttpRequestInput &input, QNetworkRequest &request, QByteArray &requestContent)
{
bool isMultiPartMode = (input.m_multiPart.get() && input.m_varLayout == OAIHttpRequestVarLayout::MULTIPART);
QNetworkReply *reply = nullptr;
if (input.m_httpMethod == "GET") {
reply = m_restManager->get(request);
} else if (input.m_httpMethod == "POST") {
if (isMultiPartMode)
reply = m_restManager->post(request, input.m_multiPart.release());
else
reply = m_restManager->post(request, requestContent);
} else if (input.m_httpMethod == "PUT") {
if (isMultiPartMode)
reply = m_restManager->put(request, input.m_multiPart.release());
else
reply = m_restManager->put(request, requestContent);
} else if (input.m_httpMethod == "HEAD") {
reply = m_restManager->head(request);
} else if (input.m_httpMethod == "DELETE") {
reply = m_restManager->deleteResource(request);
} else {
if (isMultiPartMode)
reply = m_restManager->sendCustomRequest(request, input.m_httpMethod.toLatin1(), input.m_multiPart.release());
else
reply = m_restManager->sendCustomRequest(request, input.m_httpMethod.toLatin1(), requestContent);
}
return reply;
}
- QString OAIBaseApi::serializeJsonValue(const QJsonValue &value, const QString style, bool isExplode, const QString &suffix, const QString &assignOperator, const QString &delimiter, bool percentEncode)
+ QString OAIBaseApi::serializeJsonValue(const QJsonValue &value, const SerializationOptions &opts)
{
+ const bool percentEncode = opts.flags.testFlag(SerializationFlag::NeedPercentEncoding);
QString paramString;
switch(value.type()) {
case QJsonValue::String:
case QJsonValue::Bool:
case QJsonValue::Double:
{
- paramString = suffix;
- const QString stringValue = ::OpenAPI::toStringValue(value.toVariant());
+ paramString = opts.suffix;
+ const QString stringValue = toStringValue(value.toVariant());
paramString.append(percentEncode ? QUrl::toPercentEncoding(stringValue) : stringValue);
} break;
case QJsonValue::Array:
{
const QVariantList array = value.toArray().toVariantList();
- paramString
- = serializeArrayValue(array, style, isExplode, suffix, delimiter, percentEncode);
+ paramString = serializeArrayValue(array, opts);
} break;
case QJsonValue::Object:
{
QVariantMap map = value.toObject().toVariantMap();
if (map.size() > 0) {
- if (style == "deepObject"_L1) {
+ if (opts.style == "deepObject"_L1) {
qsizetype index = 0;
for (const auto &[key, value] : map.asKeyValueRange()) {
- if (index > 0)
- paramString.append(delimiter);
- if (percentEncode) {
- paramString.append(suffix + QUrl::toPercentEncoding(u"[%1]"_s.arg(key))
- + assignOperator
- + QUrl::toPercentEncoding(::OpenAPI::toStringValue(value)));
- } else {
- paramString.append(suffix + u"[%1]"_s.arg(key) + assignOperator
- + ::OpenAPI::toStringValue(value));
- }
- index++;
+ if (index > 0)
+ paramString.append(opts.delimiter);
+ if (percentEncode) {
+ paramString.append(opts.suffix
+ + QUrl::toPercentEncoding(u"[%1]"_s.arg(key))
+ + opts.assignOperator
+ + QUrl::toPercentEncoding(toStringValue(value)));
+ } else {
+ paramString.append(opts.suffix + u"[%1]"_s.arg(key)
+ + opts.assignOperator + toStringValue(value));
+ }
+ ++index;
}
} else {
- paramString = suffix;
- paramString.append(serializeMapValue(map, assignOperator, delimiter, percentEncode));
+ paramString = opts.suffix;
+ paramString.append(serializeMapValue(map, opts));
}
} else {
qWarning() << "Serialized QJsonValue::Object is empty!";
}
} break;
case QJsonValue::Null:
case QJsonValue::Undefined:
{
- paramString = suffix;
+ paramString = opts.suffix;
qWarning() << "Path parameter serialization is not supported for the value: " << value;
} break;
}
return paramString;
}
} // namespace OpenAPI