Skip to content

Open API and SDK

Added in version 2.6.4

DataFlux Func provides comprehensive Open API support. You can use the accompanying DataFlux Func SDK to make calls programmatically.

1. Enable the Open API Documentation Page

You can enable the Open API documentation page in "Experimental Features".

The DataFlux Func SDK includes signature functionality and is released as a single file. Users can directly place it into their projects for use.

enable-openapi-doc.png

2. Create an Access Key

  1. Log in to your DataFlux Func
  2. Enable Access Key management in "Manage / Experimental Features"
  3. Click "Create" in "Manage / Access Key" to create an Access Key

3. Send Requests Using the SDK

The DataFlux Func SDK supports multiple programming languages. Download links are as follows:

Language Download Link Description
Python dataflux_func_sdk.py Single file, no dependencies for basic use. Upload functionality depends on the requests library.
Node.js dataflux_func_sdk.js Single file, no dependencies for basic use. Upload functionality depends on the form-data library.
Golang dataflux_func_sdk.go Single file, completely dependency-free.
Demo: dataflux_func_sdk_demo.go

Examples of sending requests are as follows:

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from dataflux_func_sdk import DataFluxFunc

# Create a DataFlux Func operation object
dff = DataFluxFunc(ak_id='ak-xxxxx', ak_secret='xxxxxxxxxx', host='localhost:8088')

# Enable Debug
dff.debug = True

# Send a GET request
try:
    status_code, resp = dff.get('/api/v1/do/ping')

except Exception as e:
    print(colored(e, 'red'))
    raise

# Send a POST request
try:
    body = {
        'echo': {
            'int'    : 1,
            'str'    : 'Hello World',
            'none'   : None,
            'boolean': True,
        }
    }
    status_code, resp = dff.post('/api/v1/do/echo', body=body)

except Exception as e:
    print(colored(e, 'red'))
    raise

# Upload a file
try:
    filename = 'your_file'
    with open(filename, 'rb') as _f:
        file_buffer = _f.read()

    fields = {
        'folder': 'test'
    }

    status_code, resp = dff.upload('/api/v1/resources/do/upload',
        file_buffer=file_buffer,
        filename=filename,
        fields=fields)

except Exception as e:
    print(colored(e, 'red'))
    raise
JavaScript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var fs = require('fs');
var DataFluxFunc = require('./dataflux_func_sdk.js').DataFluxFunc;

// Create a DataFlux Func operation object
var opt = {
  akId    : 'ak-xxxxx',
  akSecret: 'xxxxxxxxxx',
  host    : 'localhost:8088',
};
var dff = new DataFluxFunc(opt);

// Enable Debug
dff.debug = true;

// Send a GET request
var getOpt = {
  path: '/api/v1/do/ping',
};
dff.get(getOpt, function(err, respData, respStatusCode) {
  if (err) console.error(colored(err, 'red'))

  // Send a POST request
  var postOpt = {
    path: '/api/v1/do/echo',
    body: {
      'echo': {
        'int'    : 1,
        'str'    : 'Hello World',
        'none'   : null,
        'boolean': true,
      }
    }
  };
  dff.post(postOpt, function(err, respData, respStatusCode) {
    if (err) console.error(colored(err, 'red'))

    // Upload a file
    var filename = 'your_file';
    var uploadOpt = {
      path      : '/api/v1/resources/do/upload',
      fileBuffer: fs.readFileSync(filename),
      filename  : filename,
      fields    : {
        'folder': 'test'
      },
    };
    dff.upload(uploadOpt, function(err, respData, respStatusCode) {
      if (err) console.error(colored(err, 'red'))
    });
  });
});
Go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main

import (
    "os"
    "bytes"
    "fmt"
    "io"
    "mime/multipart"

    // DataFlux Func SDK
    "./dataflux_func_sdk"
)

var (
    colorMap = map[interface{}]string{
        "grey":    "\033[0;30m",
        "red":     "\033[0;31m",
        "green":   "\033[0;32m",
        "yellow":  "\033[0;33m",
        "blue":    "\033[0;34m",
        "magenta": "\033[0;35m",
        "cyan":    "\033[0;36m",
    }
)

func main() {
    host := "localhost:8088"
    if len(os.Args) >= 2 {
        host = os.Args[1]
    }

    // Create a DataFlux Func operation object
    dff := dataflux_func_sdk.NewDataFluxFunc("ak-xxxxx", "xxxxxxxxxx", host, 30, false)

    // Enable Debug
    dff.Debug = true

    // Send a GET request
    _, _, err := dff.Get("/api/v1/do/ping", nil, nil, "")
    if err != nil {
        panic(err)
    }

    // Send a POST request
    body := map[string]interface{}{
        "echo": map[string]interface{}{
            "int"    : 1,
            "str"    : "Hello World",
            "none"   : nil,
            "boolean": true,
        },
    }
    _, _, err = dff.Post("/api/v1/do/echo", body, nil, nil, "")
    if err != nil {
        panic(err)
    }

    // Upload a file
    filename := "dataflux_func_sdk_demo.go"
    file, _ := os.Open(filename)
    fileContents, _ := io.ReadAll(file)

    uploadBody := &bytes.Buffer{}
    writer := multipart.NewWriter(uploadBody)

    part, _ := writer.CreateFormFile("files", filename)
    part.Write(fileContents)

    fields := map[string]string{
        "folder": "test",
    }
    for key, value := range fields {
        writer.WriteField(key, fmt.Sprintf("%v", value))
    }

    writer.Close()
    contentType := writer.FormDataContentType()

    _, _, err = dff.Upload("/api/v1/resources/do/upload", filename, "", fields, nil, nil, uploadBody, contentType)
    if err != nil {
        panic(err)
    }
}