mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-12 07:02:14 +08:00
可自定义数据节点
This commit is contained in:
@@ -2,12 +2,13 @@ package com.gitee.sop.sdk.client;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.gitee.sop.sdk.common.DataNameBuilder;
|
||||
import com.gitee.sop.sdk.common.OpenConfig;
|
||||
import com.gitee.sop.sdk.common.RequestForm;
|
||||
import com.gitee.sop.sdk.sign.SopSignException;
|
||||
import com.gitee.sop.sdk.exception.SdkException;
|
||||
import com.gitee.sop.sdk.request.BaseRequest;
|
||||
import com.gitee.sop.sdk.response.BaseResponse;
|
||||
import com.gitee.sop.sdk.exception.SdkException;
|
||||
import com.gitee.sop.sdk.sign.SopSignException;
|
||||
import com.gitee.sop.sdk.sign.SopSignature;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@@ -25,16 +26,13 @@ public class OpenClient {
|
||||
|
||||
private static final OpenConfig DEFAULT_CONFIG = new OpenConfig();
|
||||
|
||||
private static final char DOT = '.';
|
||||
private static final char UNDERLINE = '_';
|
||||
public static final String DATA_SUFFIX = "_response";
|
||||
|
||||
private String url;
|
||||
private String appId;
|
||||
private String privateKey;
|
||||
|
||||
private OpenConfig openConfig;
|
||||
private OpenRequest openRequest;
|
||||
private DataNameBuilder dataNameBuilder;
|
||||
|
||||
public OpenClient(String url, String appId, String privateKey) {
|
||||
this(url, appId, privateKey, DEFAULT_CONFIG);
|
||||
@@ -50,6 +48,7 @@ public class OpenClient {
|
||||
this.openConfig = openConfig;
|
||||
|
||||
this.openRequest = new OpenRequest(openConfig);
|
||||
this.dataNameBuilder = openConfig.getDataNameBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,7 +108,7 @@ public class OpenClient {
|
||||
|
||||
protected <T extends BaseResponse> T parseResponse(String resp, BaseRequest<T> request) {
|
||||
String method = request.getMethod();
|
||||
String dataName = method.replace(DOT, UNDERLINE) + DATA_SUFFIX;
|
||||
String dataName = dataNameBuilder.build(method);
|
||||
JSONObject jsonObject = JSON.parseObject(resp);
|
||||
JSONObject data = jsonObject.getJSONObject(dataName);
|
||||
T t = data.toJavaObject(request.getResponseClass());
|
||||
|
@@ -0,0 +1,30 @@
|
||||
package com.gitee.sop.sdk.common;
|
||||
|
||||
/**
|
||||
* 返回固定的
|
||||
* {
|
||||
* "result": {
|
||||
* "code": "20000",
|
||||
* "msg": "Service Currently Unavailable",
|
||||
* "sub_code": "isp.unknow-error",
|
||||
* "sub_msg": "系统繁忙"
|
||||
* },
|
||||
* "sign": "ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE"
|
||||
* }
|
||||
* @author tanghc
|
||||
*/
|
||||
public class CustomDataNameBuilder implements DataNameBuilder {
|
||||
private String dataName = "result";
|
||||
|
||||
public CustomDataNameBuilder() {
|
||||
}
|
||||
|
||||
public CustomDataNameBuilder(String dataName) {
|
||||
this.dataName = dataName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String build(String method) {
|
||||
return dataName;
|
||||
}
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
package com.gitee.sop.sdk.common;
|
||||
|
||||
/**
|
||||
* @author tanghc
|
||||
*/
|
||||
public interface DataNameBuilder {
|
||||
/**
|
||||
* 构建数据节点名称
|
||||
* @param method 方法名
|
||||
* @return 返回数据节点名称
|
||||
*/
|
||||
String build(String method);
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
package com.gitee.sop.sdk.common;
|
||||
|
||||
/**
|
||||
* 将方法名中的"."转成"_"并在后面追加"_response"<br>
|
||||
* 如:alipay.trade.order.settle --> alipay_trade_order_settle_response<br>
|
||||
* <pre>
|
||||
* {
|
||||
* "alipay_trade_order_settle_response": {
|
||||
* "code": "20000",
|
||||
* "msg": "Service Currently Unavailable",
|
||||
* "sub_code": "isp.unknow-error",
|
||||
* "sub_msg": "系统繁忙"
|
||||
* },
|
||||
* "sign": "ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE"
|
||||
* }
|
||||
* </pre>
|
||||
* @author tanghc
|
||||
*/
|
||||
public class DefaultDataNameBuilder implements DataNameBuilder {
|
||||
private static final char DOT = '.';
|
||||
private static final char UNDERLINE = '_';
|
||||
private static final String DATA_SUFFIX = "_response";
|
||||
|
||||
@Override
|
||||
public String build(String method) {
|
||||
return method.replace(DOT, UNDERLINE) + DATA_SUFFIX;
|
||||
}
|
||||
}
|
@@ -45,4 +45,9 @@ public class OpenConfig {
|
||||
private int readTimeoutSeconds = 10;
|
||||
/** http写超时时间 */
|
||||
private int writeTimeoutSeconds = 10;
|
||||
|
||||
/**
|
||||
* 构建数据节点名称
|
||||
*/
|
||||
private DataNameBuilder dataNameBuilder = SdkConfig.dataNameBuilder;
|
||||
}
|
||||
|
@@ -13,4 +13,6 @@ public class SdkConfig {
|
||||
public static String CHARSET = "UTF-8";
|
||||
|
||||
public static String SIGN_TYPE = "RSA2";
|
||||
|
||||
public static volatile DataNameBuilder dataNameBuilder = new DefaultDataNameBuilder();
|
||||
}
|
||||
|
Reference in New Issue
Block a user