DMP第三方API数据服务实现
接口请求说明:
一、约定接口使用token认证的认证方式, POST
方式请求。
token加密方式为:
算法: AES算法,CBC模式,IV为密钥secret_key
密钥:secret_key //API数据源配置的密钥Secret key
数据载体: dmp_jwt_token //DMP系统用户的token信息,为标准jwt token,鉴于已约定密钥认证,这里不做jwt的合法性校验,直接获取payload用户信息即可
解密: AES.new(secret_key, mode=AES.MODE_CBC, IV=secret_key).decrypt(token) -> dmp_jwt_token //解密得到dmp的jwt token信息
二、请求头部携带信息:
- 请求头Cookie: token=xxx
- 请求头Authorization: Bearer xxxxx
- 请求 Content-Type为:
application/json
注意:DMP中添加API数据源填写的 接口地址(Endpoint) 即是该服务的 Root Path基路径
第三方数据服务需要实现的接口
/get-biz-params
第三方服务和DMP约定参数集, DMP在数据源编辑和数据集编辑页面依赖该接口。
参数项project_code
为约定的必须的参数,API请求过程会根据用户动态从当前上下文获取。
其他参数可以根据需要定义,最终在API取数时会在POST中透传这些参数,详见 /get-data
接口定义。
参数: 无
返回值示例:
{
"result": true, // 成功或者失败
"msg": "success",
"data": [{
"name": "project_code", // 参数名称
"range": "datasource", // 参数的作用范围:datasource、dataset
"type": "sys", // 参数类型
"key": "project_code", // 参数名
"description": "租户/企业代码",
"required": true,
"values": [] // 参数的值,可以指定多个
},
{
"name": "dbname", // 可以用来扩展连接不同的db
"range": "datasource",
"type": "query",
"key": "dbname",
"description": "数据库名",
"required": true,
"values": ["sales", "users"]
},
{
"type": "query",
"key": "city",
"range": "dataset",
"name": "city",
"description": "城市",
"values": ["深圳", "东莞"],
"required": true
}
]
}
range 属性值支持
datasource
、dataset
datasource
: 在数据源编辑时定义,可以用来对数据源进行筛选等。dataset
: 数据集编辑时定义,可以对数据集数据进行筛选等
/get-objects
获取当前数据源的表的列表,支持分页查询。比如数据源为Mysql,通过查询元数据表得到:
SELECT SQL_CALC_FOUND_ROWS TABLE_NAME AS `name`,TABLE_COMMENT AS `comment`,TABLE_TYPE AS `type` FROM information_schema.TABLES WHERE TABLE_SCHEMA =DATABASE()
参数:
{
"page": 0,
"page_size": 20,
"keyword": "user", // 关键字查询
"table_name_prefix": "" // 根据前缀查询
}
返回值格式示例:
{
"result":true,
"msg":"success",
"data":{
"items":[
{
"name":"users",
"comment":"用户表",
"type":"BASE TABLE"
},
{
"name":"sales",
"comment":"销售表",
"type":"BASE TABLE"
}
],
"total":2
}
}
/get-object-structs
获取当前数据库指定表的结构。DMP中定义数据集时依赖该接口
参数:
{
"table_name": "user" // 表名
}
返回值格式示例:
{
"result": true, // 成功或者失败
"msg": "success",
"data": [{
"name": "id",
"comment": "标识",
"type": "CHAR(36)",
"description": "标识一行记录"
}, {
"name": "title",
"comment": "标题",
"type": "VARCHAR(255)",
"description": "业务标题"
}]
}
/get-data
提供获取报表数据接口。DMP报告数据展示时取数依赖该接口
参数:
{
"biz_params": [], // DMP传入之前由接口`/get-biz-params`提供的参数
"query_structure": {}, // json结构请[参考]: (http://doc.mypaas.com.cn/dmp/dev/data-source/api-data-source-dev.html)
"tables": [] // 当前查询所使用的表名集合
}
返回值格式示例:
{
"result": true,
"msg": "success",
"sql": "select xxx", // 返回当前取数所使用到的完整sql语句
"data": [{
"field_name": "value" // 查询结果,key-value的形式
}],
"data_update_time": "2019-05-20 16:22:10", // 返回数据的更新时间精确到秒,如果有查询引用到多个表,则取最新的时间。
}
query_structure 是DMP定义的一个json查询语法,通过修改该json可动态追加where条件。
调用DMP提供的一个FaaS方法将该json转换成sql
语句。
php代码示例:
public function parser()
{
$faas_url = 'https://1353452237400551.cn-shanghai.fc.aliyuncs.com/2016-08-15/proxy/DMP/dataset_json_parser/';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $faas_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length:' . strlen($this->query_structure)));
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->query_structure);
$res = curl_exec($curl);
curl_close($curl);
if (!empty($res)) {
$arr = json_decode($res, true);
return $arr['sql'];
}
return false;
}