A post filter is a function that is used to process the results returned by APIs. You can specify one or more post filters to process the results returned by APIs. This topic describes the limits of post filters, the built-in function template provided by the system, and how to create functions and use them as post filters.

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 result data returned by the API or the preceding filter. The value is of the STRING type.

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 post filters

  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 Response Parameters in the right-side navigation pane.
  4. In the Response Parameters pane, select Filter in the Advanced Settings section.
    Filter field
  5. Select functions from the Filter drop-down list.
    Note A post filter is a function that is used to process the results returned by APIs. You can specify one or more post filters. You can create a function and use it as a post filter.
  6. Click Preview Response to view the processing results of the post filters.