From b3e3f3f50e49c6d21032e44565a93ed96d99801b Mon Sep 17 00:00:00 2001 From: Niklas Halle Date: Sun, 23 Aug 2020 15:56:30 +0200 Subject: full json responses, added 'none' as annotation type --- README.md | 3 +++ src/main.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 63 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 3e598b9..b5fd3d5 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,9 @@ The `annotation` object: The currently specified annotation types are enumerated below: +##### `none` +The text should be printed as plain text. No extra content. + ##### `bold` The text should be printed as bold. No extra content. 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 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 reply_vec; + + std::vector 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 reply_vec; + + std::vector 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/ -- cgit v1.2.3-54-g00ecf