通过OPENAPI补数据接口实现三方集成

适用模块

全平台

具体步骤

1. 申请openapi鉴权AKSK
     参考文档:https://study.sf.163.com/documents/read/service_support/dsc-p-a-0116
2. 确认具体API接口
     参考文档:http://easydemo-prod.163yun.com/media/doc/openapi_list.html
3. API接口入参获取
1. 通用参数获取方式如下:
product:为所需补数据任务所在项目,可登陆平台,通过右上角查看,详见图1A标签;
clusterId:为所需补数据任务所在项目归属集群,可登陆平台,通过右上角查看,详见图1B标签;
user: 为提交补数据任务用户邮箱地址或唯一编号;
authType:固定为AKSK
timestamp:为当前毫秒级时间戳
accessKey:为步骤一返回值
sig:根据步骤一返回的secretKey+timestamp进行md5加密结果

下图为通过中台页面获取product、clusterId及user参数值示例 DEMO-通过OPENAPI补数据接口实现三方集成 - 图1

2. 接口请求

# get方式
## 请求中包含中文字符需进行URL编码
curl -X GET  http://xxx.xxx.xxx.xxx:10098/openapi/mammut/task/v1/list?product=dsc_support&user=admin.mammut@163.com&clusterId=easyops-cluster&keyword=%E6%89%B9%E9%87%8F%E5%8F%96%E6%B6%88%E8%B0%83%E5%BA%A6%E4%BB%BB%E5%8A%A1&authType=AKSK&timestamp=1732112243587&accessKey=a7e6cdec61e14ef1b34ac4d08fb8af3d&sig=6337001be4d55198b1e3b39308c7d880

#post方式
curl -X "POST" "http://111.124.204.156:10098/openapi/easytaskops/backfill/v1/no-cascade/create" \
     -H 'Content-Type: application/json' \
     -d $'{
  "sig": "db924f30119a627c55147c4caf66a801",
  "product": "dsc_support",
  "authType": "AKSK",
  "clusterId": "easyops-cluster",
  "leftTime": "1732032000000",
  "flows": [
    {
      "project": "f39f0f271e7e424fb356dec934625b39",
      "flow": "批量取消调度任务"
    }
  ],
  "accessKey": "a7e6cdec61e14ef1b34ac4d08fb8af3d",
  "timestamp": 1732111510601,
  "user": "admin.mammut@163.com"
}'
4. 请求示例

以补数据流程为示例

#!/bin/bash

# 请求获取AKSK
# curl -X POST  "http://xxx.xxx.xxx.xxx:10098/api/opt/app/insert?owner=admin.mammut@163.com&appName=faq_demo"

base_url='http://xxx.xxx.xxx.xxx:10098/openapi'
AK='a7e6cdec61e14ef1b34ac4d08fb8af3d'
SK='bc0dbe4d92434feda97633af944a0f85'

# 获取当前时间戳
ts=`date +%s%3N`

# 获取补数据开始时间戳
left_ts=`date -d "$(date +%Y-%m-%d) 00:00:00" +%s%3N`

# 生成接口请求签名
sig=`echo -n ${SK}${ts}|md5sum|awk '{ print $1}'`

# 拼接get请求鉴权字符串
auth_str="authType=AKSK&timestamp=${ts}&accessKey=${AK}&sig=${sig}"

# 定义要搜索的工作流关键字
keyword_str="批量取消调度任务"

# 对中文进行url编码
keyword=`python -c "import urllib; print(urllib.quote('''${keyword_str}'''))"`

echo $keyword

# 根据工作流关键字请求工作流详细信息,获取其project和flow值用于补数据接口入参
url="${base_url}/mammut/task/v1/list?product=dsc_support&user=admin.mammut@163.com&clusterId=easyops-cluster&keyword=${keyword}&${auth_str}"

res=`curl -X GET $url`
echo $res

# 解析请求结果,获取project及flow参数
project=`python -c "import json; print(json.loads('''$res''')['result']['list'][0]['project'])"|cut -d '$' -f 1`
flow=`python -c "import json; print(json.loads('''$res''')['result']['list'][0]['flow'].encode('utf-8'))"`


# 发起补数据请求
## 拼接补数据请求body
body=`echo -n '{"product":"dsc_support","clusterId":"easyops-cluster","flows":[{"project": "'${project}'", "flow":"'${flow}'"}],"leftTime":"'${left_ts}'","user":"admin.mammut@163.com","authType":"AKSK","accessKey":"a7e6cdec61e14ef1b34ac4d08fb8af3d","timestamp":'${ts}',"sig":"'${sig}'"}'`

url="${base_url}/easytaskops/backfill/v1/no-cascade/create"

exec=`curl -X POST $url -H "Content-Type: application/json" -d "${body}"`

# 解析补数据请求返回结果,获取补数据任务实例ID,用于后续查询补数据任务实例状态
exec_id=`python -c "import json; print(json.loads('''$exec''')['result'])"`

# 休眠10S,等待补数据任务提交后获取状态
sleep 10

url="${base_url}/easytaskops/backfill/v1/detail/get?product=dsc_support&clusterId=easyops-cluster&id=${exec_id}&user=admin.mammut@163.com&${auth_str}"

# 请求补数据实例状态,补数据任务运行时间较长时,可定期轮询查询
curl -X GET $url