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

[C++] [Qt5] Add API timeout handling #3078

Merged
merged 1 commit into from
Jun 12, 2019
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 @@ -45,18 +45,29 @@ void {{prefix}}HttpRequestInput::add_file(QString variable_name, QString local_f
: QObject(parent), manager(nullptr)
{
qsrand(QDateTime::currentDateTime().toTime_t());

timeout = 0;
timer = new QTimer();
manager = new QNetworkAccessManager(this);
connect(manager, &QNetworkAccessManager::finished, this, &{{prefix}}HttpRequestWorker::on_manager_finished);
}

{{prefix}}HttpRequestWorker::~{{prefix}}HttpRequestWorker() {
if(timer != nullptr){
if(timer->isActive()){
timer->stop();
}
timer->deleteLater();
}
}

QMap<QByteArray, QByteArray> {{prefix}}HttpRequestWorker::getResponseHeaders() const {
return headers;
}

void {{prefix}}HttpRequestWorker::setTimeOut(int tout){
timeout = tout;
}

QString {{prefix}}HttpRequestWorker::http_attribute_encode(QString attribute_name, QString input) {
// result structure follows RFC 5987
bool need_utf_encoding = false;
Expand Down Expand Up @@ -108,7 +119,7 @@ QString {{prefix}}HttpRequestWorker::http_attribute_encode(QString attribute_nam
void {{prefix}}HttpRequestWorker::execute({{prefix}}HttpRequestInput *input) {

// reset variables

QNetworkReply* reply = nullptr;
QByteArray request_content = "";
response = "";
error_type = QNetworkReply::NoError;
Expand Down Expand Up @@ -276,19 +287,19 @@ void {{prefix}}HttpRequestWorker::execute({{prefix}}HttpRequestInput *input) {
}

if (input->http_method == "GET") {
manager->get(request);
reply = manager->get(request);
}
else if (input->http_method == "POST") {
manager->post(request, request_content);
reply = manager->post(request, request_content);
}
else if (input->http_method == "PUT") {
manager->put(request, request_content);
reply = manager->put(request, request_content);
}
else if (input->http_method == "HEAD") {
manager->head(request);
reply = manager->head(request);
}
else if (input->http_method == "DELETE") {
manager->deleteResource(request);
reply = manager->deleteResource(request);
}
else {
#if (QT_VERSION >= 0x050800)
Expand All @@ -298,11 +309,16 @@ void {{prefix}}HttpRequestWorker::execute({{prefix}}HttpRequestInput *input) {
buffer->setData(request_content);
buffer->open(QIODevice::ReadOnly);

QNetworkReply* reply = manager->sendCustomRequest(request, input->http_method.toLatin1(), buffer);
reply = manager->sendCustomRequest(request, input->http_method.toLatin1(), buffer);
buffer->setParent(reply);
#endif
}

if(timeout > 0){
timer->setSingleShot(true);
timer->setInterval(timeout);
connect(timer, &QTimer::timeout, this, [=](){ on_manager_timeout(reply); });
timer->start();
}
}

void {{prefix}}HttpRequestWorker::on_manager_finished(QNetworkReply *reply) {
Expand All @@ -318,6 +334,16 @@ void {{prefix}}HttpRequestWorker::on_manager_finished(QNetworkReply *reply) {

emit on_execution_finished(this);
}
void {{prefix}}HttpRequestWorker::on_manager_timeout(QNetworkReply *reply) {
error_type = QNetworkReply::TimeoutError;
response = "";
error_str = "Timed out waiting for response";
disconnect(manager, nullptr, nullptr, nullptr);
reply->abort();
reply->deleteLater();

emit on_execution_finished(this);
}
QSslConfiguration* {{prefix}}HttpRequestWorker::sslDefaultConfiguration;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <QObject>
#include <QString>
#include <QTimer>
#include <QMap>
#include <QNetworkAccessManager>
#include <QNetworkReply>
Expand Down Expand Up @@ -60,24 +61,25 @@ public:
QByteArray response;
QNetworkReply::NetworkError error_type;
QString error_str;

QTimer *timer;
explicit {{prefix}}HttpRequestWorker(QObject *parent = nullptr);
virtual ~{{prefix}}HttpRequestWorker();

QMap<QByteArray, QByteArray> getResponseHeaders() const;
QString http_attribute_encode(QString attribute_name, QString input);
void execute({{prefix}}HttpRequestInput *input);
static QSslConfiguration* sslDefaultConfiguration;

void setTimeOut(int tout);
signals:
void on_execution_finished({{prefix}}HttpRequestWorker *worker);

private:
QNetworkAccessManager *manager;
QMap<QByteArray, QByteArray> headers;
int timeout;
void on_manager_timeout(QNetworkReply *reply);
private slots:
void on_manager_finished(QNetworkReply *reply);

void on_manager_finished(QNetworkReply *reply);
};

{{#cppNamespaceDeclarations}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ namespace {{this}} {
{{/cppNamespaceDeclarations}}

{{classname}}::{{classname}}() : basePath("{{{basePathWithoutHost}}}"),
host("{{#serverHost}}{{#scheme}}{{scheme}}://{{/scheme}}{{serverHost}}{{#serverPort}}:{{serverPort}}{{/serverPort}}{{/serverHost}}") {
host("{{#serverHost}}{{#scheme}}{{scheme}}://{{/scheme}}{{serverHost}}{{#serverPort}}:{{serverPort}}{{/serverPort}}{{/serverHost}}"),
timeout(0){

}

{{classname}}::~{{classname}}() {

}

{{classname}}::{{classname}}(const QString& host, const QString& basePath) {
{{classname}}::{{classname}}(const QString& host, const QString& basePath, const int tout) {
this->host = host;
this->basePath = basePath;
this->timeout = tout;
}

void {{classname}}::setBasePath(const QString& basePath){
Expand All @@ -31,6 +33,10 @@ void {{classname}}::setHost(const QString& host){
this->host = host;
}

void {{classname}}::setApiTimeOutMs(const int tout){
timeout = tout;
}

void {{classname}}::addHeaders(const QString& key, const QString& value){
defaultHeaders.insert(key, value);
}
Expand Down Expand Up @@ -97,6 +103,7 @@ void
}
{{/collectionFormat}}{{/queryParams}}
{{prefix}}HttpRequestWorker *worker = new {{prefix}}HttpRequestWorker();
worker->setTimeOut(timeout);
{{prefix}}HttpRequestInput input(fullPath, "{{httpMethod}}");
{{#formParams}}
if ({{paramName}} != nullptr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ class {{classname}}: public QObject {

public:
{{classname}}();
{{classname}}(const QString& host, const QString& basePath);
{{classname}}(const QString& host, const QString& basePath, const int toutMs = 0);
~{{classname}}();

void setBasePath(const QString& basePath);
void setHost(const QString& host);
void setApiTimeOutMs(const int tout);
void addHeaders(const QString& key, const QString& value);

{{#operations}}{{#operation}}void {{nickname}}({{#allParams}}const {{{dataType}}}& {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
{{/operation}}{{/operations}}
private:
QString basePath;
QString host;
int timeout;
QMap<QString, QString> defaultHeaders;
{{#operations}}{{#operation}}void {{nickname}}Callback ({{prefix}}HttpRequestWorker * worker);
{{/operation}}{{/operations}}
Expand Down
44 changes: 35 additions & 9 deletions samples/client/petstore/cpp-qt5/client/OAIHttpRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,29 @@ OAIHttpRequestWorker::OAIHttpRequestWorker(QObject *parent)
: QObject(parent), manager(nullptr)
{
qsrand(QDateTime::currentDateTime().toTime_t());

timeout = 0;
timer = new QTimer();
manager = new QNetworkAccessManager(this);
connect(manager, &QNetworkAccessManager::finished, this, &OAIHttpRequestWorker::on_manager_finished);
}

OAIHttpRequestWorker::~OAIHttpRequestWorker() {
if(timer != nullptr){
if(timer->isActive()){
timer->stop();
}
timer->deleteLater();
}
}

QMap<QByteArray, QByteArray> OAIHttpRequestWorker::getResponseHeaders() const {
return headers;
}

void OAIHttpRequestWorker::setTimeOut(int tout){
timeout = tout;
}

QString OAIHttpRequestWorker::http_attribute_encode(QString attribute_name, QString input) {
// result structure follows RFC 5987
bool need_utf_encoding = false;
Expand Down Expand Up @@ -117,7 +128,7 @@ QString OAIHttpRequestWorker::http_attribute_encode(QString attribute_name, QStr
void OAIHttpRequestWorker::execute(OAIHttpRequestInput *input) {

// reset variables

QNetworkReply* reply = nullptr;
QByteArray request_content = "";
response = "";
error_type = QNetworkReply::NoError;
Expand Down Expand Up @@ -285,19 +296,19 @@ void OAIHttpRequestWorker::execute(OAIHttpRequestInput *input) {
}

if (input->http_method == "GET") {
manager->get(request);
reply = manager->get(request);
}
else if (input->http_method == "POST") {
manager->post(request, request_content);
reply = manager->post(request, request_content);
}
else if (input->http_method == "PUT") {
manager->put(request, request_content);
reply = manager->put(request, request_content);
}
else if (input->http_method == "HEAD") {
manager->head(request);
reply = manager->head(request);
}
else if (input->http_method == "DELETE") {
manager->deleteResource(request);
reply = manager->deleteResource(request);
}
else {
#if (QT_VERSION >= 0x050800)
Expand All @@ -307,11 +318,16 @@ void OAIHttpRequestWorker::execute(OAIHttpRequestInput *input) {
buffer->setData(request_content);
buffer->open(QIODevice::ReadOnly);

QNetworkReply* reply = manager->sendCustomRequest(request, input->http_method.toLatin1(), buffer);
reply = manager->sendCustomRequest(request, input->http_method.toLatin1(), buffer);
buffer->setParent(reply);
#endif
}

if(timeout > 0){
timer->setSingleShot(true);
timer->setInterval(timeout);
connect(timer, &QTimer::timeout, this, [=](){ on_manager_timeout(reply); });
timer->start();
}
}

void OAIHttpRequestWorker::on_manager_finished(QNetworkReply *reply) {
Expand All @@ -327,6 +343,16 @@ void OAIHttpRequestWorker::on_manager_finished(QNetworkReply *reply) {

emit on_execution_finished(this);
}
void OAIHttpRequestWorker::on_manager_timeout(QNetworkReply *reply) {
error_type = QNetworkReply::TimeoutError;
response = "";
error_str = "Timed out waiting for response";
disconnect(manager, nullptr, nullptr, nullptr);
reply->abort();
reply->deleteLater();

emit on_execution_finished(this);
}
QSslConfiguration* OAIHttpRequestWorker::sslDefaultConfiguration;


Expand Down
10 changes: 6 additions & 4 deletions samples/client/petstore/cpp-qt5/client/OAIHttpRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <QObject>
#include <QString>
#include <QTimer>
#include <QMap>
#include <QNetworkAccessManager>
#include <QNetworkReply>
Expand Down Expand Up @@ -69,24 +70,25 @@ class OAIHttpRequestWorker : public QObject {
QByteArray response;
QNetworkReply::NetworkError error_type;
QString error_str;

QTimer *timer;
explicit OAIHttpRequestWorker(QObject *parent = nullptr);
virtual ~OAIHttpRequestWorker();

QMap<QByteArray, QByteArray> getResponseHeaders() const;
QString http_attribute_encode(QString attribute_name, QString input);
void execute(OAIHttpRequestInput *input);
static QSslConfiguration* sslDefaultConfiguration;

void setTimeOut(int tout);
signals:
void on_execution_finished(OAIHttpRequestWorker *worker);

private:
QNetworkAccessManager *manager;
QMap<QByteArray, QByteArray> headers;
int timeout;
void on_manager_timeout(QNetworkReply *reply);
private slots:
void on_manager_finished(QNetworkReply *reply);

void on_manager_finished(QNetworkReply *reply);
};

}
Expand Down
Loading