11
11
{ {{defaultInclude} }}
12
12
#include <memory >
13
13
#include <utility >
14
+ #include <exception >
14
15
15
16
#include <corvusoft /restbed /session.hpp >
16
17
#include <corvusoft /restbed /resource.hpp >
18
+ #include <corvusoft /restbed /request.hpp >
17
19
#include <corvusoft /restbed /service.hpp >
20
+ #include <corvusoft /restbed /settings.hpp >
18
21
19
22
{ {#imports} }{ {{import} }}
20
23
{ {/imports} }
@@ -25,6 +28,22 @@ namespace {{this}} {
25
28
26
29
using namespace { {modelNamespace} };
27
30
31
+ ///
32
+ /// Exception to flag problems in the handlers
33
+ ///
34
+ class { {declspec} } { {classname} }Exception: public std::exception
35
+ {
36
+ public:
37
+ {{classname} }Exception(int status_code, std::string what);
38
+
39
+ int getStatus() const;
40
+ const char* what() const noexcept override;
41
+
42
+ private:
43
+ int m_status;
44
+ std::string m_what;
45
+ };
46
+
28
47
{ {#operation} }
29
48
/// <summary >
30
49
/// { {summary} }
@@ -35,60 +54,143 @@ using namespace {{modelNamespace}};
35
54
class { {declspec} } { {classname} }{ {vendorExtensions.x-codegen-resource-name} }Resource: public restbed::Resource
36
55
{
37
56
public:
38
- {{classname} }{ {vendorExtensions.x-codegen-resource-name} }Resource();
57
+ {{classname} }{ {vendorExtensions.x-codegen-resource-name} }Resource(const std::string& context = " { {contextPath } }" );
39
58
virtual ~{ {classname} }{ {vendorExtensions.x-codegen-resource-name} }Resource();
40
- void { {httpMethod} }_method_handler(const std::shared_ptr<restbed::Session > session);
59
+
60
+ protected:
61
+ //////////////////////////////////////////////////////////
62
+ // Override these to implement the server functionality //
63
+ //////////////////////////////////////////////////////////
64
+
65
+ virtual { {#returnType} }std::pair<int , { { { .} } } >{ {/returnType} }{ {^returnType} }int{ {/returnType} } handler_{ {httpMethod} }(
66
+ { {#allParams} }{ {{dataType} }} const & { {{paramName} }}{ {^-last} }, { {/-last} }{ {/allParams} });
67
+
68
+ { {#vendorExtensions.x-codegen-other-methods} }
69
+ virtual { {#returnType} }std::pair<int , { { { .} } } >{ {/returnType} }{ {^returnType} }int{ {/returnType} } handler_{ {httpMethod} }(
70
+ { {#allParams} }{ {{dataType} }} const & { {{paramName} }}{ {^-last} }, { {/-last} }{ {/allParams} });
71
+ { {/vendorExtensions.x-codegen-other-methods} }
72
+
73
+ protected:
74
+ //////////////////////////////////////
75
+ // Override these for customization //
76
+ //////////////////////////////////////
77
+
78
+ virtual std::string extractBodyContent(const std::shared_ptr<restbed::Session >& session);
79
+
80
+ { {#hasPathParams} }
81
+ { {#pathParams} }
82
+ { {#isPrimitiveType} }
83
+ virtual { {{dataType} }} getPathParam_{ {{paramName} }}(const std::shared_ptr<const restbed::Request >& request)
84
+ {
85
+ return request-> get_path_parameter (" {{{paramName}}}" , {{{defaultValue} }});
86
+ }
87
+
88
+ { {/isPrimitiveType} }
89
+ { {/pathParams} }
90
+ { {/hasPathParams} }
91
+ { {#hasQueryParams} }
92
+ { {#queryParams} }
93
+ { {#isPrimitiveType} }
94
+ virtual { {{dataType} }} getQueryParam_{ {{paramName} }}(const std::shared_ptr<const restbed::Request >& request)
95
+ {
96
+ return request-> get_query_parameter (" {{{paramName}}}" , {{{defaultValue} }});
97
+ }
98
+
99
+ { {/isPrimitiveType} }
100
+ { {/queryParams} }
101
+ { {/hasQueryParams} }
102
+ { {#hasHeaderParams} }
103
+ { {#headerParams} }
104
+ { {#isPrimitiveType} }
105
+ virtual { {{dataType} }} getHeader_{ {{baseName} }}(const std::shared_ptr<const restbed::Request >& request)
106
+ {
107
+ return request-> get_header (" {{baseName}}" , {{{defaultValue} }});
108
+ }
109
+
110
+ { {/isPrimitiveType} }
111
+ { {/headerParams} }
112
+ { {/hasHeaderParams} }
113
+
41
114
{ {#vendorExtensions.x-codegen-other-methods} }
42
- void { {httpMethod} }_method_handler(const std::shared_ptr<restbed::Session > session);
115
+ { {#hasPathParams} }
116
+ { {#pathParams} }
117
+ { {#isPrimitiveType} }
118
+ virtual { {{dataType} }} getPathParam_{ {{paramName} }}_x_extension(const std::shared_ptr<const restbed::Request >& request)
119
+ {
120
+ return request-> get_path_parameter (" {{{paramName}}}" , {{{defaultValue} }});
121
+ }
122
+ { {/isPrimitiveType} }
123
+ { {/pathParams} }
124
+ { {/hasPathParams} }
125
+ { {#hasQueryParams} }
126
+ { {#queryParams} }
127
+ { {#isPrimitiveType} }
128
+ virtual { {{dataType} }} getQueryParam_{ {{paramName} }}_x_extension(const std::shared_ptr<const restbed::Request >& request)
129
+ {
130
+ return request-> get_query_parameter (" {{{paramName}}}" , {{{defaultValue} }});
131
+ }
132
+ { {/isPrimitiveType} }
133
+ { {/queryParams} }
134
+ { {/hasQueryParams} }
135
+ { {#hasHeaderParams} }
136
+ { {#headerParams} }
137
+ { {#isPrimitiveType} }
138
+ virtual { {{dataType} }} getHeader_{ {{baseName} }}_x_extension(const std::shared_ptr<const restbed::Request >& request)
139
+ {
140
+ return request-> get_header (" {{baseName}}" , {{{defaultValue} }});
141
+ }
142
+ { {/isPrimitiveType} }
143
+ { {/headerParams} }
144
+ { {/hasHeaderParams} }
43
145
{ {/vendorExtensions.x-codegen-other-methods} }
44
146
45
- void set_handler_{ {httpMethod} }(
46
- std::function<std::pair <int, std::string >(
47
- { {#allParams} }{ {{dataType} }} const & { {^-last} }, { {/-last} }{ {/allParams} }
48
- )> handler
49
- );
147
+ virtual std::pair<int , std::string > handle{ {classname} }Exception(const { {classname} }Exception& e);
148
+ virtual std::pair<int , std::string > handleStdException(const std::exception& e);
149
+ virtual std::pair<int , std::string > handleUnspecifiedException();
150
+
151
+ virtual void setResponseHeader(const std::shared_ptr<restbed::Session >& session,
152
+ const std::string& header);
50
153
51
- { {#vendorExtensions.x-codegen-other-methods} }
52
- void set_handler_{ {httpMethod} }(
53
- std::function<std::pair <int, std::string >(
54
- { {#allParams} }{ {{dataType} }} const & { {^-last} }, { {/-last} }{ {/allParams} }
55
- )> handler
56
- );
57
- { {/vendorExtensions.x-codegen-other-methods} }
154
+
155
+ virtual void returnResponse(const std::shared_ptr<restbed::Session >& session,
156
+ const int status, const std::string& result, const std::string& contentType);
157
+ virtual void defaultSessionClose(const std::shared_ptr<restbed::Session >& session,
158
+ const int status, const std::string& result);
58
159
59
160
private:
60
- std::function<std::pair <int, std::string >(
61
- { {#allParams} }{ {{dataType} }} const & { {^-last} }, { {/-last} }{ {/allParams} }
62
- )> handler_{ {httpMethod} }_;
63
-
64
- { {#vendorExtensions.x-codegen-other-methods} }
65
- std::function<std::pair <int, std::string >(
66
- { {#allParams} }{ {{dataType} }} const & { {^-last} }, { {/-last} }{ {/allParams} }
67
- )> handler_{ {httpMethod} }_;
68
- { {/vendorExtensions.x-codegen-other-methods} }
69
-
70
- { {#allParams} }
71
- { {{dataType} }} { {paramName} }{ } ;
72
- { {/allParams} }
161
+ void handler_{ {httpMethod} }_internal(const std::shared_ptr<restbed::Session > session);
162
+ { {#vendorExtensions.x-codegen-other-methods} }
163
+ void handler_{ {httpMethod} }_internal(const std::shared_ptr<restbed::Session > session);
164
+ { {/vendorExtensions.x-codegen-other-methods} }
73
165
};
74
166
167
+
75
168
{ {/operation} }
76
169
77
170
//
78
171
// The restbed service to actually implement the REST server
79
172
//
80
- class { {declspec} } { {classname} }: public restbed::Service
173
+ class { {declspec} } { {classname} }
81
174
{
82
175
public:
83
- {{classname} }();
84
- ~{ {classname} }();
85
- void startService(int const& port);
86
- void stopService();
176
+ explicit {{classname} }(std::shared_ptr<restbed::Service > const& restbedService);
177
+ virtual ~{ {classname} }();
178
+
179
+ { {#operation} }
180
+ virtual void set{ {classname} }{ {vendorExtensions.x-codegen-resource-name} }Resource(std::shared_ptr<{ {classname} }{ {vendorExtensions.x-codegen-resource-name} }Resource> sp{ {classname} }{ {vendorExtensions.x-codegen-resource-name} }Resource);
181
+ { {/operation} }
182
+
183
+ virtual void publishDefaultResources();
184
+
185
+ virtual std::shared_ptr<restbed::Service > service();
87
186
88
187
protected:
89
188
{ {#operation} }
90
189
std::shared_ptr<{ {classname} }{ {vendorExtensions.x-codegen-resource-name} }Resource> m_sp{ {classname} }{ {vendorExtensions.x-codegen-resource-name} }Resource;
91
190
{ {/operation} }
191
+
192
+ private:
193
+ std::shared_ptr<restbed::Service > m_service;
92
194
};
93
195
94
196
0 commit comments