Script Development / Writing and Calling Functions
This document is the most basic Guide for developing scripts on DataFlux Func. After reading it, you will be able to perform the most basic development and usage tasks on DataFlux Func.
1. Pre-Reading Notes
During the use of DataFlux Func,
Please do not allow multiple people to log in with the same account, and do not allow multiple people to edit the same code simultaneously.
To avoid issues of code overwriting and loss.
2. Writing the First Function and Calling It
Writing code in DataFlux Func is not much different from writing Python code normally.
For functions that need to be exported as APIs, simply add the built-in @DFF.API(...)
decorator.
The return value of the function is the return value of the interface. When the return value is dict
or list
, the system will automatically return it as JSON.
A typical function looks like this:
Python | |
---|---|
1 2 3 4 5 6 |
|
The DataFlux Func platform provides multiple ways to call functions decorated with DFF.API(...)
:
Execution Function | Characteristics | Use Cases |
---|---|---|
Sync API | Generates a synchronous HTTP API. Returns the result directly after calling | When processing time is short, and the client needs immediate results |
Async API | Generates an asynchronous HTTP API. Responds immediately but does not return the result | When processing time is long, and the interface call is only a start signal |
Cron Job | Automatically executes based on Crontab syntax | Regular data synchronization / caching, scheduled tasks, etc. |
For details, see Script Development / Basic Concepts
Here, creating a Func API for this function allows it to be called via HTTP on the public network.
Assuming the "Func API" ID created for this function is func-api-xxxxx
, the simplest way to call this function is as follows:
Text Only | |
---|---|
1 |
|
The response is as follows (some content omitted):
Text Only | |
---|---|
1 2 3 4 |
|
3. Writing a Function That Supports File Uploading
DataFlux Func also supports uploading files via Func API.
When handling uploaded files, you can add a files
parameter to the function to receive the uploaded file information.
After uploading, DataFlux Func automatically stores the file in a temporary upload directory for the script to process further.
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 example commands on uploading files, see Script Development / Basic Concepts / Func API / POST
Simplified Parameter Passing
4. Receiving Non-JSON, Non-Form Data
Added in version 1.6.9
In some cases, requests may be initiated by third-party systems or applications in their specific format, and the request body is not in JSON or Form format. In such cases, you can use **data
as a parameter and call it in the POST simplified form.
When the system receives text or data that cannot be parsed, it automatically packages it as { "text": "<text>" }
or { "base64": "<Base64-encoded binary data>"}
and passes it to the function.
Example code:
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 in an Unknown Format
Request as follows:
Bash | |
---|---|
1 |
|
Output as follows:
Text Only | |
---|---|
1 |
|