Fawkes API Fawkes Development Version
rest_api.cpp
1
2/***************************************************************************
3 * rest_api.cpp - Webview REST API
4 *
5 * Created: Fri Mar 16 17:39:57 2018
6 * Copyright 2006-2018 Tim Niemueller [www.niemueller.de]
7 ****************************************************************************/
8
9/* This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Library General Public License for more details.
18 *
19 * Read the full text in the LICENSE.GPL file in the doc directory.
20 */
21
22#include <core/exception.h>
23#include <webview/rest_api.h>
24#include <webview/router.h>
25
26namespace fawkes {
27
28/** @class WebviewRestApi <webview/rest_api.h>
29 * Webview REST API component.
30 * This class represents a specific REST API available through Webview.
31 * The API's name will be part of the URL, e.g., '/api/[COMPONENT-NAME]/...'.
32 * The REST API can process patterns according to the OpenAPI 3 specification.
33 * @author Tim Niemueller
34 */
35
36/** Constructor.
37 * @param name of the API.
38 * The API's name will be part of the URL, e.g., '/api/[COMPONENT-NAME]/...'.
39 * @param logger logger for informative output
40 */
41WebviewRestApi::WebviewRestApi(const std::string &name, fawkes::Logger *logger)
42: name_(name),
43 logger_(logger),
44 pretty_json_(false),
45 router_{std::make_shared<WebviewRouter<Handler>>()}
46{
47}
48
49/** Get name of component.
50 * @return name of component.
51 */
52const std::string &
54{
55 return name_;
56}
57
58/** Process REST API request.
59 * @param request incoming request
60 * @param rest_url the URL stripped of the base URL prefix
61 * @return reply
62 */
64WebviewRestApi::process_request(const WebRequest *request, const std::string &rest_url)
65{
66 try {
67 std::map<std::string, std::string> path_args;
68 Handler handler = router_->find_handler(request->method(), rest_url, path_args);
69 WebviewRestParams params;
70 params.set_path_args(std::move(path_args));
71 params.set_query_args(request->get_values());
72 std::unique_ptr<WebReply> reply = handler(request->body(), params);
73 return reply.release();
74 } catch (NullPointerException &e) {
75 return NULL;
76 }
77}
78
79/** Add handler function.
80 * @param method HTTP method to react to
81 * @param path path (after component base path) to react to
82 * @param handler handler function
83 */
84void
85WebviewRestApi::add_handler(WebRequest::Method method, std::string path, Handler handler)
86{
87 router_->add(method, path, handler);
88}
89
90/** Enable or disable pretty JSON printing globally.
91 * @param pretty true to enable
92 */
93void
95{
96 pretty_json_ = pretty;
97}
98
99} // end of namespace fawkes
Interface for logging.
Definition: logger.h:42
A NULL pointer was supplied where not allowed.
Definition: software.h:32
Basic web reply.
Definition: reply.h:34
Web request meta data carrier.
Definition: request.h:42
Method method() const
Get HTTP transfer method.
Definition: request.h:84
const std::string & body() const
Get body of request.
Definition: request.h:325
Method
HTTP transfer methods.
Definition: request.h:47
const std::map< std::string, std::string > & get_values() const
Get map of GET values.
Definition: request.h:190
void set_pretty_json(bool pretty)
Enable or disable pretty JSON printing globally.
Definition: rest_api.cpp:94
WebReply * process_request(const WebRequest *request, const std::string &rest_url)
Process REST API request.
Definition: rest_api.cpp:64
const std::string & name() const
Get name of component.
Definition: rest_api.cpp:53
std::function< std::unique_ptr< WebReply >(std::string, WebviewRestParams &)> Handler
REST API call handler function type.
Definition: rest_api.h:226
void add_handler(WebRequest::Method method, std::string path, Handler handler)
Add handler function.
Definition: rest_api.cpp:85
WebviewRestApi(const std::string &name, fawkes::Logger *logger)
Constructor.
Definition: rest_api.cpp:41
REST parameters to pass to handlers.
Definition: rest_api.h:125
URL path router.
Definition: router.h:44
Fawkes library namespace.