All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cpp-qt5-client.api-body.mustache Maven / Gradle / Ivy

There is a newer version: 7.8.0
Show newest version
{{>licenseInfo}}
#include "{{classname}}.h"
#include "{{prefix}}Helpers.h"

#include 
#include 

{{#cppNamespaceDeclarations}}
namespace {{this}} {
{{/cppNamespaceDeclarations}}

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

}

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

}

{{classname}}::{{classname}}(QString host, QString basePath) {
    this->host = host;
    this->basePath = basePath;
}

{{#operations}}
{{#operation}}
void
{{classname}}::{{nickname}}({{#allParams}}const {{{dataType}}}& {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {
    QString fullPath;
    fullPath.append(this->host).append(this->basePath).append("{{{path}}}");
    {{#pathParams}}
    QString {{paramName}}PathParam("{"); 
    {{paramName}}PathParam.append("{{baseName}}").append("}");
    fullPath.replace({{paramName}}PathParam, ::{{cppNamespace}}::toStringValue({{paramName}}));
    {{/pathParams}}
    {{#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();
    {{prefix}}HttpRequestInput input(fullPath, "{{httpMethod}}");
    {{#formParams}}
    if ({{paramName}} != nullptr) {
        {{^isFile}}input.add_var("{{baseName}}", {{paramName}});{{/isFile}}{{#isFile}}input.add_file("{{baseName}}", (*{{paramName}}).local_filename, (*{{paramName}}).request_filename, (*{{paramName}}).mime_type);{{/isFile}}
    }
    {{/formParams}}{{#bodyParams}}
    {{#isContainer}}{{#isListContainer}}
    QJsonDocument doc(::{{cppNamespace}}::toJsonValue({{paramName}}).toArray());{{/isListContainer}}{{#isMapContainer}}
    QJsonDocument doc(::{{cppNamespace}}::toJsonValue({{paramName}}).toObject());{{/isMapContainer}}
    QByteArray bytes = doc.toJson();
    input.request_body.append(bytes);
    {{/isContainer}}{{^isContainer}}{{#isString}}
    QString output({{paramName}});{{/isString}}{{#isByteArray}}QString output({{paramName}});{{/isByteArray}}{{^isString}}{{^isByteArray}}
    QString output = {{paramName}}.asJson();{{/isByteArray}}{{/isString}}
    input.request_body.append(output);
    {{/isContainer}}{{/bodyParams}}
    {{#headerParams}}
    if ({{paramName}} != nullptr) {
        input.headers.insert("{{baseName}}", "{{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);

    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;
    }
    {{#returnType}}
    {{#isListContainer}}
    {{{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);
    }
    {{/isListContainer}}
    {{^isListContainer}}
    {{^isMapContainer}}
    {{#returnTypeIsPrimitive}}
    {{{returnType}}} output;  
    ::{{cppNamespace}}::fromStringValue(QString(worker->response), output);
    {{/returnTypeIsPrimitive}}
    {{/isMapContainer}}
    {{#isMapContainer}}
    {{{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);
    }
    {{/isMapContainer}}
    {{^isMapContainer}}
    {{^returnTypeIsPrimitive}}
    {{{returnType}}} output(QString(worker->response));
    {{/returnTypeIsPrimitive}}
    {{/isMapContainer}}
    {{/isListContainer}}
    {{/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}}
}
{{/cppNamespaceDeclarations}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy