This topic describes how to create and use Python functions as a filter to process the request parameters of an API operation or the results returned by an API operation.
- You must activate DataWorks Professional Edition or a more advanced edition before you use Python functions as a filter.
- Python functions must be executed on the shared resource group for DataService Studio.
- The feature of using Python functions as a filter is being optimized. You cannot create, clone, or publish new Python functions. If you need to use this feature, we recommend that you use Aviator functions. For more information, see Create an Aviator function and use the Aviator function as a filter.
- If you use one or more functions as a filter to process the request parameters of an API and one or more functions as a filter to process the results returned by the API, you must use the same type of function as the filters. You can use either Aviator or Python functions. You cannot use different types of functions as the filters for an API.
If you use Python functions as a filter, take note of the following limits:
- Only Python 3.0 functions can be used as a filter.
- Python functions support the import of only the following modules:
json, time, random, pickle, re, and math.
- The name of a function used as a filter must be
# -*- coding: utf-8 -*- # event (str) : in filter it is the API result, in other cases, it is your param # context : some environment information, temporarily useless # import module limit: json,time,random,pickle,re,math # do not modify function name import json def handler(event,context): # load str to json object obj = json.loads(event) # add your code here # end add return obj
You can modify the function template to write your own function. You can modify the names of the input parameters as needed.
Parameter 1 [context]: the context of calling the API. The value is of the STRING type. This parameter is not in use and is left empty. Parameter 2 [event]: the request parameters of the API or the result data returned by the previous function. The value is of the STRING type.
- The system converts the request parameters of an API or the result data returned by the previous filter from a key-value map to a JSON object, and passes the JSON object to the event parameter. The values in the JSON object must be of the STRING type.
- The processing result of a filter used to process the request parameters of an API is a key-value map. The key-value map is used as the input parameter of an SQL statement. Therefore, the key-value map cannot contain nested key-value pairs.
Create a Python function
- Go to the DataService Studio page.
- Log on to the DataWorks console.
- In the left-side navigation pane, click Workspaces.
- In the top navigation bar, select the region in which the workspace that you want to manage resides. Find the workspace and click DataService Studio in the Actions column.
- In the Service Development pane, move the pointer over the icon and choose . You can alternatively click the required business process, right-click Function, and then choose .
- In the Create Python Function dialog box, set the parameters as required.
Parameter Description Function Name The name of the function. The name can be up to 256 characters in length. Function Template The template that is used to create the function. Set the value to Python3 Standard v1. Running Environment The runtime environment of the function. Set the value to Python 3.0. Function Description The description of the function. The description can be up to 512 characters in length. Destination Folder The folder for storing the function.
- Click OK.
- Configure the function on its configuration tab.
- In the Edit Code section, enter the function code.
- In the Environment Configuration section, set the Memory and Function Timeout parameters.
- Click the icon in the toolbar.
Use a function as a filter for an API
- In the pane, click the business process to which the required API belongs.
- Double-click the required API. The configuration tab of the API appears.
- In the right-side navigation pane, click the Filter tab. In the Filter panel, select Use Pre-filter or Use Post-filter based on your needs. Select Python Function as Function Type. Then, you can select one or more functions from the drop-down list next to the Use Pre-filter or Use Post-filter check box. If you select multiple functions, the functions are used to process parameters based on the sequence in which they are selected.Note
- You can specify one or more functions as a filter to process the request parameters of an API or the results returned by an API.
- If the function that you want to use is not available in the drop-down list next to the Use Pre-filter or Use Post-filter check box, check whether the function is published. If the function is not published, publish the function. You can also create and publish a new function. For more information, see Publish a function.
- Click Preview Responses Returned by API Operation and check whether the processing results of the selected functions meet your expectations.