diff options
author | Niklas Halle <niklas@niklashalle.net> | 2020-08-23 15:56:30 +0200 |
---|---|---|
committer | Niklas Halle <niklas@niklashalle.net> | 2020-08-23 15:56:30 +0200 |
commit | b3e3f3f50e49c6d21032e44565a93ed96d99801b (patch) | |
tree | 91f33b030b2db21a534314f578c9a5c14ff08fa3 /src | |
parent | 6d51498a3630b904b7298cbe13b05de8cc12e5ad (diff) | |
download | n_core-b3e3f3f50e49c6d21032e44565a93ed96d99801b.tar.gz n_core-b3e3f3f50e49c6d21032e44565a93ed96d99801b.zip |
full json responses, added 'none' as annotation type
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cpp | 68 |
1 files changed, 60 insertions, 8 deletions
diff --git a/src/main.cpp b/src/main.cpp index daa9a09..627d3d0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,11 +8,11 @@ #define EMPTY_STRING std::string("") #define NO_SUCH_COMMAND std::string("no_such_command") -typedef std::function<std::string(std::string const &arguments, std::string const &session, +typedef std::function<crow::json::wvalue(std::string const &arguments, std::string const &session, void *payload)> handler_func; struct CommandHandler { - [[nodiscard]] std::string exec(std::string const &arguments, std::string const &session) const { + [[nodiscard]] crow::json::wvalue exec(std::string const &arguments, std::string const &session) const { return func(arguments, session, payload); } @@ -20,8 +20,8 @@ struct CommandHandler { void *payload{nullptr}; }; -std::string stopHandler(std::string const &arguments, std::string const &session, void *payload); -std::string klingerHandler(std::string const &arguments, std::string const &session, void *payload); +crow::json::wvalue stopHandler(std::string const &arguments, std::string const &session, void *payload); +crow::json::wvalue klingerHandler(std::string const &arguments, std::string const &session, void *payload); [[maybe_unused]] void decl_me_daddy(int not_an_int) { (void) not_an_int; } @@ -54,6 +54,8 @@ int main() { return crow::response{handler.exec(arguments, session)}; } + crow::json::wvalue response; + return crow::response{"no such command!\n"}; }); @@ -62,14 +64,64 @@ int main() { std::cout << "Stopped successfully" << std::endl; } -std::string klingerHandler(std::string const &arguments, std::string const &session, void *payload) { - return "Bonjour!"; +crow::json::wvalue klingerHandler(std::string const &arguments, std::string const &session, void *payload) { + (void)payload; + + using namespace crow::json; + + crow::json::wvalue response(type::Object); + + std::vector<wvalue> reply_vec; + + std::vector<wvalue> annotation_vec; + + wvalue annotation(type::Object); + annotation["type"] = "none"; + annotation["extra"] = ""; + + annotation_vec.push_back(std::move(annotation)); + + wvalue reply(type::Object); + reply["text"] = "Bonjour!"; + reply["annotations"] = std::move(annotation_vec); + + reply_vec.push_back(std::move(reply)); + + response["success"] = true; + response["session"] = NULL_STRING; + response["reply"] = std::move(reply_vec); + + return response; } -std::string stopHandler(std::string const &arguments, std::string const &session, void *payload) { +crow::json::wvalue stopHandler(std::string const &arguments, std::string const &session, void *payload) { auto app = (crow::SimpleApp*) payload; app->stop(); - return "stopped"; + using namespace crow::json; + + crow::json::wvalue response(type::Object); + + std::vector<wvalue> reply_vec; + + std::vector<wvalue> annotation_vec; + + wvalue annotation(type::Object); + annotation["type"] = "bold"; + annotation["extra"] = ""; + + annotation_vec.push_back(std::move(annotation)); + + wvalue reply(type::Object); + reply["text"] = "stopped."; + reply["annotations"] = std::move(annotation_vec); + + reply_vec.push_back(std::move(reply)); + + response["success"] = true; + response["session"] = NULL_STRING; + response["reply"] = std::move(reply_vec); + + return response; } // curl -d '{"command":"klinger", "b":"12"}' -H "Content-Type: application/json" -X POST http://localhost:18080/ |