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.

Background information

On the DataService Studio page, you can create, test, and publish a function. Then, you can associate the function with an API, and use the function as a filter to process the request parameters or responses of the API to perform complex queries based on your business requirements. For the sample code that shows how to use an Aviator function as a filter for an API in common scenarios, see Best practices of using Aviator functions as filters.

Limits

  • 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 def handler(event,context):.

Function template

# -*- 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. 
Note
  • 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

  1. Log on to the DataWorks console.
  2. In the left-side navigation pane, click Workspaces.
  3. In the top navigation bar, select the region in which the workspace that you want to manage resides. Find the workspace, move the pointer over the More icon in the Actions column, and then select DataService Studio.
  4. In the Service Development pane, move the pointer over the Create icon icon and choose Create Function > Create Python Function.
    You can alternatively click the required business process, right-click Function, and then choose Create Function > Create Python Function.
  5. In the Create Python Function dialog box, set the parameters as required.
    Create Python Function dialog box
    ParameterDescription
    Function NameThe name of the function. The name can be up to 256 characters in length.
    Function TemplateThe template that is used to create the function. Set the value to Python3 Standard v1.
    Running EnvironmentThe runtime environment of the function. Set the value to Python 3.0.
    Function DescriptionThe description of the function. The description can be up to 512 characters in length.
    Destination FolderThe folder for storing the function.
  6. Click OK.
  7. Configure the function on its configuration tab.
    1. In the Edit Code section, enter the function code.
    2. In the Environment Configuration section, set the Memory and Function Timeout parameters.
  8. Click the Save icon icon in the toolbar.

Use a function as a filter for an API

  1. In the Service Development pane, click the business process to which the required API belongs.
  2. Double-click the required API. The configuration tab of the API appears.
  3. 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.
    Filter panel
  4. Click Preview Responses Returned by API Operation and check whether the processing results of the selected functions meet your expectations.