DefaultYxClient.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package com.github.yxyl120.sdk;
  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.github.yxyl120.sdk.Utils.CheckRequestUtils;
  5. import com.github.yxyl120.sdk.Utils.HttpUtils;
  6. import com.github.yxyl120.sdk.enums.ResponseCode;
  7. import com.github.yxyl120.sdk.request.YxRequest;
  8. import com.github.yxyl120.sdk.response.AbstractResponse;
  9. import org.springframework.util.DigestUtils;
  10. import java.net.URLEncoder;
  11. import java.nio.charset.StandardCharsets;
  12. import java.util.ArrayList;
  13. import java.util.Collections;
  14. import java.util.Map;
  15. import java.util.TreeMap;
  16. public class DefaultYxClient implements YxClient {
  17. private String serverUrl;
  18. private String appId;
  19. private String appSecret;
  20. private ObjectMapper mapper;
  21. private static final String ENC = "UTF-8";
  22. public DefaultYxClient(String serverUrl, String appId, String appSecret) {
  23. this.serverUrl = serverUrl;
  24. this.appId = appId;
  25. this.appSecret = appSecret;
  26. this.mapper = new ObjectMapper();
  27. }
  28. /**
  29. * 执行请求
  30. *
  31. * @param request 入参
  32. * @param <T> 返回的类型
  33. * @return 返回的实体类
  34. */
  35. @Override
  36. public <T extends AbstractResponse> T execute(YxRequest<T> request) {
  37. CheckRequestUtils.doCheck(request, this.appSecret, this.appSecret.substring(0, 16));
  38. String bodyStr = toJson(request);
  39. String url = this.buildUrl(request, bodyStr);
  40. String responseBodyStr = HttpUtils.post(url, bodyStr);
  41. T readValue = parse(responseBodyStr, request.getResponseClass());
  42. if (readValue == null) {
  43. throw new YxException("解析数据异常:" + responseBodyStr, 400);
  44. }
  45. if (readValue.getCode() != ResponseCode.SUCCESS.getCode()) {
  46. throw new YxException(readValue.getMsg(), readValue.getCode());
  47. }
  48. return readValue;
  49. }
  50. private <T> T parse(String jsonStr, Class<T> tClass) {
  51. try {
  52. return mapper.readValue(jsonStr, tClass);
  53. } catch (JsonProcessingException e) {
  54. return null;
  55. }
  56. }
  57. private String buildUrl(YxRequest request, String body) {
  58. Map<String, Object> pmap = new TreeMap<>();
  59. pmap.put("timestamp", System.currentTimeMillis());
  60. pmap.put("appId", this.appId);
  61. pmap.put("version", "1.0");
  62. pmap.put("signMethod", "md5Hex");
  63. if (request.getQueryParam() != null && request.getQueryParam().size() > 0) {
  64. pmap.putAll(request.getQueryParam());
  65. }
  66. ArrayList<String> keys = new ArrayList<>(pmap.keySet());
  67. Collections.sort(keys);
  68. StringBuilder buffer = new StringBuilder();
  69. try {
  70. for (String key : keys) {
  71. if (buffer.length() != 0) {
  72. buffer.append('&');
  73. }
  74. Object value = pmap.get(key);
  75. buffer.append(key).append('=').append(obj2URLEncoder(value));
  76. }
  77. } catch (Exception ignored) {
  78. }
  79. String query = buffer.toString();
  80. buffer.append("&appSecret=")
  81. .append(this.appSecret)
  82. .append("&body=").append(body);
  83. String sign = DigestUtils.md5DigestAsHex(buffer.toString().getBytes(StandardCharsets.UTF_8));
  84. return this.serverUrl + request.getApi() + "?" + query + "&sign=" + sign;
  85. }
  86. private String obj2URLEncoder(Object object) throws Exception {
  87. if (object instanceof String || object instanceof Number) {
  88. return URLEncoder.encode(object.toString(), ENC);
  89. }
  90. return URLEncoder.encode(toJson(object), ENC);
  91. }
  92. private String toJson(Object object) {
  93. try {
  94. return mapper.writeValueAsString(object);
  95. } catch (JsonProcessingException e) {
  96. e.printStackTrace();
  97. return "";
  98. }
  99. }
  100. }