diff options
author | Sadie Powell <sadie@witchery.services> | 2025-03-13 16:01:16 +0000 |
---|---|---|
committer | Sadie Powell <sadie@witchery.services> | 2025-03-13 16:01:16 +0000 |
commit | 6a43370e13bba190e40776cce6e7f2a591b9e8ff (patch) | |
tree | 7bab0b458d8e9b919377abfd95bef154c3234bce /modules | |
parent | 1fcd045affd5218df7ab5a207ad5ffe5caa2ce4a (diff) |
Deduplicate response building code in the jsonrpc module.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/rpc/jsonrpc.cpp | 38 |
1 files changed, 18 insertions, 20 deletions
diff --git a/modules/rpc/jsonrpc.cpp b/modules/rpc/jsonrpc.cpp index 5d4dc3c77..c35be6ebd 100644 --- a/modules/rpc/jsonrpc.cpp +++ b/modules/rpc/jsonrpc.cpp @@ -28,21 +28,13 @@ class MyJSONRPCServiceInterface final private: RPC::Events events; - static void SendError(HTTPReply &reply, int64_t code, const Anope::string &message, const Anope::string &id) + static std::pair<yyjson_mut_doc *, yyjson_mut_val *> CreateReply(const Anope::string &id) { - Log(LOG_DEBUG) << "JSON-RPC error " << code << ": " << message; - - // {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "1"} auto* doc = yyjson_mut_doc_new(nullptr); auto* root = yyjson_mut_obj(doc); yyjson_mut_doc_set_root(doc, root); - auto *error = yyjson_mut_obj(doc); - yyjson_mut_obj_add_sint(doc, error, "code", code); - yyjson_mut_obj_add_strn(doc, error, "message", message.c_str(), message.length()); - - yyjson_mut_obj_add_val(doc, root, "error", error); yyjson_mut_obj_add_str(doc, root, "jsonrpc", "2.0"); if (id.empty()) @@ -50,6 +42,22 @@ private: else yyjson_mut_obj_add_strn(doc, root, "id", id.c_str(), id.length()); + return { doc, root }; + } + + static void SendError(HTTPReply &reply, int64_t code, const Anope::string &message, const Anope::string &id) + { + Log(LOG_DEBUG) << "JSON-RPC error " << code << ": " << message; + + // {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "1"} + auto [doc, root] = CreateReply(id); + + auto *error = yyjson_mut_obj(doc); + yyjson_mut_obj_add_sint(doc, error, "code", code); + yyjson_mut_obj_add_strn(doc, error, "message", message.c_str(), message.length()); + + yyjson_mut_obj_add_val(doc, root, "error", error); + auto *json = yyjson_mut_write(doc, YYJSON_WRITE_ALLOW_INVALID_UNICODE | YYJSON_WRITE_NEWLINE_AT_END, nullptr); if (json) { @@ -175,15 +183,7 @@ public: } // {"jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5]} - auto* doc = yyjson_mut_doc_new(nullptr); - - auto* root = yyjson_mut_obj(doc); - yyjson_mut_doc_set_root(doc, root); - - if (request.id.empty()) - yyjson_mut_obj_add_null(doc, root, "id"); - else - yyjson_mut_obj_add_strn(doc, root, "id", request.id.c_str(), request.id.length()); + auto [doc, root] = CreateReply(request.id); if (request.GetRoot()) { @@ -193,8 +193,6 @@ public: else yyjson_mut_obj_add_null(doc, root, "result"); - yyjson_mut_obj_add_str(doc, root, "jsonrpc", "2.0"); - auto *json = yyjson_mut_write(doc, YYJSON_WRITE_ALLOW_INVALID_UNICODE | YYJSON_WRITE_NEWLINE_AT_END, nullptr); if (json) { |