Skip to content

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-Watching Tips

When using DataFlux Func,

do not allow multiple users to log in with the same account, nor should multiple users edit the same code simultaneously.

To avoid issues of code overwriting or 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, simply add the built-in @DFF.API(...) decorator.

The return value of the function will be the return value of the API. When the return value is dict or list, the system will automatically treat it as JSON.

A typical function looks like this:

Python
1
2
3
4
5
6
@DFF.API('Hello, world')
def hello_world(message=None):
    ret = {
        'message': message
    }
    return ret

The DataFlux Func platform provides various ways to call functions decorated with DFF.API(...):

Execution Feature Characteristics Applicable Scenarios
Synchronous API (Old Version: Auth Link) Generates a synchronous HTTP API. Returns results directly after calling Short processing time, client needs immediate results
Asynchronous API (Old Version: Batch Processing) Generates an asynchronous HTTP API. Responds immediately but does not return results Long processing time, API calls only as trigger signals
Scheduled Tasks (Old Version: Auto Trigger Config) Executes automatically based on Crontab syntax Periodic syncing/caching data, scheduled tasks

Here, creating a synchronous API (Old Version: Auth Link) for this function allows public access via HTTP.

Assuming the ID of the synchronous API (Old Version: Auth Link) created for this function is auln-xxxxx, the simplest way to call this function is as follows:

Text Only
1
GET /api/v1/al/auln-xxxxx/simplified?message=Hello

Response as follows (content partially omitted):

Text Only
1
2
3
4
HTTP/1.1 200 OK
Content-Type: application/json

{"message":"Hello"}

3. Writing Functions Supporting File Uploads

DataFlux Func also supports file uploads through synchronous APIs (Old Version: Auth Links).

When handling uploaded files, you can add a files parameter to receive the uploaded file information. After uploading, DataFlux Func automatically stores the file in a temporary upload directory for subsequent script processing.

Example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Receives an Excel file and returns the content of Sheet1
from openpyxl import load_workbook

@DFF.API('Read Excel')
def read_excel(files=None):
    excel_data = []
    if files:
        workbook = load_workbook(filename=files[0]['filePath'])
        for row in workbook['Sheet1'].iter_rows(min_row=1, values_only=True):
            excel_data.append(row)

    return excel_data

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
[
    {
        "filePath"    : "<Temporary file storage path>",
        "originalname": "<Original filename>",
        "encoding"    : "<Encoding>",
        "mimetype"    : "<MIME type>",
        "size"        : "<File size>"
    },
    ...
]

An example command for uploading files can be found at Script Development / Basic Concepts / Synchronous API (Old Version: 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 does not belong to JSON or Form format. In such cases, you can use **data as input parameters and call it in 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 as follows:

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import json
import binascii

@DFF.API('Function accepting any Body format')
def tiger_balm(**data):
    if 'text' in data:
        # When the request body is text (e.g., Content-Type: text/plain)
        # The `data` parameter always contains a single `text` field storing the content
        return f"Text: {data['text']}"

    elif 'base64' in data:
        # When the request body is in an unparsable format (Content-Type: application/xxx)
        # The `data` parameter always contains a single `base64` field storing the Base64 string of the request body
        # The Base64 string can be converted to Python binary data using `binascii.a2b_base64(...)`
        b = binascii.a2b_base64(data['base64'])
        return f"Base64: {data['base64']} -> {b}"

When Request Body is Text

Request as follows:

Bash
1
curl -X POST -H "Content-Type: text/plain" -d 'hello, world!' http://localhost:8089/api/v1/al/auln-unknow-body/simplified

Output as follows:

Text Only
1
Text: hello, world!

When Request Body is Unknown Format

Request as follows:

Bash
1
curl -X POST -H "Content-Type: unknow/type" -d 'hello, world!' http://localhost:8089/api/v1/al/auln-unknow-body/simplified

Output as follows:

Text Only
1
Base64: aGVsbG8sIHdvcmxkIQ== -> b'hello, world!'