Jazzco ffd03b7e51
[cpp-qt-client] Qt progress info (#18950)
* Update HttpRequest.cpp.mustache

Use stable 4-parameter connect

* add generated files

* Revert "add generated files"

This reverts commit 1d4e78aa0d42208b576354276c274000c29863f9.

* catch download progress in HttpRequest and pass it to api

* Update HttpRequest.cpp.mustache - clean mixed in changes

Removed additional changes from other PullRequest

* Update HttpRequest.cpp.mustache - replaced tabs by spaces

* Update api-header.mustache - replaced tabs by spaces

* Update HttpRequest.h.mustache - replaced tab by spaces

* ran step 3 from Linux

* changed downloadProgress to -nickname-DownloadProgress and added it to all regions where execution finish is connected

* ran step 3

* replaced tab by spaces

* ran step 3

* activate tracing to detect link issue

* removed redundant connects and encapsulated changes in {{addDownloadProgress}}

* added generated files from step 3

* remove cli-option for httprequest - this should be always there

* added files from step 3

* improved name

* Update samples-cpp-qt-client.yaml - fix parameter order

* Update samples-cpp-qt-client.yaml
2024-08-05 14:40:14 +08:00

919 lines
32 KiB
C++

/**
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* The version of the OpenAPI document: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
#include "PFXUserApi.h"
#include "PFXServerConfiguration.h"
#include <QJsonArray>
#include <QJsonDocument>
namespace test_namespace {
PFXUserApi::PFXUserApi(const int timeOut)
: _timeOut(timeOut),
_manager(nullptr),
_isResponseCompressionEnabled(false),
_isRequestCompressionEnabled(false) {
initializeServerConfigs();
}
PFXUserApi::~PFXUserApi() {
}
void PFXUserApi::initializeServerConfigs() {
//Default server
QList<PFXServerConfiguration> defaultConf = QList<PFXServerConfiguration>();
//varying endpoint server
defaultConf.append(PFXServerConfiguration(
QUrl("http://petstore.swagger.io/v2"),
"No description provided",
QMap<QString, PFXServerVariable>()));
_serverConfigs.insert("createUser", defaultConf);
_serverIndices.insert("createUser", 0);
_serverConfigs.insert("createUsersWithArrayInput", defaultConf);
_serverIndices.insert("createUsersWithArrayInput", 0);
_serverConfigs.insert("createUsersWithListInput", defaultConf);
_serverIndices.insert("createUsersWithListInput", 0);
_serverConfigs.insert("deleteUser", defaultConf);
_serverIndices.insert("deleteUser", 0);
_serverConfigs.insert("getUserByName", defaultConf);
_serverIndices.insert("getUserByName", 0);
_serverConfigs.insert("loginUser", defaultConf);
_serverIndices.insert("loginUser", 0);
_serverConfigs.insert("logoutUser", defaultConf);
_serverIndices.insert("logoutUser", 0);
_serverConfigs.insert("updateUser", defaultConf);
_serverIndices.insert("updateUser", 0);
}
/**
* 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
*/
int PFXUserApi::setDefaultServerValue(int serverIndex, const QString &operation, const QString &variable, const QString &value) {
auto it = _serverConfigs.find(operation);
if (it != _serverConfigs.end() && serverIndex < it.value().size()) {
return _serverConfigs[operation][serverIndex].setDefaultValue(variable,value);
}
return -3;
}
void PFXUserApi::setServerIndex(const QString &operation, int serverIndex) {
if (_serverIndices.contains(operation) && serverIndex < _serverConfigs.find(operation).value().size()) {
_serverIndices[operation] = serverIndex;
}
}
void PFXUserApi::setApiKey(const QString &apiKeyName, const QString &apiKey) {
_apiKeys.insert(apiKeyName, apiKey);
}
void PFXUserApi::setBearerToken(const QString &token) {
_bearerToken = token;
}
void PFXUserApi::setUsername(const QString &username) {
_username = username;
}
void PFXUserApi::setPassword(const QString &password) {
_password = password;
}
void PFXUserApi::setTimeOut(const int timeOut) {
_timeOut = timeOut;
}
void PFXUserApi::setWorkingDirectory(const QString &path) {
_workingDirectory = path;
}
void PFXUserApi::setNetworkAccessManager(QNetworkAccessManager* manager) {
_manager = manager;
}
/**
* 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 -1 if the operation is not found
*/
int PFXUserApi::addServerConfiguration(const QString &operation, const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables) {
if (_serverConfigs.contains(operation)) {
_serverConfigs[operation].append(PFXServerConfiguration(
url,
description,
variables));
return _serverConfigs[operation].size()-1;
} else {
return -1;
}
}
/**
* Appends a new ServerConfiguration to the config map for a 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.
*/
void PFXUserApi::setNewServerForAllOperations(const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables) {
for (auto keyIt = _serverIndices.keyBegin(); keyIt != _serverIndices.keyEnd(); keyIt++) {
setServerIndex(*keyIt, addServerConfiguration(*keyIt, url, description, variables));
}
}
/**
* Appends a new ServerConfiguration to the config map for an 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.
*/
void PFXUserApi::setNewServer(const QString &operation, const QUrl &url, const QString &description, const QMap<QString, PFXServerVariable> &variables) {
setServerIndex(operation, addServerConfiguration(operation, url, description, variables));
}
void PFXUserApi::addHeaders(const QString &key, const QString &value) {
_defaultHeaders.insert(key, value);
}
void PFXUserApi::enableRequestCompression() {
_isRequestCompressionEnabled = true;
}
void PFXUserApi::enableResponseCompression() {
_isResponseCompressionEnabled = true;
}
void PFXUserApi::abortRequests() {
Q_EMIT abortRequestsSignal();
}
QString PFXUserApi::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 PFXUserApi::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 PFXUserApi::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";
}
}
void PFXUserApi::createUser(const PFXUser &pfx_user) {
QString fullPath = QString(_serverConfigs["createUser"][_serverIndices.value("createUser")].URL()+"/user");
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "POST");
{
QByteArray output = pfx_user.asJson().toUtf8();
input.request_body.append(output);
}
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(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, this, [this]() {
if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
Q_EMIT allPendingRequestsCompleted();
}
});
worker->execute(&input);
}
void PFXUserApi::createUserCallback(PFXHttpRequestWorker *worker) {
QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type != QNetworkReply::NoError) {
error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
}
worker->deleteLater();
if (worker->error_type == QNetworkReply::NoError) {
Q_EMIT createUserSignal();
Q_EMIT createUserSignalFull(worker);
} else {
#if defined(_MSC_VER)
// For MSVC
#pragma warning(push)
#pragma warning(disable : 4996)
#elif defined(__clang__)
// For Clang
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#elif defined(__GNUC__)
// For GCC
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
Q_EMIT createUserSignalE(error_type, error_str);
Q_EMIT createUserSignalEFull(worker, error_type, error_str);
#if defined(_MSC_VER)
#pragma warning(pop)
#elif defined(__clang__)
#pragma clang diagnostic pop
#elif defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
Q_EMIT createUserSignalError(error_type, error_str);
Q_EMIT createUserSignalErrorFull(worker, error_type, error_str);
}
}
void PFXUserApi::createUsersWithArrayInput(const QList<PFXUser> &pfx_user) {
QString fullPath = QString(_serverConfigs["createUsersWithArrayInput"][_serverIndices.value("createUsersWithArrayInput")].URL()+"/user/createWithArray");
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "POST");
{
QJsonDocument doc(::test_namespace::toJsonValue(pfx_user).toArray());
QByteArray bytes = doc.toJson();
input.request_body.append(bytes);
}
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(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, this, [this]() {
if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
Q_EMIT allPendingRequestsCompleted();
}
});
worker->execute(&input);
}
void PFXUserApi::createUsersWithArrayInputCallback(PFXHttpRequestWorker *worker) {
QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type != QNetworkReply::NoError) {
error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
}
worker->deleteLater();
if (worker->error_type == QNetworkReply::NoError) {
Q_EMIT createUsersWithArrayInputSignal();
Q_EMIT createUsersWithArrayInputSignalFull(worker);
} else {
#if defined(_MSC_VER)
// For MSVC
#pragma warning(push)
#pragma warning(disable : 4996)
#elif defined(__clang__)
// For Clang
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#elif defined(__GNUC__)
// For GCC
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
Q_EMIT createUsersWithArrayInputSignalE(error_type, error_str);
Q_EMIT createUsersWithArrayInputSignalEFull(worker, error_type, error_str);
#if defined(_MSC_VER)
#pragma warning(pop)
#elif defined(__clang__)
#pragma clang diagnostic pop
#elif defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
Q_EMIT createUsersWithArrayInputSignalError(error_type, error_str);
Q_EMIT createUsersWithArrayInputSignalErrorFull(worker, error_type, error_str);
}
}
void PFXUserApi::createUsersWithListInput(const QList<PFXUser> &pfx_user) {
QString fullPath = QString(_serverConfigs["createUsersWithListInput"][_serverIndices.value("createUsersWithListInput")].URL()+"/user/createWithList");
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "POST");
{
QJsonDocument doc(::test_namespace::toJsonValue(pfx_user).toArray());
QByteArray bytes = doc.toJson();
input.request_body.append(bytes);
}
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(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, this, [this]() {
if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
Q_EMIT allPendingRequestsCompleted();
}
});
worker->execute(&input);
}
void PFXUserApi::createUsersWithListInputCallback(PFXHttpRequestWorker *worker) {
QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type != QNetworkReply::NoError) {
error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
}
worker->deleteLater();
if (worker->error_type == QNetworkReply::NoError) {
Q_EMIT createUsersWithListInputSignal();
Q_EMIT createUsersWithListInputSignalFull(worker);
} else {
#if defined(_MSC_VER)
// For MSVC
#pragma warning(push)
#pragma warning(disable : 4996)
#elif defined(__clang__)
// For Clang
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#elif defined(__GNUC__)
// For GCC
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
Q_EMIT createUsersWithListInputSignalE(error_type, error_str);
Q_EMIT createUsersWithListInputSignalEFull(worker, error_type, error_str);
#if defined(_MSC_VER)
#pragma warning(pop)
#elif defined(__clang__)
#pragma clang diagnostic pop
#elif defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
Q_EMIT createUsersWithListInputSignalError(error_type, error_str);
Q_EMIT createUsersWithListInputSignalErrorFull(worker, error_type, error_str);
}
}
void PFXUserApi::deleteUser(const QString &username) {
QString fullPath = QString(_serverConfigs["deleteUser"][_serverIndices.value("deleteUser")].URL()+"/user/{username}");
{
QString usernamePathParam("{");
usernamePathParam.append("username").append("}");
QString pathPrefix, pathSuffix, pathDelimiter;
QString pathStyle = "simple";
if (pathStyle == "")
pathStyle = "simple";
pathPrefix = getParamStylePrefix(pathStyle);
pathSuffix = getParamStyleSuffix(pathStyle);
pathDelimiter = getParamStyleDelimiter(pathStyle, "username", false);
QString paramString = (pathStyle == "matrix") ? pathPrefix+"username"+pathSuffix : pathPrefix;
fullPath.replace(usernamePathParam, paramString+QUrl::toPercentEncoding(::test_namespace::toStringValue(username)));
}
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "DELETE");
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(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, this, [this]() {
if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
Q_EMIT allPendingRequestsCompleted();
}
});
worker->execute(&input);
}
void PFXUserApi::deleteUserCallback(PFXHttpRequestWorker *worker) {
QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type != QNetworkReply::NoError) {
error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
}
worker->deleteLater();
if (worker->error_type == QNetworkReply::NoError) {
Q_EMIT deleteUserSignal();
Q_EMIT deleteUserSignalFull(worker);
} else {
#if defined(_MSC_VER)
// For MSVC
#pragma warning(push)
#pragma warning(disable : 4996)
#elif defined(__clang__)
// For Clang
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#elif defined(__GNUC__)
// For GCC
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
Q_EMIT deleteUserSignalE(error_type, error_str);
Q_EMIT deleteUserSignalEFull(worker, error_type, error_str);
#if defined(_MSC_VER)
#pragma warning(pop)
#elif defined(__clang__)
#pragma clang diagnostic pop
#elif defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
Q_EMIT deleteUserSignalError(error_type, error_str);
Q_EMIT deleteUserSignalErrorFull(worker, error_type, error_str);
}
}
void PFXUserApi::getUserByName(const QString &username) {
QString fullPath = QString(_serverConfigs["getUserByName"][_serverIndices.value("getUserByName")].URL()+"/user/{username}");
{
QString usernamePathParam("{");
usernamePathParam.append("username").append("}");
QString pathPrefix, pathSuffix, pathDelimiter;
QString pathStyle = "simple";
if (pathStyle == "")
pathStyle = "simple";
pathPrefix = getParamStylePrefix(pathStyle);
pathSuffix = getParamStyleSuffix(pathStyle);
pathDelimiter = getParamStyleDelimiter(pathStyle, "username", false);
QString paramString = (pathStyle == "matrix") ? pathPrefix+"username"+pathSuffix : pathPrefix;
fullPath.replace(usernamePathParam, paramString+QUrl::toPercentEncoding(::test_namespace::toStringValue(username)));
}
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "GET");
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(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, this, [this]() {
if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
Q_EMIT allPendingRequestsCompleted();
}
});
worker->execute(&input);
}
void PFXUserApi::getUserByNameCallback(PFXHttpRequestWorker *worker) {
QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type != QNetworkReply::NoError) {
error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
}
PFXUser output(QString(worker->response));
worker->deleteLater();
if (worker->error_type == QNetworkReply::NoError) {
Q_EMIT getUserByNameSignal(output);
Q_EMIT getUserByNameSignalFull(worker, output);
} else {
#if defined(_MSC_VER)
// For MSVC
#pragma warning(push)
#pragma warning(disable : 4996)
#elif defined(__clang__)
// For Clang
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#elif defined(__GNUC__)
// For GCC
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
Q_EMIT getUserByNameSignalE(output, error_type, error_str);
Q_EMIT getUserByNameSignalEFull(worker, error_type, error_str);
#if defined(_MSC_VER)
#pragma warning(pop)
#elif defined(__clang__)
#pragma clang diagnostic pop
#elif defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
Q_EMIT getUserByNameSignalError(output, error_type, error_str);
Q_EMIT getUserByNameSignalErrorFull(worker, error_type, error_str);
}
}
void PFXUserApi::loginUser(const QString &username, const QString &password) {
QString fullPath = QString(_serverConfigs["loginUser"][_serverIndices.value("loginUser")].URL()+"/user/login");
QString queryPrefix, querySuffix, queryDelimiter, queryStyle;
{
queryStyle = "form";
if (queryStyle == "")
queryStyle = "form";
queryPrefix = getParamStylePrefix(queryStyle);
querySuffix = getParamStyleSuffix(queryStyle);
queryDelimiter = getParamStyleDelimiter(queryStyle, "username", true);
if (fullPath.indexOf("?") > 0)
fullPath.append(queryPrefix);
else
fullPath.append("?");
fullPath.append(QUrl::toPercentEncoding("username")).append(querySuffix).append(QUrl::toPercentEncoding(username));
}
{
queryStyle = "form";
if (queryStyle == "")
queryStyle = "form";
queryPrefix = getParamStylePrefix(queryStyle);
querySuffix = getParamStyleSuffix(queryStyle);
queryDelimiter = getParamStyleDelimiter(queryStyle, "password", true);
if (fullPath.indexOf("?") > 0)
fullPath.append(queryPrefix);
else
fullPath.append("?");
fullPath.append(QUrl::toPercentEncoding("password")).append(querySuffix).append(QUrl::toPercentEncoding(password));
}
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "GET");
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(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, this, [this]() {
if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
Q_EMIT allPendingRequestsCompleted();
}
});
worker->execute(&input);
}
void PFXUserApi::loginUserCallback(PFXHttpRequestWorker *worker) {
QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type != QNetworkReply::NoError) {
error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
}
QString output;
::test_namespace::fromStringValue(QString(worker->response), output);
worker->deleteLater();
if (worker->error_type == QNetworkReply::NoError) {
Q_EMIT loginUserSignal(output);
Q_EMIT loginUserSignalFull(worker, output);
} else {
#if defined(_MSC_VER)
// For MSVC
#pragma warning(push)
#pragma warning(disable : 4996)
#elif defined(__clang__)
// For Clang
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#elif defined(__GNUC__)
// For GCC
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
Q_EMIT loginUserSignalE(output, error_type, error_str);
Q_EMIT loginUserSignalEFull(worker, error_type, error_str);
#if defined(_MSC_VER)
#pragma warning(pop)
#elif defined(__clang__)
#pragma clang diagnostic pop
#elif defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
Q_EMIT loginUserSignalError(output, error_type, error_str);
Q_EMIT loginUserSignalErrorFull(worker, error_type, error_str);
}
}
void PFXUserApi::logoutUser() {
QString fullPath = QString(_serverConfigs["logoutUser"][_serverIndices.value("logoutUser")].URL()+"/user/logout");
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "GET");
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(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, this, [this]() {
if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
Q_EMIT allPendingRequestsCompleted();
}
});
worker->execute(&input);
}
void PFXUserApi::logoutUserCallback(PFXHttpRequestWorker *worker) {
QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type != QNetworkReply::NoError) {
error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
}
worker->deleteLater();
if (worker->error_type == QNetworkReply::NoError) {
Q_EMIT logoutUserSignal();
Q_EMIT logoutUserSignalFull(worker);
} else {
#if defined(_MSC_VER)
// For MSVC
#pragma warning(push)
#pragma warning(disable : 4996)
#elif defined(__clang__)
// For Clang
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#elif defined(__GNUC__)
// For GCC
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
Q_EMIT logoutUserSignalE(error_type, error_str);
Q_EMIT logoutUserSignalEFull(worker, error_type, error_str);
#if defined(_MSC_VER)
#pragma warning(pop)
#elif defined(__clang__)
#pragma clang diagnostic pop
#elif defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
Q_EMIT logoutUserSignalError(error_type, error_str);
Q_EMIT logoutUserSignalErrorFull(worker, error_type, error_str);
}
}
void PFXUserApi::updateUser(const QString &username, const PFXUser &pfx_user) {
QString fullPath = QString(_serverConfigs["updateUser"][_serverIndices.value("updateUser")].URL()+"/user/{username}");
{
QString usernamePathParam("{");
usernamePathParam.append("username").append("}");
QString pathPrefix, pathSuffix, pathDelimiter;
QString pathStyle = "simple";
if (pathStyle == "")
pathStyle = "simple";
pathPrefix = getParamStylePrefix(pathStyle);
pathSuffix = getParamStyleSuffix(pathStyle);
pathDelimiter = getParamStyleDelimiter(pathStyle, "username", false);
QString paramString = (pathStyle == "matrix") ? pathPrefix+"username"+pathSuffix : pathPrefix;
fullPath.replace(usernamePathParam, paramString+QUrl::toPercentEncoding(::test_namespace::toStringValue(username)));
}
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "PUT");
{
QByteArray output = pfx_user.asJson().toUtf8();
input.request_body.append(output);
}
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(this, &PFXUserApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, this, [this]() {
if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
Q_EMIT allPendingRequestsCompleted();
}
});
worker->execute(&input);
}
void PFXUserApi::updateUserCallback(PFXHttpRequestWorker *worker) {
QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type != QNetworkReply::NoError) {
error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
}
worker->deleteLater();
if (worker->error_type == QNetworkReply::NoError) {
Q_EMIT updateUserSignal();
Q_EMIT updateUserSignalFull(worker);
} else {
#if defined(_MSC_VER)
// For MSVC
#pragma warning(push)
#pragma warning(disable : 4996)
#elif defined(__clang__)
// For Clang
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#elif defined(__GNUC__)
// For GCC
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
Q_EMIT updateUserSignalE(error_type, error_str);
Q_EMIT updateUserSignalEFull(worker, error_type, error_str);
#if defined(_MSC_VER)
#pragma warning(pop)
#elif defined(__clang__)
#pragma clang diagnostic pop
#elif defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
Q_EMIT updateUserSignalError(error_type, error_str);
Q_EMIT updateUserSignalErrorFull(worker, error_type, error_str);
}
}
void PFXUserApi::tokenAvailable(){
oauthToken token;
switch (_OauthMethod) {
case 1: //implicit flow
token = _implicitFlow.getToken(_latestScope.join(" "));
if(token.isValid()){
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
_latestWorker->execute(&_latestInput);
}else{
_implicitFlow.removeToken(_latestScope.join(" "));
qDebug() << "Could not retrieve a valid token";
}
break;
case 2: //authorization flow
token = _authFlow.getToken(_latestScope.join(" "));
if(token.isValid()){
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
_latestWorker->execute(&_latestInput);
}else{
_authFlow.removeToken(_latestScope.join(" "));
qDebug() << "Could not retrieve a valid token";
}
break;
case 3: //client credentials flow
token = _credentialFlow.getToken(_latestScope.join(" "));
if(token.isValid()){
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
_latestWorker->execute(&_latestInput);
}else{
_credentialFlow.removeToken(_latestScope.join(" "));
qDebug() << "Could not retrieve a valid token";
}
break;
case 4: //resource owner password flow
token = _passwordFlow.getToken(_latestScope.join(" "));
if(token.isValid()){
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
_latestWorker->execute(&_latestInput);
}else{
_credentialFlow.removeToken(_latestScope.join(" "));
qDebug() << "Could not retrieve a valid token";
}
break;
default:
qDebug() << "No Oauth method set!";
break;
}
}
} // namespace test_namespace