Bläddra i källkod

feat:模拟开放API的客户端,将Client参数构建的过程封装

yang yi 11 månader sedan
förälder
incheckning
629534669e

+ 2 - 20
open-api-client/src/main/java/space/anyi/client/ClientApplication.java

@@ -20,25 +20,7 @@ public class ClientApplication {
     public static final String ACCESS_KEY = "accessKey";
     public static final String SECRET_KEY = "secretKey";
     public static void main(String[] args) {
-        String url = "http://localhost:10002/open-api/user/body";
-        User user = new User("杨逸");
-        String json = JSONUtil.toJsonStr(user);
-        String timestamp = System.currentTimeMillis() + "";
-        HttpResponse httpResponse = HttpRequest.post(url)
-                .header("accessKey",ACCESS_KEY)//accessKey
-                .header("timestamp", timestamp)//时间戳
-                .header("sign",getSign(timestamp))//摘要签名
-                .header("nonce",""+(int)Math.random()*100)//随机数
-                .header(Header.CONTENT_TYPE, "application/json")
-                .charset(StandardCharsets.UTF_8)
-                .body(json)
-                .execute();
-        int status = httpResponse.getStatus();
-        System.out.println("status = " + status);
-        String body = httpResponse.body();
-        System.out.println("body = " + body);
-    }
-    static String getSign(String timestamp){
-        return MD5.create().digestHex16(SECRET_KEY+timestamp+SECRET_KEY);
+        OpenAPIClient client = new OpenAPIClient(ACCESS_KEY, SECRET_KEY,"http://localhost:10002/open-api/user/body");
+        client.handler("yangyi");
     }
 }

+ 67 - 0
open-api-client/src/main/java/space/anyi/client/OpenAPIClient.java

@@ -0,0 +1,67 @@
+package space.anyi.client;
+
+import cn.hutool.crypto.digest.MD5;
+import cn.hutool.http.Header;
+import cn.hutool.http.HttpRequest;
+import cn.hutool.http.HttpResponse;
+import cn.hutool.json.JSONUtil;
+import lombok.Data;
+import space.anyi.openApi.comom.model.User;
+
+import java.nio.charset.StandardCharsets;
+
+/**
+ * @ProjectName: open-api-client
+ * @FileName: OpenAPIClient
+ * @Author: 杨逸
+ * @Data:2025/10/18 15:18
+ * @Description: 客户端
+ */
+@Data
+public class OpenAPIClient {
+    private String accessKey;
+    private String secretKey;
+    private String url;
+
+    public OpenAPIClient() {
+    }
+
+    public OpenAPIClient(String accessKey, String secretKey, String url) {
+        this.accessKey = accessKey;
+        this.secretKey = secretKey;
+        this.url = url;
+    }
+    public String handler(String data){
+        User user = new User(data);
+        String json = JSONUtil.toJsonStr(user);
+        String timestamp = System.currentTimeMillis() + "";
+        HttpResponse httpResponse = HttpRequest.post(url)
+                .header("accessKey",accessKey)//accessKey
+                .header("timestamp", timestamp)//时间戳
+                .header("sign",getSign(secretKey,timestamp))//摘要签名
+                .header("nonce",""+(int)Math.random()*100)//随机数
+                .header(Header.CONTENT_TYPE, "application/json")
+                .charset(StandardCharsets.UTF_8)
+                .body(json)
+                .execute();
+        int status = httpResponse.getStatus();
+        System.out.println("status = " + status);
+        String body = httpResponse.body();
+        System.out.println("body = " + body);
+        return body;
+    }
+
+    /**
+     * 获取摘要签名
+     * @param secretKey
+     * @param timestamp
+     * @return {@code String }
+     * @description:
+     * @author: 杨逸
+     * @data:2025/10/18 15:21:23
+     * @since 1.0.0
+     */
+    private String getSign(String secretKey,String timestamp){
+        return MD5.create().digestHex16(secretKey+timestamp+secretKey);
+    }
+}