mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 12:56:28 +08:00
5.0
This commit is contained in:
@@ -25,7 +25,7 @@
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>2.0.16</version>
|
||||
<version>2.0.52</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
@@ -44,7 +44,7 @@
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.4</version>
|
||||
<version>1.18.34</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
@@ -102,5 +102,12 @@
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>maven_central</id>
|
||||
<name>Maven Central</name>
|
||||
<url>https://repo.maven.apache.org/maven2/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
</project>
|
||||
|
@@ -6,12 +6,11 @@ import com.alibaba.fastjson.parser.Feature;
|
||||
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.common.Result;
|
||||
import com.gitee.sop.sdk.common.SopSdkConstants;
|
||||
import com.gitee.sop.sdk.common.SopSdkErrors;
|
||||
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.response.ErrorResponse;
|
||||
import com.gitee.sop.sdk.sign.SopSignException;
|
||||
import com.gitee.sop.sdk.sign.SopSignature;
|
||||
import com.gitee.sop.sdk.sign.StringUtils;
|
||||
@@ -135,7 +134,7 @@ public class OpenClient {
|
||||
* @param <T> 返回对应的Response
|
||||
* @return 返回Response
|
||||
*/
|
||||
public <T extends BaseResponse> T execute(BaseRequest<T> request) {
|
||||
public <T> Result<T> execute(BaseRequest<T> request) {
|
||||
return this.execute(request, null);
|
||||
}
|
||||
|
||||
@@ -147,7 +146,7 @@ public class OpenClient {
|
||||
* @param <T> 返回对应的Response
|
||||
* @return 返回Response
|
||||
*/
|
||||
public <T extends BaseResponse> T execute(BaseRequest<T> request, String accessToken) {
|
||||
public <T> Result<T> execute(BaseRequest<T> request, String accessToken) {
|
||||
RequestForm requestForm = request.createRequestForm(this.openConfig);
|
||||
// 表单数据
|
||||
Map<String, String> form = requestForm.getForm();
|
||||
@@ -191,28 +190,25 @@ public class OpenClient {
|
||||
* @param <T> 返回结果
|
||||
* @return 返回对于的Response对象
|
||||
*/
|
||||
protected <T extends BaseResponse> T parseResponse(String resp, BaseRequest<T> request) {
|
||||
protected <T> Result<T> parseResponse(String resp, BaseRequest<T> request) {
|
||||
String method = request.getMethod();
|
||||
String rootNodeName = dataNameBuilder.build(method);
|
||||
JSONObject jsonObject = JSON.parseObject(resp, Feature.OrderedField);
|
||||
String errorResponseName = this.openConfig.getErrorResponseName();
|
||||
boolean errorResponse = jsonObject.containsKey(errorResponseName);
|
||||
if (errorResponse) {
|
||||
rootNodeName = errorResponseName;
|
||||
}
|
||||
JSONObject data = jsonObject.getJSONObject(rootNodeName);
|
||||
String sign = jsonObject.getString(openConfig.getSignName());
|
||||
// 是否要验证返回的sign
|
||||
if (StringUtils.areNotEmpty(sign, publicKeyPlatform)) {
|
||||
String signContent = buildBizJson(rootNodeName, resp);
|
||||
if (!this.checkResponseSign(signContent, sign, publicKeyPlatform)) {
|
||||
ErrorResponse error = SopSdkErrors.CHECK_RESPONSE_SIGN_ERROR.getErrorResponse();
|
||||
data = JSON.parseObject(JSON.toJSONString(error));
|
||||
return SopSdkErrors.CHECK_RESPONSE_SIGN_ERROR.getErrorResult();
|
||||
}
|
||||
}
|
||||
T t = data.toJavaObject(request.getResponseClass());
|
||||
t.setBody(jsonObject.getString(rootNodeName));
|
||||
return t;
|
||||
Result result = jsonObject.toJavaObject(Result.class);
|
||||
if (data != null) {
|
||||
T dataObj = data.toJavaObject(request.getResponseClass());
|
||||
result.setData(dataObj);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -4,9 +4,9 @@ import com.alibaba.fastjson.JSON;
|
||||
import com.gitee.sop.sdk.common.OpenConfig;
|
||||
import com.gitee.sop.sdk.common.RequestForm;
|
||||
import com.gitee.sop.sdk.common.RequestMethod;
|
||||
import com.gitee.sop.sdk.common.Result;
|
||||
import com.gitee.sop.sdk.common.SopSdkErrors;
|
||||
import com.gitee.sop.sdk.common.UploadFile;
|
||||
import com.gitee.sop.sdk.response.ErrorResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
@@ -68,7 +68,7 @@ public class OpenRequest {
|
||||
}
|
||||
|
||||
protected String causeException(Exception e) {
|
||||
ErrorResponse result = SopSdkErrors.HTTP_ERROR.getErrorResponse();
|
||||
Result result = SopSdkErrors.HTTP_ERROR.getErrorResult();
|
||||
return JSON.toJSONString(result);
|
||||
}
|
||||
}
|
||||
|
@@ -14,7 +14,7 @@ package com.gitee.sop.sdk.common;
|
||||
* @author 六如
|
||||
*/
|
||||
public class CustomDataNameBuilder implements DataNameBuilder {
|
||||
private String dataName = "result";
|
||||
private String dataName = "data";
|
||||
|
||||
public CustomDataNameBuilder() {
|
||||
}
|
||||
|
@@ -7,10 +7,10 @@ import lombok.Data;
|
||||
*/
|
||||
@Data
|
||||
public class OpenConfig {
|
||||
public static DataNameBuilder DATA_NAME_BUILDER = new DefaultDataNameBuilder();
|
||||
public static DataNameBuilder DATA_NAME_BUILDER = new CustomDataNameBuilder();
|
||||
|
||||
/** 成功返回码值 */
|
||||
private String successCode = "10000";
|
||||
private String successCode = "0";
|
||||
/** 默认版本号 */
|
||||
private String defaultVersion = "1.0";
|
||||
/** 字符编码 */
|
||||
|
@@ -0,0 +1,23 @@
|
||||
package com.gitee.sop.sdk.common;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import com.gitee.sop.sdk.sign.StringUtils;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author 六如
|
||||
*/
|
||||
@Data
|
||||
public class Result<T> {
|
||||
private String code;
|
||||
private String msg;
|
||||
private String subCode;
|
||||
private String subMsg;
|
||||
private T data;
|
||||
|
||||
@JSONField(serialize = false)
|
||||
public boolean isSuccess() {
|
||||
return StringUtils.isEmpty(subCode);
|
||||
}
|
||||
|
||||
}
|
@@ -1,7 +1,5 @@
|
||||
package com.gitee.sop.sdk.common;
|
||||
|
||||
import com.gitee.sop.sdk.response.ErrorResponse;
|
||||
|
||||
/**
|
||||
* @author 六如
|
||||
*/
|
||||
@@ -23,13 +21,13 @@ public enum SopSdkErrors {
|
||||
this.subMsg = msg;
|
||||
}
|
||||
|
||||
public ErrorResponse getErrorResponse() {
|
||||
ErrorResponse errorResponse = new ErrorResponse();
|
||||
errorResponse.setCode(code);
|
||||
errorResponse.setSubCode(subCode);
|
||||
errorResponse.setSubMsg(subMsg);
|
||||
errorResponse.setMsg(msg);
|
||||
return errorResponse;
|
||||
public Result getErrorResult() {
|
||||
Result result = new Result();
|
||||
result.setCode(code);
|
||||
result.setSubCode(subCode);
|
||||
result.setSubMsg(subMsg);
|
||||
result.setMsg(msg);
|
||||
return result;
|
||||
}
|
||||
|
||||
private String code;
|
||||
|
@@ -1,11 +1,9 @@
|
||||
package com.gitee.sop.sdk.model;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class GetStoryModel {
|
||||
|
||||
@JSONField(name = "name")
|
||||
private String name;
|
||||
private Integer id;
|
||||
}
|
||||
|
@@ -5,7 +5,6 @@ import com.gitee.sop.sdk.common.OpenConfig;
|
||||
import com.gitee.sop.sdk.common.RequestForm;
|
||||
import com.gitee.sop.sdk.common.RequestMethod;
|
||||
import com.gitee.sop.sdk.common.UploadFile;
|
||||
import com.gitee.sop.sdk.response.BaseResponse;
|
||||
import com.gitee.sop.sdk.util.ClassUtil;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
@@ -34,7 +33,7 @@ import java.util.Map;
|
||||
*
|
||||
* @author 六如
|
||||
*/
|
||||
public abstract class BaseRequest<T extends BaseResponse> {
|
||||
public abstract class BaseRequest<T> {
|
||||
|
||||
private static final String EMPTY_JSON = "{}";
|
||||
|
||||
|
@@ -1,22 +0,0 @@
|
||||
package com.gitee.sop.sdk.request;
|
||||
|
||||
import com.gitee.sop.sdk.response.CommonResponse;
|
||||
|
||||
/**
|
||||
* @author 六如
|
||||
*/
|
||||
public class CommonRequest extends BaseRequest<CommonResponse> {
|
||||
|
||||
public CommonRequest(String method) {
|
||||
super(method, null);
|
||||
}
|
||||
|
||||
public CommonRequest(String method, String version) {
|
||||
super(method, version);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String method() {
|
||||
return "";
|
||||
}
|
||||
}
|
@@ -1,42 +0,0 @@
|
||||
package com.gitee.sop.sdk.response;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import com.gitee.sop.sdk.sign.StringUtils;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* 返回对象,后续返回对象都要继承这个类
|
||||
* {
|
||||
* "alipay_trade_close_response": {
|
||||
* "code": "20000",
|
||||
* "msg": "Service Currently Unavailable",
|
||||
* "sub_code": "isp.unknown-error",
|
||||
* "sub_msg": "系统繁忙"
|
||||
* },
|
||||
* "sign": "ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE"
|
||||
* }
|
||||
* @author 六如
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
public abstract class BaseResponse {
|
||||
|
||||
@JSONField(name = "request_id")
|
||||
private String requestId;
|
||||
private String code;
|
||||
private String msg;
|
||||
@JSONField(name = "sub_code")
|
||||
private String subCode;
|
||||
@JSONField(name = "sub_msg")
|
||||
private String subMsg;
|
||||
@JSONField(serialize = false)
|
||||
private String body;
|
||||
|
||||
@JSONField(serialize = false)
|
||||
public boolean isSuccess() {
|
||||
return StringUtils.isEmpty(subCode);
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -1,8 +0,0 @@
|
||||
package com.gitee.sop.sdk.response;
|
||||
|
||||
/**
|
||||
* @author 六如
|
||||
*/
|
||||
public class CommonResponse extends BaseResponse {
|
||||
|
||||
}
|
@@ -11,9 +11,9 @@ import java.util.List;
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public class DemoFileUploadResponse extends BaseResponse {
|
||||
public class DemoFileUploadResponse {
|
||||
|
||||
private List<FileMeta> files = new ArrayList();
|
||||
private List<FileMeta> files = new ArrayList<>();
|
||||
|
||||
@Data
|
||||
public static class FileMeta {
|
||||
|
@@ -1,7 +0,0 @@
|
||||
package com.gitee.sop.sdk.response;
|
||||
|
||||
/**
|
||||
* @author 六如
|
||||
*/
|
||||
public class ErrorResponse extends BaseResponse {
|
||||
}
|
@@ -1,14 +1,12 @@
|
||||
package com.gitee.sop.sdk.response;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public class GetStoryResponse extends BaseResponse {
|
||||
public class GetStoryResponse {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Date gmt_create;
|
||||
private Date addTime;
|
||||
}
|
||||
|
@@ -1,14 +1,12 @@
|
||||
package com.gitee.sop.sdk.response;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* @author 六如
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public class OpenAuthTokenAppResponse extends BaseResponse {
|
||||
public class OpenAuthTokenAppResponse {
|
||||
/**
|
||||
* 授权令牌
|
||||
*/
|
||||
|
@@ -28,7 +28,7 @@ public abstract class StringUtils {
|
||||
return true;
|
||||
}
|
||||
for (int i = 0; i < strLen; i++) {
|
||||
if ((Character.isWhitespace(value.charAt(i)) == false)) {
|
||||
if ((!Character.isWhitespace(value.charAt(i)))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@@ -1,16 +1,13 @@
|
||||
package com.gitee.sop.sdk;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.gitee.sop.sdk.client.OpenClient;
|
||||
import com.gitee.sop.sdk.common.RequestMethod;
|
||||
import com.gitee.sop.sdk.common.Result;
|
||||
import com.gitee.sop.sdk.common.UploadFile;
|
||||
import com.gitee.sop.sdk.model.DemoFileUploadModel;
|
||||
import com.gitee.sop.sdk.model.GetStoryModel;
|
||||
import com.gitee.sop.sdk.request.CommonRequest;
|
||||
import com.gitee.sop.sdk.request.DemoFileUploadRequest;
|
||||
import com.gitee.sop.sdk.request.GetStoryRequest;
|
||||
import com.gitee.sop.sdk.response.CommonResponse;
|
||||
import com.gitee.sop.sdk.response.DemoFileUploadResponse;
|
||||
import com.gitee.sop.sdk.response.GetStoryResponse;
|
||||
import junit.framework.TestCase;
|
||||
@@ -18,69 +15,47 @@ import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class SdkTest extends TestCase {
|
||||
String url = "http://localhost:8081/api";
|
||||
String appId = "2019032617262200001";
|
||||
/** 开发者私钥 */
|
||||
/**
|
||||
* 开发者私钥
|
||||
*/
|
||||
String privateKeyIsv = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXJv1pQFqWNA/++OYEV7WYXwexZK/J8LY1OWlP9X0T6wHFOvxNKRvMkJ5544SbgsJpVcvRDPrcxmhPbi/sAhdO4x2PiPKIz9Yni2OtYCCeaiE056B+e1O2jXoLeXbfi9fPivJZk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giNGQKBgGxU6P2aDAt6VNwWosHSe+d2WWXt8IZBhO9d6dn0f7ORvcjmCqNKTNGgrkewMZEuVcliueJquR47IROdY8qmwqcBAN7Vg2K7r7CPlTKAWTRYMJxCT1Hi5gwJb+CZF3+IeYqsJk2NF2s0w5WJTE70k1BSvQsfIzAIDz2yE1oPHvwVAoGAA6e+xQkVH4fMEph55RJIZ5goI4Y76BSvt2N5OKZKd4HtaV+eIhM3SDsVYRLIm9ZquJHMiZQGyUGnsvrKL6AAVNK7eQZCRDk9KQz+0GKOGqku0nOZjUbAu6A2/vtXAaAuFSFx1rUQVVjFulLexkXR3KcztL1Qu2k5pB6Si0K/uwQ=";
|
||||
/** 开放平台提供的公钥
|
||||
* 前往SOP-ADMIN,ISV管理--秘钥管理,生成平台提供的公私钥,然后把【平台公钥】放到这里 */
|
||||
/**
|
||||
* 开放平台提供的公钥
|
||||
* 前往SOP-ADMIN,ISV管理--秘钥管理,生成平台提供的公私钥,然后把【平台公钥】放到这里
|
||||
*/
|
||||
String publicKeyPlatform = "";
|
||||
|
||||
// 声明一个就行
|
||||
OpenClient client = new OpenClient(url, appId, privateKeyIsv, publicKeyPlatform);
|
||||
|
||||
// 标准用法
|
||||
@Test
|
||||
public void testGet() {
|
||||
// 创建请求对象
|
||||
GetStoryRequest request = new GetStoryRequest();
|
||||
// 请求参数
|
||||
GetStoryModel model = new GetStoryModel();
|
||||
model.setName("白雪公主");
|
||||
model.setId(1);
|
||||
request.setBizModel(model);
|
||||
|
||||
// 发送请求
|
||||
GetStoryResponse response = client.execute(request);
|
||||
Result<GetStoryResponse> result = client.execute(request);
|
||||
|
||||
if (response.isSuccess()) {
|
||||
if (result.isSuccess()) {
|
||||
GetStoryResponse response = result.getData();
|
||||
// 返回结果
|
||||
System.out.println(String.format("response:%s",
|
||||
JSON.toJSONString(response)));
|
||||
} else {
|
||||
System.out.println("错误,subCode:" + response.getSubCode() + ", subMsg:" + response.getSubMsg());
|
||||
System.out.println("错误,subCode:" + result.getSubCode() + ", subMsg:" + result.getSubMsg());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 懒人版,如果不想添加Request,Response,可以用这种方式,返回全部是String,后续自己处理json
|
||||
@Test
|
||||
public void testLazy() {
|
||||
// 创建请求对象
|
||||
CommonRequest request = new CommonRequest("story.get");
|
||||
// 请求参数
|
||||
Map<String, Object> bizModel = new HashMap<>();
|
||||
bizModel.put("name", "白雪公主");
|
||||
request.setBizModel(bizModel);
|
||||
// request.setRequestMethod(RequestMethod.GET);
|
||||
|
||||
// 发送请求
|
||||
CommonResponse response = client.execute(request);
|
||||
|
||||
if (response.isSuccess()) {
|
||||
// 返回结果
|
||||
String body = response.getBody();
|
||||
JSONObject jsonObject = JSON.parseObject(body);
|
||||
System.out.println(jsonObject);
|
||||
} else {
|
||||
System.out.println("错误,subCode:" + response.getSubCode() + ", subMsg:" + response.getSubMsg());
|
||||
}
|
||||
}
|
||||
|
||||
// 文件上传
|
||||
@Test
|
||||
public void testUpload() throws IOException {
|
||||
@@ -96,15 +71,16 @@ public class SdkTest extends TestCase {
|
||||
request.addFile(new UploadFile("file1", new File(root + "/src/main/resources/file1.txt")));
|
||||
request.addFile(new UploadFile("file2", new File(root + "/src/main/resources/file2.txt")));
|
||||
|
||||
DemoFileUploadResponse response = client.execute(request);
|
||||
Result<DemoFileUploadResponse> result = client.execute(request);
|
||||
|
||||
System.out.println("--------------------");
|
||||
if (response.isSuccess()) {
|
||||
if (result.isSuccess()) {
|
||||
DemoFileUploadResponse response = result.getData();
|
||||
List<DemoFileUploadResponse.FileMeta> responseFiles = response.getFiles();
|
||||
System.out.println("您上传的文件信息:");
|
||||
responseFiles.forEach(System.out::println);
|
||||
} else {
|
||||
System.out.println(JSON.toJSONString(response));
|
||||
System.out.println(JSON.toJSONString(result));
|
||||
}
|
||||
System.out.println("--------------------");
|
||||
}
|
||||
|
Reference in New Issue
Block a user