Script Development / Writing and Calling Functions
This document is the most basic guide for developing scripts on DataFlux Func. After reading, you will be able to perform basic development and usage tasks on DataFlux Func.
1. Pre-View Tips
When using DataFlux Func,
do not allow multiple users to log in with the same account or edit the same code simultaneously.
To avoid issues of code overlap and loss.
2. Writing the First Function and Calling It
Writing code in DataFlux Func is not much different from writing normal Python code.
For functions that need to be exported as APIs, adding the built-in @DFF.API(...)
decorator will suffice.
The function's return value is also the interface's return value. When the return value is dict
or list
, the system automatically returns it as JSON.
A typical function is as follows:
Python | |
---|---|
1 2 3 4 5 6 |
|
The DataFlux Func platform provides several ways to call functions decorated by DFF.API(...)
:
Execution Function | Features | Use Cases |
---|---|---|
Synchronous API (Old: Auth Link) | Generates a synchronous HTTP API. Returns results directly after calling | Short processing time, client needs immediate results |
Asynchronous API (Old: Batch Job) | Generates an asynchronous HTTP API. Responds immediately after calling but does not return processing results | Longer processing time, API call only serves as a start signal |
Scheduled Tasks (Old: Auto Trigger) | Executes automatically based on Crontab syntax | Periodic data synchronization/caching, scheduled tasks |
See more at Script Development / Basic Concepts
Here, creating a synchronous API (Old: Auth Link) for this function allows it to be called via HTTP over the public network.
Assuming the ID of the created synchronous API (Old: Auth Link) for this function is auln-xxxxx
, the simplest way to call this function is as follows:
Text Only | |
---|---|
1 |
|
Response as follows (some content omitted):
Text Only | |
---|---|
1 2 3 4 |
|
3. Writing Functions Supporting File Uploads
DataFlux Func also supports file uploads through synchronous APIs (Old: Auth Link).
When handling uploaded files, you can add a files
parameter to receive file information.
After uploading, DataFlux Func automatically stores the files in a temporary upload directory for further script processing.
Example | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
The files
parameter is automatically filled by the DataFlux Func system, with the following content:
JSON | |
---|---|
1 2 3 4 5 6 7 8 9 10 |
|
For an example command to upload files, see Script Development / Basic Concepts / Synchronous API (Old: Auth Link) / POST
Simplified Parameters.
4. Receiving Non-JSON, From Data
Added in version 1.6.9
In some cases, requests may be initiated by third-party systems or applications in their own specific formats, and the request body is neither JSON nor Form format. In such situations, you can use **data
as a parameter and invoke it using the POST simplified form.
When the system receives text or unparsable data, it automatically packages it into { "text": "<text>" }
or { "base64": "<Base64 encoded binary data>"}
and passes it to the function.
Example code is as follows:
Python | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
When the Request Body is Text
Request as follows:
Bash | |
---|---|
1 |
|
Output as follows:
Text Only | |
---|---|
1 |
|
When the Request Body is of Unknown Format
Request as follows:
Bash | |
---|---|
1 |
|
Output as follows:
Text Only | |
---|---|
1 |
|