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 
26 namespace 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  */
41 WebviewRestApi::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  */
52 const 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  */
63 WebReply *
64 WebviewRestApi::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  */
84 void
85 WebviewRestApi::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  */
93 void
95 {
96  pretty_json_ = pretty;
97 }
98 
99 } // end of namespace fawkes
fawkes::WebviewRestApi::name
const std::string & name() const
Get name of component.
Definition: rest_api.cpp:56
fawkes::WebRequest
Definition: request.h:40
fawkes::WebviewRestApi::Handler
std::function< std::unique_ptr< WebReply >std::string, WebviewRestParams &)> Handler
REST API call handler function type.
Definition: rest_api.h:229
fawkes::WebviewRestApi::process_request
WebReply * process_request(const WebRequest *request, const std::string &rest_url)
Process REST API request.
Definition: rest_api.cpp:67
fawkes::WebRequest::get_values
const std::map< std::string, std::string > & get_values() const
Get map of GET values.
Definition: request.h:189
fawkes::Logger
Definition: logger.h:40
fawkes
fawkes::WebRequest::Method
Method
HTTP transfer methods.
Definition: request.h:46
fawkes::WebviewRestParams
REST parameters to pass to handlers.
Definition: rest_api.h:127
fawkes::WebRequest::body
const std::string & body() const
Get body of request.
Definition: request.h:324
fawkes::WebviewRestApi::WebviewRestApi
WebviewRestApi(const std::string &name, fawkes::Logger *logger)
Constructor.
Definition: rest_api.cpp:44
fawkes::WebRequest::method
Method method() const
Get HTTP transfer method.
Definition: request.h:83
fawkes::WebviewRestApi::set_pretty_json
void set_pretty_json(bool pretty)
Enable or disable pretty JSON printing globally.
Definition: rest_api.cpp:97
fawkes::NullPointerException
Definition: software.h:35
fawkes::WebReply
Definition: reply.h:37
fawkes::WebviewRestApi::add_handler
void add_handler(WebRequest::Method method, std::string path, Handler handler)
Add handler function.
Definition: rest_api.cpp:88