diff options
author | Jonathan Aillet <jonathan.aillet@iot.bzh> | 2018-05-23 20:12:30 +0200 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2018-12-13 15:02:54 +0100 |
commit | 7f90ca127eaabe77a60077946ce6bc0742d4d952 (patch) | |
tree | 9911b78bf31631578ba11b4d94d97814a3c9ac99 | |
parent | a4961bebc382b08ed700652716ff958f45b7ec5d (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 |