Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
cpp-qt5-client.api-body.mustache Maven / Gradle / Ivy
{{>licenseInfo}}
#include "{{classname}}.h"
#include "{{prefix}}Helpers.h"
#include "{{prefix}}ServerConfiguration.h"
#include
#include
{{#cppNamespaceDeclarations}}
namespace {{this}} {
{{/cppNamespaceDeclarations}}
{{classname}}::{{classname}}(const QString &scheme, const QString &host, int port, const QString &basePath, const int timeOut)
: _scheme(scheme),
_host(host),
_port(port),
_basePath(basePath),
_timeOut(timeOut),
_manager(nullptr),
isResponseCompressionEnabled(false),
isRequestCompressionEnabled(false) {
initializeServerConfigs();
}
{{classname}}::~{{classname}}() {
}
void {{classname}}::initializeServerConfigs(){
//Default server
QList<{{prefix}}ServerConfiguration> defaultConf = QList<{{prefix}}ServerConfiguration>();
//varying endpoint server
QList<{{prefix}}ServerConfiguration> serverConf = QList<{{prefix}}ServerConfiguration>();
{{#vendorExtensions.x-cpp-global-server-list}}
defaultConf.append({{prefix}}ServerConfiguration(
"{{{url}}}",
"{{{description}}}{{^description}}No description provided{{/description}}",
{{#variables}}{{#-first}}QMap{ {{/-first}}
{"{{{name}}}", {{prefix}}ServerVariable("{{{description}}}{{^description}}No description provided{{/description}}","{{{defaultValue}}}",
QSet{ {{#enumValues}}{"{{{.}}}"}{{#-last}} })},{{/-last}}{{^-last}},{{/-last}}{{/enumValues}}{{^enumValues}}{"{{defaultValue}}"} })},{{/enumValues}}{{#-last}} }));{{/-last}}
{{/variables}}{{^variables}}QMap()));{{/variables}}
{{/vendorExtensions.x-cpp-global-server-list}}
{{#operations}}
{{#operation}}
{{^servers}}
_serverConfigs.insert("{{nickname}}",defaultConf);
_serverIndices.insert("{{nickname}}",0);
{{/servers}}
{{#servers}}
serverConf.append({{prefix}}ServerConfiguration(
"{{{url}}}",
"{{{description}}}{{^description}}No description provided{{/description}}",
{{#variables}}{{#-first}}QMap{ {{/-first}}
{"{{{name}}}", {{prefix}}ServerVariable("{{{description}}}{{^description}}No description provided{{/description}}","{{{defaultValue}}}",
QSet{ {{#enumValues}}{"{{{.}}}"}{{#-last}} })}, {{/-last}}{{^-last}},{{/-last}}{{/enumValues}}{{^enumValues}}{"{{defaultValue}}"} })},{{/enumValues}}{{#-last}} }));{{/-last}}
{{/variables}}{{^variables}}QMap()));{{/variables}}
{{#-last}}_serverConfigs.insert("{{nickname}}",serverConf);
_serverIndices.insert("{{nickname}}",0);{{/-last}}
{{/servers}}
{{/operation}}
{{/operations}}
}
/**
* returns 0 on success and -1, -2 or -3 on failure.
* -1 when the variable does not exist and -2 if the value is not defined in the enum and -3 if the operation or server index is not found
*/
int {{classname}}::setDefaultServerValue(int serverIndex, const QString &operation, const QString &variable, const QString &value){
auto it = _serverConfigs.find(operation);
if(it != _serverConfigs.end() && serverIndex < it.value().size() ){
return _serverConfigs[operation][serverIndex].setDefaultValue(variable,value);
}
return -3;
}
void {{classname}}::setServerIndex(const QString &operation, int serverIndex){
if(_serverIndices.contains(operation) && serverIndex < _serverConfigs.find(operation).value().size() )
_serverIndices[operation] = serverIndex;
}
void {{classname}}::setScheme(const QString &scheme) {
_scheme = scheme;
}
void {{classname}}::setHost(const QString &host) {
_host = host;
}
void {{classname}}::setPort(int port) {
_port = port;
}
void {{classname}}::setApiKey(const QString &apiKeyName, const QString &apiKey){
_apiKeys.insert(apiKeyName,apiKey);
}
void {{classname}}::setBearerToken(const QString &token){
_bearerToken = token;
}
void {{classname}}::setUsername(const QString &username) {
_username = username;
}
void {{classname}}::setPassword(const QString &password) {
_password = password;
}
void {{classname}}::setBasePath(const QString &basePath) {
_basePath = basePath;
}
void {{classname}}::setTimeOut(const int timeOut) {
_timeOut = timeOut;
}
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);
}
void {{classname}}::enableRequestCompression() {
isRequestCompressionEnabled = true;
}
void {{classname}}::enableResponseCompression() {
isResponseCompressionEnabled = true;
}
void {{classname}}::abortRequests(){
emit abortRequestsSignal();
}
{{#operations}}
{{#operation}}
void {{classname}}::{{nickname}}({{#allParams}}const {{{dataType}}} &{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) {
QString fullPath = QString(_serverConfigs["{{nickname}}"][_serverIndices.value("{{nickname}}")].URL()+"{{{path}}}");
{{#pathParams}}
QString {{paramName}}PathParam("{");
{{paramName}}PathParam.append("{{baseName}}").append("}");
fullPath.replace({{paramName}}PathParam, QUrl::toPercentEncoding(::{{cppNamespace}}::toStringValue({{paramName}})));
{{/pathParams}}{{#authMethods}}{{#isApiKey}}{{#isKeyInHeader}}
if(_apiKeys.contains("{{name}}")){
addHeaders("{{name}}",_apiKeys.find("{{name}}").value());
}
{{/isKeyInHeader}}{{#isKeyInQuery}}
if(_apiKeys.contains("{{name}}")){
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?");
fullPath.append("{{{name}}}=").append(_apiKeys.find("{{name}}").value());
}
{{/isKeyInQuery}}{{/isApiKey}}{{#isBasicBearer}}
if(!_bearerToken.isEmpty())
addHeaders("Authorization", "Bearer " + _bearerToken);
{{/isBasicBearer}}{{#isBasicBasic}}
if(!_username.isEmpty() && !_password.isEmpty()){
QByteArray b64;
b64.append(_username.toUtf8() + ":" + _password.toUtf8());
addHeaders("Authorization","Basic " + b64.toBase64());
}{{/isBasicBasic}}{{/authMethods}}
{{#queryParams}}{{^collectionFormat}}
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?");
fullPath.append(QUrl::toPercentEncoding("{{baseName}}")).append("=").append(QUrl::toPercentEncoding(::{{cppNamespace}}::toStringValue({{paramName}})));
{{/collectionFormat}}{{#collectionFormat}}
if ({{{paramName}}}.size() > 0) {
if (QString("{{collectionFormat}}").indexOf("multi") == 0) {
foreach ({{{baseType}}} t, {{paramName}}) {
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?");
fullPath.append("{{{baseName}}}=").append(::{{cppNamespace}}::toStringValue(t));
}
} else if (QString("{{collectionFormat}}").indexOf("ssv") == 0) {
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?");
fullPath.append("{{baseName}}=");
qint32 count = 0;
foreach ({{{baseType}}} t, {{paramName}}) {
if (count > 0) {
fullPath.append(" ");
}
fullPath.append(::{{cppNamespace}}::toStringValue(t));
}
} else if (QString("{{collectionFormat}}").indexOf("tsv") == 0) {
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?");
fullPath.append("{{baseName}}=");
qint32 count = 0;
foreach ({{{baseType}}} t, {{paramName}}) {
if (count > 0) {
fullPath.append("\t");
}
fullPath.append(::{{cppNamespace}}::toStringValue(t));
}
}
}
{{/collectionFormat}}{{/queryParams}}
{{prefix}}HttpRequestWorker *worker = new {{prefix}}HttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);{{#contentCompression}}
worker->setResponseCompressionEnabled(isResponseCompressionEnabled);
worker->setRequestCompressionEnabled(isRequestCompressionEnabled);{{/contentCompression}}
{{prefix}}HttpRequestInput input(fullPath, "{{httpMethod}}");
{{#formParams}}{{^isFile}}
input.add_var("{{baseName}}", ::{{cppNamespace}}::toStringValue({{paramName}}));{{/isFile}}{{#isFile}}
input.add_file("{{baseName}}", {{paramName}}.local_filename, {{paramName}}.request_filename, {{paramName}}.mime_type);{{/isFile}}{{/formParams}}{{#bodyParams}}{{#isContainer}}{{#isArray}}
QJsonDocument doc(::{{cppNamespace}}::toJsonValue({{paramName}}).toArray());{{/isArray}}{{#isMap}}
QJsonDocument doc(::{{cppNamespace}}::toJsonValue({{paramName}}).toObject());{{/isMap}}
QByteArray bytes = doc.toJson();
input.request_body.append(bytes);
{{/isContainer}}{{^isContainer}}{{#isString}}
QByteArray output = {{paramName}}.toUtf8();{{/isString}}{{#isByteArray}}QByteArray output({{paramName}});{{/isByteArray}}{{^isString}}{{^isByteArray}}{{^isFile}}
QByteArray output = {{paramName}}.asJson().toUtf8();{{/isFile}}{{/isByteArray}}{{/isString}}{{#isFile}}{{#hasConsumes}}input.headers.insert("Content-Type", {{#consumes}}{{^-first}}, {{/-first}}"{{mediaType}}"{{/consumes}});{{/hasConsumes}}
QByteArray output = {{paramName}}.asByteArray();{{/isFile}}
input.request_body.append(output);
{{/isContainer}}{{/bodyParams}}{{#headerParams}}
if (!::{{cppNamespace}}::toStringValue({{paramName}}).isEmpty()) {
input.headers.insert("{{baseName}}", ::{{cppNamespace}}::toStringValue({{paramName}}));
}
{{/headerParams}}
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); }
connect(worker, &{{prefix}}HttpRequestWorker::on_execution_finished, this, &{{classname}}::{{nickname}}Callback);
connect(this, &{{classname}}::abortRequestsSignal, worker, &QObject::deleteLater);
worker->execute(&input);
}
void {{classname}}::{{nickname}}Callback({{prefix}}HttpRequestWorker *worker) {
QString msg;
QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type == QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length());
} else {
msg = "Error: " + worker->error_str;
error_str = QString("%1, %2").arg(worker->error_str).arg(QString(worker->response));
}
{{#returnType}}
{{#isArray}}
{{{returnType}}} output;
QString json(worker->response);
QByteArray array(json.toStdString().c_str());
QJsonDocument doc = QJsonDocument::fromJson(array);
QJsonArray jsonArray = doc.array();
foreach (QJsonValue obj, jsonArray) {
{{{returnBaseType}}} val;
::{{cppNamespace}}::fromJsonValue(val, obj);
output.append(val);
}
{{/isArray}}
{{^isArray}}
{{^isMap}}
{{#returnTypeIsPrimitive}}
{{{returnType}}} output;
::{{cppNamespace}}::fromStringValue(QString(worker->response), output);
{{/returnTypeIsPrimitive}}
{{/isMap}}
{{#isMap}}
{{{returnType}}} output;
QString json(worker->response);
QByteArray array(json.toStdString().c_str());
QJsonDocument doc = QJsonDocument::fromJson(array);
QJsonObject obj = doc.object();
foreach (QString key, obj.keys()) {
{{returnBaseType}} val;
::{{cppNamespace}}::fromJsonValue(val, obj[key]);
output.insert(key, val);
}
{{/isMap}}
{{^isMap}}
{{^returnTypeIsPrimitive}}
{{{returnType}}} output{{^isResponseFile}}(QString(worker->response)){{/isResponseFile}}{{#isResponseFile}} = worker->getHttpFileElement(){{/isResponseFile}};
{{/returnTypeIsPrimitive}}
{{/isMap}}
{{/isArray}}
{{/returnType}}
worker->deleteLater();
if (worker->error_type == QNetworkReply::NoError) {
emit {{nickname}}Signal({{#returnType}}output{{/returnType}});
emit {{nickname}}SignalFull(worker{{#returnType}}, output{{/returnType}});
} else {
emit {{nickname}}SignalE({{#returnType}}output, {{/returnType}}error_type, error_str);
emit {{nickname}}SignalEFull(worker, error_type, error_str);
}
}
{{/operation}}
{{/operations}}
{{#cppNamespaceDeclarations}}
} // namespace {{this}}
{{/cppNamespaceDeclarations}}