From 04186fd953298be8b8c5673b5fcd01f0893986ff Mon Sep 17 00:00:00 2001 From: José Bollo Date: Wed, 2 Aug 2017 19:14:09 +0200 Subject: subcall_req: introduce afb_req_subcall_req MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The function 'afb_req_subcall_req' can be used to make asynchronous subcalls. It improves the function 'afb_req_subcall' by automatically keeping the request opened for the callback and by passing it, the request, as an extra argument of the callback. Change-Id: I2dc79c01fc25c7a65b9c8cc9e001a5b85fba99df Signed-off-by: José Bollo --- bindings/samples/HelloWorld.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'bindings') diff --git a/bindings/samples/HelloWorld.c b/bindings/samples/HelloWorld.c index 731ea530..002c4721 100644 --- a/bindings/samples/HelloWorld.c +++ b/bindings/samples/HelloWorld.c @@ -192,6 +192,27 @@ static void subcall (afb_req request) afb_req_subcall(request, api, verb, object, subcallcb, afb_req_store(request)); } +static void subcallreqcb (void *prequest, int status, json_object *object, afb_req request) +{ + if (status < 0) + afb_req_fail(request, "failed", json_object_to_json_string(object)); + else + afb_req_success(request, json_object_get(object), NULL); +} + +static void subcallreq (afb_req request) +{ + const char *api = afb_req_value(request, "api"); + const char *verb = afb_req_value(request, "verb"); + const char *args = afb_req_value(request, "args"); + json_object *object = api && verb && args ? json_tokener_parse(args) : NULL; + + if (object == NULL) + afb_req_fail(request, "failed", "bad arguments"); + else + afb_req_subcall_req(request, api, verb, object, subcallreqcb, NULL); +} + static void subcallsync (afb_req request) { int rc; @@ -418,6 +439,7 @@ static const afb_verb_v2 verbs[]= { { .verb="pingJson", .callback=pingJson }, { .verb="pingevent", .callback=pingEvent }, { .verb="subcall", .callback=subcall }, + { .verb="subcallreq", .callback=subcallreq }, { .verb="subcallsync", .callback=subcallsync }, { .verb="eventadd", .callback=eventadd }, { .verb="eventdel", .callback=eventdel }, -- cgit 1.2.3-korg