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 2 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,6 +358,12 @@ void {{prefix}}HttpRequestWorker::execute({{prefix}}HttpRequestInput *input) {
buffer->setParent(reply);
#endif
}
if (reply != nullptr) {
reply->setParent(this);
connect(reply, &QNetworkReply::finished, [this, reply] {
on_manager_finished(reply);
});
}
if (timeOutTimer.interval() > 0) {
QObject::connect(&timeOutTimer, &QTimer::timeout, [=]() { on_manager_timeout(reply); });
timeOutTimer.start();
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
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
17 changes: 12 additions & 5 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,6 +365,12 @@ void PFXHttpRequestWorker::execute(PFXHttpRequestInput *input) {
buffer->setParent(reply);
#endif
}
if (reply != nullptr) {
reply->setParent(this);
connect(reply, &QNetworkReply::finished, [this, reply] {
on_manager_finished(reply);
});
}
if (timeOutTimer.interval() > 0) {
QObject::connect(&timeOutTimer, &QTimer::timeout, [=]() { on_manager_timeout(reply); });
timeOutTimer.start();
Expand Down
2 changes: 1 addition & 1 deletion 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
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
13 changes: 9 additions & 4 deletions samples/client/petstore/cpp-qt5/client/PFXStoreApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ PFXStoreApi::PFXStoreApi(const QString &scheme, const QString &host, int port, c
_port(port),
_basePath(basePath),
_timeOut(timeOut),
_manager(nullptr),
isResponseCompressionEnabled(false),
isRequestCompressionEnabled(false) {}

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

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

void PFXStoreApi::addHeaders(const QString &key, const QString &value) {
defaultHeaders.insert(key, value);
}
Expand Down Expand Up @@ -80,7 +85,7 @@ void PFXStoreApi::deleteOrder(const QString &order_id) {
order_idPathParam.append("orderId").append("}");
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->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "DELETE");
Expand Down Expand Up @@ -122,7 +127,7 @@ void PFXStoreApi::getInventory() {
.arg(_basePath)
.arg("/store/inventory");

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 @@ -177,7 +182,7 @@ void PFXStoreApi::getOrderById(const qint64 &order_id) {
order_idPathParam.append("orderId").append("}");
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->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "GET");
Expand Down Expand Up @@ -220,7 +225,7 @@ void PFXStoreApi::placeOrder(const PFXOrder &body) {
.arg(_basePath)
.arg("/store/order");

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/PFXStoreApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <QString>

#include <QObject>
#include <QNetworkAccessManager>

namespace test_namespace {

Expand All @@ -35,6 +36,7 @@ class PFXStoreApi : 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 @@ -51,6 +53,7 @@ class PFXStoreApi : public QObject {
QString _basePath;
int _timeOut;
QString _workingDirectory;
QNetworkAccessManager* _manager;
QMap<QString, QString> defaultHeaders;
bool isResponseCompressionEnabled;
bool isRequestCompressionEnabled;
Expand Down
Loading