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

qt5cpp.api-body.mustache Maven / Gradle / Ivy

There is a newer version: 3.0.0-rc1
Show newest version
{{>licenseInfo}}
#include "{{classname}}.h"
#include "{{prefix}}Helpers.h"
#include "{{prefix}}ModelFactory.h"
#include "{{prefix}}QObjectWrapper.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}}{{{dataType}}}{{#isBodyParam}}&{{/isBodyParam}} {{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, stringValue({{paramName}}));
    {{/pathParams}}

    {{#queryParams}}
    {{^collectionFormat}}
    if (fullPath.indexOf("?") > 0)
      fullPath.append("&");
    else
      fullPath.append("?");
    fullPath.append(QUrl::toPercentEncoding("{{baseName}}"))
        .append("=")
        .append(QUrl::toPercentEncoding(stringValue({{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(stringValue(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(stringValue(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(stringValue(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}}
    QJsonObject {{paramName}}_jobj;
    toJsonArray((QList*){{paramName}}, {{paramName}}_jobj, QString("body"), QString("{{prefix}}User*"));

    QJsonDocument doc({{paramName}}_jobj);
    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 = {{{defaultResponse}}};
    QString json(worker->response);
    QByteArray array (json.toStdString().c_str());
    QJsonDocument doc = QJsonDocument::fromJson(array);
    QJsonArray jsonArray = doc.array();
    auto wrapper = new {{prefix}}QObjectWrapper<{{{returnType}}}> (output);
    wrapper->deleteLater();
    foreach(QJsonValue obj, jsonArray) {
        {{{returnBaseType}}}* o = new {{returnBaseType}}();
        QJsonObject jv = obj.toObject();
        QJsonObject * ptr = (QJsonObject*)&jv;
        o->fromJsonObject(*ptr);
        auto objwrapper = new {{prefix}}QObjectWrapper<{{{returnBaseType}}}*> (o);
        objwrapper->deleteLater();
        output->append(o);
    }
    {{/isListContainer}}
    {{^isListContainer}}
    {{^isMapContainer}}
    {{#returnTypeIsPrimitive}}
    {{{returnType}}} output;  // TODO add primitive output support
    {{#isByteArray}}auto wrapper = new {{prefix}}QObjectWrapper<{{{returnType}}}> (output);
    wrapper->deleteLater();{{/isByteArray}}
    {{#isDateTime}}auto wrapper = new {{prefix}}QObjectWrapper<{{{returnType}}}> (output);
    wrapper->deleteLater();{{/isDateTime}}
    {{#isDate}}auto wrapper = new {{prefix}}QObjectWrapper<{{{returnType}}}> (output);
    wrapper->deleteLater();{{/isDate}}
    {{#isString}}auto wrapper = new {{prefix}}QObjectWrapper<{{{returnType}}}> (output);
    wrapper->deleteLater();{{/isString}}
    {{/returnTypeIsPrimitive}}
    {{/isMapContainer}}
    {{#isMapContainer}}
    {{{returnType}}} output = {{{defaultResponse}}};
    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;
        setValue(&val, obj[key], "{{returnBaseType}}", QString());
        output->insert(key, val);
    }
    {{/isMapContainer}}
    {{^isMapContainer}}
    {{^returnTypeIsPrimitive}}
    QString json(worker->response);
    {{{returnType}}} output = static_cast<{{{returnType}}}>(create(json, QString("{{{returnBaseType}}}")));
    auto wrapper = new {{prefix}}QObjectWrapper<{{{returnType}}}> (output);
    wrapper->deleteLater();
    {{/returnTypeIsPrimitive}}
    {{/isMapContainer}}
    {{/isListContainer}}
    {{/returnType}}
    worker->deleteLater();

    if (worker->error_type == QNetworkReply::NoError) {
        emit {{nickname}}Signal({{#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