@@ -9,7 +9,8 @@ namespace {{apiNamespace}}
99
1010using namespace {{helpersNamespace} };
1111{ {#hasModelImport} }
12- using namespace { {modelNamespace} };{ {/hasModelImport} }
12+ using namespace { {modelNamespace} };
13+ { {/hasModelImport} }
1314
1415const std::string { {classname} }::base = "{ {basePathWithoutHost} }";
1516
@@ -32,14 +33,12 @@ void {{classname}}::setupRoutes() {
3233 router->addCustomHandler(Routes::bind(& { {classname} }::{ {classnameSnakeLowerCase} }_default_handler, this));
3334}
3435
35- void { {classname} }::handleParsingException(const std::exception& ex, Pistache::Http::ResponseWriter &response) const noexcept
36- {
36+ void { {classname} }::handleParsingException(const std::exception& ex, Pistache::Http::ResponseWriter &response) const noexcept {
3737 std::pair< Pistache::Http::Code, std::string> codeAndError = handleParsingException(ex);
3838 response.send(codeAndError.first, codeAndError.second);
3939}
4040
41- std::pair<Pistache::Http::Code , std::string > { {classname} }::handleParsingException(const std::exception& ex) const noexcept
42- {
41+ std::pair<Pistache::Http::Code , std::string > { {classname} }::handleParsingException(const std::exception& ex) const noexcept {
4342 try {
4443 throw;
4544 } catch (nlohmann::detail::exception &e) {
@@ -51,179 +50,187 @@ std::pair<Pistache::Http::Code, std::string> {{classname}}::handleParsingExcepti
5150 }
5251}
5352
54- void { {classname} }::handleOperationException(const std::exception& ex, Pistache::Http::ResponseWriter &response) const noexcept
55- {
53+ void { {classname} }::handleOperationException(const std::exception& ex, Pistache::Http::ResponseWriter &response) const noexcept {
5654 std::pair< Pistache::Http::Code, std::string> codeAndError = handleOperationException(ex);
5755 response.send(codeAndError.first, codeAndError.second);
5856}
5957
60- std::pair<Pistache::Http::Code , std::string > { {classname} }::handleOperationException(const std::exception& ex) const noexcept
61- {
58+ std::pair<Pistache::Http::Code , std::string > { {classname} }::handleOperationException(const std::exception& ex) const noexcept {
6259 return std::make_pair(Pistache::Http::Code::Internal_Server_Error, ex.what());
6360}
6461
6562{ {#operation} }
66- void { {classname} }::{ {operationIdSnakeCase} }_handler(const Pistache::Rest::Request & { {#hasParams } } request{ {/hasParams } } , Pistache::Http::ResponseWriter response) {
63+ void { {classname} }::{ {operationIdSnakeCase} }_handler(const Pistache::Rest::Request& request, Pistache::Http::ResponseWriter response) {
6764 try {
68-
69- {{#vendorExtensions.x-codegen-pistache-is-parsing-supported} }
70- { {#hasPathParams} }
71- // Getting the path params
72- { {#pathParams} }
73- auto { {paramName} } = request.param(":{ {paramName} }").as<{ {dataType} }>();
74- { {/pathParams} }
75- { {/hasPathParams} }{ {#hasBodyParam} }
76- // Getting the body param
77- { {#bodyParam} }
78- { {^isPrimitiveType} }{ {^isContainer} }
79- { {baseType} } { {paramName} };{ {/isContainer} }{ {#isArray} }std::vector<{ {items.baseType} }> { {paramName} };{ {/isArray} }{ {#isMap} }std::map<std::string , { { items.baseType} } > { {paramName} };{ {/isMap} }{ {/isPrimitiveType} }
80- { {#isPrimitiveType} }
81- { {dataType} } { {paramName} };
82- { {/isPrimitiveType} }
83- { {/bodyParam} }
84- { {/hasBodyParam} }{ {#hasQueryParams} }
85- // Getting the query params
86- { {#queryParams} }
87- auto { {paramName} }Query = request.query().get("{ {baseName} }");
88- std::optional<{ {^isContainer} }{ {dataType} }{ {/isContainer} }{ {#isArray} }std::vector<{ {items.baseType} }>{ {/isArray} }> { {paramName} };
89- if({ {paramName} }Query.has_value()){
90- {{^isContainer} }{ {dataType} }{ {/isContainer} }{ {#isArray} }std::vector<{ {items.baseType} }>{ {/isArray} } valueQuery_instance;
91- if(fromStringValue({ {paramName} }Query.value(), valueQuery_instance)){
92- {{paramName} } = valueQuery_instance;
65+ {{#vendorExtensions.x-codegen-pistache-is-parsing-supported} }
66+
67+ { {#hasPathParams} }
68+ // Getting the path params
69+ { {#pathParams} }
70+ auto { {paramName} } = request.param(":{ {paramName} }").as<{ {dataType} }>();
71+ { {/pathParams} }
72+ { {/hasPathParams} }
73+
74+ { {#hasBodyParam} }
75+ // Getting the body param
76+ { {#bodyParam} }
77+ { {^isPrimitiveType} }{ {^isContainer} }
78+ { {baseType} } { {paramName} };
79+ { {/isContainer} }
80+ { {#isArray} }std::vector<{ {items.baseType} }> { {paramName} };{ {/isArray} }
81+ { {#isMap} }std::map<std::string , { { items.baseType} } > { {paramName} };{ {/isMap} }
82+ { {/isPrimitiveType} }
83+ { {#isPrimitiveType} }
84+ { {dataType} } { {paramName} };
85+ { {/isPrimitiveType} }
86+ { {/bodyParam} }
87+ { {/hasBodyParam} }
88+
89+ { {#hasQueryParams} }
90+ // Getting the query params
91+ { {#queryParams} }
92+ auto { {paramName} }Query = request.query().get("{ {baseName} }");
93+ std::optional<{ {^isContainer} }{ {dataType} }{ {/isContainer} }{ {#isArray} }std::vector<{ {items.baseType} }>{ {/isArray} }> { {paramName} };
94+ if ({ {paramName} }Query.has_value()) {
95+ {{^isContainer} }{ {dataType} }{ {/isContainer} }{ {#isArray} }std::vector<{ {items.baseType} }>{ {/isArray} } valueQuery_instance;
96+ if (fromStringValue({ {paramName} }Query.value(), valueQuery_instance)) {
97+ {{paramName} } = valueQuery_instance;
98+ }
9399 }
94- }
95- { {/queryParams} }
96- { {/hasQueryParams} }{ {#hasHeaderParams} }
97- // Getting the header params
98- { {#headerParams} }
99- auto { {paramName} } = request.headers().tryGetRaw("{ {baseName} }");
100- { {/headerParams} }
101- { {/hasHeaderParams} }
102-
103- try {
104- {{#hasBodyParam} }
105- { {#bodyParam} }
106- { {^isPrimitiveType} }
107- nlohmann::json::parse(request.body()).get_to({ {paramName} });{ {#isArray} }
108- for (const auto& validationParam : { {paramName} })
109- validationParam.validate();{ {/isArray} }{ {^isArray} }
110- { {paramName} }.validate();{ {/isArray} }
111- { {/isPrimitiveType} }
112- { {#isPrimitiveType} }
113- { {paramName} } = request.body();
114- { {/isPrimitiveType} }
115- } catch (std::exception &e) {
116- this-> handleParsingException (e, response);
117- return;
118- }
119-
120- try {
121-
122- {{/bodyParam} }
123- { {/hasBodyParam} }
100+ { {/queryParams} }
101+ { {/hasQueryParams} }
102+
103+ { {#hasHeaderParams} }
104+ // Getting the header params
105+ { {#headerParams} }
106+ auto { {paramName} } = request.headers().tryGetRaw("{ {baseName} }");
107+ { {/headerParams} }
108+ { {/hasHeaderParams} }
109+
110+ { {#hasBodyParam} }
111+ try {
112+ {{#bodyParam} }
113+ { {^isPrimitiveType} }
114+ nlohmann::json::parse(request.body()).get_to({ {paramName} });
115+ { {#isArray} }
116+ for (const auto& validationParam : { {paramName} })
117+ validationParam.validate();
118+ { {/isArray} }
119+ { {^isArray} }
120+ { {paramName} }.validate();
121+ { {/isArray} }
122+ { {/isPrimitiveType} }
123+ { {#isPrimitiveType} }
124+ { {paramName} } = request.body();
125+ { {/isPrimitiveType} }
126+ { {/bodyParam} }
127+ } catch (std::exception& e) {
128+ this-> handleParsingException (e, response);
129+ return;
130+ }
131+ { {/hasBodyParam} }
124132
125- { {#authMethods} }
126- #ifndef HTTP_BASIC_AUTH_DEFINED
127- #define HTTP_BASIC_AUTH_DEFINED 0
128- #endif
129- #ifndef HTTP_BEARER_AUTH_DEFINED
130- #define HTTP_BEARER_AUTH_DEFINED 0
131- #endif
132- { {/authMethods} }
133+ try {
134+ {{#authMethods} }
135+ #ifndef HTTP_BASIC_AUTH_DEFINED
136+ #define HTTP_BASIC_AUTH_DEFINED 0
137+ #endif
138+ #ifndef HTTP_BEARER_AUTH_DEFINED
139+ #define HTTP_BEARER_AUTH_DEFINED 0
140+ #endif
141+ { {/authMethods} }
133142
134143
135144
136- { {#authMethods} }{ {#isBasicBasic} }
145+ { {#authMethods} }{ {#isBasicBasic} }
137146#undef HTTP_BASIC_AUTH_DEFINED
138147#define HTTP_BASIC_AUTH_DEFINED 1
139148
140- auto basicAuthHeader = request.headers().tryGet<Pistache::Http::Header::Authorization >();
149+ auto basicAuthHeader = request.headers().tryGet<Pistache::Http::Header::Authorization >();
141150
142- if( (!basicAuthHeader) || (basicAuthHeader->getMethod() != Pistache::Http::Header::Authorization::Method::Basic))
143- {
144- response.send(Pistache::Http::Code::Unauthorized, " " );
145- return;
146- }
147- HttpBasicCredentials credentials{ basicAuthHeader-> getBasicUser (), basicAuthHeader-> getBasicPassword ()} ;
148- if( ! this->basicCredentialsAuthenticator.has_value())
149- {
150- response.send(Pistache::Http::Code::Unauthorized, " " );
151- return;
152- }
151+ if(!basicAuthHeader || (basicAuthHeader->getMethod() != Pistache::Http::Header::Authorization::Method::Basic))
152+ {
153+ response.send(Pistache::Http::Code::Unauthorized, " " );
154+ return;
155+ }
153156
154- if( ! this->basicCredentialsAuthenticator.value()(credentials))
155- {
156- response.send(Pistache::Http::Code::Unauthorized, " " );
157- return;
158- }
157+ HttpBasicCredentials credentials{ basicAuthHeader-> getBasicUser (), basicAuthHeader-> getBasicPassword ()} ;
158+
159+ if (!this->basicCredentialsAuthenticator.has_value() || !this->basicCredentialsAuthenticator.value()(credentials))
160+ {
161+ response.send(Pistache::Http::Code::Unauthorized, " " );
162+ return;
163+ }
159164
160- { {/isBasicBasic} }{ {/authMethods} }
165+ { {/isBasicBasic} }
166+ { {/authMethods} }
161167
162- { {#authMethods} }{ {#isBasicBearer} }
168+ { {#authMethods} }
169+ { {#isBasicBearer} }
163170#undef HTTP_BEARER_AUTH_DEFINED
164171#define HTTP_BEARER_AUTH_DEFINED 1
165- auto bearerAuthHeader = request.headers().tryGet<Pistache::Http::Header::Authorization >();
166-
167- if( (!bearerAuthHeader) || (bearerAuthHeader->getMethod() != Pistache::Http::Header::Authorization::Method::Bearer))
168- {
169- response.send(Pistache::Http::Code::Unauthorized, " " );
170- return;
171- }
172- std::string completeHeaderValue = bearerAuthHeader->value();
173- const std::string tokenAsString(completeHeaderValue.begin() + std::string("Bearer ").length(), completeHeaderValue.end());
174-
175- HttpBearerToken bearerToken{ tokenAsString} ;
176- if( ! this->bearerTokenAuthenticator.has_value())
177- {
178- response.send(Pistache::Http::Code::Unauthorized, " " );
179- return;
180- }
181-
182- if( ! this->bearerTokenAuthenticator.value()(bearerToken))
183- {
184- response.send(Pistache::Http::Code::Unauthorized, " " );
185- return;
186- }
187172
188-
189-
190- { {/isBasicBearer} }{ {/authMethods} }
191-
192- this->{ {operationIdSnakeCase} }({ {#authMethods} }{ {#isBasicBasic} }credentials,{ {/isBasicBasic} }{ {#isBasicBearer} }bearerToken,{ {/isBasicBearer} }{ {/authMethods} }{ {#allParams} }{ {paramName} }, { {/allParams} }response);
193- { {/vendorExtensions.x-codegen-pistache-is-parsing-supported} }
194- { {^vendorExtensions.x-codegen-pistache-is-parsing-supported} }
195- try {
196- this-> {{operationIdSnakeCase} }(request, response);
197- { {/vendorExtensions.x-codegen-pistache-is-parsing-supported} }
198- } catch (Pistache::Http::HttpError &e) {
199- response.send(static_cast< Pistache::Http::Code> (e.code()), e.what());
200- return;
201- } catch (std::exception &e) {
202- this-> handleOperationException (e, response);
203- return;
204- }
173+ auto bearerAuthHeader = request.headers().tryGet<Pistache::Http::Header::Authorization >();
174+
175+ if (!bearerAuthHeader || (bearerAuthHeader->getMethod() != Pistache::Http::Header::Authorization::Method::Bearer))
176+ {
177+ response.send(Pistache::Http::Code::Unauthorized, " " );
178+ return;
179+ }
180+
181+ std::string completeHeaderValue = bearerAuthHeader->value();
182+ const std::string tokenAsString(completeHeaderValue.begin() + std::string("Bearer ").length(), completeHeaderValue.end());
183+
184+ HttpBearerToken bearerToken{ tokenAsString} ;
185+
186+ if (!this->bearerTokenAuthenticator.has_value() || !this->bearerTokenAuthenticator.value()(bearerToken))
187+ {
188+ response.send(Pistache::Http::Code::Unauthorized, " " );
189+ return;
190+ }
191+ { {/isBasicBearer} }
192+ { {/authMethods} }
193+
194+ this->{ {operationIdSnakeCase} }({ {#authMethods} }{ {#isBasicBasic} }credentials,{ {/isBasicBasic} }{ {#isBasicBearer} }bearerToken,{ {/isBasicBearer} }{ {/authMethods} }{ {#allParams} }{ {paramName} }, { {/allParams} }response);
195+ { {/vendorExtensions.x-codegen-pistache-is-parsing-supported} }
196+ { {^vendorExtensions.x-codegen-pistache-is-parsing-supported} }
197+
198+ try {
199+ this-> {{operationIdSnakeCase} }(request, response);
200+ { {/vendorExtensions.x-codegen-pistache-is-parsing-supported} }
201+ } catch (Pistache::Http::HttpError &e) {
202+ response.send(static_cast< Pistache::Http::Code> (e.code()), e.what());
203+ return;
204+ } catch (std::exception &e) {
205+ this-> handleOperationException (e, response);
206+ return;
207+ }
205208
206209 } catch (std::exception &e) {
207210 response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
208211 }
209212
213+ { {#hasAuthMethods} }
214+ #ifndef HTTP_BASIC_AUTH_DEFINED
215+ #define HTTP_BASIC_AUTH_DEFINED 0
216+ #endif
217+ #ifndef HTTP_BEARER_AUTH_DEFINED
218+ #define HTTP_BEARER_AUTH_DEFINED 0
219+ #endif
210220#define REST_PATH "{ {{vendorExtensions.x-codegen-pistache-path} }}" { {! this is nessecary, because the path does not exist in the authMethods scope.} }
211- { {#authMethods} }
212221 { {! This static assert may be rendered more that once, so the compilation will fail even harder! } }
213- static_assert(HTTP_BASIC_AUTH_DEFINED + HTTP_BEARER_AUTH_DEFINED < 2, "Path '" REST_PATH "' has more than one security scheme specified, and the Pistache server generator does not support that." );
214- { {/authMethods} }
222+ static_assert(HTTP_BASIC_AUTH_DEFINED + HTTP_BEARER_AUTH_DEFINED < 2, "Path '" REST_PATH "' has more than one security scheme specified, and the Pistache server generator does not support that.");
215223#undef REST_PATH
216-
217- { {#authMethods} }
218- #ifdef HTTP_BEARER_AUTH_DEFINED
219- #undef HTTP_BEARER_AUTH_DEFINED
220- #endif
221- #ifdef HTTP_BASIC_AUTH_DEFINED
222- #undef HTTP_BASIC_AUTH_DEFINED
223- #endif
224- { {/authMethods} }
224+ #ifdef HTTP_BEARER_AUTH_DEFINED
225+ #undef HTTP_BEARER_AUTH_DEFINED
226+ #endif
227+ #ifdef HTTP_BASIC_AUTH_DEFINED
228+ #undef HTTP_BASIC_AUTH_DEFINED
229+ #endif
230+ { {/hasAuthMethods} }
225231
226232}
233+
227234{ {/operation} }
228235
229236void { {classname} }::{ {classnameSnakeLowerCase} }_default_handler(const Pistache::Rest::Request & , Pistache::Http::ResponseWriter response) {
0 commit comments