Fawkes API
Fawkes Development Version
|
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_;
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(),
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());
292 output.to_json(pretty_json_
295 return std::make_unique<WebviewRestReply>(e.
code(),
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(),
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());
367 output.to_json(pretty_json_
370 return std::make_unique<WebviewRestReply>(e.
code(),
377 r->add_header(
"Content-type",
"text/plain");
389 std::shared_ptr<WebviewRouter<Handler>> router_;
const std::string & name() const
Get name of component.
WebReply::Code code()
Get HTTP response code.
Web request meta data carrier.
void append_va(const char *format, va_list va)
Append messages to the message list.
std::function< std::unique_ptr< WebReply >std::string, WebviewRestParams &)> Handler
REST API call handler function type.
void set_pretty_json(bool pretty)
Enable or disable pretty printed results.
WebReply * process_request(const WebRequest *request, const std::string &rest_url)
Process REST API request.
void add_handler(WebRequest::Method method, std::string path, std::function< std::unique_ptr< WebReply >(I, WebviewRestParams &)> handler)
Add handler function.
void add_handler(WebRequest::Method method, std::string path, std::function< O(I &, WebviewRestParams &)> handler)
Add handler function.
void append(const char *format,...)
Append messages to the message list.
WebviewRestParams()
Constructor.
const std::string & content_type() const
Get content type of response.
void add_handler(WebRequest::Method method, std::string path, std::function< O(WebviewRestParams &)> handler)
Add handler function.
Fawkes library namespace.
WebviewRestException(WebReply::Code code, const char *format,...)
Constructor.
Method
HTTP transfer methods.
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
void add_header(const std::string &header, const std::string &content)
Add a HTTP header.
bool has_query_arg(const std::string &what)
Check if query argument is set.
REST parameters to pass to handlers.
virtual const char * what_no_backtrace() const
Get primary string (does not implicitly print the back trace).
void add_handler(WebRequest::Method method, std::string path, std::function< std::unique_ptr< WebReply >(WebviewRestParams &)> handler)
Add simple handler.
Webview REST API component.
std::string query_arg(const std::string &what)
Get a query argument.
std::string path_arg(const std::string &what)
Get a path argument.
bool pretty_json()
Is pretty-printed JSON enabled?
WebviewRestException(WebReply::Code code, const T &o, bool pretty=false)
Constructor.
WebviewRestApi(const std::string &name, fawkes::Logger *logger)
Constructor.
WebviewRestReply(WebReply::Code code, const std::string &body="", const std::string &content_type="application/json")
Constructor.
void set_pretty_json(bool pretty)
Enable or disable pretty JSON printing globally.
virtual const std::string & body()
Get body.
Code code() const
Get response code.
REST processing exception.
@ HTTP_INTERNAL_SERVER_ERROR
INTERNAL_SERVER_ERROR.
void add_handler(WebRequest::Method method, std::string path, Handler handler)
Add handler function.
Base class for exceptions in Fawkes.