API数据源示例
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

257 lines
10 KiB

package com.fanruan.hihidata.datasource.server;
import com.fanruan.hihidata.datasource.server.http.HttpKits;
import org.apache.http.HttpEntity;
import org.apache.http.entity.StringEntity;
import org.json.JSONArray;
import org.json.JSONObject;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* API数据源所有操作的示例集合,只需要传入授权参数即可
* @author richie
* Created by richie on 2022/9/16
*/
public class DatasourceOperation {
private final Authentication auth;
public DatasourceOperation(Authentication auth) {
this.auth = auth;
}
/**
* 开启或者关闭API数据源入口
* @throws Exception 操作失败则抛出此异常
*/
public void open() throws Exception {
JSONObject body = new JSONObject();
body.put("status", "1");
HttpEntity entity = new StringEntity(body.toString(), StandardCharsets.UTF_8);
HttpKits.post(String.format("%s/api/v1/datasource/status/modify", ProjectConstants.BASE_URI), headers(), entity);
}
/**
* 查看当前API数据源的开关状态
* @return API数据源处于开启状态,则返回"1",否则返回"0"
* @throws Exception 操作失败则抛出此异常
*/
public String status() throws Exception {
Map<String, String> params = new HashMap<>();
String res = HttpKits.post(String.format("%s/api/v1/datasource/status", ProjectConstants.BASE_URI), headers(), params);
JSONObject data = new JSONObject(res);
return data.getString("data");
}
/**
* 在API数据源下创建一个分组
* @param groupName 分组名
* @return 该分组的Id
* @throws Exception 操作失败则抛出此异常
*/
public String createGroup(String groupName) throws Exception {
JSONObject body = new JSONObject();
body.put("groupName", groupName);
HttpEntity entity = new StringEntity(body.toString(), StandardCharsets.UTF_8);
String res = HttpKits.post(String.format("%s/api/v1/datasource/group/create", ProjectConstants.BASE_URI), headers(), entity);
JSONObject data = new JSONObject(res);
return data.getString("data");
}
/**
* 修改分组的名字
* @param groupId 分组Id
* @param newGroupName 新的分组名字
* @throws Exception 操作失败则抛出此异常
*/
public void modifyGroup(String groupId, String newGroupName) throws Exception {
JSONObject body = new JSONObject();
body.put("groupId", groupId);
body.put("groupName", newGroupName);
HttpEntity entity = new StringEntity(body.toString(), StandardCharsets.UTF_8);
HttpKits.post(String.format("%s/api/v1/datasource/group/modify", ProjectConstants.BASE_URI), headers(), entity);
}
/**
* 列出所有的分组
* @return 以JSON数组的形式输出所有分组的信息的集合
* @throws Exception 操作失败则抛出此异常
*/
public JSONArray listAllGroup() throws Exception {
Map<String, String> params = new HashMap<>();
String res = HttpKits.post(String.format("%s/api/v1/datasource/group/list", ProjectConstants.BASE_URI), headers(), params);
JSONObject data = new JSONObject(res);
return data.getJSONArray("data");
}
/**
* 列出指定分组下所有的数据源表
* @param groupId 分组Id
* @return 分组下的所有数据源表组成的一个JSON数组
* @throws Exception 操作失败则抛出此异常
*/
public JSONArray listTablesOfGroup(String groupId) throws Exception {
JSONObject body = new JSONObject();
body.put("groupId", groupId);
HttpEntity entity = new StringEntity(body.toString(), StandardCharsets.UTF_8);
String res = HttpKits.post(String.format("%s/api/v1/datasource/group/table/list", ProjectConstants.BASE_URI), headers(), entity);
JSONObject data = new JSONObject(res);
return data.getJSONArray("data");
}
/**
* 在默认分组下创建一个数据源表
* @param tableName 数据源表名
* @return 数据源表Id
* @throws Exception 操作失败则抛出此异常
*/
public String createTable(String tableName) throws Exception {
JSONObject body = new JSONObject();
body.put("tableName", tableName);
HttpEntity entity = new StringEntity(body.toString(), StandardCharsets.UTF_8);
String res = HttpKits.post(String.format("%s/api/v1/datasource/table/create", ProjectConstants.BASE_URI), headers(), entity);
JSONObject data = new JSONObject(res);
return data.getString("data");
}
/**
* 在指定的分组下创建 一个数据源表
* @param tableName 数据源表名
* @param groupId 分组Id
* @return 数据源表Id
* @throws Exception 操作失败则抛出此异常
*/
public String createTable(String tableName, String groupId) throws Exception {
JSONObject body = new JSONObject();
body.put("tableName", tableName);
body.put("groupId", groupId);
HttpEntity entity = new StringEntity(body.toString(), StandardCharsets.UTF_8);
String res = HttpKits.post(String.format("%s/api/v1/datasource/table/create", ProjectConstants.BASE_URI), headers(), entity);
JSONObject data = new JSONObject(res);
return data.getString("data");
}
/**
* 在指定的分组下创建 一个增量更新的数据源表
* @param tableName 数据源表名
* @param groupId 分组Id
* @return 数据源表Id
* @throws Exception 操作失败则抛出此异常
*/
public String createTableIncreased(String tableName, String groupId) throws Exception {
JSONObject body = new JSONObject();
body.put("tableName", tableName);
body.put("groupId", groupId);
body.put("increase", true);
HttpEntity entity = new StringEntity(body.toString(), StandardCharsets.UTF_8);
String res = HttpKits.post(String.format("%s/api/v1/datasource/table/create", ProjectConstants.BASE_URI), headers(), entity);
JSONObject data = new JSONObject(res);
return data.getString("data");
}
/**
* 修改已有的数据源表的名字
* @param tableId 数据源表Id
* @param newTableName 新的数据源表名字
* @param replaceOldData 是否后续的数据是替换原数据的,true表示在原数据之后追加,false表示替换原数据
* @throws Exception 操作失败则抛出此异常
*/
public void modifyTable(String tableId, String newTableName, boolean replaceOldData) throws Exception {
JSONObject body = new JSONObject();
body.put("tableId", tableId);
body.put("tableName", newTableName);
body.put("increase", replaceOldData);
HttpEntity entity = new StringEntity(body.toString(), StandardCharsets.UTF_8);
HttpKits.post(String.format("%s/api/v1/datasource/table/modify", ProjectConstants.BASE_URI), headers(), entity);
}
/**
* 生成指定的数据源表数据上传的地址
* @param tableId 数据源表Id
* @return 上传数据的地址
* @throws Exception 操作失败则抛出此异常
*/
public String requestUploadUrl(String tableId) throws Exception {
JSONObject body = new JSONObject();
body.put("tableId", tableId);
HttpEntity entity = new StringEntity(body.toString(), StandardCharsets.UTF_8);
String res = HttpKits.post(String.format("%s/api/v1/datasource/table/upload/url", ProjectConstants.BASE_URI), headers(), entity);
JSONObject data = new JSONObject(res);
return data.getString("data");
}
/**
* 标记数据源表的数据上传已完成
* @param tableId 数据源表Id
* @throws Exception 操作失败则抛出此异常
*/
public void markAsFinish(String tableId) throws Exception {
JSONObject body = new JSONObject();
body.put("tableId", tableId);
HttpEntity entity = new StringEntity(body.toString(), StandardCharsets.UTF_8);
HttpKits.post(String.format("%s/api/v1/datasource/table/upload/finish", ProjectConstants.BASE_URI), headers(), entity);
}
/**
* 删除数据源表
* @param tableId 数据源表Id
* @throws Exception 操作失败则抛出此异常
*/
public void deleteTable(String tableId) throws Exception {
JSONObject body = new JSONObject();
body.put("tableId", tableId);
HttpEntity entity = new StringEntity(body.toString(), StandardCharsets.UTF_8);
HttpKits.post(String.format("%s/api/v1/datasource/table/delete", ProjectConstants.BASE_URI), headers(), entity);
}
/**
* 导出分析表csv文件
*
* @param tableId 数据源表Id
* @throws Exception 操作失败则抛出此异常
*/
public String exportTable(String tableId) throws Exception {
JSONObject body = new JSONObject();
body.put("tableId", tableId);
HttpEntity entity = new StringEntity(body.toString(), StandardCharsets.UTF_8);
String res = HttpKits.post(String.format("%s/api/v1/export", ProjectConstants.BASE_URI), headers(), entity);
JSONObject data = new JSONObject(res);
return data.getString("data");
}
/**
* 批量导出分析表csv文件
*
* @param tableIds 数据源表Ids
* @throws Exception 操作失败则抛出此异常
* @return
*/
public JSONArray exportTableBatch(List<String> tableIds) throws Exception {
JSONObject body = new JSONObject();
body.put("tableIds", tableIds);
HttpEntity entity = new StringEntity(body.toString(), StandardCharsets.UTF_8);
String res = HttpKits.post(String.format("%s/api/v1/export/batch", ProjectConstants.BASE_URI), headers(), entity);
JSONObject data = new JSONObject(res);
return data.getJSONArray("data");
}
/**
* 删除API数据源下的所有分组和表
* @throws Exception 操作失败则抛出此异常
*/
public void reset() throws Exception {
Map<String, String> params = new HashMap<>();
HttpKits.post(String.format("%s/api/v1/datasource/reset", ProjectConstants.BASE_URI), headers(), params);
}
private Map<String, String> headers() throws Exception {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", auth.getToken());
return headers;
}
}