aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNiklas Halle <niklas@niklashalle.net>2020-08-23 15:56:30 +0200
committerNiklas Halle <niklas@niklashalle.net>2020-08-23 15:56:30 +0200
commitb3e3f3f50e49c6d21032e44565a93ed96d99801b (patch)
tree91f33b030b2db21a534314f578c9a5c14ff08fa3
parent6d51498a3630b904b7298cbe13b05de8cc12e5ad (diff)
downloadn_core-b3e3f3f50e49c6d21032e44565a93ed96d99801b.tar.gz
n_core-b3e3f3f50e49c6d21032e44565a93ed96d99801b.zip
full json responses, added 'none' as annotation type
-rw-r--r--README.md3
-rw-r--r--src/main.cpp68
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<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/