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.
- DataWorks Professional Edition or a more advanced edition is activated.
- The DataWorks workspace is created in the China (Shanghai) region.
- 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
# -*- 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
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
- 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 where the target workspace resides. Find the target workspace and click DataService Studio in the Actions column.
- On the Service Development tab, move the pointer over and choose .
You can also click the target 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, 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.
- 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 in the toolbar.
Use post filters
- On the tab, click the business process to which the target API belongs.
- Double-click the target API. The configuration tab of the API appears.
- On the configuration tab of the API, click Response Parameters in the right-side navigation pane.
- In the Filter in the Advanced Settings section.
- 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.
- Click Preview Response to view the processing results of the post filters.