[cpp][Qt5] Add the ability to pass QNetworkAccessManager as a parameter (#6053)

* [C++][Qt5] Add the ability to pass QNetworkAccessManager as a parameter
* connect to QNetworkReply::finished instead of QNetworkAccessManager::finished

* [C++][Qt5] regen samples for cpp-qt5-client

* [C++][Qt5] disconect from reply by timeout

* [C++][Qt5] regen samples for cpp-qt5-client

Co-authored-by: alfredotg <alfredotg@tu.ru>
This commit is contained in:
Alofredo 2020-05-07 09:05:29 +03:00 committed by GitHub
parent adb6bf9d81
commit 3bfd6de327
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 95 additions and 49 deletions

View File

@ -44,12 +44,13 @@ void {{prefix}}HttpRequestInput::add_file(QString variable_name, QString local_f
files.append(file); files.append(file);
} }
{{prefix}}HttpRequestWorker::{{prefix}}HttpRequestWorker(QObject *parent) {{prefix}}HttpRequestWorker::{{prefix}}HttpRequestWorker(QObject *parent, QNetworkAccessManager *_manager)
: QObject(parent), manager(nullptr), timeOutTimer(this), isResponseCompressionEnabled(false), isRequestCompressionEnabled(false), httpResponseCode(-1) { : QObject(parent), manager(_manager), timeOutTimer(this), isResponseCompressionEnabled(false), isRequestCompressionEnabled(false), httpResponseCode(-1) {
qsrand(QDateTime::currentDateTime().toTime_t()); qsrand(QDateTime::currentDateTime().toTime_t());
manager = new QNetworkAccessManager(this); if (manager == nullptr) {
manager = new QNetworkAccessManager(this);
}
workingDirectory = QDir::currentPath(); workingDirectory = QDir::currentPath();
connect(manager, &QNetworkAccessManager::finished, this, &{{prefix}}HttpRequestWorker::on_manager_finished);
timeOutTimer.setSingleShot(true); timeOutTimer.setSingleShot(true);
} }
@ -347,7 +348,7 @@ void {{prefix}}HttpRequestWorker::execute({{prefix}}HttpRequestInput *input) {
reply = manager->deleteResource(request); reply = manager->deleteResource(request);
} else { } else {
#if (QT_VERSION >= 0x050800) #if (QT_VERSION >= 0x050800)
manager->sendCustomRequest(request, input->http_method.toLatin1(), request_content); reply = manager->sendCustomRequest(request, input->http_method.toLatin1(), request_content);
#else #else
QBuffer *buffer = new QBuffer; QBuffer *buffer = new QBuffer;
buffer->setData(request_content); buffer->setData(request_content);
@ -357,13 +358,21 @@ void {{prefix}}HttpRequestWorker::execute({{prefix}}HttpRequestInput *input) {
buffer->setParent(reply); buffer->setParent(reply);
#endif #endif
} }
if (reply != nullptr) {
reply->setParent(this);
connect(reply, &QNetworkReply::finished, [this, reply] {
on_reply_finished(reply);
});
}
if (timeOutTimer.interval() > 0) { if (timeOutTimer.interval() > 0) {
QObject::connect(&timeOutTimer, &QTimer::timeout, [=]() { on_manager_timeout(reply); }); QObject::connect(&timeOutTimer, &QTimer::timeout, [this, reply] {
on_reply_timeout(reply);
});
timeOutTimer.start(); timeOutTimer.start();
} }
} }
void {{prefix}}HttpRequestWorker::on_manager_finished(QNetworkReply *reply) { void {{prefix}}HttpRequestWorker::on_reply_finished(QNetworkReply *reply) {
bool codeSts = false; bool codeSts = false;
if(timeOutTimer.isActive()) { if(timeOutTimer.isActive()) {
QObject::disconnect(&timeOutTimer, &QTimer::timeout, nullptr, nullptr); QObject::disconnect(&timeOutTimer, &QTimer::timeout, nullptr, nullptr);
@ -387,11 +396,11 @@ void {{prefix}}HttpRequestWorker::on_manager_finished(QNetworkReply *reply) {
emit on_execution_finished(this); emit on_execution_finished(this);
} }
void {{prefix}}HttpRequestWorker::on_manager_timeout(QNetworkReply *reply) { void {{prefix}}HttpRequestWorker::on_reply_timeout(QNetworkReply *reply) {
error_type = QNetworkReply::TimeoutError; error_type = QNetworkReply::TimeoutError;
response = ""; response = "";
error_str = "Timed out waiting for response"; error_str = "Timed out waiting for response";
disconnect(manager, nullptr, nullptr, nullptr); disconnect(reply, nullptr, nullptr, nullptr);
reply->abort(); reply->abort();
reply->deleteLater(); reply->deleteLater();
emit on_execution_finished(this); emit on_execution_finished(this);

View File

@ -50,7 +50,7 @@ class {{prefix}}HttpRequestWorker : public QObject {
Q_OBJECT Q_OBJECT
public: public:
explicit {{prefix}}HttpRequestWorker(QObject *parent = nullptr); explicit {{prefix}}HttpRequestWorker(QObject *parent = nullptr, QNetworkAccessManager *manager = nullptr);
virtual ~{{prefix}}HttpRequestWorker(); virtual ~{{prefix}}HttpRequestWorker();
QByteArray response; QByteArray response;
@ -87,13 +87,11 @@ private:
bool isRequestCompressionEnabled; bool isRequestCompressionEnabled;
int httpResponseCode; int httpResponseCode;
void on_manager_timeout(QNetworkReply *reply); void on_reply_timeout(QNetworkReply *reply);
void on_reply_finished(QNetworkReply *reply);
void process_response(QNetworkReply *reply); void process_response(QNetworkReply *reply);
QByteArray decompress(const QByteArray& data); QByteArray decompress(const QByteArray& data);
QByteArray compress(const QByteArray& input, int level, {{prefix}}CompressionType compressType); QByteArray compress(const QByteArray& input, int level, {{prefix}}CompressionType compressType);
private slots:
void on_manager_finished(QNetworkReply *reply);
}; };
{{#cppNamespaceDeclarations}} {{#cppNamespaceDeclarations}}

View File

@ -15,6 +15,7 @@ namespace {{this}} {
_port(port), _port(port),
_basePath(basePath), _basePath(basePath),
_timeOut(timeOut), _timeOut(timeOut),
_manager(nullptr),
isResponseCompressionEnabled(false), isResponseCompressionEnabled(false),
isRequestCompressionEnabled(false) {} isRequestCompressionEnabled(false) {}
@ -45,6 +46,10 @@ void {{classname}}::setWorkingDirectory(const QString &path) {
_workingDirectory = path; _workingDirectory = path;
} }
void {{classname}}::setNetworkAccessManager(QNetworkAccessManager* manager) {
_manager = manager;
}
void {{classname}}::addHeaders(const QString &key, const QString &value) { void {{classname}}::addHeaders(const QString &key, const QString &value) {
defaultHeaders.insert(key, value); defaultHeaders.insert(key, value);
} }
@ -120,7 +125,7 @@ void {{classname}}::{{nickname}}({{#allParams}}const {{{dataType}}} &{{paramName
} }
} }
{{/collectionFormat}}{{/queryParams}} {{/collectionFormat}}{{/queryParams}}
{{prefix}}HttpRequestWorker *worker = new {{prefix}}HttpRequestWorker(this); {{prefix}}HttpRequestWorker *worker = new {{prefix}}HttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut); worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);{{#contentCompression}} worker->setWorkingDirectory(_workingDirectory);{{#contentCompression}}
worker->setResponseCompressionEnabled(isResponseCompressionEnabled); worker->setResponseCompressionEnabled(isResponseCompressionEnabled);

View File

@ -8,6 +8,7 @@
{{/imports}} {{/imports}}
#include <QObject> #include <QObject>
#include <QNetworkAccessManager>
{{#cppNamespaceDeclarations}} {{#cppNamespaceDeclarations}}
namespace {{this}} { namespace {{this}} {
@ -26,6 +27,7 @@ public:
void setBasePath(const QString &basePath); void setBasePath(const QString &basePath);
void setTimeOut(const int timeOut); void setTimeOut(const int timeOut);
void setWorkingDirectory(const QString &path); void setWorkingDirectory(const QString &path);
void setNetworkAccessManager(QNetworkAccessManager* manager);
void addHeaders(const QString &key, const QString &value); void addHeaders(const QString &key, const QString &value);
void enableRequestCompression(); void enableRequestCompression();
void enableResponseCompression(); void enableResponseCompression();
@ -39,6 +41,7 @@ private:
QString _basePath; QString _basePath;
int _timeOut; int _timeOut;
QString _workingDirectory; QString _workingDirectory;
QNetworkAccessManager* _manager;
QMap<QString, QString> defaultHeaders; QMap<QString, QString> defaultHeaders;
bool isResponseCompressionEnabled; bool isResponseCompressionEnabled;
bool isRequestCompressionEnabled; bool isRequestCompressionEnabled;

View File

@ -51,12 +51,13 @@ void PFXHttpRequestInput::add_file(QString variable_name, QString local_filename
files.append(file); files.append(file);
} }
PFXHttpRequestWorker::PFXHttpRequestWorker(QObject *parent) PFXHttpRequestWorker::PFXHttpRequestWorker(QObject *parent, QNetworkAccessManager *_manager)
: QObject(parent), manager(nullptr), timeOutTimer(this), isResponseCompressionEnabled(false), isRequestCompressionEnabled(false), httpResponseCode(-1) { : QObject(parent), manager(_manager), timeOutTimer(this), isResponseCompressionEnabled(false), isRequestCompressionEnabled(false), httpResponseCode(-1) {
qsrand(QDateTime::currentDateTime().toTime_t()); qsrand(QDateTime::currentDateTime().toTime_t());
manager = new QNetworkAccessManager(this); if (manager == nullptr) {
manager = new QNetworkAccessManager(this);
}
workingDirectory = QDir::currentPath(); workingDirectory = QDir::currentPath();
connect(manager, &QNetworkAccessManager::finished, this, &PFXHttpRequestWorker::on_manager_finished);
timeOutTimer.setSingleShot(true); timeOutTimer.setSingleShot(true);
} }
@ -354,7 +355,7 @@ void PFXHttpRequestWorker::execute(PFXHttpRequestInput *input) {
reply = manager->deleteResource(request); reply = manager->deleteResource(request);
} else { } else {
#if (QT_VERSION >= 0x050800) #if (QT_VERSION >= 0x050800)
manager->sendCustomRequest(request, input->http_method.toLatin1(), request_content); reply = manager->sendCustomRequest(request, input->http_method.toLatin1(), request_content);
#else #else
QBuffer *buffer = new QBuffer; QBuffer *buffer = new QBuffer;
buffer->setData(request_content); buffer->setData(request_content);
@ -364,13 +365,21 @@ void PFXHttpRequestWorker::execute(PFXHttpRequestInput *input) {
buffer->setParent(reply); buffer->setParent(reply);
#endif #endif
} }
if (reply != nullptr) {
reply->setParent(this);
connect(reply, &QNetworkReply::finished, [this, reply] {
on_reply_finished(reply);
});
}
if (timeOutTimer.interval() > 0) { if (timeOutTimer.interval() > 0) {
QObject::connect(&timeOutTimer, &QTimer::timeout, [=]() { on_manager_timeout(reply); }); QObject::connect(&timeOutTimer, &QTimer::timeout, [this, reply] {
on_reply_timeout(reply);
});
timeOutTimer.start(); timeOutTimer.start();
} }
} }
void PFXHttpRequestWorker::on_manager_finished(QNetworkReply *reply) { void PFXHttpRequestWorker::on_reply_finished(QNetworkReply *reply) {
bool codeSts = false; bool codeSts = false;
if(timeOutTimer.isActive()) { if(timeOutTimer.isActive()) {
QObject::disconnect(&timeOutTimer, &QTimer::timeout, nullptr, nullptr); QObject::disconnect(&timeOutTimer, &QTimer::timeout, nullptr, nullptr);
@ -394,11 +403,11 @@ void PFXHttpRequestWorker::on_manager_finished(QNetworkReply *reply) {
emit on_execution_finished(this); emit on_execution_finished(this);
} }
void PFXHttpRequestWorker::on_manager_timeout(QNetworkReply *reply) { void PFXHttpRequestWorker::on_reply_timeout(QNetworkReply *reply) {
error_type = QNetworkReply::TimeoutError; error_type = QNetworkReply::TimeoutError;
response = ""; response = "";
error_str = "Timed out waiting for response"; error_str = "Timed out waiting for response";
disconnect(manager, nullptr, nullptr, nullptr); disconnect(reply, nullptr, nullptr, nullptr);
reply->abort(); reply->abort();
reply->deleteLater(); reply->deleteLater();
emit on_execution_finished(this); emit on_execution_finished(this);

View File

@ -58,7 +58,7 @@ class PFXHttpRequestWorker : public QObject {
Q_OBJECT Q_OBJECT
public: public:
explicit PFXHttpRequestWorker(QObject *parent = nullptr); explicit PFXHttpRequestWorker(QObject *parent = nullptr, QNetworkAccessManager *manager = nullptr);
virtual ~PFXHttpRequestWorker(); virtual ~PFXHttpRequestWorker();
QByteArray response; QByteArray response;
@ -95,13 +95,11 @@ private:
bool isRequestCompressionEnabled; bool isRequestCompressionEnabled;
int httpResponseCode; int httpResponseCode;
void on_manager_timeout(QNetworkReply *reply); void on_reply_timeout(QNetworkReply *reply);
void on_reply_finished(QNetworkReply *reply);
void process_response(QNetworkReply *reply); void process_response(QNetworkReply *reply);
QByteArray decompress(const QByteArray& data); QByteArray decompress(const QByteArray& data);
QByteArray compress(const QByteArray& input, int level, PFXCompressionType compressType); QByteArray compress(const QByteArray& input, int level, PFXCompressionType compressType);
private slots:
void on_manager_finished(QNetworkReply *reply);
}; };
} // namespace test_namespace } // namespace test_namespace

View File

@ -23,6 +23,7 @@ PFXPetApi::PFXPetApi(const QString &scheme, const QString &host, int port, const
_port(port), _port(port),
_basePath(basePath), _basePath(basePath),
_timeOut(timeOut), _timeOut(timeOut),
_manager(nullptr),
isResponseCompressionEnabled(false), isResponseCompressionEnabled(false),
isRequestCompressionEnabled(false) {} isRequestCompressionEnabled(false) {}
@ -53,6 +54,10 @@ void PFXPetApi::setWorkingDirectory(const QString &path) {
_workingDirectory = path; _workingDirectory = path;
} }
void PFXPetApi::setNetworkAccessManager(QNetworkAccessManager* manager) {
_manager = manager;
}
void PFXPetApi::addHeaders(const QString &key, const QString &value) { void PFXPetApi::addHeaders(const QString &key, const QString &value) {
defaultHeaders.insert(key, value); defaultHeaders.insert(key, value);
} }
@ -77,7 +82,7 @@ void PFXPetApi::addPet(const PFXPet &body) {
.arg(_basePath) .arg(_basePath)
.arg("/pet"); .arg("/pet");
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this); PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut); worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory); worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "POST"); PFXHttpRequestInput input(fullPath, "POST");
@ -125,7 +130,7 @@ void PFXPetApi::deletePet(const qint64 &pet_id, const QString &api_key) {
pet_idPathParam.append("petId").append("}"); pet_idPathParam.append("petId").append("}");
fullPath.replace(pet_idPathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(pet_id))); fullPath.replace(pet_idPathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(pet_id)));
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this); PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut); worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory); worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "DELETE"); PFXHttpRequestInput input(fullPath, "DELETE");
@ -209,7 +214,7 @@ void PFXPetApi::findPetsByStatus(const QList<QString> &status) {
} }
} }
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this); PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut); worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory); worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "GET"); PFXHttpRequestInput input(fullPath, "GET");
@ -299,7 +304,7 @@ void PFXPetApi::findPetsByTags(const QList<QString> &tags) {
} }
} }
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this); PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut); worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory); worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "GET"); PFXHttpRequestInput input(fullPath, "GET");
@ -354,7 +359,7 @@ void PFXPetApi::getPetById(const qint64 &pet_id) {
pet_idPathParam.append("petId").append("}"); pet_idPathParam.append("petId").append("}");
fullPath.replace(pet_idPathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(pet_id))); fullPath.replace(pet_idPathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(pet_id)));
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this); PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut); worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory); worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "GET"); PFXHttpRequestInput input(fullPath, "GET");
@ -397,7 +402,7 @@ void PFXPetApi::updatePet(const PFXPet &body) {
.arg(_basePath) .arg(_basePath)
.arg("/pet"); .arg("/pet");
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this); PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut); worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory); worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "PUT"); PFXHttpRequestInput input(fullPath, "PUT");
@ -445,7 +450,7 @@ void PFXPetApi::updatePetWithForm(const qint64 &pet_id, const QString &name, con
pet_idPathParam.append("petId").append("}"); pet_idPathParam.append("petId").append("}");
fullPath.replace(pet_idPathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(pet_id))); fullPath.replace(pet_idPathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(pet_id)));
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this); PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut); worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory); worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "POST"); PFXHttpRequestInput input(fullPath, "POST");
@ -492,7 +497,7 @@ void PFXPetApi::uploadFile(const qint64 &pet_id, const QString &additional_metad
pet_idPathParam.append("petId").append("}"); pet_idPathParam.append("petId").append("}");
fullPath.replace(pet_idPathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(pet_id))); fullPath.replace(pet_idPathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(pet_id)));
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this); PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut); worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory); worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "POST"); PFXHttpRequestInput input(fullPath, "POST");

View File

@ -20,6 +20,7 @@
#include <QString> #include <QString>
#include <QObject> #include <QObject>
#include <QNetworkAccessManager>
namespace test_namespace { namespace test_namespace {
@ -36,6 +37,7 @@ public:
void setBasePath(const QString &basePath); void setBasePath(const QString &basePath);
void setTimeOut(const int timeOut); void setTimeOut(const int timeOut);
void setWorkingDirectory(const QString &path); void setWorkingDirectory(const QString &path);
void setNetworkAccessManager(QNetworkAccessManager* manager);
void addHeaders(const QString &key, const QString &value); void addHeaders(const QString &key, const QString &value);
void enableRequestCompression(); void enableRequestCompression();
void enableResponseCompression(); void enableResponseCompression();
@ -56,6 +58,7 @@ private:
QString _basePath; QString _basePath;
int _timeOut; int _timeOut;
QString _workingDirectory; QString _workingDirectory;
QNetworkAccessManager* _manager;
QMap<QString, QString> defaultHeaders; QMap<QString, QString> defaultHeaders;
bool isResponseCompressionEnabled; bool isResponseCompressionEnabled;
bool isRequestCompressionEnabled; bool isRequestCompressionEnabled;

View File

@ -23,6 +23,7 @@ PFXStoreApi::PFXStoreApi(const QString &scheme, const QString &host, int port, c
_port(port), _port(port),
_basePath(basePath), _basePath(basePath),
_timeOut(timeOut), _timeOut(timeOut),
_manager(nullptr),
isResponseCompressionEnabled(false), isResponseCompressionEnabled(false),
isRequestCompressionEnabled(false) {} isRequestCompressionEnabled(false) {}
@ -53,6 +54,10 @@ void PFXStoreApi::setWorkingDirectory(const QString &path) {
_workingDirectory = path; _workingDirectory = path;
} }
void PFXStoreApi::setNetworkAccessManager(QNetworkAccessManager* manager) {
_manager = manager;
}
void PFXStoreApi::addHeaders(const QString &key, const QString &value) { void PFXStoreApi::addHeaders(const QString &key, const QString &value) {
defaultHeaders.insert(key, value); defaultHeaders.insert(key, value);
} }
@ -80,7 +85,7 @@ void PFXStoreApi::deleteOrder(const QString &order_id) {
order_idPathParam.append("orderId").append("}"); order_idPathParam.append("orderId").append("}");
fullPath.replace(order_idPathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(order_id))); fullPath.replace(order_idPathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(order_id)));
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this); PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut); worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory); worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "DELETE"); PFXHttpRequestInput input(fullPath, "DELETE");
@ -122,7 +127,7 @@ void PFXStoreApi::getInventory() {
.arg(_basePath) .arg(_basePath)
.arg("/store/inventory"); .arg("/store/inventory");
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this); PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut); worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory); worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "GET"); PFXHttpRequestInput input(fullPath, "GET");
@ -177,7 +182,7 @@ void PFXStoreApi::getOrderById(const qint64 &order_id) {
order_idPathParam.append("orderId").append("}"); order_idPathParam.append("orderId").append("}");
fullPath.replace(order_idPathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(order_id))); fullPath.replace(order_idPathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(order_id)));
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this); PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut); worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory); worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "GET"); PFXHttpRequestInput input(fullPath, "GET");
@ -220,7 +225,7 @@ void PFXStoreApi::placeOrder(const PFXOrder &body) {
.arg(_basePath) .arg(_basePath)
.arg("/store/order"); .arg("/store/order");
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this); PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut); worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory); worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "POST"); PFXHttpRequestInput input(fullPath, "POST");

View File

@ -19,6 +19,7 @@
#include <QString> #include <QString>
#include <QObject> #include <QObject>
#include <QNetworkAccessManager>
namespace test_namespace { namespace test_namespace {
@ -35,6 +36,7 @@ public:
void setBasePath(const QString &basePath); void setBasePath(const QString &basePath);
void setTimeOut(const int timeOut); void setTimeOut(const int timeOut);
void setWorkingDirectory(const QString &path); void setWorkingDirectory(const QString &path);
void setNetworkAccessManager(QNetworkAccessManager* manager);
void addHeaders(const QString &key, const QString &value); void addHeaders(const QString &key, const QString &value);
void enableRequestCompression(); void enableRequestCompression();
void enableResponseCompression(); void enableResponseCompression();
@ -51,6 +53,7 @@ private:
QString _basePath; QString _basePath;
int _timeOut; int _timeOut;
QString _workingDirectory; QString _workingDirectory;
QNetworkAccessManager* _manager;
QMap<QString, QString> defaultHeaders; QMap<QString, QString> defaultHeaders;
bool isResponseCompressionEnabled; bool isResponseCompressionEnabled;
bool isRequestCompressionEnabled; bool isRequestCompressionEnabled;

View File

@ -23,6 +23,7 @@ PFXUserApi::PFXUserApi(const QString &scheme, const QString &host, int port, con
_port(port), _port(port),
_basePath(basePath), _basePath(basePath),
_timeOut(timeOut), _timeOut(timeOut),
_manager(nullptr),
isResponseCompressionEnabled(false), isResponseCompressionEnabled(false),
isRequestCompressionEnabled(false) {} isRequestCompressionEnabled(false) {}
@ -53,6 +54,10 @@ void PFXUserApi::setWorkingDirectory(const QString &path) {
_workingDirectory = path; _workingDirectory = path;
} }
void PFXUserApi::setNetworkAccessManager(QNetworkAccessManager* manager) {
_manager = manager;
}
void PFXUserApi::addHeaders(const QString &key, const QString &value) { void PFXUserApi::addHeaders(const QString &key, const QString &value) {
defaultHeaders.insert(key, value); defaultHeaders.insert(key, value);
} }
@ -77,7 +82,7 @@ void PFXUserApi::createUser(const PFXUser &body) {
.arg(_basePath) .arg(_basePath)
.arg("/user"); .arg("/user");
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this); PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut); worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory); worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "POST"); PFXHttpRequestInput input(fullPath, "POST");
@ -122,7 +127,7 @@ void PFXUserApi::createUsersWithArrayInput(const QList<PFXUser> &body) {
.arg(_basePath) .arg(_basePath)
.arg("/user/createWithArray"); .arg("/user/createWithArray");
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this); PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut); worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory); worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "POST"); PFXHttpRequestInput input(fullPath, "POST");
@ -168,7 +173,7 @@ void PFXUserApi::createUsersWithListInput(const QList<PFXUser> &body) {
.arg(_basePath) .arg(_basePath)
.arg("/user/createWithList"); .arg("/user/createWithList");
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this); PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut); worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory); worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "POST"); PFXHttpRequestInput input(fullPath, "POST");
@ -217,7 +222,7 @@ void PFXUserApi::deleteUser(const QString &username) {
usernamePathParam.append("username").append("}"); usernamePathParam.append("username").append("}");
fullPath.replace(usernamePathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(username))); fullPath.replace(usernamePathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(username)));
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this); PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut); worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory); worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "DELETE"); PFXHttpRequestInput input(fullPath, "DELETE");
@ -262,7 +267,7 @@ void PFXUserApi::getUserByName(const QString &username) {
usernamePathParam.append("username").append("}"); usernamePathParam.append("username").append("}");
fullPath.replace(usernamePathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(username))); fullPath.replace(usernamePathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(username)));
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this); PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut); worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory); worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "GET"); PFXHttpRequestInput input(fullPath, "GET");
@ -317,7 +322,7 @@ void PFXUserApi::loginUser(const QString &username, const QString &password) {
fullPath.append("?"); fullPath.append("?");
fullPath.append(QUrl::toPercentEncoding("password")).append("=").append(QUrl::toPercentEncoding(::test_namespace::toStringValue(password))); fullPath.append(QUrl::toPercentEncoding("password")).append("=").append(QUrl::toPercentEncoding(::test_namespace::toStringValue(password)));
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this); PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut); worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory); worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "GET"); PFXHttpRequestInput input(fullPath, "GET");
@ -361,7 +366,7 @@ void PFXUserApi::logoutUser() {
.arg(_basePath) .arg(_basePath)
.arg("/user/logout"); .arg("/user/logout");
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this); PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut); worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory); worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "GET"); PFXHttpRequestInput input(fullPath, "GET");
@ -406,7 +411,7 @@ void PFXUserApi::updateUser(const QString &username, const PFXUser &body) {
usernamePathParam.append("username").append("}"); usernamePathParam.append("username").append("}");
fullPath.replace(usernamePathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(username))); fullPath.replace(usernamePathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(username)));
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this); PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut); worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory); worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "PUT"); PFXHttpRequestInput input(fullPath, "PUT");

View File

@ -19,6 +19,7 @@
#include <QString> #include <QString>
#include <QObject> #include <QObject>
#include <QNetworkAccessManager>
namespace test_namespace { namespace test_namespace {
@ -35,6 +36,7 @@ public:
void setBasePath(const QString &basePath); void setBasePath(const QString &basePath);
void setTimeOut(const int timeOut); void setTimeOut(const int timeOut);
void setWorkingDirectory(const QString &path); void setWorkingDirectory(const QString &path);
void setNetworkAccessManager(QNetworkAccessManager* manager);
void addHeaders(const QString &key, const QString &value); void addHeaders(const QString &key, const QString &value);
void enableRequestCompression(); void enableRequestCompression();
void enableResponseCompression(); void enableResponseCompression();
@ -55,6 +57,7 @@ private:
QString _basePath; QString _basePath;
int _timeOut; int _timeOut;
QString _workingDirectory; QString _workingDirectory;
QNetworkAccessManager* _manager;
QMap<QString, QString> defaultHeaders; QMap<QString, QString> defaultHeaders;
bool isResponseCompressionEnabled; bool isResponseCompressionEnabled;
bool isRequestCompressionEnabled; bool isRequestCompressionEnabled;