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.