Skip to content

Script Development / Writing and Calling Functions

This document serves as the most fundamental guide for developing scripts on DataFlux Func. After reading this, you will be able to perform basic development and usage tasks on DataFlux Func.

1. Notes Before You Start

During the use of DataFlux Func,

Avoid multiple people logging in with the same account or editing the same code simultaneously.

To prevent issues such as code overwrite and loss

2. Writing Your First Function and Invoking It

Writing code in DataFlux Func is not very 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 a function is the return value of the interface. When the return value is a dict or list, the system will automatically treat it as JSON and return accordingly.

A typical function is as follows:

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 multiple ways to call functions decorated with @DFF.API(...):

Execution Feature Features Use Cases
Sync API (Legacy: Auth Link) Generates a synchronous HTTP API. Returns results directly after invocation Situations where processing time is short and clients require immediate results
Async API (Legacy: Batch) Generates an asynchronous HTTP API. Responds immediately after invocation but does not return processing results Scenarios where processing takes longer and the interface call is merely a start signal
Cron Job (Legacy: Auto Trigger Configuration) Automatically executes based on Crontab syntax Periodic data synchronization/caching, scheduled tasks, etc.

Here, create a Sync API (Legacy: Auth Link) for this function to enable invoking it via HTTP on the public network.

Assuming the ID of the Sync API (Legacy: Auth Link) created for this function is auln-xxxxx, the simplest way to invoke this function is as follows:

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

The response would be like (some content omitted):

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

{"message":"Hello"}

3. Writing a Function That Supports File Uploads

DataFlux Func also supports uploading files through Sync API (Legacy: Auth Link).

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

Example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Receive Excel file and return 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 in by the DataFlux Func system, and its contents are as follows:

JSON
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[
    {
        "filePath"    : "<Temporary storage path of the file>",
        "originalname": "<Original filename>",
        "encoding"    : "<Encoding>",
        "mimetype"    : "<MIME type>",
        "size"        : "<File size>"
    },
    ...
]

For example commands to upload files, see Script Development / Basic Concepts / Sync API (Legacy: Auth Link) / POST Simplified Parameters

4. Receiving Non-JSON and Non-Form Data

Added in version 1.6.9

In some cases, requests may originate from third-party systems or applications in their own specific formats, and the request body may not be in JSON or 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 will automatically package it into { "text": "<text>" } or { "base64": "<Base64 encoded binary data>"} and pass it to the function.

An example code snippet is 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 holding 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 containing 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 example:

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

Output example:

Text Only
1
Text: hello, world!

When Request Body Is Unknown Format

Request example:

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

Output example:

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