22 #include "config-rest-api.h"
24 #include <config/config.h>
25 #include <rapidjson/document.h>
26 #include <rapidjson/pointer.h>
27 #include <webview/rest_api_manager.h>
40 :
Thread(
"ConfigurationRestApi",
Thread::OPMODE_WAITFORWAKEUP)
55 std::bind(&ConfigurationRestApi::cb_get_config,
57 std::placeholders::_1));
73 static rapidjson::Value
74 create_value(std::unique_ptr<fawkes::Configuration::ValueIterator> &i,
75 rapidjson::Document::AllocatorType & a)
80 v.Reserve(i->get_list_size(), a);
82 std::vector<float> ivs = i->get_floats();
83 for (
const auto &ivsv : ivs) {
84 v.PushBack(rapidjson::Value(ivsv).Move(), a);
86 }
else if (i->is_uint()) {
87 std::vector<unsigned int> ivs = i->get_uints();
88 for (
const auto &ivsv : ivs) {
89 v.PushBack(rapidjson::Value(ivsv).Move(), a);
91 }
else if (i->is_int()) {
92 std::vector<int> ivs = i->get_ints();
93 for (
const auto &ivsv : ivs) {
94 v.PushBack(rapidjson::Value(ivsv).Move(), a);
96 }
else if (i->is_bool()) {
97 std::vector<bool> ivs = i->get_bools();
98 for (
const auto &ivsv : ivs) {
99 v.PushBack(rapidjson::Value(ivsv).Move(), a);
101 }
else if (i->is_string()) {
102 std::vector<std::string> ivs = i->get_strings();
103 for (
const auto &ivsv : ivs) {
104 v.PushBack(rapidjson::Value(ivsv, a).Move(), a);
109 v.SetFloat(i->get_float());
110 }
else if (i->is_uint()) {
111 v.SetUint(i->get_uint());
112 }
else if (i->is_int()) {
113 v.SetInt(i->get_int());
114 }
else if (i->is_bool()) {
115 v.SetBool(i->get_bool());
116 }
else if (i->is_string()) {
117 v.SetString(i->get_string(), a);
126 std::string query{params.
query_arg(
"query")};
132 std::shared_ptr<rapidjson::Document> d = std::make_shared<rapidjson::Document>();
133 rapidjson::Document::AllocatorType & a = d->GetAllocator();
137 std::unique_ptr<fawkes::Configuration::ValueIterator> i{
config->
search(query.c_str())};
139 std::vector<std::string> path_elements{str_split(i->path(),
'/')};
140 rapidjson::Value::MemberIterator parent = d->MemberEnd();
142 rapidjson::Value::MemberIterator m = d->MemberBegin();
143 rapidjson::Value::MemberIterator m_end = d->MemberEnd();
145 if (path_elements.size() > 1) {
146 for (
size_t p = 0; p < path_elements.size() - 1; ++p) {
147 m = std::find_if(m, m_end, [&path_elements, &p](
const auto &v) {
148 return path_elements[p] == v.name.GetString();
153 if (parent != d->MemberEnd()) {
154 parent->value.AddMember(rapidjson::Value(path_elements[p], a).Move(),
155 rapidjson::Value(rapidjson::kObjectType).Move(),
157 parent = parent->value.FindMember(path_elements[p].c_str());
159 d->AddMember(rapidjson::Value(path_elements[p], a).Move(),
160 rapidjson::Value(rapidjson::kObjectType).Move(),
162 parent = d->FindMember(path_elements[p].c_str());
165 m = parent->value.MemberBegin();
166 m_end = parent->value.MemberEnd();
169 if (parent == d->MemberEnd()) {
170 d->AddMember(rapidjson::Value(path_elements.back(), a).Move(),
171 create_value(i, a).Move(),
174 parent->value.AddMember(rapidjson::Value(path_elements.back(), a).Move(),
175 create_value(i, a).Move(),
ConfigTree representation for JSON transfer.
static std::string api_version()
Get version of implemented API.
void set_apiVersion(const std::string &apiVersion)
Set apiVersion value.
void set_kind(const std::string &kind)
Set kind value.
void set_config(const std::shared_ptr< rapidjson::Document > &config)
Set config value.
ConfigurationRestApi()
Constructor.
~ConfigurationRestApi()
Destructor.
virtual void init()
Initialize the thread.
virtual void finalize()
Finalize the thread.
virtual void loop()
Code to execute in the thread.
Configuration * config
This is the Configuration member used to access the configuration.
virtual ValueIterator * search(const char *path)=0
Iterator with search results.
Logger * logger
This is the Logger member used to access the logger.
Thread class encapsulation of pthreads.
WebviewRestApiManager * webview_rest_api_manager
Webview REST API manager.
void unregister_api(WebviewRestApi *api)
Remove a request processor.
void register_api(WebviewRestApi *api)
Add a REST API.
Webview REST API component.
void add_handler(WebRequest::Method method, std::string path, Handler handler)
Add handler function.
REST parameters to pass to handlers.
std::string query_arg(const std::string &what)
Get a query argument.
Fawkes library namespace.