Skip to content

Open API and SDK

New in version 2.6.4

DataFlux Func provides complete Open API support, which can be invoked programmatically using the companion DataFlux Func SDK.

1. Enable the Open API Documentation Page

In the "Experimental Features" section, you can enable the Open API documentation page.

The DataFlux Func SDK includes signing capability and is distributed as a single file. Users can directly add it to their project for use.

enable-openapi-doc.png

2. Create an Access Key

  1. Log in to your DataFlux Func
  2. Enable Access Key management in "Admin / Experimental Features"
  3. In "Admin / Access Key", click "New" to create an Access Key

3. Use the SDK to Send Requests

The DataFlux Func SDK supports multiple programming languages. The download URLs are as follows:

Language Download URL Description
Python dataflux_func_sdk.py Single file; basic usage has no dependencies. The upload feature depends on the requests library
Node.js dataflux_func_sdk.js Single file; basic usage has no dependencies. The upload feature depends on the form-data library
Golang dataflux_func_sdk.go Single file, completely dependency-free
Demo: dataflux_func_sdk_demo.go

An example of sending requests is 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 GET request
try:
    status_code, resp = dff.get('/api/v1/do/ping')

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

# Send 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 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 GET request
var getOpt = {
  path: '/api/v1/do/ping',
};
dff.get(getOpt, function(err, respData, respStatusCode) {
  if (err) console.error(colored(err, 'red'))

  // Send 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 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 GET request
    _, _, err := dff.Get("/api/v1/do/ping", nil, nil, "")
    if err != nil {
        panic(err)
    }

    // Send 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 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)
    }
}