Skip to content

[C++][Restbed] Add handler callback methods #2911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

{{{defaultInclude}}}
#include <memory>
#include <utility>

#include <corvusoft/restbed/session.hpp>
#include <corvusoft/restbed/resource.hpp>
#include <corvusoft/restbed/service.hpp>
Expand Down Expand Up @@ -49,6 +51,31 @@ public:
{{#vendorExtensions.x-codegen-otherMethods}}
void {{httpMethod}}_method_handler(const std::shared_ptr<restbed::Session> session);
{{/vendorExtensions.x-codegen-otherMethods}}

void set_handler_{{httpMethod}}(
std::function<std::pair<int, std::string>(
{{#allParams}}{{{dataType}}} const &{{#hasMore}}, {{/hasMore}}{{/allParams}}
)> handler
);

{{#vendorExtensions.x-codegen-otherMethods}}
void set_handler_{{httpMethod}}(
std::function<std::pair<int, std::string>(
{{#allParams}}{{{dataType}}} const &{{#hasMore}}, {{/hasMore}}{{/allParams}}
)> handler
);
{{/vendorExtensions.x-codegen-otherMethods}}

private:
std::function<std::pair<int, std::string>(
{{#allParams}}{{{dataType}}} const &{{#hasMore}}, {{/hasMore}}{{/allParams}}
)> handler_{{httpMethod}}_;

{{#vendorExtensions.x-codegen-otherMethods}}
std::function<std::pair<int, std::string>(
{{#allParams}}{{{dataType}}} const &{{#hasMore}}, {{/hasMore}}{{/allParams}}
)> handler_{{httpMethod}}_;
{{/vendorExtensions.x-codegen-otherMethods}}
};

{{/operation}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ void {{classname}}::stopService() {
{
}

void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::set_handler_{{httpMethod}}(
std::function<std::pair<int, std::string>(
{{#allParams}}{{{dataType}}} const &{{#hasMore}}, {{/hasMore}}{{/allParams}}
)> handler) {
handler_{{httpMethod}}_ = std::move(handler);
}

{{#vendorExtensions.x-codegen-otherMethods}}
void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::set_handler_{{httpMethod}}(
std::function<std::pair<int, std::string>(
{{#allParams}}{{{dataType}}} const &{{#hasMore}}, {{/hasMore}}{{/allParams}}
)> handler) {
handler_{{httpMethod}}_ = std::move(handler);
}
{{/vendorExtensions.x-codegen-otherMethods}}

void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMethod}}_method_handler(const std::shared_ptr<restbed::Session> session) {

const auto request = session->get_request();
Expand All @@ -65,12 +81,12 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
{

const auto request = session->get_request();
std::string requestBody = restbed::String::format("%.*s\n", ( int ) body.size( ), body.data( ));
std::string file = restbed::String::format("%.*s\n", ( int ) body.size( ), body.data( ));
/**
* Get body params or form params here from the requestBody string
* Get body params or form params here from the file string
*/
{{/hasBodyParam}}

{{#hasPathParams}}
// Getting the path params
{{#pathParams}}
Expand All @@ -79,7 +95,7 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
{{/isPrimitiveType}}
{{/pathParams}}
{{/hasPathParams}}

{{#hasQueryParams}}
// Getting the query params
{{#queryParams}}
Expand All @@ -97,21 +113,25 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
{{/isPrimitiveType}}
{{/headerParams}}
{{/hasHeaderParams}}

// Change the value of this variable to the appropriate response before sending the response
int status_code = 200;

/**
* Process the received information here
*/

std::string result = "successful operation";

if (handler_{{httpMethod}}_)
{
std::tie(status_code, result) = handler_{{httpMethod}}_(
{{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}
);
}

{{#responses}}
if (status_code == {{code}}) {
{{#headers}}
// Description: {{description}}
session->set_header("{{baseName}}", ""); // Change second param to your header value
{{/headers}}
session->close({{code}}, "{{message}}", { {"Connection", "close"} });
session->close({{code}}, result.empty() ? "{{message}}" : std::move(result), { {"Connection", "close"} });
return;
}
{{/responses}}
Expand All @@ -133,7 +153,7 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
{

const auto request = session->get_request();
std::string requestBody = restbed::String::format("%.*s\n", ( int ) body.size( ), body.data( ));
std::string file = restbed::String::format("%.*s\n", ( int ) body.size( ), body.data( ));
{{/hasBodyParam}}

{{#hasPathParams}}
Expand All @@ -144,7 +164,7 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
{{/isPrimitiveType}}
{{/pathParams}}
{{/hasPathParams}}

{{#hasQueryParams}}
// Getting the query params
{{#queryParams}}
Expand All @@ -162,14 +182,18 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
{{/isPrimitiveType}}
{{/headerParams}}
{{/hasHeaderParams}}

// Change the value of this variable to the appropriate response before sending the response
int status_code = 200;

/**
* Process the received information here
*/

std::string result = "successful operation";

if (handler_{{httpMethod}}_)
{
std::tie(status_code, result) = handler_{{httpMethod}}_(
{{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}
);
}

{{#responses}}
if (status_code == {{code}}) {
{{#baseType}}
Expand All @@ -179,7 +203,7 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
// Description: {{description}}
session->set_header("{{baseName}}", ""); // Change second param to your header value
{{/headers}}
session->close({{code}}, "{{message}}", { {"Connection", "close"} });
session->close({{code}}, result.empty() ? "{{message}}" : std::move(result), { {"Connection", "close"} });
return;
}
{{/responses}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.3.1-SNAPSHOT
4.0.1-SNAPSHOT
Loading