✨ 完善VCS,LBS,OTA等API操作
This commit is contained in:
parent
8a0a546e44
commit
73b3673758
|
@ -17,6 +17,8 @@ import java.util.Map;
|
|||
@Setter
|
||||
public class ApiResponse implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -1380654611024977212L;
|
||||
|
||||
private Integer code;
|
||||
|
||||
private String msg;
|
||||
|
|
306
src/main/java/net/javase/onenet/api/FuseApi.java
Normal file
306
src/main/java/net/javase/onenet/api/FuseApi.java
Normal file
|
@ -0,0 +1,306 @@
|
|||
package net.javase.onenet.api;
|
||||
|
||||
import cn.hutool.http.ContentType;
|
||||
import cn.hutool.http.Method;
|
||||
import net.javase.onenet.config.Config;
|
||||
import net.javase.onenet.enums.ApiEnum;
|
||||
import net.javase.onenet.enums.SignMethod;
|
||||
import net.javase.onenet.model.FuseLbsModel;
|
||||
import net.javase.onenet.model.FuseOtaModel;
|
||||
import net.javase.onenet.model.RegistDeviceIdentityModel;
|
||||
import net.javase.onenet.model.VoiceNotifyModel;
|
||||
import net.javase.onenet.utils.RequestUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* FuseApi
|
||||
*
|
||||
* @author Frank
|
||||
*/
|
||||
public class FuseApi {
|
||||
|
||||
/**
|
||||
* <a href="https://open.iot.10086.cn/doc/v5/fuse/detail/1432">批量自动注册标识</a>
|
||||
*
|
||||
* @param config {@link Config}
|
||||
* @param body http body参数
|
||||
* @return {@link ApiResponse}
|
||||
*/
|
||||
public static ApiResponse batchAutoRegistDeviceIdentity(Config config, Map<String, Object> body) {
|
||||
return ApiRequest.builder()
|
||||
.api(ApiEnum.FUSE_IDENTITY_DEVICE.api())
|
||||
.method(Method.POST)
|
||||
.contentType(ContentType.JSON)
|
||||
.signMethod(SignMethod.MD5)
|
||||
.body(body)
|
||||
.userid(config.getUserid())
|
||||
.build()
|
||||
.send(config.getAccessKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* <a href="https://open.iot.10086.cn/doc/v5/fuse/detail/1432">批量自动注册标识</a>
|
||||
*
|
||||
* @param config {@link Config}
|
||||
* @param model {@link RegistDeviceIdentityModel}
|
||||
* @return {@link ApiResponse}
|
||||
*/
|
||||
public static ApiResponse batchAutoRegistDeviceIdentity(Config config, RegistDeviceIdentityModel model) {
|
||||
return batchAutoRegistDeviceIdentity(config, model.toMap());
|
||||
}
|
||||
|
||||
/**
|
||||
* <a href="https://open.iot.10086.cn/doc/v5/fuse/detail/1432">批量自动注册标识</a>
|
||||
*
|
||||
* @param config {@link Config}
|
||||
* @param body http body参数
|
||||
* @return {@link ApiResponse}
|
||||
*/
|
||||
public static ApiResponse voiceNotify(Config config, Map<String, Object> body) {
|
||||
return ApiRequest.builder()
|
||||
.api(ApiEnum.VOICE_NOTIFY.api())
|
||||
.method(Method.POST)
|
||||
.contentType(ContentType.JSON)
|
||||
.signMethod(SignMethod.MD5)
|
||||
.body(body)
|
||||
.userid(config.getUserid())
|
||||
.build()
|
||||
.send(config.getAccessKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* <a href="https://open.iot.10086.cn/doc/v5/fuse/detail/1432">批量自动注册标识</a>
|
||||
*
|
||||
* @param config {@link Config}
|
||||
* @param model {@link VoiceNotifyModel}
|
||||
* @return {@link ApiResponse}
|
||||
*/
|
||||
public static ApiResponse voiceNotify(Config config, VoiceNotifyModel model) {
|
||||
return voiceNotify(config, model.toMap());
|
||||
}
|
||||
|
||||
/**
|
||||
* <a href="https://open.iot.10086.cn/doc/v5/fuse/detail/1437">基站定位获取最新位置</a>
|
||||
*
|
||||
* @param config {@link Config}
|
||||
* @param query http query参数
|
||||
* @return {@link ApiResponse}
|
||||
*/
|
||||
public static ApiResponse latestLocation(Config config, Map<String, Object> query) {
|
||||
return ApiRequest.builder()
|
||||
.api(ApiEnum.LBS_LATEST_LOCATION.api())
|
||||
.method(Method.GET)
|
||||
.signMethod(SignMethod.MD5)
|
||||
.query(query)
|
||||
.userid(config.getUserid())
|
||||
.build()
|
||||
.send(config.getAccessKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* <a href="https://open.iot.10086.cn/doc/v5/fuse/detail/1437">基站定位获取最新位置</a>
|
||||
*
|
||||
* @param config {@link Config}
|
||||
* @param model {@link FuseLbsModel}
|
||||
* @return {@link ApiResponse}
|
||||
*/
|
||||
public static ApiResponse latestLocation(Config config, FuseLbsModel model) {
|
||||
return latestLocation(config, model.toMap());
|
||||
}
|
||||
|
||||
/**
|
||||
* <a href="https://open.iot.10086.cn/doc/v5/fuse/detail/1438">基站定位历史轨迹查询</a>
|
||||
*
|
||||
* @param config {@link Config}
|
||||
* @param query http query参数
|
||||
* @return {@link ApiResponse}
|
||||
*/
|
||||
public static ApiResponse getTrail(Config config, Map<String, Object> query) {
|
||||
return ApiRequest.builder()
|
||||
.api(ApiEnum.LBS_GET_TRAIL.api())
|
||||
.method(Method.GET)
|
||||
.signMethod(SignMethod.MD5)
|
||||
.query(query)
|
||||
.userid(config.getUserid())
|
||||
.build()
|
||||
.send(config.getAccessKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* <a href="https://open.iot.10086.cn/doc/v5/fuse/detail/1438">基站定位历史轨迹查询</a>
|
||||
*
|
||||
* @param config {@link Config}
|
||||
* @param model {@link FuseLbsModel}
|
||||
* @return {@link ApiResponse}
|
||||
*/
|
||||
public static ApiResponse getTrail(Config config, FuseLbsModel model) {
|
||||
return getTrail(config, model.toMap());
|
||||
}
|
||||
|
||||
/**
|
||||
* <a href="https://open.iot.10086.cn/doc/v5/fuse/detail/1439">WIFI定位获取最新位置</a>
|
||||
*
|
||||
* @param config {@link Config}
|
||||
* @param query http query参数
|
||||
* @return {@link ApiResponse}
|
||||
*/
|
||||
public static ApiResponse latestWifiLocation(Config config, Map<String, Object> query) {
|
||||
return ApiRequest.builder()
|
||||
.api(ApiEnum.LBS_LATEST_WIFI_LOCATION.api())
|
||||
.method(Method.GET)
|
||||
.signMethod(SignMethod.MD5)
|
||||
.query(query)
|
||||
.userid(config.getUserid())
|
||||
.build()
|
||||
.send(config.getAccessKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* <a href="https://open.iot.10086.cn/doc/v5/fuse/detail/1439">WIFI定位获取最新位置</a>
|
||||
*
|
||||
* @param config {@link Config}
|
||||
* @param model {@link FuseLbsModel}
|
||||
* @return {@link ApiResponse}
|
||||
*/
|
||||
public static ApiResponse latestWifiLocation(Config config, FuseLbsModel model) {
|
||||
return latestWifiLocation(config, model.toMap());
|
||||
}
|
||||
|
||||
/**
|
||||
* <a href="https://open.iot.10086.cn/doc/v5/fuse/detail/1443">WIFI定位历史轨迹查询</a>
|
||||
*
|
||||
* @param config {@link Config}
|
||||
* @param query http query参数
|
||||
* @return {@link ApiResponse}
|
||||
*/
|
||||
public static ApiResponse getWifiTrail(Config config, Map<String, Object> query) {
|
||||
return ApiRequest.builder()
|
||||
.api(ApiEnum.LBS_GET_WIFI_TRAIL.api())
|
||||
.method(Method.GET)
|
||||
.signMethod(SignMethod.MD5)
|
||||
.query(query)
|
||||
.userid(config.getUserid())
|
||||
.build()
|
||||
.send(config.getAccessKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* <a href="https://open.iot.10086.cn/doc/v5/fuse/detail/1443">WIFI定位历史轨迹查询</a>
|
||||
*
|
||||
* @param config {@link Config}
|
||||
* @param model {@link FuseLbsModel}
|
||||
* @return {@link ApiResponse}
|
||||
*/
|
||||
public static ApiResponse getWifiTrail(Config config, FuseLbsModel model) {
|
||||
return getWifiTrail(config, model.toMap());
|
||||
}
|
||||
|
||||
/**
|
||||
* <a href="https://open.iot.10086.cn/doc/v5/fuse/detail/1447">检测升级任务</a>
|
||||
*
|
||||
* @param config {@link Config}
|
||||
* @param model {@link FuseOtaModel}
|
||||
* @return {@link ApiResponse}
|
||||
*/
|
||||
public static ApiResponse otaCheck(Config config, FuseOtaModel model) {
|
||||
return ApiRequest.builder()
|
||||
.api(ApiEnum.OTA_CHECK.api(model.getProId(), model.getDevName()))
|
||||
.method(Method.GET)
|
||||
.signMethod(SignMethod.MD5)
|
||||
.query(model.toMap())
|
||||
.userid(config.getUserid())
|
||||
.build()
|
||||
.send(config.getAccessKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* <a href="https://open.iot.10086.cn/doc/v5/fuse/detail/1448">下载升级包</a>
|
||||
*
|
||||
* @param config {@link Config}
|
||||
* @param model {@link FuseOtaModel}
|
||||
* @param target {@link File}
|
||||
*/
|
||||
public static void otaDownload(Config config, FuseOtaModel model, File target) {
|
||||
RequestUtil.download(ApiRequest.builder()
|
||||
.api(ApiEnum.OTA_DOWNLOAD.api(model.getProId(), model.getDevName(), model.getTid()))
|
||||
.method(Method.GET)
|
||||
.signMethod(SignMethod.MD5)
|
||||
.query(model.toMap())
|
||||
.userid(config.getUserid())
|
||||
.build(), config.getAccessKey(), target);
|
||||
}
|
||||
|
||||
/**
|
||||
* <a href="https://open.iot.10086.cn/doc/v5/fuse/detail/1449">上报升级状态</a>
|
||||
*
|
||||
* @param config {@link Config}
|
||||
* @param model {@link FuseOtaModel}
|
||||
* @return {@link ApiResponse}
|
||||
*/
|
||||
public static ApiResponse otaStatus(Config config, FuseOtaModel model) {
|
||||
return ApiRequest.builder()
|
||||
.api(ApiEnum.OTA_STATUS.api(model.getProId(), model.getDevName(), model.getTid()))
|
||||
.method(Method.POST)
|
||||
.signMethod(SignMethod.MD5)
|
||||
.query(model.toMap())
|
||||
.userid(config.getUserid())
|
||||
.build()
|
||||
.send(config.getAccessKey());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* <a href="https://open.iot.10086.cn/doc/v5/fuse/detail/1450">检测升级状态</a>
|
||||
*
|
||||
* @param config {@link Config}
|
||||
* @param model {@link FuseOtaModel}
|
||||
* @return {@link ApiResponse}
|
||||
*/
|
||||
public static ApiResponse otaTidCheck(Config config, FuseOtaModel model) {
|
||||
return ApiRequest.builder()
|
||||
.api(ApiEnum.OTA_TID_CHECK.api(model.getProId(), model.getDevName(), model.getTid()))
|
||||
.method(Method.GET)
|
||||
.signMethod(SignMethod.MD5)
|
||||
.query(model.toMap())
|
||||
.userid(config.getUserid())
|
||||
.build()
|
||||
.send(config.getAccessKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* <a href="https://open.iot.10086.cn/doc/v5/fuse/detail/1451">查看设备版本号</a>
|
||||
*
|
||||
* @param config {@link Config}
|
||||
* @param model {@link FuseOtaModel}
|
||||
* @return {@link ApiResponse}
|
||||
*/
|
||||
public static ApiResponse otaVersion(Config config, FuseOtaModel model) {
|
||||
return ApiRequest.builder()
|
||||
.api(ApiEnum.OTA_VERSION.api(model.getProId(), model.getDevName()))
|
||||
.method(Method.GET)
|
||||
.signMethod(SignMethod.MD5)
|
||||
.userid(config.getUserid())
|
||||
.build()
|
||||
.send(config.getAccessKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* <a href="https://open.iot.10086.cn/doc/v5/fuse/detail/1454">上报设备版本号</a>
|
||||
*
|
||||
* @param config {@link Config}
|
||||
* @param model {@link FuseOtaModel}
|
||||
* @return {@link ApiResponse}
|
||||
*/
|
||||
public static ApiResponse otaVersionUp(Config config, FuseOtaModel model) {
|
||||
return ApiRequest.builder()
|
||||
.api(ApiEnum.OTA_VERSION.api(model.getProId(), model.getDevName()))
|
||||
.method(Method.POST)
|
||||
.signMethod(SignMethod.MD5)
|
||||
.userid(config.getUserid())
|
||||
.build()
|
||||
.send(config.getAccessKey());
|
||||
}
|
||||
|
||||
}
|
83
src/main/java/net/javase/onenet/model/FuseLbsModel.java
Normal file
83
src/main/java/net/javase/onenet/model/FuseLbsModel.java
Normal file
|
@ -0,0 +1,83 @@
|
|||
package net.javase.onenet.model;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import net.javase.onenet.annotation.ApiParam;
|
||||
import net.javase.onenet.model.base.BaseModel;
|
||||
|
||||
/**
|
||||
* FuseLbsModel
|
||||
*
|
||||
* @author Frank
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
public class FuseLbsModel extends BaseModel {
|
||||
|
||||
/**
|
||||
* 产品名称
|
||||
*/
|
||||
@ApiParam(name = "product_id")
|
||||
private String productId;
|
||||
|
||||
/**
|
||||
* 设备名称
|
||||
*/
|
||||
@ApiParam(name = "device_name")
|
||||
private String deviceName;
|
||||
|
||||
/**
|
||||
* NB设备的imei号
|
||||
*/
|
||||
private String imei;
|
||||
|
||||
/**
|
||||
* 表示查询轨迹的开始时间,默认起始时间为本周一零点,可选,精确到毫秒级别
|
||||
*/
|
||||
private String start;
|
||||
|
||||
/**
|
||||
* 表示查询轨迹的结束时间,默认结束时间为当前时间,可选,精确到毫秒级别
|
||||
*/
|
||||
private String end;
|
||||
|
||||
/**
|
||||
* 指定页码,可选
|
||||
*/
|
||||
private String page;
|
||||
|
||||
/**
|
||||
* 指定每页输出设备个数,可选,默认10,可选
|
||||
*/
|
||||
private String per_page;
|
||||
|
||||
/**
|
||||
* 最大经度,可选
|
||||
*/
|
||||
private String max_lon;
|
||||
|
||||
/**
|
||||
* 最小经度,可选
|
||||
*/
|
||||
private String min_lon;
|
||||
|
||||
/**
|
||||
* 最大纬度,可选
|
||||
*/
|
||||
private String max_lat;
|
||||
|
||||
/**
|
||||
* 最小纬度,可选
|
||||
*/
|
||||
private String min_lat;
|
||||
|
||||
/**
|
||||
* 可选,默认为ASC(升序)
|
||||
*/
|
||||
private String sort;
|
||||
|
||||
|
||||
|
||||
}
|
69
src/main/java/net/javase/onenet/model/FuseOtaModel.java
Normal file
69
src/main/java/net/javase/onenet/model/FuseOtaModel.java
Normal file
|
@ -0,0 +1,69 @@
|
|||
package net.javase.onenet.model;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import net.javase.onenet.annotation.ApiParam;
|
||||
import net.javase.onenet.model.base.BaseModel;
|
||||
|
||||
/**
|
||||
* FuseOtaModel
|
||||
*
|
||||
* @author Frank
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
public class FuseOtaModel extends BaseModel {
|
||||
|
||||
/**
|
||||
* 产品id
|
||||
*/
|
||||
@ApiParam(ignore = true)
|
||||
private String proId;
|
||||
|
||||
/**
|
||||
* 设备名称
|
||||
*/
|
||||
@ApiParam(ignore = true)
|
||||
private String devName;
|
||||
|
||||
/**
|
||||
* tid
|
||||
*/
|
||||
@ApiParam(ignore = true)
|
||||
private String tid;
|
||||
|
||||
/**
|
||||
* 1:fota任务,2:sota任务
|
||||
*/
|
||||
private Integer type;
|
||||
|
||||
/**
|
||||
* 设备当前版本
|
||||
*/
|
||||
private String version;
|
||||
|
||||
/**
|
||||
* 分片下载字段
|
||||
*/
|
||||
private Integer range;
|
||||
|
||||
/**
|
||||
* 状态码
|
||||
*/
|
||||
private Integer step;
|
||||
|
||||
/**
|
||||
* 模组版本号
|
||||
*/
|
||||
@ApiParam(name = "f_version")
|
||||
private String fVersion;
|
||||
|
||||
/**
|
||||
* 应用服务版本号
|
||||
*/
|
||||
@ApiParam(name = "s_version")
|
||||
private String sVersion;
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package net.javase.onenet.model;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import net.javase.onenet.annotation.ApiParam;
|
||||
import net.javase.onenet.model.base.BaseModel;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* RegistDeviceIdentityModel
|
||||
*
|
||||
* @author Frank
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
public class RegistDeviceIdentityModel extends BaseModel {
|
||||
|
||||
/**
|
||||
* 产品名称
|
||||
*/
|
||||
@ApiParam(name = "product_id")
|
||||
private String productId;
|
||||
|
||||
/**
|
||||
* 设备信息集合, 一次最多创建1000个设备。<br>
|
||||
* 每个集合元素是设备名称,例如["device01", "device02"],每个元素的设备名称只能是1-32位英文、数字、特殊字符 -_
|
||||
*/
|
||||
private List<String> devices;
|
||||
|
||||
/**
|
||||
* 是否开启自动更新,0:关闭,1:开启
|
||||
*/
|
||||
@ApiParam(name = "auto_mode")
|
||||
private String autoMode;
|
||||
|
||||
}
|
107
src/main/java/net/javase/onenet/model/VoiceNotifyModel.java
Normal file
107
src/main/java/net/javase/onenet/model/VoiceNotifyModel.java
Normal file
|
@ -0,0 +1,107 @@
|
|||
package net.javase.onenet.model;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import net.javase.onenet.annotation.ApiParam;
|
||||
import net.javase.onenet.model.base.BaseModel;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* VoiceNotifyModel
|
||||
*
|
||||
* @author Frank
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
public class VoiceNotifyModel extends BaseModel {
|
||||
|
||||
/**
|
||||
* 外呼被叫号码MSISDN <br>
|
||||
* 举例8618625150488
|
||||
*/
|
||||
@ApiParam(name = "participant_address")
|
||||
private String participantAddress;
|
||||
|
||||
/**
|
||||
* 号显号码(业务号码)
|
||||
* 由OneNET平台提供
|
||||
*/
|
||||
private String display;
|
||||
|
||||
/**
|
||||
* 呼叫操作指示,参考CallActionType定义
|
||||
* 仅支持Play和PlayAndCollect
|
||||
*/
|
||||
private List<CallActionType> actions;
|
||||
|
||||
/**
|
||||
* 用户指定的通知地址(请求方式为POST)
|
||||
* 如无此字段,则根据OneNET平台配置为准
|
||||
*/
|
||||
@ApiParam(name = "notify_url")
|
||||
private String notifyUrl;
|
||||
|
||||
|
||||
/**
|
||||
* CallActionType 呼叫操作类型
|
||||
*
|
||||
* @author Frank
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
public static class CallActionType {
|
||||
/**
|
||||
* 操作类型
|
||||
* Play:放音
|
||||
* PlayAndCollect:放音及收号
|
||||
*/
|
||||
private String operation;
|
||||
|
||||
/**
|
||||
* tts模板编号
|
||||
* 用户向OneNET提交的通知模板,需审核通过后才能使用
|
||||
*/
|
||||
private String tts_template;
|
||||
|
||||
/**
|
||||
* 模板中需要填充的参数参数内容为UTF-8编码,JSON Object的格式为{"var1":"aa","var2":"aa"},其中var1,var2为模板变量名称,var的个数可变
|
||||
*/
|
||||
private String param_value;
|
||||
|
||||
/**
|
||||
* 收号长度当operation=PlayAndCollect时有效,如果replayAfterCollection=false或未配置,系统会将用户输入的号码通过呼叫状态通知API发送给第三方;如果replayAfterCollection=true,则不会将收号结果上报给第三方
|
||||
* 取值范围
|
||||
* 1~32:收号,取值表示收号位长,最大支持32位
|
||||
*/
|
||||
private String collect_length;
|
||||
|
||||
/**
|
||||
* 重新播放标识
|
||||
* 当operation=PlayAndCollect时有效,此字段用于设置是否在收号后重新播放指定的放音
|
||||
* true:播放
|
||||
* false:不播放
|
||||
* 默认值:false
|
||||
*/
|
||||
private String replay_after_collection;
|
||||
|
||||
/**
|
||||
* 当replayAfterCollection=true时此参数有效
|
||||
* 此字段用于设置触发重新放音的收号内容,只有实际收号内容与该参数值一致时,才重新放音
|
||||
* 例如:该参数设置为1,当用户根据收号语音提示按1,两者一致,则重复放音默认值为1
|
||||
*/
|
||||
private String collect_content_trigger_replaying;
|
||||
|
||||
/**
|
||||
* 重复放音次数
|
||||
* 当操作类型为Play时,此字段有效
|
||||
* 默认为1次
|
||||
*/
|
||||
private String relay_time;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -9,6 +9,7 @@ import net.javase.onenet.api.ApiRequest;
|
|||
import net.javase.onenet.api.ApiResponse;
|
||||
import net.javase.onenet.exception.ApiException;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
|
@ -78,4 +79,47 @@ public class RequestUtil {
|
|||
throw new ApiException("请求异常", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件下载
|
||||
*
|
||||
* @param req {@link ApiRequest}
|
||||
* @param accessKey accessKey
|
||||
* @param target {@link File}
|
||||
*/
|
||||
public static void download(ApiRequest req, String accessKey, File target) {
|
||||
Method method = req.getMethod();
|
||||
String url = req.getDomain().getDomain() + req.url();
|
||||
if (req.getQuery() != null && req.getQuery().size() > 0) {
|
||||
String query = URLUtil.buildQuery(req.getQuery(), StandardCharsets.UTF_8);
|
||||
url += "?" + query;
|
||||
}
|
||||
HttpRequest request = HttpRequest.of(url).method(method);
|
||||
if (req.getContentType() != null) {
|
||||
request.contentType(req.getContentType().toString());
|
||||
}
|
||||
if (req.getBody() != null && req.getBody().size() > 0) {
|
||||
request.body(JSONObject.toJSONString(req.getBody()));
|
||||
}
|
||||
|
||||
String expirationTime = String.valueOf((System.currentTimeMillis() / 1000) + 600);
|
||||
String token = SignUtil.assembleToken(req.getVersion(),
|
||||
req.getRes(), expirationTime, req.getSignMethod(), accessKey);
|
||||
request.header("authorization", token);
|
||||
download(request, target);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件下载
|
||||
*
|
||||
* @param request {@link HttpRequest}
|
||||
* @param target {@link File}
|
||||
*/
|
||||
public static void download(HttpRequest request, File target) {
|
||||
try (HttpResponse response = request.execute()) {
|
||||
response.writeBody(target);
|
||||
} catch (Exception ex) {
|
||||
throw new ApiException("文件下载异常", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user