All Downloads are FREE. Search and download functionalities are using the official Maven repository.
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-restbed-server.api-source.mustache Maven / Gradle / Ivy
{{>licenseInfo}}
{{#operations}}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "{{classname}}.h"
{{#apiNamespaceDeclarations}}
namespace {{this}} {
{{/apiNamespaceDeclarations}}
using namespace {{modelNamespace}};
namespace {
[[maybe_unused]]
std::string selectPreferredContentType(const std::vector& contentTypes) {
if (contentTypes.size() == 0) {
return "application/json";
}
if (contentTypes.size() == 1) {
return contentTypes.at(0);
}
static const std::array preferredTypes = {"json", "xml"};
for (const auto& preferredType: preferredTypes) {
const auto ret = std::find_if(contentTypes.cbegin(),
contentTypes.cend(),
[preferredType](const std::string& str) {
return str.find(preferredType) != std::string::npos;});
if (ret != contentTypes.cend()) {
return *ret;
}
}
return contentTypes.at(0);
}
}
{{classname}}Exception::{{classname}}Exception(int status_code, std::string what)
: m_status(status_code),
m_what(what)
{
}
int {{classname}}Exception::getStatus() const
{
return m_status;
}
const char* {{classname}}Exception::what() const noexcept
{
return m_what.c_str();
}
template
MODEL_T extractJsonModelBodyParam(const std::string& bodyContent)
{
std::stringstream sstream(bodyContent);
boost::property_tree::ptree pt;
boost::property_tree::json_parser::read_json(sstream, pt);
auto model = MODEL_T(pt);
return model;
}
template
std::vector extractJsonArrayBodyParam(const std::string& bodyContent)
{
std::stringstream sstream(bodyContent);
boost::property_tree::ptree pt;
boost::property_tree::json_parser::read_json(sstream, pt);
auto arrayRet = std::vector();
for (const auto& child: pt) {
arrayRet.emplace_back(MODEL_T(child.second));
}
return arrayRet;
}
template
std::string convertMapResponse(const std::map& map)
{
boost::property_tree::ptree pt;
for(const auto &kv: map) {
pt.push_back(boost::property_tree::ptree::value_type(
boost::lexical_cast(kv.first),
boost::property_tree::ptree(
boost::lexical_cast(kv.second))));
}
std::stringstream sstream;
write_json(sstream, pt);
std::string result = sstream.str();
return result;
}
namespace {{classname}}Resources {
{{#operation}}
{{vendorExtensions.x-codegen-resource-name}}Resource::{{vendorExtensions.x-codegen-resource-name}}Resource(const std::string& context /* = "{{contextPath}}" */)
{
this->set_path(context + "{{path}}");
this->set_method_handler("{{httpMethod}}",
std::bind(&{{vendorExtensions.x-codegen-resource-name}}Resource::handler_{{httpMethod}}_internal, this,
std::placeholders::_1));
{{#vendorExtensions.x-codegen-other-methods}}
this->set_method_handler("{{httpMethod}}",
std::bind(&{{vendorExtensions.x-codegen-resource-name}}Resource::handler_{{httpMethod}}_internal, this,
std::placeholders::_1));
{{/vendorExtensions.x-codegen-other-methods}}
}
std::pair {{vendorExtensions.x-codegen-resource-name}}Resource::handle{{classname}}Exception(const {{classname}}Exception& e)
{
return std::make_pair(e.getStatus(), e.what());
}
std::pair {{vendorExtensions.x-codegen-resource-name}}Resource::handleStdException(const std::exception& e)
{
return std::make_pair(500, e.what());
}
std::pair {{vendorExtensions.x-codegen-resource-name}}Resource::handleUnspecifiedException()
{
return std::make_pair(500, "Unknown exception occurred");
}
void {{vendorExtensions.x-codegen-resource-name}}Resource::setResponseHeader(const std::shared_ptr& session, const std::string& header)
{
session->set_header(header, "");
}
void {{vendorExtensions.x-codegen-resource-name}}Resource::returnResponse(const std::shared_ptr& session, const int status, const std::string& result, std::multimap& responseHeaders)
{
responseHeaders.insert(std::make_pair("Connection", "close"));
session->close(status, result, responseHeaders);
}
void {{vendorExtensions.x-codegen-resource-name}}Resource::defaultSessionClose(const std::shared_ptr& session, const int status, const std::string& result)
{
session->close(status, result, { {"Connection", "close"} });
}
void {{vendorExtensions.x-codegen-resource-name}}Resource::handler_{{httpMethod}}_internal(const std::shared_ptr session)
{
{{#lambda.indented}}
{{>api-source-HandlerBody}}
{{/lambda.indented}}
}
{{#vendorExtensions.x-codegen-other-methods}}
// x-extension
void {{vendorExtensions.x-codegen-resource-name}}Resource::handler_{{httpMethod}}_internal(const std::shared_ptr session) {
{{#lambda.indented}}
{{>api-source-HandlerBody}}
{{/lambda.indented}}
}
{{/vendorExtensions.x-codegen-other-methods}}
{{#returnType}}std::pair{{/returnType}}{{^returnType}}int{{/returnType}} {{vendorExtensions.x-codegen-resource-name}}Resource::handler_{{httpMethod}}(
{{#allParams}}{{{dataType}}} & {{{paramName}}}{{^-last}}, {{/-last}}{{/allParams}})
{
return handler_{{httpMethod}}_func({{#allParams}}{{{paramName}}}{{^-last}}, {{/-last}}{{/allParams}});
}
{{#vendorExtensions.x-codegen-other-methods}}
{{#returnType}}std::pair{{/returnType}}{{^returnType}}int{{/returnType}} {{vendorExtensions.x-codegen-resource-name}}Resource::handler_{{httpMethod}}(
{{#allParams}}{{{dataType}}} & {{{paramName}}}{{^-last}}, {{/-last}}{{/allParams}})
{
return handler_{{httpMethod}}_func({{#allParams}}{{{paramName}}}{{^-last}}, {{/-last}}{{/allParams}});
}
{{/vendorExtensions.x-codegen-other-methods}}
std::string {{vendorExtensions.x-codegen-resource-name}}Resource::extractBodyContent(const std::shared_ptr& session) {
const auto request = session->get_request();
int content_length = request->get_header("Content-Length", 0);
std::string bodyContent;
session->fetch(content_length,
[&bodyContent](const std::shared_ptr session,
const restbed::Bytes &body) {
bodyContent = restbed::String::format(
"%.*s\n", (int)body.size(), body.data());
});
return bodyContent;
}
std::string {{vendorExtensions.x-codegen-resource-name}}Resource::extractFormParamsFromBody(const std::string& paramName, const std::string& body) {
const auto uri = restbed::Uri("urlencoded?" + body, true);
const auto params = uri.get_query_parameters();
const auto result = params.find(paramName);
if (result != params.cend()) {
return result->second;
}
return "";
}
{{/operation}}
} /* namespace {{classname}}Resources */
{{classname}}::{{classname}}(std::shared_ptr const& restbedService)
: m_service(restbedService)
{
}
{{classname}}::~{{classname}}() {}
{{#operation}}
std::shared_ptr<{{classname}}Resources::{{vendorExtensions.x-codegen-resource-name}}Resource> {{classname}}::get{{vendorExtensions.x-codegen-resource-name}}Resource() {
if (!m_sp{{vendorExtensions.x-codegen-resource-name}}Resource) {
setResource(std::make_shared<{{classname}}Resources::{{vendorExtensions.x-codegen-resource-name}}Resource>());
}
return m_sp{{vendorExtensions.x-codegen-resource-name}}Resource;
}
{{/operation}}
{{#operation}}
void {{classname}}::setResource(std::shared_ptr<{{classname}}Resources::{{vendorExtensions.x-codegen-resource-name}}Resource> resource) {
m_sp{{vendorExtensions.x-codegen-resource-name}}Resource = resource;
m_service->publish(m_sp{{vendorExtensions.x-codegen-resource-name}}Resource);
}
{{/operation}}
{{#operation}}
void {{classname}}::set{{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource(std::shared_ptr<{{classname}}Resources::{{vendorExtensions.x-codegen-resource-name}}Resource> sp{{vendorExtensions.x-codegen-resource-name}}Resource) {
m_sp{{vendorExtensions.x-codegen-resource-name}}Resource = sp{{vendorExtensions.x-codegen-resource-name}}Resource;
m_service->publish(m_sp{{vendorExtensions.x-codegen-resource-name}}Resource);
}
{{/operation}}
void {{classname}}::publishDefaultResources() {
{{#operation}}
if (!m_sp{{vendorExtensions.x-codegen-resource-name}}Resource) {
setResource(std::make_shared<{{classname}}Resources::{{vendorExtensions.x-codegen-resource-name}}Resource>());
}
{{/operation}}
}
std::shared_ptr {{classname}}::service() {
return m_service;
}
{{#apiNamespaceDeclarations}}
}
{{/apiNamespaceDeclarations}}
{{/operations}}