Commit 02d5428f authored by zhangc's avatar zhangc

commit 添加api请求相关类

parent 7ecbcfde
......@@ -66,8 +66,12 @@ public enum GatewayResultCode implements ResultCode {
API_STATUS_EXCEPTION("API_STATUS_EXCEPTION", "API状态异常"),
/** 请求组织状态异常 */
ORG_STATE_EXCEPTION("ORG_STATE_EXCEPTION", "请求组织状态异常");
ORG_STATE_EXCEPTION("ORG_STATE_EXCEPTION", "请求组织状态异常"),
/** 请求信息不存在 */
REQUEST_INFO_UNEXIST("REQUEST_INFO_UNEXIST", "请求信息不存在"),
/** 输出类型错误 */
OUTPUT_TYPE_EXCEPTION("OUTPUT_TYPE_EXCEPTION", "输出类型错误!");
/**
* 初始化保存到map里方便根据code获取
*/
......
......@@ -16,7 +16,7 @@ import java.util.Map;
*/
public class OpenApiRequest {
private String appKey; //apiKey
private String apiKey; //apiKey
private String openApiParams;
......@@ -41,11 +41,11 @@ public class OpenApiRequest {
}
public String getAppKey() {
return appKey;
return apiKey;
}
public void setAppKey(String appKey) {
this.appKey = appKey;
this.apiKey = appKey;
}
@SuppressWarnings("unchecked")
......
......@@ -75,7 +75,7 @@ public class AuthFilter extends AbstractFilter {
JSONObject parameter = JSONObject.parseObject(request.getParams());
if (null != parameter) {
String authCode = parameter.getString("authCode");
ApiAuth authAuth = authService.getAuthUser(authCode);
ApiAuth authAuth = authService.getAuthUser(authCode,apiInterface.getId());
if (null == authAuth) {
throw new GatewayException(GatewayResultCode.REQUEST_NOT_AUTH);
}
......
......@@ -13,4 +13,6 @@ import com.jz.dm.models.domian.ApiInterface;
* @Description:
**/
public interface ApiInterfaceMapper extends BaseMapper<ApiInterface> {
}
......@@ -18,9 +18,9 @@ import java.util.Map;
@Data
public class GatewayRequest implements Serializable {
@ParamName("app_key")
@ParamName("api_key")
@ApiModelProperty(value = "appKey API唯一标识")
private String appKey;
private String apiKey;
@ApiModelProperty(value = "接口名称")
private String method;
......@@ -88,7 +88,7 @@ public class GatewayRequest implements Serializable {
*/
@Override
public String toString() {
return "GatewayRequest [appKey=" + appKey + ", method=" + method + ", format=" + format
return "GatewayRequest [appKey=" + apiKey + ", method=" + method + ", format=" + format
+ ", signType=" + signType + ", sign=" + sign
+ ", timestamp=" + timestamp + ", version=" + version
+ ", params=" + params + ", extAttributes="
......
......@@ -24,8 +24,15 @@ public interface ApiInterfaceService {
/**
* 获取api信息
* @param appKey
* @param apiKey
* @return
*/
ApiInterface getApiInfo(String apiKey);
/**
* 获取apikey信息
* @param apiKey
* @return
*/
ApiInterface getApiInfo(String appKey);
ApiInterface getReqTargetInfo(String apiKey);
}
......@@ -59,7 +59,8 @@ public interface AuthService {
/**
* 获取API授权信息
* @param authCode
* @param apiId
* @return
*/
ApiAuth getAuthUser( String authCode);
ApiAuth getAuthUser( String authCode,Long apiId);
}
......@@ -54,14 +54,22 @@ public class ApiInterfaceServiceImpl implements ApiInterfaceService {
/**
* 获取API信息
* @param appKey
* @param apiKey
* @return
*/
@Override
public ApiInterface getApiInfo(String appKey) {
public ApiInterface getApiInfo(String apiKey) {
QueryWrapper<ApiInterface> query = new QueryWrapper<>();
query.eq("is_deleted",0);
query.eq("api_key",apiKey);
return apiInterfaceMapper.selectOne(query);
}
@Override
public ApiInterface getReqTargetInfo(String apiKey) {
QueryWrapper<ApiInterface> query = new QueryWrapper<>();
query.eq("api_key",apiKey);
query.eq("is_deleted",0);
query.eq("api_key",appKey);
return apiInterfaceMapper.selectOne(query);
}
}
......@@ -267,12 +267,14 @@ public class AuthServiceImpl implements AuthService {
/**
* 获取API授权信息
* @param authCode
* @param apiId
* @return
*/
@Override
public ApiAuth getAuthUser(String authCode) {
public ApiAuth getAuthUser(String authCode,Long apiId) {
QueryWrapper<ApiAuth> query = new QueryWrapper<>();
query.eq("auth_code",authCode);
query.eq("api_interface_id",apiId);
query.eq("is_deleted",0);
return apiAuthMapper.selectOne(query);
}
......
......@@ -4,9 +4,11 @@ import com.alibaba.fastjson.JSONObject;
import com.jz.common.utils.HttpsUtils;
import com.jz.common.utils.RedisUtils;
import com.jz.dm.common.enums.GatewayResultCode;
import com.jz.dm.common.exception.GatewayException;
import com.jz.dm.common.util.OpenApiRequest;
import com.jz.dm.common.util.OpenApiResponse;
import com.jz.dm.gateway.OpenApiService;
import com.jz.dm.models.domian.ApiInterface;
import com.jz.dm.service.ApiInterfaceService;
import com.jz.dm.web.annotation.AccessLimit;
import com.jz.dm.web.annotation.ApiLogAspect;
......@@ -47,53 +49,80 @@ public class ApiQueryService implements OpenApiService {
private RedisUtils redisUtils;
@Autowired
private HttpsUtils httpsUtils;
@Override
@ApiLogAspect(description = "API请求日志")
@AccessLimit(limit = 10000,sec = 1)
@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
@AccessLimit(limit = 10000, sec = 1)
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public void doService(OpenApiRequest request, OpenApiResponse response) {
JSONObject parameter = JSONObject.parseObject(request.getOpenApiParams());
String apiType = parameter.getString("apiType");
if (StringUtils.isNotBlank(apiType)){
try {
switch (apiType){
case "QUERY_TAG": //标签查询
//String reqParam = parameter.getString("request_param");
//apiInterfaceService.getTagReqData();
break;
case "BIG_DATA_QUERY"://大数据查询
break;
case "CUSTOM_QUERY": //自定义查询(三方查询)
break;
case "DATA_BANK_QUERY": //数据银行查询
String reqParams = parameter.getString("reqParams");
if (StringUtils.isNotBlank(reqParams)){
String apiInterface = redisUtils.get(request.getAppKey());
if (null != apiInterface){//存在
JSONObject jsonObject = JSONObject.parseObject(apiInterface);
String requestParam = jsonObject.getString("requestParam");
String targetUrl =jsonObject.getString("targetUrl");
Map param = (Map)JSONObject.parseObject(requestParam);
String respResult = httpsUtils.doGet(targetUrl, param);
response.setAttribute(respResult);
}else {//不存在查询数据库
//apiInterfaceService.getReqTargetInfo(request.getAppKey());
}
}
break;
case "DATA_BAG_QUERY": //数据包查询
break;
default :
response.setCode(GatewayResultCode.ILLEGAL_REQUEST.getCode());
response.setMsg(GatewayResultCode.ILLEGAL_REQUEST.getMsg());
break;
}
} catch(Exception e) {
log.error("~~~~~~~~~~~~~~~请求api信息异常~~~~~~~~~~~~~");
log.error("异常信息:{}",e.getMessage());
response.setCode(GatewayResultCode.ILLEGAL_REQUEST.getCode());
response.setMsg(GatewayResultCode.ILLEGAL_REQUEST.getMsg());
}
}
String selectType = parameter.getString("selectType");
String reqParams = parameter.getString("reqParams");
Map paramMap =null;
if (StringUtils.isNotBlank(reqParams)) {
paramMap = (Map) JSONObject.parseObject(reqParams);
}
String apiInterface = redisUtils.get(request.getAppKey());
if (StringUtils.isNotBlank(selectType)) {
try {
switch (selectType) {
case "QUERY_TAG": //标签查询
break;
case "BIG_DATA_QUERY"://大数据查询
break;
case "CUSTOM_QUERY": //自定义查询(三方查询)
if (StringUtils.isNotBlank(apiInterface)){//redis中存在
JSONObject jsonCustom = JSONObject.parseObject(apiInterface);
String targetUrl = jsonCustom.getString("targetUrl");
rangRequestTarget("json",targetUrl,null,response);
}else {//redis中不存在,查询数据库
ApiInterface apiReqDto =
apiInterfaceService.getReqTargetInfo(request.getAppKey());
if (null == apiReqDto) {
throw new GatewayException(GatewayResultCode.REQUEST_INFO_UNEXIST);
}
rangRequestTarget("json",apiReqDto.getTargetUrl(),null,response);
}
break;
case "DATA_BANK_QUERY": //数据银行查询
if (null != apiInterface) {//redis中存在
JSONObject jsonObject = JSONObject.parseObject(apiInterface);
String targetUrl = jsonObject.getString("targetUrl");
String outputType = jsonObject.getString("outputType");
rangRequestTarget(outputType, targetUrl, paramMap, response);
} else {//不存在查询数据库
ApiInterface apiReqDto =
apiInterfaceService.getReqTargetInfo(request.getAppKey());
if (null == apiReqDto) {
throw new GatewayException(GatewayResultCode.REQUEST_INFO_UNEXIST);
}
rangRequestTarget(apiReqDto.getOutputType(), apiReqDto.getTargetUrl(),paramMap,response);
}
break;
case "DATA_BAG_QUERY": //数据银行数据包查询
break;
default:
response.setCode(GatewayResultCode.ILLEGAL_REQUEST.getCode());
response.setMsg(GatewayResultCode.ILLEGAL_REQUEST.getMsg());
break;
}
} catch (Exception e) {
log.error("~~~~~~~~~~~~~~~请求api信息异常~~~~~~~~~~~~~");
log.error("异常信息:{}", e.getMessage());
response.setCode(GatewayResultCode.ILLEGAL_REQUEST.getCode());
response.setMsg(GatewayResultCode.ILLEGAL_REQUEST.getMsg());
}
}
}
private void rangRequestTarget(String outputType, String targetUrl, Map<String, String> param, OpenApiResponse response) {
if ("flow".equals(outputType)) {//文件流形式请求
// httpsUtils.doGetImg()TODO 流形式未实现
} else if ("json".equals(outputType)) {
String respResult = httpsUtils.doGet(targetUrl, param);
response.setAttribute(respResult);
} else {
throw new GatewayException(GatewayResultCode.OUTPUT_TYPE_EXCEPTION);
}
}
}
......@@ -9,4 +9,5 @@
update_user, is_deleted
</sql>
</mapper>
\ No newline at end of file
......@@ -39,7 +39,7 @@ public class TestHttpReq extends SpringTestCase {
headers.put("Charsert", "UTF-8");
headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
String response = httpsUtils.submitPost(baseUrlPost, jsonObject,headers);
System.out.println(response);
}
}
package com.jz.common.utils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
......@@ -45,7 +44,7 @@ public class HttpsUtils {
* @param headers 请求头
* @return
*/
public String submitPost(String url, JSONObject params, Map<String, String> headers) {
public String submitPost(String url, String params, Map<String, String> headers) {
CloseableHttpClient httpClient = getHttpClient();
String body = null;
CloseableHttpResponse response = null;
......@@ -57,8 +56,8 @@ public class HttpsUtils {
httpPost.addHeader(e.getKey(), e.getValue());
}
}
if (StringUtils.isNotBlank(params.toString())) {
httpPost.setEntity(new StringEntity(params.toString(), Consts.UTF_8));
if (StringUtils.isNotBlank(params)) {
httpPost.setEntity(new StringEntity(params, Consts.UTF_8));
}
response = httpClient.execute(httpPost);
body = getBody(response.getEntity());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment