Skip to content

Commit

Permalink
[C++][Restbed] Add handler callback methods (#2911)
Browse files Browse the repository at this point in the history
* [C++][Restbed] Add handler callback methods

* [C++][Restbed] Update Petstore sample
  • Loading branch information
muttleyxd authored and stkrwork committed May 30, 2019
1 parent aae9763 commit 87adba9
Show file tree
Hide file tree
Showing 21 changed files with 773 additions and 258 deletions.
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
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
@@ -1 +1 @@
3.3.1-SNAPSHOT
4.0.1-SNAPSHOT

0 comments on commit 87adba9

Please sign in to comment.