def _method_call(meth, method_spec_json, param_values_json):
    """Generic service method calls

    :param method_spec_json: Method descriptor JSON string
    :type method_spec_json: kbtypes.Unicode
    :ui_name method_spec_json: Method descriptor JSON string
    :param param_values_json: Parameter values JSON string
    :type param_values_json: kbtypes.Unicode
    :ui_name param_values_json: Parameter values JSON string
    :return: Service method response
    :rtype: kbtypes.Unicode
    """
    token = os.environ['KB_AUTH_TOKEN']
    workspace = os.environ['KB_WORKSPACE_ID']
    methodSpec = json.loads(correct_method_specs_json(method_spec_json))
    paramValues = json.loads(correct_method_specs_json(param_values_json))
    methodOut = None
    behavior = methodSpec['behavior']
    scriptStep = is_script_method(methodSpec)
    async = is_async_method(methodSpec)
    if scriptStep or async:
        wsClient = workspaceService(service.URLS.workspace, token = token)
        steps = []
        methodId = methodSpec['info']['id']
        app = { 'name' : 'App wrapper for method ' + methodId,'steps' : steps }
        steps.append(create_app_step(workspace, token, wsClient, methodSpec, paramValues, methodId, scriptStep))
        meth.debug(json.dumps(app))
        njsClient = NarrativeJobService(service.URLS.job_service, token = token)
        appState = njsClient.run_app(app)
        jobId = "method:" + appState["job_id"]
        meth.register_app(jobId)
        methodOut = {'job_id': jobId}
    else:
        input = {}
        output = None
        if 'kb_service_input_mapping' in behavior:
            rpcArgs = prepare_generic_method_input(token, workspace, methodSpec, paramValues, input);
            url = behavior['kb_service_url']
            serviceName = behavior['kb_service_name']
            methodName = behavior['kb_service_method']
            if serviceName:
                methodName = serviceName + '.' + methodName
            genericClient = GenericService(url = url, token = token)
            output = genericClient.call_method(methodName, rpcArgs)
        else:
            parameters = methodSpec['parameters']
            for paramPos in range(0, len(parameters)):
                param = parameters[paramPos]
                paramId = param['id']
                paramValue = paramValues[paramPos]
                input[paramId] = paramValue
        methodOut = prepare_generic_method_output(token, workspace, methodSpec, input, output)
        if 'job_id_output_field' in methodSpec:
            jobIdField = methodSpec['job_id_output_field']
            if jobIdField in methodOut:
                meth.register_job(methodOut[jobIdField])
    return json.dumps(methodOut)
예제 #2
0
def _method_call(meth, method_spec_json, param_values_json):
    """Generic service method calls

    :param method_spec_json: Method descriptor JSON string
    :type method_spec_json: kbtypes.Unicode
    :ui_name method_spec_json: Method descriptor JSON string
    :param param_values_json: Parameter values JSON string
    :type param_values_json: kbtypes.Unicode
    :ui_name param_values_json: Parameter values JSON string
    :return: Service method response
    :rtype: kbtypes.Unicode
    """
    token = os.environ['KB_AUTH_TOKEN']
    workspace = os.environ['KB_WORKSPACE_ID']
    methodSpec = json.loads(method_spec_json)
    paramValues = json.loads(param_values_json)
    methodOut = None

    if is_script_method(methodSpec):
        wsClient = workspaceService(service.URLS.workspace, token = token)
        steps = []
        methodId = methodSpec['info']['id']
        app = { 'name' : 'App wrapper for method ' + methodId,'steps' : steps }
        steps.append(create_app_step(workspace, token, wsClient, methodSpec, paramValues, methodId, True))
        njsClient = NarrativeJobService(service.URLS.job_service, token = token)
        appState = njsClient.run_app(app)
        jobId = "method:" + appState["job_id"]
        meth.register_app(jobId)
        methodOut = {'job_id': jobId}
    else:
        input = {}
        rpcArgs = prepare_generic_method_input(token, workspace, methodSpec, paramValues, input);
        behavior = methodSpec['behavior']
        url = behavior['kb_service_url']
        serviceName = behavior['kb_service_name']
        methodName = behavior['kb_service_method']
        if serviceName:
            methodName = serviceName + '.' + methodName
        genericClient = GenericService(url = url, token = token)
        output = genericClient.call_method(methodName, rpcArgs)
        methodOut = prepare_generic_method_output(token, workspace, methodSpec, input, output)
        if 'job_id_output_field' in methodSpec:
            jobIdField = methodSpec['job_id_output_field']
            if jobIdField in methodOut:
                meth.register_job(methodOut[jobIdField])
    return json.dumps(methodOut)
예제 #3
0
def _app_call(meth, app_spec_json, method_specs_json, param_values_json):
    """Makes a call to the app service

    :param app_spec_json: The App Spec
    :type app_spec_json: kbtypes.Unicode
    :ui_name app_spec_json: The App Spec
    :param method_specs_json: The Method Specs
    :type method_specs_json: kbtypes.Unicode
    :ui_name method_specs_json: The Method Specs
    :param param_values_json: Param values
    :type param_values_json: kbtypes.Unicode
    :ui_name param_values_json: Param values
    :rtype: kbtypes.Unicode
    :return: running job info
    """
    token, workspace = meth.token, meth.workspace_id

    appSpec = json.loads(app_spec_json)
    paramValues = json.loads(correct_method_specs_json(param_values_json))

    methIdToSpec = json.loads(correct_method_specs_json(method_specs_json))

    #raise ValueError("========\nExternal=" + method_specs_json + "\n=======================\nInternal=" + json.dumps(methIdToSpec))

    app = create_app_for_njs(workspace, token, service.URLS,
                             appSpec['info']['id'], appSpec['steps'],
                             methIdToSpec, paramValues)

    #raise ValueError("App sending to NJS: " + json.dumps(app))
    meth.debug(json.dumps(app))
    njsClient = NarrativeJobService(service.URLS.job_service, token=token)
    appState = njsClient.run_app(app)

    # assuming we get a job ID out of this, do the following:
    job_id = "njs:" + appState["job_id"]
    meth.register_app(job_id)

    return json.dumps({'job_id': job_id, 'app': app, 'app_state': appState})
예제 #4
0
def _app_call(meth, app_spec_json, method_specs_json, param_values_json):
    """Makes a call to the app service

    :param app_spec_json: The App Spec
    :type app_spec_json: kbtypes.Unicode
    :ui_name app_spec_json: The App Spec
    :param method_specs_json: The Method Specs
    :type method_specs_json: kbtypes.Unicode
    :ui_name method_specs_json: The Method Specs
    :param param_values_json: Param values
    :type param_values_json: kbtypes.Unicode
    :ui_name param_values_json: Param values
    :rtype: kbtypes.Unicode
    :return: running job info
    """
    token, workspace = meth.token, meth.workspace_id

    appSpec = json.loads(app_spec_json)
    paramValues = json.loads(param_values_json)

    methIdToSpec = json.loads(correct_method_specs_json(method_specs_json))

    # raise ValueError("========\nExternal=" + method_specs_json + "\n=======================\nInternal=" + json.dumps(methIdToSpec))

    app = create_app_for_njs(
        workspace, token, service.URLS, appSpec["info"]["id"], appSpec["steps"], methIdToSpec, paramValues
    )

    # raise ValueError("App sending to NJS: " + json.dumps(app))
    meth.debug(json.dumps(app))
    njsClient = NarrativeJobService(service.URLS.job_service, token=token)
    appState = njsClient.run_app(app)

    # assuming we get a job ID out of this, do the following:
    job_id = "njs:" + appState["job_id"]
    meth.register_app(job_id)

    return json.dumps({"job_id": job_id, "app": app, "app_state": appState})