Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ void {{prefix}}HttpRequestInput::add_file(QString variable_name, QString local_f
files.append(file);
}

{{prefix}}HttpRequestWorker::{{prefix}}HttpRequestWorker(QObject *parent)
: QObject(parent), manager(nullptr), timeOutTimer(this), isResponseCompressionEnabled(false), isRequestCompressionEnabled(false), httpResponseCode(-1) {
{{prefix}}HttpRequestWorker::{{prefix}}HttpRequestWorker(QObject *parent, QNetworkAccessManager *_manager)
: QObject(parent), manager(_manager), timeOutTimer(this), isResponseCompressionEnabled(false), isRequestCompressionEnabled(false), httpResponseCode(-1) {
qsrand(QDateTime::currentDateTime().toTime_t());
manager = new QNetworkAccessManager(this);
if (manager == nullptr) {
manager = new QNetworkAccessManager(this);
}
workingDirectory = QDir::currentPath();
connect(manager, &QNetworkAccessManager::finished, this, &{{prefix}}HttpRequestWorker::on_manager_finished);
timeOutTimer.setSingleShot(true);
}

Expand Down Expand Up @@ -347,7 +348,7 @@ void {{prefix}}HttpRequestWorker::execute({{prefix}}HttpRequestInput *input) {
reply = manager->deleteResource(request);
} else {
#if (QT_VERSION >= 0x050800)
manager->sendCustomRequest(request, input->http_method.toLatin1(), request_content);
reply = manager->sendCustomRequest(request, input->http_method.toLatin1(), request_content);
#else
QBuffer *buffer = new QBuffer;
buffer->setData(request_content);
Expand All @@ -357,13 +358,21 @@ void {{prefix}}HttpRequestWorker::execute({{prefix}}HttpRequestInput *input) {
buffer->setParent(reply);
#endif
}
if (reply != nullptr) {
reply->setParent(this);
connect(reply, &QNetworkReply::finished, [this, reply] {
on_reply_finished(reply);
});
}
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();
}
}

void {{prefix}}HttpRequestWorker::on_manager_finished(QNetworkReply *reply) {
void {{prefix}}HttpRequestWorker::on_reply_finished(QNetworkReply *reply) {
bool codeSts = false;
if(timeOutTimer.isActive()) {
QObject::disconnect(&timeOutTimer, &QTimer::timeout, nullptr, nullptr);
Expand All @@ -387,11 +396,11 @@ void {{prefix}}HttpRequestWorker::on_manager_finished(QNetworkReply *reply) {
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;
response = "";
error_str = "Timed out waiting for response";
disconnect(manager, nullptr, nullptr, nullptr);
disconnect(reply, nullptr, nullptr, nullptr);
reply->abort();
reply->deleteLater();
emit on_execution_finished(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class {{prefix}}HttpRequestWorker : public QObject {
Q_OBJECT

public:
explicit {{prefix}}HttpRequestWorker(QObject *parent = nullptr);
explicit {{prefix}}HttpRequestWorker(QObject *parent = nullptr, QNetworkAccessManager *manager = nullptr);
virtual ~{{prefix}}HttpRequestWorker();

QByteArray response;
Expand Down Expand Up @@ -87,13 +87,11 @@ private:
bool isRequestCompressionEnabled;
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);
QByteArray decompress(const QByteArray& data);
QByteArray compress(const QByteArray& input, int level, {{prefix}}CompressionType compressType);

private slots:
void on_manager_finished(QNetworkReply *reply);
};

{{#cppNamespaceDeclarations}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace {{this}} {
_port(port),
_basePath(basePath),
_timeOut(timeOut),
_manager(nullptr),
isResponseCompressionEnabled(false),
isRequestCompressionEnabled(false) {}

Expand Down Expand Up @@ -45,6 +46,10 @@ void {{classname}}::setWorkingDirectory(const QString &path) {
_workingDirectory = path;
}

void {{classname}}::setNetworkAccessManager(QNetworkAccessManager* manager) {
_manager = manager;
}

void {{classname}}::addHeaders(const QString &key, const QString &value) {
defaultHeaders.insert(key, value);
}
Expand Down Expand Up @@ -120,7 +125,7 @@ void {{classname}}::{{nickname}}({{#allParams}}const {{{dataType}}} &{{paramName
}
}
{{/collectionFormat}}{{/queryParams}}
{{prefix}}HttpRequestWorker *worker = new {{prefix}}HttpRequestWorker(this);
{{prefix}}HttpRequestWorker *worker = new {{prefix}}HttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);{{#contentCompression}}
worker->setResponseCompressionEnabled(isResponseCompressionEnabled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
{{/imports}}

#include <QObject>
#include <QNetworkAccessManager>

{{#cppNamespaceDeclarations}}
namespace {{this}} {
Expand All @@ -26,6 +27,7 @@ public:
void setBasePath(const QString &basePath);
void setTimeOut(const int timeOut);
void setWorkingDirectory(const QString &path);
void setNetworkAccessManager(QNetworkAccessManager* manager);
void addHeaders(const QString &key, const QString &value);
void enableRequestCompression();
void enableResponseCompression();
Expand All @@ -39,6 +41,7 @@ private:
QString _basePath;
int _timeOut;
QString _workingDirectory;
QNetworkAccessManager* _manager;
QMap<QString, QString> defaultHeaders;
bool isResponseCompressionEnabled;
bool isRequestCompressionEnabled;
Expand Down
27 changes: 18 additions & 9 deletions samples/client/petstore/cpp-qt5/client/PFXHttpRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ void PFXHttpRequestInput::add_file(QString variable_name, QString local_filename
files.append(file);
}

PFXHttpRequestWorker::PFXHttpRequestWorker(QObject *parent)
: QObject(parent), manager(nullptr), timeOutTimer(this), isResponseCompressionEnabled(false), isRequestCompressionEnabled(false), httpResponseCode(-1) {
PFXHttpRequestWorker::PFXHttpRequestWorker(QObject *parent, QNetworkAccessManager *_manager)
: QObject(parent), manager(_manager), timeOutTimer(this), isResponseCompressionEnabled(false), isRequestCompressionEnabled(false), httpResponseCode(-1) {
qsrand(QDateTime::currentDateTime().toTime_t());
manager = new QNetworkAccessManager(this);
if (manager == nullptr) {
manager = new QNetworkAccessManager(this);
}
workingDirectory = QDir::currentPath();
connect(manager, &QNetworkAccessManager::finished, this, &PFXHttpRequestWorker::on_manager_finished);
timeOutTimer.setSingleShot(true);
}

Expand Down Expand Up @@ -354,7 +355,7 @@ void PFXHttpRequestWorker::execute(PFXHttpRequestInput *input) {
reply = manager->deleteResource(request);
} else {
#if (QT_VERSION >= 0x050800)
manager->sendCustomRequest(request, input->http_method.toLatin1(), request_content);
reply = manager->sendCustomRequest(request, input->http_method.toLatin1(), request_content);
#else
QBuffer *buffer = new QBuffer;
buffer->setData(request_content);
Expand All @@ -364,13 +365,21 @@ void PFXHttpRequestWorker::execute(PFXHttpRequestInput *input) {
buffer->setParent(reply);
#endif
}
if (reply != nullptr) {
reply->setParent(this);
connect(reply, &QNetworkReply::finished, [this, reply] {
on_reply_finished(reply);
});
}
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();
}
}

void PFXHttpRequestWorker::on_manager_finished(QNetworkReply *reply) {
void PFXHttpRequestWorker::on_reply_finished(QNetworkReply *reply) {
bool codeSts = false;
if(timeOutTimer.isActive()) {
QObject::disconnect(&timeOutTimer, &QTimer::timeout, nullptr, nullptr);
Expand All @@ -394,11 +403,11 @@ void PFXHttpRequestWorker::on_manager_finished(QNetworkReply *reply) {
emit on_execution_finished(this);
}

void PFXHttpRequestWorker::on_manager_timeout(QNetworkReply *reply) {
void PFXHttpRequestWorker::on_reply_timeout(QNetworkReply *reply) {
error_type = QNetworkReply::TimeoutError;
response = "";
error_str = "Timed out waiting for response";
disconnect(manager, nullptr, nullptr, nullptr);
disconnect(reply, nullptr, nullptr, nullptr);
reply->abort();
reply->deleteLater();
emit on_execution_finished(this);
Expand Down
8 changes: 3 additions & 5 deletions samples/client/petstore/cpp-qt5/client/PFXHttpRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class PFXHttpRequestWorker : public QObject {
Q_OBJECT

public:
explicit PFXHttpRequestWorker(QObject *parent = nullptr);
explicit PFXHttpRequestWorker(QObject *parent = nullptr, QNetworkAccessManager *manager = nullptr);
virtual ~PFXHttpRequestWorker();

QByteArray response;
Expand Down Expand Up @@ -95,13 +95,11 @@ class PFXHttpRequestWorker : public QObject {
bool isRequestCompressionEnabled;
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);
QByteArray decompress(const QByteArray& data);
QByteArray compress(const QByteArray& input, int level, PFXCompressionType compressType);

private slots:
void on_manager_finished(QNetworkReply *reply);
};

} // namespace test_namespace
Expand Down
21 changes: 13 additions & 8 deletions samples/client/petstore/cpp-qt5/client/PFXPetApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ PFXPetApi::PFXPetApi(const QString &scheme, const QString &host, int port, const
_port(port),
_basePath(basePath),
_timeOut(timeOut),
_manager(nullptr),
isResponseCompressionEnabled(false),
isRequestCompressionEnabled(false) {}

Expand Down Expand Up @@ -53,6 +54,10 @@ void PFXPetApi::setWorkingDirectory(const QString &path) {
_workingDirectory = path;
}

void PFXPetApi::setNetworkAccessManager(QNetworkAccessManager* manager) {
_manager = manager;
}

void PFXPetApi::addHeaders(const QString &key, const QString &value) {
defaultHeaders.insert(key, value);
}
Expand All @@ -77,7 +82,7 @@ void PFXPetApi::addPet(const PFXPet &body) {
.arg(_basePath)
.arg("/pet");

PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this);
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "POST");
Expand Down Expand Up @@ -125,7 +130,7 @@ void PFXPetApi::deletePet(const qint64 &pet_id, const QString &api_key) {
pet_idPathParam.append("petId").append("}");
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->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "DELETE");
Expand Down Expand Up @@ -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->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "GET");
Expand Down Expand Up @@ -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->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "GET");
Expand Down Expand Up @@ -354,7 +359,7 @@ void PFXPetApi::getPetById(const qint64 &pet_id) {
pet_idPathParam.append("petId").append("}");
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->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "GET");
Expand Down Expand Up @@ -397,7 +402,7 @@ void PFXPetApi::updatePet(const PFXPet &body) {
.arg(_basePath)
.arg("/pet");

PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this);
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "PUT");
Expand Down Expand Up @@ -445,7 +450,7 @@ void PFXPetApi::updatePetWithForm(const qint64 &pet_id, const QString &name, con
pet_idPathParam.append("petId").append("}");
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->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "POST");
Expand Down Expand Up @@ -492,7 +497,7 @@ void PFXPetApi::uploadFile(const qint64 &pet_id, const QString &additional_metad
pet_idPathParam.append("petId").append("}");
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->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "POST");
Expand Down
3 changes: 3 additions & 0 deletions samples/client/petstore/cpp-qt5/client/PFXPetApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <QString>

#include <QObject>
#include <QNetworkAccessManager>

namespace test_namespace {

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