common code reuse 39/5939/1
authorJosé Bollo <jose.bollo@iot.bzh>
Fri, 10 Jun 2016 12:29:14 +0000 (14:29 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Fri, 10 Jun 2016 12:29:14 +0000 (14:29 +0200)
Change-Id: I5f400a92165bb380ec5f703dd8ca567e90aebca2
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/afb-api-dbus.c
src/afb-msg-json.c
src/afb-msg-json.h
src/afb-subcall.c
src/afb-ws-json1.c

index 905c723..7452339 100644 (file)
@@ -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)
index 8f543ff..59af9d9 100644 (file)
@@ -19,6 +19,8 @@
 
 #include <json-c/json.h>
 
+#include <afb/afb-req-itf.h>
+
 #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;
+}
+
+
index 53ab086..59827be 100644 (file)
@@ -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);
+
index 4d18bee..bf64173 100644 (file)
@@ -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)
index 31e9e42..03e57fd 100644 (file)
@@ -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)