Send variable value to function

When I use a workflow as endpoint of an ‘executeButton’, I specify the variable I want to use in the workflow in the endpoint config via variableAssignments:

sth like this:

"variableAssignments": [
      {
        "variableName": "selected_edge_id",
        "variableType": "STRING",
        "variableValue": "",
        "$variableValue": {
          "value": "{{selectedEdgeId_Variable.0}}"
        }
      }]

This all works fine, but now I want to use a function as endpoint.
When I change the endpoint to type to ‘function’, how do I specify the config for the variable, I tried a lot of things, e.g. this, but couldnt figure out how to do it.

"endpointParameters": [
      {
        "name": "selected_edge_id",
        "type": "STRING",
        "valueFromVariable": "",
        "$valueFromVariable": {
          "value": "{{selectedEdgeId_Variable.0}}"
        }
      }]

How do I do it correctly?

Is this ‘function’ code correct to print the variable?

def handle(req):
    selected_edge_id = str(req["args"]["selected_edge_id"])
    return {'message': f'The selected edge_id is {str(selected_edge_id)}.'}

I think you are using valueFromVariable wrong. It basically accepts the name of the variable and sends the value directly (see the documentation: https://internal.onedata.de/apps/apps-docs/odml-documentation/AppBuilder/endpoints/Endpoints.html#config-for-functions) and you don’t have to use the {{...}} notation.

Alternatively you could also use the first option and then insert your variable value in the “normal” way.

Regarding the second part of your question: I always forget how the exact structure looks for different ways to send variables, perhaps return req["args"] once to figure it out :slight_smile:

Hm I also tried it with the variable config you mentioned.
It feels like sth. else is the problem.
I also receive an Error message, if I just comment out everything (endpointParameters) and want to return req[“args”] as you suggested.

{
  "id": "create_linkage_function_endpoint",
  "type": "function",
  "async": true,
  "config": {
    "functionId": "00000000-0000-0000-0000-000000000000",
    "@functionId": {
      "partial": "id_function_create_linkage"
    }
  }
}
{
  "type": "executeButton",
  "id": "RunLinkage_execButton",
  "config": {
    "endpoint": "create_linkage_function_endpoint",
    "icon": "",
    "disabled": false,
    "label": "Create Linkage",
    "messages": {
      "started": {
        "headline": "Started",
        "message": "Workflow was started"
      },
      "error": {
        "headline": "Error",
        "message": "Workflow returned an error"
      },
      "failed": {
        "headline": "Failed",
        "message": "Workflow execution failed"
      },
      "aborted": {
        "headline": "Aborted",
        "message": "Workflow execution was aborted"
      },
      "success": {
        "headline": "Success",
        "message": "Workflow execution was successful"
      },
      "hideAfterSeconds": 4,
      "disabled": false
    }
  }
}
def handle(req):
    return(req["args"])

Can you try to remove the "async": true ? Somehow I never used that in a Function endpoint and it is also not mentioned in the documentation example.

If this helps it should be clarified in the documentation / a warning added to not set this for function endpoints

Unfortunately that didn’t help either, I’ll continue testing tomorrow.

The config of your endpoint must look different if you want to trigger a function than a workflow. Check out the documentation …/apps/apps-docs/odml-documentation/AppBuilder/endpoints/Endpoints.html

You must specify the payload, which is what is transfered to your function.
The follwoing should work

{
  "id": "endpoint_name",
  "config": {
    "functionId": "<uuid>",
    "payload": {
      "input": {
        "$function_arg_1": {
          "value": "{{apps.appSessionId}}"
        },
        "$function_arg_2": {
          "value": "{{my_apps_variable}}"
        },
        "function_arg_1": "",
        "function_arg_2": ""
      }
    }
  },
  "type": "function"
}
1 Like