Slightly refactor make_json_rpc_call()

This commit is contained in:
Přemysl Eric Janouch 2020-10-09 23:58:18 +02:00
parent 897a263ee7
commit e4d1529b4d
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 29 additions and 36 deletions

View File

@ -2882,17 +2882,22 @@ fail:
return success; return success;
} }
static bool static void
is_valid_json_rpc_id (json_t *v) maybe_print_verbose (struct app_context *ctx, intptr_t attribute,
char *utf8, size_t len)
{ {
return json_is_string (v) || json_is_integer (v) if (!ctx->verbose)
|| json_is_real (v) || json_is_null (v); // These two shouldn't be used return;
}
static bool char *term = iconv_xstrdup (ctx->term_from_utf8, utf8, len, NULL);
is_valid_json_rpc_params (json_t *v) if (!term)
print_error ("%s: %s", "verbose", "character conversion failed");
else
{ {
return json_is_array (v) || json_is_object (v); print_attributed (ctx, stdout, attribute, "%s", term);
fputs ("\n", stdout);
free (term);
}
} }
static void static void
@ -2907,19 +2912,7 @@ make_json_rpc_call (struct app_context *ctx,
if (params) json_object_set (request, "params", params); if (params) json_object_set (request, "params", params);
char *req_utf8 = json_dumps (request, 0); char *req_utf8 = json_dumps (request, 0);
if (ctx->verbose) maybe_print_verbose (ctx, ATTR_OUTGOING, req_utf8, -1);
{
char *req_term = iconv_xstrdup
(ctx->term_from_utf8, req_utf8, -1, NULL);
if (!req_term)
print_error ("%s: %s", "verbose", "character conversion failed");
else
{
print_attributed (ctx, stdout, ATTR_OUTGOING, "%s", req_term);
fputs ("\n", stdout);
}
free (req_term);
}
struct str buf = str_make (); struct str buf = str_make ();
struct error *e = NULL; struct error *e = NULL;
@ -2931,20 +2924,7 @@ make_json_rpc_call (struct app_context *ctx,
goto fail; goto fail;
} }
if (ctx->verbose) maybe_print_verbose (ctx, ATTR_INCOMING, buf.str, buf.len);
{
char *buf_term =
iconv_xstrdup (ctx->term_from_utf8, buf.str, buf.len, NULL);
if (!buf_term)
print_error ("%s: %s", "verbose", "character conversion failed");
else
{
print_attributed (ctx, stdout, ATTR_INCOMING, "%s", buf_term);
fputs ("\n", stdout);
}
free (buf_term);
}
if (!process_response (ctx, id, &buf, pipeline)) if (!process_response (ctx, id, &buf, pipeline))
{ {
char *s = iconv_xstrdup (ctx->term_from_utf8, char *s = iconv_xstrdup (ctx->term_from_utf8,
@ -2962,6 +2942,19 @@ fail:
json_decref (request); json_decref (request);
} }
static bool
is_valid_json_rpc_id (json_t *v)
{
return json_is_string (v) || json_is_integer (v)
|| json_is_real (v) || json_is_null (v); // These two shouldn't be used
}
static bool
is_valid_json_rpc_params (json_t *v)
{
return json_is_array (v) || json_is_object (v);
}
static void static void
process_input (char *user_input, void *user_data) process_input (char *user_input, void *user_data)
{ {