OpenApiInvoker.java
2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.glsx.service.component;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import com.glsx.constant.OpenAPIConstant;
import com.glsx.exception.ApiException;
import com.glsx.utils.SystemProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* @author fengzhi
* @version 1.0.0
* @ClassName OpenApiInvoker.java
* @description: http协议接口
* @createTime 2020年10月21日 14:26:00
*/
@Slf4j
@Component
public class OpenApiInvoker {
/**
*
*/
private String invokeGet(String url,Map<String,Object> params) {
if (params==null) {
log.info("openapi data is NULL:"+url);
return null;
}
String res = null;
try {
Integer timeOut = Integer.parseInt(SystemProperty.getProperty(OpenAPIConstant.HTTP_TIME_OUT));
log.info("invoker openapiGet serverUrl:" + url);
HttpResponse httpResponse = HttpRequest.get(url).form(params).timeout(timeOut * 1000).execute();
log.info("httpResponse:"+httpResponse.getStatus());
if(httpResponse.getStatus() == 200)
res= httpResponse.body();
} catch (Exception e) {
log.error("invoker openapiGet error:" +url+"==="+ e.getMessage());
throw new ApiException(e.getMessage());
}
log.info("invoker openapiGet api res:" + res);
return res;
}
private String invokePost(String url,Map<String,Object> params) {
if (params==null) {
log.info("openapi data is NULL:"+url);
return null;
}
String res = null;
try {
Integer timeOut = Integer.parseInt(SystemProperty.getProperty(OpenAPIConstant.HTTP_TIME_OUT));
log.info("invoker openapiPost serverUrl:" + url);
HttpResponse httpResponse = HttpRequest.post(url).body(JSONObject.toJSONString(params)).timeout(timeOut * 1000).execute();
log.info("httpResponse:"+httpResponse.getStatus());
if(httpResponse.getStatus() == 200)
res= httpResponse.body();
} catch (Exception e) {
log.error("invoker openapiPost error:" +url+"==="+ e.getMessage());
throw new ApiException(e.getMessage());
}
log.info("invoker openapiPost api res:" +url+"==="+ res);
return res;
}
public String invokeFenceOrRescue(String methodName,Map<String,Object> params) {
String serverUrl = SystemProperty.getProperty(OpenAPIConstant.HTTP_OPENAPI_SERVER_1);
String url = serverUrl+methodName;
return invokeGet(url,params);
}
public String invokCommandOrDeviceStatus(String methodName,Map<String,Object> params) {
String serverUrl = SystemProperty.getProperty(OpenAPIConstant.HTTP_OPENAPI_SERVER_2);
String url = serverUrl+methodName;
return invokePost(url,params);
}
}