Skip to content

Open API and SDK

Added in version 2.6.4

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

1. Enable Open API Documentation Page

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

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

enable-openapi-doc.png

2. Create Access Key

  1. Log in to your DataFlux Func
  2. In "Management / Experimental Features," enable Access Key management
  3. In "Management / Access Key," click "Create" to create an Access Key

3. Send Requests Using the SDK

The DataFlux Func SDK supports multiple programming languages. Download links are provided below:

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

Example of sending requests:

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
51
from dataflux_func_sdk import DataFluxFunc

# Create 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',
            'unicode': u'你好,世界!',
            '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
52
var fs = require('fs');
var DataFluxFunc = require('./dataflux_func_sdk.js').DataFluxFunc;

// Create 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',
        'unicode': '你好,世界!',
        '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
84
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 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",
            "unicode": "你好,世界!",
            "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)
    }
}