22 #ifndef _LIBS_WEBVIEW_REST_API_H_
23 #define _LIBS_WEBVIEW_REST_API_H_
25 #include <core/exception.h>
26 #include <logging/logger.h>
27 #include <utils/misc/string_split.h>
28 #include <webview/reply.h>
29 #include <webview/request.h>
57 const std::string &
body =
"",
58 const std::string &content_type =
"application/json")
91 template <typename T, typename = std::enable_if_t<std::is_class<T>::value>>
95 append(
"%s", o.to_json(pretty).c_str());
113 return content_type_;
118 std::string content_type_;
124 class WebviewRestParams
144 if (path_args_.find(what) != path_args_.end()) {
145 return path_args_[what];
160 if (query_args_.find(what) != query_args_.end()) {
161 return query_args_[what];
176 return (query_args_.find(what) != query_args_.end());
196 pretty_json_ = pretty;
201 set_path_args(std::map<std::string, std::string> &&args)
203 path_args_ = std::move(args);
207 set_query_args(
const std::map<std::string, std::string> &args)
214 std::map<std::string, std::string> path_args_;
215 std::map<std::string, std::string> query_args_;
228 const std::string &
name()
const;
246 [handler](
const std::string &body,
251 return std::make_unique<WebviewRestReply>(e.code(),
252 e.what_no_backtrace(),
258 r->add_header(
"Content-type",
"text/plain");
269 template <
class O,
class I>
277 [
this, handler](
const std::string &body,
280 input.from_json(body);
282 O output{handler(input, m)};
285 }
catch (std::runtime_error &e) {
286 logger_->
log_warn((
"RestAPI|" + name_).c_str(),
"%s", e.what());
288 if (m.has_query_arg(
"pretty")) {
289 m.set_pretty_json(
true);
292 output.to_json(pretty_json_
293 || m.pretty_json()));
295 return std::make_unique<WebviewRestReply>(e.code(),
296 e.what_no_backtrace(),
302 r->add_header(
"Content-type",
"text/plain");
321 [
this, handler](
const std::string &body,
324 input.from_json(body);
326 return handler(std::forward<I>(input), m);
328 return std::make_unique<WebviewRestReply>(e.code(),
329 e.what_no_backtrace(),
335 r->add_header(
"Content-type",
"text/plain");
354 [
this, handler](
const std::string &body,
357 O output{handler(m)};
360 }
catch (std::runtime_error &e) {
361 logger_->
log_warn((
"RestAPI|" + name_).c_str(),
"%s", e.what());
363 if (m.has_query_arg(
"pretty")) {
364 m.set_pretty_json(
true);
367 output.to_json(pretty_json_
368 || m.pretty_json()));
370 return std::make_unique<WebviewRestReply>(e.code(),
371 e.what_no_backtrace(),
377 r->add_header(
"Content-type",
"text/plain");
389 std::shared_ptr<WebviewRouter<Handler>> router_;