|
|
@@ -1,6 +1,7 @@
|
|
|
package com.zhongshu.iot.server.core.httpRequest;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
import com.github.microservice.core.util.net.apache.MethodType;
|
|
|
import com.github.microservice.http.APIResponseModel;
|
|
|
import com.github.microservice.http.ApiConfParam;
|
|
|
@@ -12,6 +13,7 @@ import com.zhongshu.iot.server.core.service.other.RequestInfoService;
|
|
|
import com.zhongshu.iot.server.core.util.net.apache.HttpClientUtil;
|
|
|
import com.zhongshu.iot.server.core.util.net.apache.HttpModel;
|
|
|
import com.zhongshu.iot.server.core.util.net.apache.ResponseModel;
|
|
|
+import com.zhongshu.opengateway.client.util.SignUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
@@ -19,6 +21,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.StopWatch;
|
|
|
|
|
|
+import java.util.HashMap;
|
|
|
+
|
|
|
/**
|
|
|
* @author TRX
|
|
|
* @date 2024/6/25
|
|
|
@@ -59,8 +63,7 @@ public class ApiRequestService extends SuperService {
|
|
|
StopWatch stopWatch = new StopWatch();
|
|
|
stopWatch.start();
|
|
|
String url = fullCardConf.getUrl() + fullCardConf.getCardServerName() + apiConfParam.getApiName();
|
|
|
- ResponseModel request = HttpClientUtil.request(HttpModel.builder()
|
|
|
- .url(url).method(MethodType.Json).charset("utf-8").body(data).build());
|
|
|
+ ResponseModel request = HttpClientUtil.request(HttpModel.builder().url(url).method(MethodType.Json).charset("utf-8").body(data).build());
|
|
|
if (request.getCode() == 200) {
|
|
|
responseModel = BeanUtil.copyProperties(request.getBody(), APIResponseModel.class);
|
|
|
} else {
|
|
|
@@ -79,17 +82,27 @@ public class ApiRequestService extends SuperService {
|
|
|
return responseModel;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
public APIResponseModel requestAPI(String url, Object data) {
|
|
|
+ return requestAPI(url, data, "", "");
|
|
|
+ }
|
|
|
+
|
|
|
+ public APIResponseModel requestAPI(String url, Object data, String ak, String sk) {
|
|
|
APIResponseModel responseModel = new APIResponseModel();
|
|
|
try {
|
|
|
+ HashMap<String, Object> headers = new HashMap<>();
|
|
|
+ // 签名
|
|
|
+ if (StringUtils.isNotEmpty(ak) && StringUtils.isNotEmpty(sk)) {
|
|
|
+ String authorization = SignUtil.sign(JSONUtil.toJsonStr(data), ak, sk);
|
|
|
+ headers.put("Authorization", authorization);
|
|
|
+ }
|
|
|
ApiConfParam apiConfParam = new ApiConfParam();
|
|
|
apiConfParam.setApiName(url);
|
|
|
apiConfParam.setMethodType(MethodType.Json.name());
|
|
|
StopWatch stopWatch = new StopWatch();
|
|
|
stopWatch.start();
|
|
|
- ResponseModel request = HttpClientUtil.request(HttpModel.builder()
|
|
|
- .url(url).method(MethodType.Json).charset("utf-8").body(data).build());
|
|
|
+ ResponseModel request = HttpClientUtil.request(HttpModel.builder().url(url)
|
|
|
+ .header(headers)
|
|
|
+ .method(MethodType.Json).charset("utf-8").body(data).build());
|
|
|
int code = request.getCode();
|
|
|
if (code == 200) {
|
|
|
responseModel = BeanUtil.copyProperties(request.getBody(), APIResponseModel.class);
|
|
|
@@ -110,5 +123,4 @@ public class ApiRequestService extends SuperService {
|
|
|
requestInfoService.addRequestInfo(data, responseModel);
|
|
|
return responseModel;
|
|
|
}
|
|
|
-
|
|
|
}
|