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
Usage notes
- 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.