From b69290c369ba67ee804bc5a0a440210c0e5d37a3 Mon Sep 17 00:00:00 2001 From: José Bollo Date: Fri, 10 Jun 2016 14:29:14 +0200 Subject: common code reuse MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I5f400a92165bb380ec5f703dd8ca567e90aebca2 Signed-off-by: José Bollo --- src/afb-api-dbus.c | 15 ++------------- src/afb-msg-json.c | 19 +++++++++++++++++++ src/afb-msg-json.h | 3 +++ src/afb-subcall.c | 13 +------------ src/afb-ws-json1.c | 14 +------------- 5 files changed, 26 insertions(+), 38 deletions(-) diff --git a/src/afb-api-dbus.c b/src/afb-api-dbus.c index 905c723d..7452339b 100644 --- a/src/afb-api-dbus.c +++ b/src/afb-api-dbus.c @@ -32,6 +32,7 @@ #include "afb-common.h" #include "session.h" +#include "afb-msg-json.h" #include "afb-apis.h" #include "afb-api-so.h" #include "afb-context.h" @@ -400,19 +401,7 @@ static struct json_object *dbus_req_json(struct dbus_req *dreq) /* get the argument of the request of 'name' */ static struct afb_arg dbus_req_get(struct dbus_req *dreq, const char *name) { - struct afb_arg arg; - struct json_object *value, *root; - - root = dbus_req_json(dreq); - if (root != NULL && json_object_object_get_ex(root, name, &value)) { - arg.name = name; - arg.value = json_object_get_string(value); - } else { - arg.name = NULL; - arg.value = NULL; - } - arg.path = NULL; - return arg; + return afb_msg_json_get_arg(dbus_req_json(dreq), name); } static void dbus_req_reply(struct dbus_req *dreq, uint8_t type, const char *first, const char *second) diff --git a/src/afb-msg-json.c b/src/afb-msg-json.c index 8f543ff1..59af9d96 100644 --- a/src/afb-msg-json.c +++ b/src/afb-msg-json.c @@ -19,6 +19,8 @@ #include +#include + #include "afb-msg-json.h" #include "afb-context.h" @@ -89,3 +91,20 @@ struct json_object *afb_msg_json_event(const char *event, struct json_object *ob return msg; } +struct afb_arg afb_msg_json_get_arg(struct json_object *object, const char *name) +{ + struct afb_arg arg; + struct json_object *value; + + if (json_object_object_get_ex(object, name, &value)) { + arg.name = name; + arg.value = json_object_get_string(value); + } else { + arg.name = NULL; + arg.value = NULL; + } + arg.path = NULL; + return arg; +} + + diff --git a/src/afb-msg-json.h b/src/afb-msg-json.h index 53ab0861..59827bed 100644 --- a/src/afb-msg-json.h +++ b/src/afb-msg-json.h @@ -19,6 +19,7 @@ struct json_object; struct afb_context; +struct afb_arg; extern struct json_object *afb_msg_json_reply(const char *status, const char *info, struct json_object *resp, struct afb_context *context, const char *reqid); extern struct json_object *afb_msg_json_reply_ok(const char *info, struct json_object *resp, struct afb_context *context, const char *reqid); @@ -26,3 +27,5 @@ extern struct json_object *afb_msg_json_reply_error(const char *status, const ch extern struct json_object *afb_msg_json_event(const char *event, struct json_object *object); +extern struct afb_arg afb_msg_json_get_arg(struct json_object *object, const char *name); + diff --git a/src/afb-subcall.c b/src/afb-subcall.c index 4d18bee7..bf641738 100644 --- a/src/afb-subcall.c +++ b/src/afb-subcall.c @@ -99,18 +99,7 @@ static struct json_object *subcall_json(struct afb_subcall *subcall) static struct afb_arg subcall_get(struct afb_subcall *subcall, const char *name) { - struct afb_arg arg; - struct json_object *value; - - if (json_object_object_get_ex(subcall->args, name, &value)) { - arg.name = name; - arg.value = json_object_get_string(value); - } else { - arg.name = NULL; - arg.value = NULL; - } - arg.path = NULL; - return arg; + return afb_msg_json_get_arg(subcall->args, name); } static void subcall_emit(struct afb_subcall *subcall, int iserror, struct json_object *object) diff --git a/src/afb-ws-json1.c b/src/afb-ws-json1.c index 31e9e42e..03e57fd5 100644 --- a/src/afb-ws-json1.c +++ b/src/afb-ws-json1.c @@ -224,19 +224,7 @@ static struct json_object *wsreq_json(struct afb_wsreq *wsreq) static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name) { - struct afb_arg arg; - struct json_object *value, *root; - - root = wsreq_json(wsreq); - if (json_object_object_get_ex(root, name, &value)) { - arg.name = name; - arg.value = json_object_get_string(value); - } else { - arg.name = NULL; - arg.value = NULL; - } - arg.path = NULL; - return arg; + return afb_msg_json_get_arg(wsreq_json(wsreq), name); } static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info) -- cgit 1.2.3-korg