diff --git a/include/villas/api/node_request.hpp b/include/villas/api/node_request.hpp index 69b628fb4..8a97df9f9 100644 --- a/include/villas/api/node_request.hpp +++ b/include/villas/api/node_request.hpp @@ -40,29 +40,7 @@ public: struct vnode *node; virtual void - prepare() - { - int ret; - - auto *nodes = session->getSuperNode()->getNodes(); - - uuid_t uuid; - ret = uuid_parse(matches[1].c_str(), uuid); - if (ret) { - node = vlist_lookup_name(nodes, matches[1]); - if (!node) - throw BadRequest("Unknown node", "{ s: s }", - "node", matches[1].c_str() - ); - } - else { - node = vlist_lookup_uuid(nodes, uuid); - if (!node) - throw BadRequest("No node found with with matching UUID", "{ s: s }", - "uuid", matches[1].c_str() - ); - } - } + prepare(); }; } /* namespace api */ diff --git a/include/villas/api/path_request.hpp b/include/villas/api/path_request.hpp index 209d7c92e..ab0b2a237 100644 --- a/include/villas/api/path_request.hpp +++ b/include/villas/api/path_request.hpp @@ -40,24 +40,7 @@ public: struct vpath *path; virtual void - prepare() - { - int ret; - uuid_t uuid; - - ret = uuid_parse(matches[1].c_str(), uuid); - if (ret) - throw BadRequest("Invalid UUID", "{ s: s }", - "uuid", matches[1].c_str() - ); - - auto *paths = session->getSuperNode()->getPaths(); - path = vlist_lookup_uuid(paths, uuid); - if (!path) - throw BadRequest("No path found with with matching UUID", "{ s: s }", - "uuid", matches[1].c_str() - ); - } + prepare(); }; } /* namespace api */ diff --git a/include/villas/api/request.hpp b/include/villas/api/request.hpp index aac018f92..67530d73b 100644 --- a/include/villas/api/request.hpp +++ b/include/villas/api/request.hpp @@ -79,9 +79,15 @@ public: json_decref(body); } - virtual Response * execute() = 0; + virtual void + prepare() + { } - virtual void decode(); + virtual Response * + execute() = 0; + + virtual void + decode(); const std::string & getMatch(int idx) const diff --git a/lib/api/CMakeLists.txt b/lib/api/CMakeLists.txt index 90f788cd5..cf63aab73 100644 --- a/lib/api/CMakeLists.txt +++ b/lib/api/CMakeLists.txt @@ -24,6 +24,8 @@ set(API_SRC session.cpp request.cpp + node_request.cpp + path_request.cpp response.cpp requests/status.cpp diff --git a/lib/api/node_request.cpp b/lib/api/node_request.cpp new file mode 100644 index 000000000..f6d9f9b37 --- /dev/null +++ b/lib/api/node_request.cpp @@ -0,0 +1,51 @@ +/** Node API Request. + * + * @file + * @author Steffen Vogel + * @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC + * @license GNU General Public License (version 3) + * + * VILLASnode + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + *********************************************************************************/ + +#include + +using namespace villas::node::api; + +void +NodeRequest::prepare() +{ + int ret; + + auto *nodes = session->getSuperNode()->getNodes(); + + uuid_t uuid; + ret = uuid_parse(matches[1].c_str(), uuid); + if (ret) { + node = vlist_lookup_name(nodes, matches[1]); + if (!node) + throw BadRequest("Unknown node", "{ s: s }", + "node", matches[1].c_str() + ); + } + else { + node = vlist_lookup_uuid(nodes, uuid); + if (!node) + throw BadRequest("No node found with with matching UUID", "{ s: s }", + "uuid", matches[1].c_str() + ); + } +} diff --git a/lib/api/path_request.cpp b/lib/api/path_request.cpp new file mode 100644 index 000000000..2fd320e08 --- /dev/null +++ b/lib/api/path_request.cpp @@ -0,0 +1,47 @@ +/** Path API Request. + * + * @file + * @author Steffen Vogel + * @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC + * @license GNU General Public License (version 3) + * + * VILLASnode + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + *********************************************************************************/ + +#include +#include + +using namespace villas::node::api; + +void +PathRequest::prepare() +{ + int ret; + uuid_t uuid; + + ret = uuid_parse(matches[1].c_str(), uuid); + if (ret) + throw BadRequest("Invalid UUID", "{ s: s }", + "uuid", matches[1].c_str() + ); + + auto *paths = session->getSuperNode()->getPaths(); + path = vlist_lookup_uuid(paths, uuid); + if (!path) + throw BadRequest("No path found with with matching UUID", "{ s: s }", + "uuid", matches[1].c_str() + ); +} diff --git a/lib/api/request.cpp b/lib/api/request.cpp index e252d82c6..b23c6cedd 100644 --- a/lib/api/request.cpp +++ b/lib/api/request.cpp @@ -59,6 +59,8 @@ Request * RequestFactory::create(Session *s, const std::string &uri, Session::Me p->method = meth; p->contentLength = ct; + p->prepare(); + return p; }