json-rpc-test-server: implement rpc.discover

This commit is contained in:
Přemysl Eric Janouch 2020-10-13 20:23:05 +02:00
parent c87869bef7
commit d489362a28
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 35 additions and 2 deletions

View File

@ -1446,6 +1446,38 @@ json_rpc_handler_info_cmp (const void *first, const void *second)
((struct json_rpc_handler_info *) second)->method_name);
}
static json_t *
open_rpc_describe (const char *method, json_t *result)
{
return json_pack ("{sssoso}", "name", method, "params", json_pack ("[]"),
"result", json_pack ("{ssso}", "name", method, "schema", result));
}
// This server rarely sees changes and we can afford to hardcode the schema
static json_t *
json_rpc_discover (struct server_context *ctx, json_t *params)
{
(void) ctx;
(void) params;
json_t *info = json_pack ("{ssss}",
"title", PROGRAM_NAME, "version", PROGRAM_VERSION);
json_t *methods = json_pack ("[ooo]",
open_rpc_describe ("date", json_pack ("{ssso}", "type", "object",
"properties", json_pack ("{s{ss}s{ss}s{ss}s{ss}s{ss}s{ss}}",
"year", "type", "number",
"month", "type", "number",
"day", "type", "number",
"hours", "type", "number",
"minutes", "type", "number",
"seconds", "type", "number"))),
open_rpc_describe ("ping", json_pack ("{ss}", "type", "string")),
open_rpc_describe ("rpc.discover", json_pack ("{ss}", "$ref",
"https://github.com/open-rpc/meta-schema/raw/master/schema.json")));
return json_rpc_response (NULL, json_pack ("{sssoso}",
"openrpc", "1.2.6", "info", info, "methods", methods), NULL);
}
static json_t *
json_rpc_ping (struct server_context *ctx, json_t *params)
{
@ -1487,8 +1519,9 @@ process_json_rpc_request (struct server_context *ctx, json_t *request)
// Eventually it might be better to move this into a map in the context.
static struct json_rpc_handler_info handlers[] =
{
{ "date", json_rpc_date },
{ "ping", json_rpc_ping },
{ "date", json_rpc_date },
{ "ping", json_rpc_ping },
{ "rpc.discover", json_rpc_discover },
};
if (!json_is_object (request))