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/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;
+ }
+
+ 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 {
+ return "none";
+ }
+ }
+
+ QString OAIBaseApi::getParamStyleSuffix(const QString &style)
+ {
+ if (style == "matrix") {
+ return "=";
+ } else if (style == "label") {
+ return "";
+ } else if (style == "form") {
+ return "=";
+ } else if (style == "simple") {
+ return "";
+ } else if (style == "spaceDelimited") {
+ return "=";
+ } else if (style == "pipeDelimited") {
+ return "=";
+ } else {
+ return "none";
+ }
+ }
+
+ QString OAIBaseApi::getParamStyleDelimiter(const QString &style, const QString &name, bool isExplode)
+ {
+ if (style == "matrix") {
+ return (isExplode) ? ";" + name + "=" : ",";
+
+ } else if (style == "label") {
+ return (isExplode) ? "." : ",";
+
+ } else if (style == "form") {
+ return (isExplode) ? "&" + name + "=" : ",";
+
+ } else if (style == "simple") {
+ return ",";
+ } else if (style == "spaceDelimited") {
+ return (isExplode) ? "&" + name + "=" : " ";
+
+ } else if (style == "pipeDelimited") {
+ return (isExplode) ? "&" + name + "=" : "|";
+
+ } else if (style == "deepObject") {
+ return (isExplode) ? "&" : "none";
+
+ } else {
+ return "none";
+ }
+ }
+
+ 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)
+ {
+ QNetworkReply *reply = nullptr;
+ if (input.m_httpMethod == "GET") {
+ reply = m_restManager->get(request);
+ } else if (input.m_httpMethod == "POST") {
+ reply = m_restManager->post(request, requestContent);
+ } else if (input.m_httpMethod == "PUT") {
+ 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 {
+ reply = m_restManager->sendCustomRequest(request, input.m_httpMethod.toLatin1(), requestContent);
+ }
+
+ return reply;
+ }
+
+ } // namespace OpenAPI
+