diff options
author | Jonathan Aillet <jonathan.aillet@iot.bzh> | 2018-05-23 20:12:30 +0200 |
---|---|---|
committer | Jonathan Aillet <jonathan.aillet@iot.bzh> | 2018-05-28 12:47:22 +0000 |
commit | 8ce6d20a550874dd5f3650eea3a93987f0f6dc5e (patch) | |
tree | 9911b78bf31631578ba11b4d94d97814a3c9ac99 | |
parent | 64671ea79fc6b4eb68126842339800b25549cd67 (diff) |
Correct way that api actions are handled in controller
Don't create a new object each time a action is executed.
Correction in the way that args are added in the api query.
Correction to return the response of the api call if received.
Optimization of how get/put are done in 'ActionExecOne' function.
Change-Id: I30d0d6450599b7da7bfa9ad29b21d1be8ea6243e
Signed-off-by: Jonathan Aillet <jonathan.aillet@iot.bzh>
-rw-r--r-- | ctl-lib/ctl-action.c | 48 |
1 files changed, 25 insertions, 23 deletions
diff --git a/ctl-lib/ctl-action.c b/ctl-lib/ctl-action.c index d460a27..764fd25 100644 --- a/ctl-lib/ctl-action.c +++ b/ctl-lib/ctl-action.c @@ -57,36 +57,38 @@ int ActionExecOne(CtlSourceT *source, CtlActionT* action, json_object *queryJ) { switch (action->type) { case CTL_TYPE_API: { - json_object *returnJ, *subcallArgsJ = json_object_new_object(); + json_object *returnJ, *toReturnJ; - if (queryJ) { - - json_object_object_foreach(queryJ, key, val) { - json_object_get(val); - json_object_object_add(subcallArgsJ, key, val); - } - } + json_type typeJ = json_object_get_type(queryJ); if (action->argsJ) { - // Merge queryJ and argsJ before sending request - if (json_object_get_type(action->argsJ) == json_type_object) { - - json_object_object_foreach(action->argsJ, key, val) { - json_object_get(val); - json_object_object_add(subcallArgsJ, key, val); + switch(typeJ) { + case json_type_object: { + json_object_object_foreach(action->argsJ, key, val) { + json_object_get(val); + json_object_object_add(queryJ, key, val); + } + break; } - } else { - json_object_get(action->argsJ); - json_object_object_add(subcallArgsJ, "args", action->argsJ); + case json_type_null: + break; + default: + AFB_ApiError(action->api, "ActionExecOne(queryJ should be an object) uid=%s args=%s", source->uid, json_object_get_string(queryJ)); + return err; } } - json_object_object_add(subcallArgsJ, "uid", json_object_new_string(source->uid)); - /* AFB Subcall will release the json_object doing the json_object_put() call */ - int err = AFB_ServiceSync(action->api, action->exec.subcall.api, action->exec.subcall.verb, subcallArgsJ, &returnJ); - if (err) { + int err = AFB_ServiceSync(action->api, action->exec.subcall.api, action->exec.subcall.verb, json_object_get(queryJ), &returnJ); + if(err && source->request) + AFB_ReqFailF(source->request, "subcall-fail", "ActionExecOne(AppFw) uid=%s api=%s verb=%s args=%s", source->uid, action->exec.subcall.api, action->exec.subcall.verb, json_object_get_string(action->argsJ)); + else if(err && ! source->request) AFB_ApiError(action->api, "ActionExecOne(AppFw) uid=%s api=%s verb=%s args=%s", source->uid, action->exec.subcall.api, action->exec.subcall.verb, json_object_get_string(action->argsJ)); + else if(source->request) { + if(wrap_json_unpack(returnJ, "{s:o}", "response", &toReturnJ)) + AFB_ApiError(action->api, "ActionExecOne(Can't unpack response) uid=%s api=%s verb=%s args=%s", source->uid, action->exec.subcall.api, action->exec.subcall.verb, json_object_get_string(action->argsJ)); + else + AFB_ReqSucess(source->request, toReturnJ, NULL); } break; } @@ -113,7 +115,7 @@ int ActionExecOne(CtlSourceT *source, CtlActionT* action, json_object *queryJ) { break; } } - json_object_put(queryJ); + return err; } @@ -133,7 +135,7 @@ static void ActionDynRequest(AFB_ReqT request) { source.api = action->api; // provide request and execute the action - ActionExecOne(&source, action, json_object_get(queryJ)); + ActionExecOne(&source, action, queryJ); } #endif |