A prefilter is a function that is used to process request parameters of APIs. You can specify one or more prefilters to customize the request content for APIs. This topic describes the limits of prefilters, the built-in function template provided by the system, and how to create functions and use them as prefilters.

Prerequisites

  • DataWorks Professional Edition or a more advanced edition is activated.
  • The DataWorks workspace is created in the China (Shanghai) region.
Filters have the following limits:
  • Only Python 3.0 functions can be used as filters.
  • Filters support importing only the following modules: json, time, random, pickle, re, and math.
  • The function name of a filter must be def handler(event,context):.

Function template

The system provides the following built-in 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) # Convert the string specified by the event parameter to a JSON object.
# 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 preceding filter. The value is of the STRING type.
Note
  • The system converts the request parameters of the API or the result data returned by the preceding 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 strings.
  • The processing result of a prefilter is a key-value map. The key-value map is used as the input parameter of the SQL statement. Therefore, the key-value map cannot contain nested key-value pairs.

Create a Python function

  1. Go to the DataService Studio page.
    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 where the target workspace resides. Find the target workspace and click DataService Studio in the Actions column.
  2. On the Service Development tab, move the pointer over Create icon and choose Function > Create Python Function.
    You can also click the target business process, right-click Function, and then choose New > Create Python Function.
  3. In the Create Python Function dialog box, set the parameters as required.
    Create Python Function dialog box
    Parameter Description
    Function Name The name of the function, which can be up to 256 characters in length.
    Function Template The template 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, which can be up to 512 characters in length.
    Target Folder The folder for storing the function.
  4. Click OK.
  5. 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.
  6. Click Save icon in the toolbar.

Use prefilters

  1. On the Service Development tab, click the business process to which the target API belongs.
  2. Double-click the target API. The configuration tab of the API appears.
  3. On the configuration tab of the API, click Request Parameters in the right-side navigation pane.
  4. In the Request Parameters pane, select Use prefilter in the Advanced Settings section.
    Use prefilter field
  5. Select functions from the Use prefilter drop-down list.
    Note A prefilter is a function that is used to process request parameters of APIs. You can specify one or more prefilters. You can create a function and use it as a prefilter.
  6. Click Preview Response to view the processing results of the prefilters.