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

cpp-pistache-server.api-source.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
{{>licenseInfo}}
{{#operations}}

#include "{{classname}}.h"
#include "{{prefix}}Helpers.h"

{{#apiNamespaceDeclarations}}
namespace {{this}} {
{{/apiNamespaceDeclarations}}

using namespace {{helpersNamespace}};
{{#hasModelImport}}
using namespace {{modelNamespace}};{{/hasModelImport}}

{{classname}}::{{classname}}(std::shared_ptr rtr) { 
    router = rtr;
}

void {{classname}}::init() {
    setupRoutes();
}

void {{classname}}::setupRoutes() {
    using namespace Pistache::Rest;

    {{#operation}}
    Routes::{{httpMethod}}(*router, base + "{{{vendorExtensions.x-codegen-pistache-path}}}", Routes::bind(&{{classname}}::{{operationIdSnakeCase}}_handler, this));
    {{/operation}}

    // Default handler, called when a route is not found
    router->addCustomHandler(Routes::bind(&{{classname}}::{{classnameSnakeLowerCase}}_default_handler, this));
}

{{#operation}}
void {{classname}}::{{operationIdSnakeCase}}_handler(const Pistache::Rest::Request &{{#hasParams}}request{{/hasParams}}, Pistache::Http::ResponseWriter response) {
    {{#vendorExtensions.x-codegen-pistache-is-parsing-supported}}
    {{#hasPathParams}}
    // Getting the path params
    {{#pathParams}}
    auto {{paramName}} = request.param(":{{paramName}}").as<{{dataType}}>();
    {{/pathParams}}
    {{/hasPathParams}}{{#hasBodyParam}}
    // Getting the body param
    {{#bodyParam}}
    {{^isPrimitiveType}}{{^isContainer}}
    {{baseType}} {{paramName}};{{/isContainer}}{{#isArray}}std::vector<{{items.baseType}}> {{paramName}};{{/isArray}}{{#isMap}}std::map {{paramName}};{{/isMap}}{{/isPrimitiveType}}
    {{#isPrimitiveType}}
    {{dataType}} {{paramName}};
    {{/isPrimitiveType}}
    {{/bodyParam}}
    {{/hasBodyParam}}{{#hasQueryParams}}
    // Getting the query params
    {{#queryParams}}
    auto {{paramName}}Query = request.query().get("{{baseName}}");
    Pistache::Optional<{{^isContainer}}{{dataType}}{{/isContainer}}{{#isArray}}std::vector<{{items.baseType}}>{{/isArray}}> {{paramName}};
    if(!{{paramName}}Query.isEmpty()){
        {{^isContainer}}{{dataType}}{{/isContainer}}{{#isArray}}std::vector<{{items.baseType}}>{{/isArray}} valueQuery_instance;
        if(fromStringValue({{paramName}}Query.get(), valueQuery_instance)){
            {{paramName}} = Pistache::Some(valueQuery_instance);
        }
    }
    {{/queryParams}}
    {{/hasQueryParams}}{{#hasHeaderParams}}
    // Getting the header params
    {{#headerParams}}
    auto {{paramName}} = request.headers().tryGetRaw("{{baseName}}");
    {{/headerParams}}
    {{/hasHeaderParams}}

    try {
    {{#hasBodyParam}}
    {{#bodyParam}}
    {{^isPrimitiveType}}
      nlohmann::json::parse(request.body()).get_to({{paramName}});
    {{/isPrimitiveType}}
    {{#isPrimitiveType}}
      {{paramName}} = request.body();
    {{/isPrimitiveType}}
    {{/bodyParam}}
    {{/hasBodyParam}}
      this->{{operationIdSnakeCase}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}response);
    {{/vendorExtensions.x-codegen-pistache-is-parsing-supported}}
    {{^vendorExtensions.x-codegen-pistache-is-parsing-supported}}
    try {
      this->{{operationIdSnakeCase}}(request, response);
    {{/vendorExtensions.x-codegen-pistache-is-parsing-supported}}
    } catch (nlohmann::detail::exception &e) {
        //send a 400 error
        response.send(Pistache::Http::Code::Bad_Request, e.what());
        return;
    } catch (Pistache::Http::HttpError &e) {
        response.send(static_cast(e.code()), e.what());
        return;
    } catch (std::exception &e) {
        //send a 500 error
        response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
        return;
    }

}
{{/operation}}

void {{classname}}::{{classnameSnakeLowerCase}}_default_handler(const Pistache::Rest::Request &, Pistache::Http::ResponseWriter response) {
    response.send(Pistache::Http::Code::Not_Found, "The requested method does not exist");
}

{{#apiNamespaceDeclarations}}
}
{{/apiNamespaceDeclarations}}

{{/operations}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy