forked from loafle/openapi-generator-original
[C++] [Qt5] Add request compression and handling identity (#5088)
* Add request and response compression and handling identity * Minor updates for identity and if conditions
This commit is contained in:
@@ -103,8 +103,12 @@ void PFXHttpRequestWorker::setWorkingDirectory(const QString &path) {
|
||||
}
|
||||
}
|
||||
|
||||
void PFXHttpRequestWorker::setCompressionEnabled(bool enable) {
|
||||
isCompressionEnabled = enable;
|
||||
void PFXHttpRequestWorker::setResponseCompressionEnabled(bool enable) {
|
||||
isResponseCompressionEnabled = enable;
|
||||
}
|
||||
|
||||
void PFXHttpRequestWorker::setRequestCompressionEnabled(bool enable) {
|
||||
isRequestCompressionEnabled = enable;
|
||||
}
|
||||
|
||||
QString PFXHttpRequestWorker::http_attribute_encode(QString attribute_name, QString input) {
|
||||
@@ -292,7 +296,11 @@ void PFXHttpRequestWorker::execute(PFXHttpRequestInput *input) {
|
||||
if (input->request_body.size() > 0) {
|
||||
qDebug() << "got a request body";
|
||||
request_content.clear();
|
||||
request_content.append(input->request_body);
|
||||
if(!isFormData && (input->var_layout != MULTIPART) && isRequestCompressionEnabled){
|
||||
request_content.append(compress(input->request_body, 7, PFXCompressionType::Gzip));
|
||||
} else {
|
||||
request_content.append(input->request_body);
|
||||
}
|
||||
}
|
||||
// prepare connection
|
||||
|
||||
@@ -309,14 +317,19 @@ void PFXHttpRequestWorker::execute(PFXHttpRequestInput *input) {
|
||||
} else {
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, input->headers.value("Content-Type"));
|
||||
}
|
||||
if(isRequestCompressionEnabled){
|
||||
request.setRawHeader("Content-Encoding", "gzip");
|
||||
}
|
||||
} else if (input->var_layout == URL_ENCODED) {
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||
} else if (input->var_layout == MULTIPART) {
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data; boundary=" + boundary);
|
||||
}
|
||||
|
||||
if(isCompressionEnabled){
|
||||
request.setRawHeader("Accept-Encoding", "deflate, gzip");
|
||||
if(isResponseCompressionEnabled){
|
||||
request.setRawHeader("Accept-Encoding", "gzip");
|
||||
} else {
|
||||
request.setRawHeader("Accept-Encoding", "identity");
|
||||
}
|
||||
|
||||
if (input->http_method == "GET") {
|
||||
@@ -398,6 +411,8 @@ void PFXHttpRequestWorker::process_response(QNetworkReply *reply) {
|
||||
auto compressionTypes = encoding.first().split(',', QString::SkipEmptyParts);
|
||||
if(compressionTypes.contains("gzip", Qt::CaseInsensitive) || compressionTypes.contains("deflate", Qt::CaseInsensitive)){
|
||||
response = decompress(reply->readAll());
|
||||
} else if(compressionTypes.contains("identity", Qt::CaseInsensitive)){
|
||||
response = reply->readAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -409,9 +424,17 @@ void PFXHttpRequestWorker::process_response(QNetworkReply *reply) {
|
||||
}
|
||||
|
||||
QByteArray PFXHttpRequestWorker::decompress(const QByteArray& data){
|
||||
QByteArray result;
|
||||
bool sts = false;
|
||||
return sts ? result : QByteArray();
|
||||
|
||||
Q_UNUSED(data);
|
||||
return QByteArray();
|
||||
}
|
||||
|
||||
QByteArray PFXHttpRequestWorker::compress(const QByteArray& input, int level, PFXCompressionType compressType) {
|
||||
|
||||
Q_UNUSED(input);
|
||||
Q_UNUSED(level);
|
||||
Q_UNUSED(compressType);
|
||||
return QByteArray();
|
||||
}
|
||||
|
||||
QSslConfiguration *PFXHttpRequestWorker::sslDefaultConfiguration;
|
||||
|
||||
@@ -71,21 +71,28 @@ public:
|
||||
void setWorkingDirectory(const QString &path);
|
||||
PFXHttpFileElement getHttpFileElement(const QString &fieldname = QString());
|
||||
QByteArray *getMultiPartField(const QString &fieldname = QString());
|
||||
void setCompressionEnabled(bool enable);
|
||||
void setResponseCompressionEnabled(bool enable);
|
||||
void setRequestCompressionEnabled(bool enable);
|
||||
signals:
|
||||
void on_execution_finished(PFXHttpRequestWorker *worker);
|
||||
|
||||
private:
|
||||
enum PFXCompressionType{
|
||||
Zlib,
|
||||
Gzip
|
||||
};
|
||||
QNetworkAccessManager *manager;
|
||||
QMap<QString, QString> headers;
|
||||
QMap<QString, PFXHttpFileElement> files;
|
||||
QMap<QString, QByteArray *> multiPartFields;
|
||||
QString workingDirectory;
|
||||
int _timeOut;
|
||||
bool isCompressionEnabled;
|
||||
bool isResponseCompressionEnabled;
|
||||
bool isRequestCompressionEnabled;
|
||||
void on_manager_timeout(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);
|
||||
};
|
||||
|
||||
@@ -23,7 +23,8 @@ PFXPetApi::PFXPetApi(const QString &scheme, const QString &host, int port, const
|
||||
_port(port),
|
||||
_basePath(basePath),
|
||||
_timeOut(timeOut),
|
||||
_compress(false) {}
|
||||
isResponseCompressionEnabled(false),
|
||||
isRequestCompressionEnabled(false) {}
|
||||
|
||||
PFXPetApi::~PFXPetApi() {
|
||||
}
|
||||
@@ -56,8 +57,12 @@ void PFXPetApi::addHeaders(const QString &key, const QString &value) {
|
||||
defaultHeaders.insert(key, value);
|
||||
}
|
||||
|
||||
void PFXPetApi::enableContentCompression() {
|
||||
_compress = true;
|
||||
void PFXPetApi::enableRequestCompression() {
|
||||
isRequestCompressionEnabled = true;
|
||||
}
|
||||
|
||||
void PFXPetApi::enableResponseCompression() {
|
||||
isResponseCompressionEnabled = true;
|
||||
}
|
||||
|
||||
void PFXPetApi::addPet(const PFXPet &body) {
|
||||
@@ -71,7 +76,6 @@ void PFXPetApi::addPet(const PFXPet &body) {
|
||||
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this);
|
||||
worker->setTimeOut(_timeOut);
|
||||
worker->setWorkingDirectory(_workingDirectory);
|
||||
worker->setCompressionEnabled(_compress);
|
||||
PFXHttpRequestInput input(fullPath, "POST");
|
||||
|
||||
QString output = body.asJson();
|
||||
@@ -120,7 +124,6 @@ void PFXPetApi::deletePet(const qint64 &pet_id, const QString &api_key) {
|
||||
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this);
|
||||
worker->setTimeOut(_timeOut);
|
||||
worker->setWorkingDirectory(_workingDirectory);
|
||||
worker->setCompressionEnabled(_compress);
|
||||
PFXHttpRequestInput input(fullPath, "DELETE");
|
||||
|
||||
if (api_key != nullptr) {
|
||||
@@ -205,7 +208,6 @@ void PFXPetApi::findPetsByStatus(const QList<QString> &status) {
|
||||
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this);
|
||||
worker->setTimeOut(_timeOut);
|
||||
worker->setWorkingDirectory(_workingDirectory);
|
||||
worker->setCompressionEnabled(_compress);
|
||||
PFXHttpRequestInput input(fullPath, "GET");
|
||||
|
||||
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); }
|
||||
@@ -296,7 +298,6 @@ void PFXPetApi::findPetsByTags(const QList<QString> &tags) {
|
||||
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this);
|
||||
worker->setTimeOut(_timeOut);
|
||||
worker->setWorkingDirectory(_workingDirectory);
|
||||
worker->setCompressionEnabled(_compress);
|
||||
PFXHttpRequestInput input(fullPath, "GET");
|
||||
|
||||
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); }
|
||||
@@ -352,7 +353,6 @@ void PFXPetApi::getPetById(const qint64 &pet_id) {
|
||||
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this);
|
||||
worker->setTimeOut(_timeOut);
|
||||
worker->setWorkingDirectory(_workingDirectory);
|
||||
worker->setCompressionEnabled(_compress);
|
||||
PFXHttpRequestInput input(fullPath, "GET");
|
||||
|
||||
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); }
|
||||
@@ -396,7 +396,6 @@ void PFXPetApi::updatePet(const PFXPet &body) {
|
||||
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this);
|
||||
worker->setTimeOut(_timeOut);
|
||||
worker->setWorkingDirectory(_workingDirectory);
|
||||
worker->setCompressionEnabled(_compress);
|
||||
PFXHttpRequestInput input(fullPath, "PUT");
|
||||
|
||||
QString output = body.asJson();
|
||||
@@ -445,7 +444,6 @@ void PFXPetApi::updatePetWithForm(const qint64 &pet_id, const QString &name, con
|
||||
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this);
|
||||
worker->setTimeOut(_timeOut);
|
||||
worker->setWorkingDirectory(_workingDirectory);
|
||||
worker->setCompressionEnabled(_compress);
|
||||
PFXHttpRequestInput input(fullPath, "POST");
|
||||
|
||||
input.add_var("name", ::test_namespace::toStringValue(name));
|
||||
@@ -493,7 +491,6 @@ void PFXPetApi::uploadFile(const qint64 &pet_id, const QString &additional_metad
|
||||
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this);
|
||||
worker->setTimeOut(_timeOut);
|
||||
worker->setWorkingDirectory(_workingDirectory);
|
||||
worker->setCompressionEnabled(_compress);
|
||||
PFXHttpRequestInput input(fullPath, "POST");
|
||||
|
||||
input.add_var("additionalMetadata", ::test_namespace::toStringValue(additional_metadata));
|
||||
|
||||
@@ -37,7 +37,8 @@ public:
|
||||
void setTimeOut(const int timeOut);
|
||||
void setWorkingDirectory(const QString &path);
|
||||
void addHeaders(const QString &key, const QString &value);
|
||||
void enableContentCompression();
|
||||
void enableRequestCompression();
|
||||
void enableResponseCompression();
|
||||
|
||||
void addPet(const PFXPet &body);
|
||||
void deletePet(const qint64 &pet_id, const QString &api_key);
|
||||
@@ -55,7 +56,8 @@ private:
|
||||
int _timeOut;
|
||||
QString _workingDirectory;
|
||||
QMap<QString, QString> defaultHeaders;
|
||||
bool _compress;
|
||||
bool isResponseCompressionEnabled;
|
||||
bool isRequestCompressionEnabled;
|
||||
|
||||
void addPetCallback(PFXHttpRequestWorker *worker);
|
||||
void deletePetCallback(PFXHttpRequestWorker *worker);
|
||||
|
||||
@@ -23,7 +23,8 @@ PFXStoreApi::PFXStoreApi(const QString &scheme, const QString &host, int port, c
|
||||
_port(port),
|
||||
_basePath(basePath),
|
||||
_timeOut(timeOut),
|
||||
_compress(false) {}
|
||||
isResponseCompressionEnabled(false),
|
||||
isRequestCompressionEnabled(false) {}
|
||||
|
||||
PFXStoreApi::~PFXStoreApi() {
|
||||
}
|
||||
@@ -56,8 +57,12 @@ void PFXStoreApi::addHeaders(const QString &key, const QString &value) {
|
||||
defaultHeaders.insert(key, value);
|
||||
}
|
||||
|
||||
void PFXStoreApi::enableContentCompression() {
|
||||
_compress = true;
|
||||
void PFXStoreApi::enableRequestCompression() {
|
||||
isRequestCompressionEnabled = true;
|
||||
}
|
||||
|
||||
void PFXStoreApi::enableResponseCompression() {
|
||||
isResponseCompressionEnabled = true;
|
||||
}
|
||||
|
||||
void PFXStoreApi::deleteOrder(const QString &order_id) {
|
||||
@@ -74,7 +79,6 @@ void PFXStoreApi::deleteOrder(const QString &order_id) {
|
||||
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this);
|
||||
worker->setTimeOut(_timeOut);
|
||||
worker->setWorkingDirectory(_workingDirectory);
|
||||
worker->setCompressionEnabled(_compress);
|
||||
PFXHttpRequestInput input(fullPath, "DELETE");
|
||||
|
||||
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); }
|
||||
@@ -117,7 +121,6 @@ void PFXStoreApi::getInventory() {
|
||||
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this);
|
||||
worker->setTimeOut(_timeOut);
|
||||
worker->setWorkingDirectory(_workingDirectory);
|
||||
worker->setCompressionEnabled(_compress);
|
||||
PFXHttpRequestInput input(fullPath, "GET");
|
||||
|
||||
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); }
|
||||
@@ -173,7 +176,6 @@ void PFXStoreApi::getOrderById(const qint64 &order_id) {
|
||||
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this);
|
||||
worker->setTimeOut(_timeOut);
|
||||
worker->setWorkingDirectory(_workingDirectory);
|
||||
worker->setCompressionEnabled(_compress);
|
||||
PFXHttpRequestInput input(fullPath, "GET");
|
||||
|
||||
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); }
|
||||
@@ -217,7 +219,6 @@ void PFXStoreApi::placeOrder(const PFXOrder &body) {
|
||||
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this);
|
||||
worker->setTimeOut(_timeOut);
|
||||
worker->setWorkingDirectory(_workingDirectory);
|
||||
worker->setCompressionEnabled(_compress);
|
||||
PFXHttpRequestInput input(fullPath, "POST");
|
||||
|
||||
QString output = body.asJson();
|
||||
|
||||
@@ -36,7 +36,8 @@ public:
|
||||
void setTimeOut(const int timeOut);
|
||||
void setWorkingDirectory(const QString &path);
|
||||
void addHeaders(const QString &key, const QString &value);
|
||||
void enableContentCompression();
|
||||
void enableRequestCompression();
|
||||
void enableResponseCompression();
|
||||
|
||||
void deleteOrder(const QString &order_id);
|
||||
void getInventory();
|
||||
@@ -50,7 +51,8 @@ private:
|
||||
int _timeOut;
|
||||
QString _workingDirectory;
|
||||
QMap<QString, QString> defaultHeaders;
|
||||
bool _compress;
|
||||
bool isResponseCompressionEnabled;
|
||||
bool isRequestCompressionEnabled;
|
||||
|
||||
void deleteOrderCallback(PFXHttpRequestWorker *worker);
|
||||
void getInventoryCallback(PFXHttpRequestWorker *worker);
|
||||
|
||||
@@ -23,7 +23,8 @@ PFXUserApi::PFXUserApi(const QString &scheme, const QString &host, int port, con
|
||||
_port(port),
|
||||
_basePath(basePath),
|
||||
_timeOut(timeOut),
|
||||
_compress(false) {}
|
||||
isResponseCompressionEnabled(false),
|
||||
isRequestCompressionEnabled(false) {}
|
||||
|
||||
PFXUserApi::~PFXUserApi() {
|
||||
}
|
||||
@@ -56,8 +57,12 @@ void PFXUserApi::addHeaders(const QString &key, const QString &value) {
|
||||
defaultHeaders.insert(key, value);
|
||||
}
|
||||
|
||||
void PFXUserApi::enableContentCompression() {
|
||||
_compress = true;
|
||||
void PFXUserApi::enableRequestCompression() {
|
||||
isRequestCompressionEnabled = true;
|
||||
}
|
||||
|
||||
void PFXUserApi::enableResponseCompression() {
|
||||
isResponseCompressionEnabled = true;
|
||||
}
|
||||
|
||||
void PFXUserApi::createUser(const PFXUser &body) {
|
||||
@@ -71,7 +76,6 @@ void PFXUserApi::createUser(const PFXUser &body) {
|
||||
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this);
|
||||
worker->setTimeOut(_timeOut);
|
||||
worker->setWorkingDirectory(_workingDirectory);
|
||||
worker->setCompressionEnabled(_compress);
|
||||
PFXHttpRequestInput input(fullPath, "POST");
|
||||
|
||||
QString output = body.asJson();
|
||||
@@ -117,7 +121,6 @@ void PFXUserApi::createUsersWithArrayInput(const QList<PFXUser> &body) {
|
||||
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this);
|
||||
worker->setTimeOut(_timeOut);
|
||||
worker->setWorkingDirectory(_workingDirectory);
|
||||
worker->setCompressionEnabled(_compress);
|
||||
PFXHttpRequestInput input(fullPath, "POST");
|
||||
|
||||
QJsonDocument doc(::test_namespace::toJsonValue(body).toArray());
|
||||
@@ -164,7 +167,6 @@ void PFXUserApi::createUsersWithListInput(const QList<PFXUser> &body) {
|
||||
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this);
|
||||
worker->setTimeOut(_timeOut);
|
||||
worker->setWorkingDirectory(_workingDirectory);
|
||||
worker->setCompressionEnabled(_compress);
|
||||
PFXHttpRequestInput input(fullPath, "POST");
|
||||
|
||||
QJsonDocument doc(::test_namespace::toJsonValue(body).toArray());
|
||||
@@ -214,7 +216,6 @@ void PFXUserApi::deleteUser(const QString &username) {
|
||||
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this);
|
||||
worker->setTimeOut(_timeOut);
|
||||
worker->setWorkingDirectory(_workingDirectory);
|
||||
worker->setCompressionEnabled(_compress);
|
||||
PFXHttpRequestInput input(fullPath, "DELETE");
|
||||
|
||||
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); }
|
||||
@@ -260,7 +261,6 @@ void PFXUserApi::getUserByName(const QString &username) {
|
||||
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this);
|
||||
worker->setTimeOut(_timeOut);
|
||||
worker->setWorkingDirectory(_workingDirectory);
|
||||
worker->setCompressionEnabled(_compress);
|
||||
PFXHttpRequestInput input(fullPath, "GET");
|
||||
|
||||
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); }
|
||||
@@ -316,7 +316,6 @@ void PFXUserApi::loginUser(const QString &username, const QString &password) {
|
||||
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this);
|
||||
worker->setTimeOut(_timeOut);
|
||||
worker->setWorkingDirectory(_workingDirectory);
|
||||
worker->setCompressionEnabled(_compress);
|
||||
PFXHttpRequestInput input(fullPath, "GET");
|
||||
|
||||
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); }
|
||||
@@ -361,7 +360,6 @@ void PFXUserApi::logoutUser() {
|
||||
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this);
|
||||
worker->setTimeOut(_timeOut);
|
||||
worker->setWorkingDirectory(_workingDirectory);
|
||||
worker->setCompressionEnabled(_compress);
|
||||
PFXHttpRequestInput input(fullPath, "GET");
|
||||
|
||||
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); }
|
||||
@@ -407,7 +405,6 @@ void PFXUserApi::updateUser(const QString &username, const PFXUser &body) {
|
||||
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this);
|
||||
worker->setTimeOut(_timeOut);
|
||||
worker->setWorkingDirectory(_workingDirectory);
|
||||
worker->setCompressionEnabled(_compress);
|
||||
PFXHttpRequestInput input(fullPath, "PUT");
|
||||
|
||||
QString output = body.asJson();
|
||||
|
||||
@@ -36,7 +36,8 @@ public:
|
||||
void setTimeOut(const int timeOut);
|
||||
void setWorkingDirectory(const QString &path);
|
||||
void addHeaders(const QString &key, const QString &value);
|
||||
void enableContentCompression();
|
||||
void enableRequestCompression();
|
||||
void enableResponseCompression();
|
||||
|
||||
void createUser(const PFXUser &body);
|
||||
void createUsersWithArrayInput(const QList<PFXUser> &body);
|
||||
@@ -54,7 +55,8 @@ private:
|
||||
int _timeOut;
|
||||
QString _workingDirectory;
|
||||
QMap<QString, QString> defaultHeaders;
|
||||
bool _compress;
|
||||
bool isResponseCompressionEnabled;
|
||||
bool isRequestCompressionEnabled;
|
||||
|
||||
void createUserCallback(PFXHttpRequestWorker *worker);
|
||||
void createUsersWithArrayInputCallback(PFXHttpRequestWorker *worker);
|
||||
|
||||
Reference in New Issue
Block a user