Data Receive API

POST Request #

url: /echart/dataset_api/
Content-Type: application/x-www-form-urlencoded

Request parameter data #

{
    "visitor": "xxx",
    "token": "xxx",
    "stamp": "xxxxx",
    "type": "xxx",
    "ds": [["A","B"],["a1","b1"],["a2","b2"]],
    "update": 0
}

Parameter Description #

visitor: username
type: dataset ID
stamp: timestamp (milliseconds since 1970)
token: SHA1 encrypted, token=SHA1(secret_key + stamp + visitor + type)
update: 0=insert, 1=update
ds: 2D array or refer to data filling/update format

“secret_key” is the admin’s “token” in API service settings

Response Format #

{
  "status": 200,
  "msg": "success"
}

Python Example #

def post_data():
    import requests
    import hashlib
    url='http://localhost:8000/echart/dataset_api/'
    visitor = 'xxx'
    key = 'xxxxxx'
    id = 123

    ContentType = "application/x-www-form-urlencoded"
    stamp = int(time.time() * 1000)
    token = hashlib.sha1(f"{key}{stamp}{visitor}{id}".encode('utf-8')).hexdigest()
    ds = json.dumps([["A", "B"],["a1", "b1"],["a2", "b2"]])
    data = {
        "visitor": visitor,
        "token": token,
        "stamp": stamp,
        "type": id,
        "ds": ds
    }
    res = requests.post(url, data=data, headers={'Content-Type': ContentType})
    print(res.json())

Dataset Configuration #

dataset = {
    "table": "table_name(field1, field2...)",
    "preSql": ["truncate table abc"],
    "postSql": ["insert into xxx select ...."]
}

File Upload API #

POST url: /etl/api/upload_file_api/
POST data:
data = {"type": "xxx", "token": "xxx", "visitor": "xxxx", "mail": "xxx@xxxx"}
# Optional: "filename":"xxxx" to reprocess existing file
def post_file():
    import requests, hashlib
    url = '/etl/api/upload_file_api/'
    visitor = 'xxx'
    key = 'xxxxxx'
    name = 'xxx'
    stamp = int(time.time() * 1000)
    token = hashlib.sha1(f"{key}{stamp}{visitor}{name}".encode('utf-8')).hexdigest()
    data = {"visitor": visitor, "token": token, "stamp": stamp, "type": name}
    files = {'file': open('xxxx.xlsx', 'rb')}
    response = requests.post(url, files=files, data=data)
    return response.json()