Test, publish, and use a function

Updated at:
Copy as MD

This topic describes how to test, publish, and use a function in DataService Studio. You can use functions as filters to process API requests and responses.

Prerequisites

Before you test a function, ensure you have created and saved it. For more information, see Manage functions.

Limitations

Only the workspace owner and RAM users with the Workspace Administrator, Develop, or O&M role can test functions. For more information, see Add a workspace member and manage role permissions.

Step 1: Test the function

  1. Go to the Data Services page.

    1. Log on to the DataWorks console. In the target region, click Data Analysis and Service > DataService Studio in the left-side navigation pane. Select a workspace from the drop-down list and click Go to DataService Studio.

    2. On the Service Development page, in the function list, double-click the function in development.

  2. On the function's edit page, click Test in the top toolbar.

    ## --- AviatorScript ---
    ## $0: the default name of the input param.
    ## Expressions must be separated by the semicolon ";".
    ## Example 1:
    ## $0.a: Get the attribute "a" from the input param "$0".
    ## $0.b[index]: Get the index of the array "b" from the input param "$0". The index cannot be an expression or a variable.
    ## Example 2:
    ## Input: {"keyA":[1,2],"keyB":"value"}
    ## Function: $0.keyA[0]=3; $0.keyB="new_value"; return $0;
    ## Output: {"keyA":[3,2],"keyB":"new_value"}
  3. In the Function Test dialog box, enter the parameters and click Confirm.

    You can view the execution status on the Run Results tab at the bottom. If the test fails, check the error message, modify the function accordingly, and test it again.

Step 2: Publish the function

After the function passes the test, you can publish it.

  1. On the Service Development page, in the function list, double-click the function that passed the test.

  2. In the top toolbar, click Submission.

  3. On the function's edit page, click Version in the right-side navigation pane. Find the version with the Can Be Published status and click Publish in the Actions column.

    Note

    After you publish the function, you can click Version in the right-side navigation pane on the function's edit page to find the publishing history. You can then view the Version Details or click Roll Back.

Step 3: Use the function

Use cases

  • As an API pre-filter, a function can preprocess API request parameters during a call.

  • As an API post-filter, a function can further process and transform the API response.

Limitations

  • The pre-filter and post-filter for an API must use the same function type. Both must be Aviator functions or both must be Python functions.

  • The Python function feature is being upgraded. Therefore, creating, cloning, and publishing new Python functions are not currently supported.

Use as an API filter

  1. On the Service Development page, expand Workflow > API.

  2. Double-click the API to open its edit page.

  3. In the right-side navigation pane, click Filter.

    Select the Use Pre-filter or Use Post-filter checkbox. The Function Type defaults to Aviator Function. Click the drop-down list for the pre-filter or post-filter to select one or more functions. If you add multiple functions, they run in the order you add them to process the API parameters.

    Note
    • A filter consists of one or more functions that preprocess API request parameters or further process query results.

    • If you cannot find the target function in the drop-down list, ensure the function is published. You can also create and publish a new function. For more information, see Publish the function.

  4. Click Preview Responses Returned by API Operation to verify the filter's output.