Skip to content

Commit 87adba9

Browse files
muttleyxdstkrwork
authored andcommitted
[C++][Restbed] Add handler callback methods (#2911)
* [C++][Restbed] Add handler callback methods * [C++][Restbed] Update Petstore sample
1 parent aae9763 commit 87adba9

File tree

21 files changed

+773
-258
lines changed

21 files changed

+773
-258
lines changed

modules/openapi-generator/src/main/resources/cpp-restbed-server/api-header.mustache

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
{{{defaultInclude}}}
1212
#include <memory>
13+
#include <utility>
14+
1315
#include <corvusoft/restbed/session.hpp>
1416
#include <corvusoft/restbed/resource.hpp>
1517
#include <corvusoft/restbed/service.hpp>
@@ -49,6 +51,31 @@ public:
4951
{{#vendorExtensions.x-codegen-otherMethods}}
5052
void {{httpMethod}}_method_handler(const std::shared_ptr<restbed::Session> session);
5153
{{/vendorExtensions.x-codegen-otherMethods}}
54+
55+
void set_handler_{{httpMethod}}(
56+
std::function<std::pair<int, std::string>(
57+
{{#allParams}}{{{dataType}}} const &{{#hasMore}}, {{/hasMore}}{{/allParams}}
58+
)> handler
59+
);
60+
61+
{{#vendorExtensions.x-codegen-otherMethods}}
62+
void set_handler_{{httpMethod}}(
63+
std::function<std::pair<int, std::string>(
64+
{{#allParams}}{{{dataType}}} const &{{#hasMore}}, {{/hasMore}}{{/allParams}}
65+
)> handler
66+
);
67+
{{/vendorExtensions.x-codegen-otherMethods}}
68+
69+
private:
70+
std::function<std::pair<int, std::string>(
71+
{{#allParams}}{{{dataType}}} const &{{#hasMore}}, {{/hasMore}}{{/allParams}}
72+
)> handler_{{httpMethod}}_;
73+
74+
{{#vendorExtensions.x-codegen-otherMethods}}
75+
std::function<std::pair<int, std::string>(
76+
{{#allParams}}{{{dataType}}} const &{{#hasMore}}, {{/hasMore}}{{/allParams}}
77+
)> handler_{{httpMethod}}_;
78+
{{/vendorExtensions.x-codegen-otherMethods}}
5279
};
5380

5481
{{/operation}}

modules/openapi-generator/src/main/resources/cpp-restbed-server/api-source.mustache

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ void {{classname}}::stopService() {
5454
{
5555
}
5656

57+
void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::set_handler_{{httpMethod}}(
58+
std::function<std::pair<int, std::string>(
59+
{{#allParams}}{{{dataType}}} const &{{#hasMore}}, {{/hasMore}}{{/allParams}}
60+
)> handler) {
61+
handler_{{httpMethod}}_ = std::move(handler);
62+
}
63+
64+
{{#vendorExtensions.x-codegen-otherMethods}}
65+
void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::set_handler_{{httpMethod}}(
66+
std::function<std::pair<int, std::string>(
67+
{{#allParams}}{{{dataType}}} const &{{#hasMore}}, {{/hasMore}}{{/allParams}}
68+
)> handler) {
69+
handler_{{httpMethod}}_ = std::move(handler);
70+
}
71+
{{/vendorExtensions.x-codegen-otherMethods}}
72+
5773
void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMethod}}_method_handler(const std::shared_ptr<restbed::Session> session) {
5874
5975
const auto request = session->get_request();
@@ -65,12 +81,12 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
6581
{
6682
6783
const auto request = session->get_request();
68-
std::string requestBody = restbed::String::format("%.*s\n", ( int ) body.size( ), body.data( ));
84+
std::string file = restbed::String::format("%.*s\n", ( int ) body.size( ), body.data( ));
6985
/**
70-
* Get body params or form params here from the requestBody string
86+
* Get body params or form params here from the file string
7187
*/
7288
{{/hasBodyParam}}
73-
89+
7490
{{#hasPathParams}}
7591
// Getting the path params
7692
{{#pathParams}}
@@ -79,7 +95,7 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
7995
{{/isPrimitiveType}}
8096
{{/pathParams}}
8197
{{/hasPathParams}}
82-
98+
8399
{{#hasQueryParams}}
84100
// Getting the query params
85101
{{#queryParams}}
@@ -97,21 +113,25 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
97113
{{/isPrimitiveType}}
98114
{{/headerParams}}
99115
{{/hasHeaderParams}}
100-
116+
101117
// Change the value of this variable to the appropriate response before sending the response
102118
int status_code = 200;
103-
104-
/**
105-
* Process the received information here
106-
*/
107-
119+
std::string result = "successful operation";
120+
121+
if (handler_{{httpMethod}}_)
122+
{
123+
std::tie(status_code, result) = handler_{{httpMethod}}_(
124+
{{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}
125+
);
126+
}
127+
108128
{{#responses}}
109129
if (status_code == {{code}}) {
110130
{{#headers}}
111131
// Description: {{description}}
112132
session->set_header("{{baseName}}", ""); // Change second param to your header value
113133
{{/headers}}
114-
session->close({{code}}, "{{message}}", { {"Connection", "close"} });
134+
session->close({{code}}, result.empty() ? "{{message}}" : std::move(result), { {"Connection", "close"} });
115135
return;
116136
}
117137
{{/responses}}
@@ -133,7 +153,7 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
133153
{
134154
135155
const auto request = session->get_request();
136-
std::string requestBody = restbed::String::format("%.*s\n", ( int ) body.size( ), body.data( ));
156+
std::string file = restbed::String::format("%.*s\n", ( int ) body.size( ), body.data( ));
137157
{{/hasBodyParam}}
138158

139159
{{#hasPathParams}}
@@ -144,7 +164,7 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
144164
{{/isPrimitiveType}}
145165
{{/pathParams}}
146166
{{/hasPathParams}}
147-
167+
148168
{{#hasQueryParams}}
149169
// Getting the query params
150170
{{#queryParams}}
@@ -162,14 +182,18 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
162182
{{/isPrimitiveType}}
163183
{{/headerParams}}
164184
{{/hasHeaderParams}}
165-
185+
166186
// Change the value of this variable to the appropriate response before sending the response
167187
int status_code = 200;
168-
169-
/**
170-
* Process the received information here
171-
*/
172-
188+
std::string result = "successful operation";
189+
190+
if (handler_{{httpMethod}}_)
191+
{
192+
std::tie(status_code, result) = handler_{{httpMethod}}_(
193+
{{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}
194+
);
195+
}
196+
173197
{{#responses}}
174198
if (status_code == {{code}}) {
175199
{{#baseType}}
@@ -179,7 +203,7 @@ void {{classname}}{{vendorExtensions.x-codegen-resourceName}}Resource::{{httpMet
179203
// Description: {{description}}
180204
session->set_header("{{baseName}}", ""); // Change second param to your header value
181205
{{/headers}}
182-
session->close({{code}}, "{{message}}", { {"Connection", "close"} });
206+
session->close({{code}}, result.empty() ? "{{message}}" : std::move(result), { {"Connection", "close"} });
183207
return;
184208
}
185209
{{/responses}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.3.1-SNAPSHOT
1+
4.0.1-SNAPSHOT

0 commit comments

Comments
 (0)