From b10ee75da5c996c5589753f69ec63784939cc68b Mon Sep 17 00:00:00 2001 From: imhao183 Date: Wed, 9 Jun 2021 16:38:05 +0800 Subject: [PATCH] =?UTF-8?q?SDK=E6=94=AF=E6=8C=81=E5=A4=9A=E4=B8=AA?= =?UTF-8?q?=E6=9C=BA=E5=99=A8=E4=BA=BA=E8=B4=A6=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wildfirechat/sdk/ChannelServiceApi.java | 2 +- .../java/cn/wildfirechat/sdk/ChatConfig.java | 15 --- .../main/java/cn/wildfirechat/sdk/Main.java | 53 +++++------ .../cn/wildfirechat/sdk/RobotService.java | 92 ++++++++++--------- .../sdk/utilities/RobotHttpUtils.java | 19 ++-- 5 files changed, 87 insertions(+), 94 deletions(-) delete mode 100644 sdk/src/main/java/cn/wildfirechat/sdk/ChatConfig.java diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/ChannelServiceApi.java b/sdk/src/main/java/cn/wildfirechat/sdk/ChannelServiceApi.java index e5a2dfa2..37aeb5b5 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/ChannelServiceApi.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/ChannelServiceApi.java @@ -9,7 +9,7 @@ import java.util.List; //仅专业版支持,社区版不支持 public class ChannelServiceApi { - private ChannelHttpUtils httpUtils; + private final ChannelHttpUtils httpUtils; public ChannelServiceApi(String imurl, String channelId, String secret) { httpUtils = new ChannelHttpUtils(imurl, channelId, secret); diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/ChatConfig.java b/sdk/src/main/java/cn/wildfirechat/sdk/ChatConfig.java deleted file mode 100644 index f3e6101b..00000000 --- a/sdk/src/main/java/cn/wildfirechat/sdk/ChatConfig.java +++ /dev/null @@ -1,15 +0,0 @@ -package cn.wildfirechat.sdk; - -import cn.wildfirechat.sdk.utilities.AdminHttpUtils; -import cn.wildfirechat.sdk.utilities.ChannelHttpUtils; -import cn.wildfirechat.sdk.utilities.RobotHttpUtils; - -public class ChatConfig { - public static void initAdmin(String url, String secret) { - AdminHttpUtils.init(url, secret); - } - - public static void initRobot(String url, String robotId, String secret) { - RobotHttpUtils.init(url, robotId, secret); - } -} diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/Main.java b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java index 1b8bc161..76f22154 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/Main.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java @@ -852,13 +852,14 @@ public class Main { static void testRobot() throws Exception { //初始化机器人API,注意端口是80,不是18080 String robotId = "robot1"; - RobotHttpUtils.init("http://localhost", robotId, "123456"); + + RobotService robotService = new RobotService("http://localhost", robotId, "123456"); //*********************************************** //**** 机器人API //*********************************************** - IMResult userInfoIMResult = RobotService.getProfile(robotId); + IMResult userInfoIMResult = robotService.getProfile(robotId); if(userInfoIMResult != null && userInfoIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("get profile success"); } else { @@ -867,14 +868,14 @@ public class Main { } String displayName = "testrobot"+System.currentTimeMillis(); - IMResult voidIMResult1 = RobotService.updateProfile(Modify_DisplayName, displayName); + IMResult voidIMResult1 = robotService.updateProfile(Modify_DisplayName, displayName); if(voidIMResult1 != null && voidIMResult1.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("modify profile success"); } else { System.out.println("modify profile failure"); System.exit(-1); } - userInfoIMResult = RobotService.getProfile(robotId); + userInfoIMResult = robotService.getProfile(robotId); if(userInfoIMResult != null && userInfoIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS && displayName.equals(userInfoIMResult.getResult().getDisplayName())) { System.out.println("get profile success"); } else { @@ -883,7 +884,7 @@ public class Main { } String robotCallback = "http://hellow123"; - voidIMResult1 = RobotService.setCallback(robotCallback); + voidIMResult1 = robotService.setCallback(robotCallback); if(voidIMResult1 != null && voidIMResult1.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("set callback success"); } else { @@ -891,7 +892,7 @@ public class Main { System.exit(-1); } - IMResult callbackPojoIMResult = RobotService.getCallback(); + IMResult callbackPojoIMResult = robotService.getCallback(); if(callbackPojoIMResult != null && callbackPojoIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS && robotCallback.equals(callbackPojoIMResult.getResult().getUrl())) { System.out.println("get callback success"); } else { @@ -899,7 +900,7 @@ public class Main { System.exit(-1); } - voidIMResult1 = RobotService.deleteCallback(); + voidIMResult1 = robotService.deleteCallback(); if(voidIMResult1 != null && voidIMResult1.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("delete callback success"); } else { @@ -907,7 +908,7 @@ public class Main { System.exit(-1); } - callbackPojoIMResult = RobotService.getCallback(); + callbackPojoIMResult = robotService.getCallback(); if(callbackPojoIMResult != null && callbackPojoIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS && StringUtil.isNullOrEmpty(callbackPojoIMResult.getResult().getUrl())) { System.out.println("get callback success"); } else { @@ -922,7 +923,7 @@ public class Main { payload.setType(1); payload.setSearchableContent("hello world"); - IMResult resultRobotSendMessage = RobotService.sendMessage("robot1", conversation, payload); + IMResult resultRobotSendMessage = robotService.sendMessage("robot1", conversation, payload); if (resultRobotSendMessage != null && resultRobotSendMessage.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("robot send message success"); } else { @@ -930,7 +931,7 @@ public class Main { System.exit(-1); } - IMResult resultRobotGetUserInfo = RobotService.getUserInfo("userId1"); + IMResult resultRobotGetUserInfo = robotService.getUserInfo("userId1"); if (resultRobotGetUserInfo != null && resultRobotGetUserInfo.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("robot get user info success"); } else { @@ -959,7 +960,7 @@ public class Main { member3.setMember_id("user3"); members.add(member3); - IMResult resultCreateGroup = RobotService.createGroup(groupInfo, members, null, null); + IMResult resultCreateGroup = robotService.createGroup(groupInfo, members, null, null); if (resultCreateGroup != null && resultCreateGroup.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("create group success"); } else { @@ -967,7 +968,7 @@ public class Main { System.exit(-1); } - IMResult resultGetGroupInfo = RobotService.getGroupInfo(groupInfo.getTarget_id()); + IMResult resultGetGroupInfo = robotService.getGroupInfo(groupInfo.getTarget_id()); if (resultGetGroupInfo != null && resultGetGroupInfo.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { if (groupInfo.getExtra().equals(resultGetGroupInfo.getResult().getExtra()) && groupInfo.getName().equals(resultGetGroupInfo.getResult().getName()) @@ -982,7 +983,7 @@ public class Main { System.exit(-1); } - IMResult voidIMResult = RobotService.modifyGroupInfo(groupInfo.getTarget_id(), ProtoConstants.ModifyGroupInfoType.Modify_Group_Name,"HelloWorld", null, null); + IMResult voidIMResult = robotService.modifyGroupInfo(groupInfo.getTarget_id(), ProtoConstants.ModifyGroupInfoType.Modify_Group_Name,"HelloWorld", null, null); if (voidIMResult != null && voidIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("modify group success"); } else { @@ -991,7 +992,7 @@ public class Main { } - IMResult resultGetMembers = RobotService.getGroupMembers(groupInfo.getTarget_id()); + IMResult resultGetMembers = robotService.getGroupMembers(groupInfo.getTarget_id()); if (resultGetMembers != null && resultGetMembers.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("get group member success"); } else { @@ -1003,7 +1004,7 @@ public class Main { m.setMember_id("user1"); m.setAlias("hello user1"); - voidIMResult = RobotService.addGroupMembers(groupInfo.getTarget_id(), Arrays.asList(m), null, null); + voidIMResult = robotService.addGroupMembers(groupInfo.getTarget_id(), Arrays.asList(m), null, null); if (voidIMResult != null && voidIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("add group member success"); } else { @@ -1011,7 +1012,7 @@ public class Main { System.exit(-1); } - voidIMResult = RobotService.kickoffGroupMembers(groupInfo.getTarget_id(), Arrays.asList("user3"), null, null); + voidIMResult = robotService.kickoffGroupMembers(groupInfo.getTarget_id(), Arrays.asList("user3"), null, null); if (voidIMResult != null && voidIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("kickoff group member success"); } else { @@ -1019,7 +1020,7 @@ public class Main { System.exit(-1); } - voidIMResult = RobotService.setGroupMemberAlias(groupInfo.getTarget_id(), "user3", "test user3", null, null); + voidIMResult = robotService.setGroupMemberAlias(groupInfo.getTarget_id(), "user3", "test user3", null, null); if (voidIMResult != null && voidIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("set group member alias success"); } else { @@ -1028,7 +1029,7 @@ public class Main { } if(commercialServer) { - voidIMResult = RobotService.setGroupManager(groupInfo.getTarget_id(), Arrays.asList("user4", "user5"), true, null, null); + voidIMResult = robotService.setGroupManager(groupInfo.getTarget_id(), Arrays.asList("user4", "user5"), true, null, null); if (voidIMResult != null && voidIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("set group manager success"); } else { @@ -1036,7 +1037,7 @@ public class Main { System.exit(-1); } - voidIMResult = RobotService.setGroupManager(groupInfo.getTarget_id(), Arrays.asList("user4", "user5"), false, null, null); + voidIMResult = robotService.setGroupManager(groupInfo.getTarget_id(), Arrays.asList("user4", "user5"), false, null, null); if (voidIMResult != null && voidIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("cancel group manager success"); } else { @@ -1050,7 +1051,7 @@ public class Main { //仅专业版支持 if (commercialServer) { //开启群成员禁言 - voidIMResult = RobotService.muteGroupMember(groupInfo.getTarget_id(), Arrays.asList("user5"), true, null, null); + voidIMResult = robotService.muteGroupMember(groupInfo.getTarget_id(), Arrays.asList("user5"), true, null, null); if (voidIMResult != null && voidIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("mute group member success"); } else { @@ -1058,7 +1059,7 @@ public class Main { System.exit(-1); } //关闭群成员禁言 - voidIMResult = RobotService.muteGroupMember(groupInfo.getTarget_id(), Arrays.asList("user5"), false, null, null); + voidIMResult = robotService.muteGroupMember(groupInfo.getTarget_id(), Arrays.asList("user5"), false, null, null); if (voidIMResult != null && voidIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("unmute group member success"); } else { @@ -1067,7 +1068,7 @@ public class Main { } //开启群成员白名单,当群全局禁言时,白名单用户可以发言 - voidIMResult = RobotService.allowGroupMember(groupInfo.getTarget_id(), Arrays.asList("user5"), true, null, null); + voidIMResult = robotService.allowGroupMember(groupInfo.getTarget_id(), Arrays.asList("user5"), true, null, null); if (voidIMResult != null && voidIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("allow group member success"); } else { @@ -1076,7 +1077,7 @@ public class Main { } //关闭群成员白名单 - voidIMResult = RobotService.allowGroupMember(groupInfo.getTarget_id(), Arrays.asList("user5"), false, null, null); + voidIMResult = robotService.allowGroupMember(groupInfo.getTarget_id(), Arrays.asList("user5"), false, null, null); if (voidIMResult != null && voidIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("unallow group member success"); } else { @@ -1084,7 +1085,7 @@ public class Main { System.exit(-1); } } - voidIMResult = RobotService.transferGroup(groupInfo.getTarget_id(), "user2", null, null); + voidIMResult = robotService.transferGroup(groupInfo.getTarget_id(), "user2", null, null); if (voidIMResult != null && voidIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("transfer success"); } else { @@ -1092,7 +1093,7 @@ public class Main { System.exit(-1); } - resultGetGroupInfo = RobotService.getGroupInfo(groupInfo.getTarget_id()); + resultGetGroupInfo = robotService.getGroupInfo(groupInfo.getTarget_id()); if (resultGetGroupInfo != null && resultGetGroupInfo.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { if ("user2".equals(resultGetGroupInfo.getResult().getOwner())) { groupInfo.setOwner("user2"); @@ -1105,7 +1106,7 @@ public class Main { System.exit(-1); } - voidIMResult = RobotService.quitGroup(groupInfo.getTarget_id(), null, null); + voidIMResult = robotService.quitGroup(groupInfo.getTarget_id(), null, null); if (voidIMResult != null && voidIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("quit group success"); } else { diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/RobotService.java b/sdk/src/main/java/cn/wildfirechat/sdk/RobotService.java index 335cba0a..668f4baf 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/RobotService.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/RobotService.java @@ -9,64 +9,70 @@ import cn.wildfirechat.sdk.utilities.RobotHttpUtils; import java.util.List; public class RobotService { - public static IMResult sendMessage(String sender, Conversation conversation, MessagePayload payload) throws Exception { + private final RobotHttpUtils robotHttpUtils; + + public RobotService(String url, String robotId, String robotSecret) { + robotHttpUtils = new RobotHttpUtils(url, robotId, robotSecret); + } + + public IMResult sendMessage(String sender, Conversation conversation, MessagePayload payload) throws Exception { String path = APIPath.Robot_Message_Send; SendMessageData messageData = new SendMessageData(); messageData.setSender(sender); messageData.setConv(conversation); messageData.setPayload(payload); - return RobotHttpUtils.httpJsonPost(path, messageData, SendMessageResult.class); + return robotHttpUtils.httpJsonPost(path, messageData, SendMessageResult.class); } - public static IMResult getUserInfo(String userId) throws Exception { + public IMResult getUserInfo(String userId) throws Exception { String path = APIPath.Robot_User_Info; InputGetUserInfo getUserInfo = new InputGetUserInfo(userId, null, null); - return RobotHttpUtils.httpJsonPost(path, getUserInfo, InputOutputUserInfo.class); + return robotHttpUtils.httpJsonPost(path, getUserInfo, InputOutputUserInfo.class); } - public static IMResult getUserInfoByMobile(String phone) throws Exception { + public IMResult getUserInfoByMobile(String phone) throws Exception { String path = APIPath.Robot_User_Info; InputGetUserInfo getUserInfo = new InputGetUserInfo(null, null, phone); - return RobotHttpUtils.httpJsonPost(path, getUserInfo, InputOutputUserInfo.class); + return robotHttpUtils.httpJsonPost(path, getUserInfo, InputOutputUserInfo.class); } - public static IMResult getUserInfoByName(String userName) throws Exception { + public IMResult getUserInfoByName(String userName) throws Exception { String path = APIPath.Robot_User_Info; InputGetUserInfo getUserInfo = new InputGetUserInfo(null, userName, null); - return RobotHttpUtils.httpJsonPost(path, getUserInfo, InputOutputUserInfo.class); + return robotHttpUtils.httpJsonPost(path, getUserInfo, InputOutputUserInfo.class); } - public static IMResult setCallback(String url) throws Exception { + public IMResult setCallback(String url) throws Exception { String path = APIPath.Robot_Set_Callback; RobotCallbackPojo pojo = new RobotCallbackPojo(); pojo.setUrl(url); - return RobotHttpUtils.httpJsonPost(path, pojo, Void.class); + return robotHttpUtils.httpJsonPost(path, pojo, Void.class); } - public static IMResult getCallback() throws Exception { + public IMResult getCallback() throws Exception { String path = APIPath.Robot_Get_Callback; - return RobotHttpUtils.httpJsonPost(path, null, RobotCallbackPojo.class); + return robotHttpUtils.httpJsonPost(path, null, RobotCallbackPojo.class); } - public static IMResult deleteCallback() throws Exception { + public IMResult deleteCallback() throws Exception { String path = APIPath.Robot_Delete_Callback; - return RobotHttpUtils.httpJsonPost(path, null, Void.class); + return robotHttpUtils.httpJsonPost(path, null, Void.class); } - public static IMResult getProfile(String robotId) throws Exception { + public IMResult getProfile(String robotId) throws Exception { return getUserInfo(robotId); } /* type可选范围为MyInfoType,注意不能修改电话号码,如果要修改电话号码请使用adminapi进行修改 */ - public static IMResult updateProfile(int/*MyInfoType*/ type, String value) throws Exception { + public IMResult updateProfile(int/*MyInfoType*/ type, String value) throws Exception { String path = APIPath.Robot_Update_Profile; IntStringPairPojo pojo = new IntStringPairPojo(type, value); - return RobotHttpUtils.httpJsonPost(path, pojo, Void.class); + return robotHttpUtils.httpJsonPost(path, pojo, Void.class); } - public static IMResult createGroup(PojoGroupInfo group_info, List members, List to_lines, MessagePayload notify_message) throws Exception { + public IMResult createGroup(PojoGroupInfo group_info, List members, List to_lines, MessagePayload notify_message) throws Exception { String path = APIPath.Robot_Create_Group; PojoGroup pojoGroup = new PojoGroup(); pojoGroup.setGroup_info(group_info); @@ -76,37 +82,37 @@ public class RobotService { createGroup.setTo_lines(to_lines); createGroup.setNotify_message(notify_message); - return RobotHttpUtils.httpJsonPost(path, createGroup, OutputCreateGroupResult.class); + return robotHttpUtils.httpJsonPost(path, createGroup, OutputCreateGroupResult.class); } - public static IMResult getGroupInfo(String groupId) throws Exception { + public IMResult getGroupInfo(String groupId) throws Exception { String path = APIPath.Robot_Group_Get_Info; InputGetGroup input = new InputGetGroup(); input.setGroupId(groupId); - return RobotHttpUtils.httpJsonPost(path, input, PojoGroupInfo.class); + return robotHttpUtils.httpJsonPost(path, input, PojoGroupInfo.class); } - public static IMResult dismissGroup(String groupId, List to_lines, MessagePayload notify_message) throws Exception { + public IMResult dismissGroup(String groupId, List to_lines, MessagePayload notify_message) throws Exception { String path = APIPath.Robot_Group_Dismiss; InputDismissGroup dismissGroup = new InputDismissGroup(); dismissGroup.setGroup_id(groupId); dismissGroup.setTo_lines(to_lines); dismissGroup.setNotify_message(notify_message); - return RobotHttpUtils.httpJsonPost(path, dismissGroup, Void.class); + return robotHttpUtils.httpJsonPost(path, dismissGroup, Void.class); } - public static IMResult transferGroup(String groupId, String newOwner, List to_lines, MessagePayload notify_message) throws Exception { + public IMResult transferGroup(String groupId, String newOwner, List to_lines, MessagePayload notify_message) throws Exception { String path = APIPath.Robot_Group_Transfer; InputTransferGroup transferGroup = new InputTransferGroup(); transferGroup.setGroup_id(groupId); transferGroup.setNew_owner(newOwner); transferGroup.setTo_lines(to_lines); transferGroup.setNotify_message(notify_message); - return RobotHttpUtils.httpJsonPost(path, transferGroup, Void.class); + return robotHttpUtils.httpJsonPost(path, transferGroup, Void.class); } - public static IMResult modifyGroupInfo(String groupId, /*ModifyGroupInfoType*/int type, String value, List to_lines, MessagePayload notify_message) throws Exception { + public IMResult modifyGroupInfo(String groupId, /*ModifyGroupInfoType*/int type, String value, List to_lines, MessagePayload notify_message) throws Exception { String path = APIPath.Robot_Group_Modify_Info; InputModifyGroupInfo modifyGroupInfo = new InputModifyGroupInfo(); modifyGroupInfo.setGroup_id(groupId); @@ -114,28 +120,28 @@ public class RobotService { modifyGroupInfo.setType(type); modifyGroupInfo.setValue(value); modifyGroupInfo.setNotify_message(notify_message); - return RobotHttpUtils.httpJsonPost(path, modifyGroupInfo, Void.class); + return robotHttpUtils.httpJsonPost(path, modifyGroupInfo, Void.class); } - public static IMResult getGroupMembers(String groupId) throws Exception { + public IMResult getGroupMembers(String groupId) throws Exception { String path = APIPath.Robot_Group_Member_List; InputGetGroup input = new InputGetGroup(); input.setGroupId(groupId); - return RobotHttpUtils.httpJsonPost(path, input, OutputGroupMemberList.class); + return robotHttpUtils.httpJsonPost(path, input, OutputGroupMemberList.class); } - public static IMResult addGroupMembers(String groupId, List groupMembers, List to_lines, MessagePayload notify_message) throws Exception { + public IMResult addGroupMembers(String groupId, List groupMembers, List to_lines, MessagePayload notify_message) throws Exception { String path = APIPath.Robot_Group_Member_Add; InputAddGroupMember addGroupMember = new InputAddGroupMember(); addGroupMember.setGroup_id(groupId); addGroupMember.setMembers(groupMembers); addGroupMember.setTo_lines(to_lines); addGroupMember.setNotify_message(notify_message); - return RobotHttpUtils.httpJsonPost(path, addGroupMember, Void.class); + return robotHttpUtils.httpJsonPost(path, addGroupMember, Void.class); } - public static IMResult setGroupManager(String groupId, List groupMemberIds, boolean isManager, List to_lines, MessagePayload notify_message) throws Exception { + public IMResult setGroupManager(String groupId, List groupMemberIds, boolean isManager, List to_lines, MessagePayload notify_message) throws Exception { String path = APIPath.Robot_Group_Set_Manager; InputSetGroupManager addGroupMember = new InputSetGroupManager(); addGroupMember.setGroup_id(groupId); @@ -143,10 +149,10 @@ public class RobotService { addGroupMember.setIs_manager(isManager); addGroupMember.setTo_lines(to_lines); addGroupMember.setNotify_message(notify_message); - return RobotHttpUtils.httpJsonPost(path, addGroupMember, Void.class); + return robotHttpUtils.httpJsonPost(path, addGroupMember, Void.class); } - public static IMResult muteGroupMember(String groupId, List groupMemberIds, boolean isMute, List to_lines, MessagePayload notify_message) throws Exception { + public IMResult muteGroupMember(String groupId, List groupMemberIds, boolean isMute, List to_lines, MessagePayload notify_message) throws Exception { String path = APIPath.Robot_Group_Mute_Member; InputMuteGroupMember addGroupMember = new InputMuteGroupMember(); addGroupMember.setGroup_id(groupId); @@ -154,10 +160,10 @@ public class RobotService { addGroupMember.setIs_manager(isMute); addGroupMember.setTo_lines(to_lines); addGroupMember.setNotify_message(notify_message); - return RobotHttpUtils.httpJsonPost(path, addGroupMember, Void.class); + return robotHttpUtils.httpJsonPost(path, addGroupMember, Void.class); } - public static IMResult allowGroupMember(String groupId, List groupMemberIds, boolean isAllow, List to_lines, MessagePayload notify_message) throws Exception { + public IMResult allowGroupMember(String groupId, List groupMemberIds, boolean isAllow, List to_lines, MessagePayload notify_message) throws Exception { String path = APIPath.Robot_Group_Allow_Member; InputMuteGroupMember addGroupMember = new InputMuteGroupMember(); addGroupMember.setGroup_id(groupId); @@ -165,29 +171,29 @@ public class RobotService { addGroupMember.setIs_manager(isAllow); addGroupMember.setTo_lines(to_lines); addGroupMember.setNotify_message(notify_message); - return RobotHttpUtils.httpJsonPost(path, addGroupMember, Void.class); + return robotHttpUtils.httpJsonPost(path, addGroupMember, Void.class); } - public static IMResult kickoffGroupMembers(String groupId, List groupMemberIds, List to_lines, MessagePayload notify_message) throws Exception { + public IMResult kickoffGroupMembers(String groupId, List groupMemberIds, List to_lines, MessagePayload notify_message) throws Exception { String path = APIPath.Robot_Group_Member_Kickoff; InputKickoffGroupMember kickoffGroupMember = new InputKickoffGroupMember(); kickoffGroupMember.setGroup_id(groupId); kickoffGroupMember.setMembers(groupMemberIds); kickoffGroupMember.setTo_lines(to_lines); kickoffGroupMember.setNotify_message(notify_message); - return RobotHttpUtils.httpJsonPost(path, kickoffGroupMember, Void.class); + return robotHttpUtils.httpJsonPost(path, kickoffGroupMember, Void.class); } - public static IMResult quitGroup(String groupId, List to_lines, MessagePayload notify_message) throws Exception { + public IMResult quitGroup(String groupId, List to_lines, MessagePayload notify_message) throws Exception { String path = APIPath.Robot_Group_Member_Quit; InputQuitGroup quitGroup = new InputQuitGroup(); quitGroup.setGroup_id(groupId); quitGroup.setTo_lines(to_lines); quitGroup.setNotify_message(notify_message); - return RobotHttpUtils.httpJsonPost(path, quitGroup, Void.class); + return robotHttpUtils.httpJsonPost(path, quitGroup, Void.class); } - public static IMResult setGroupMemberAlias(String groupId, String memberId, String alias, List to_lines, MessagePayload notify_message) throws Exception { + public IMResult setGroupMemberAlias(String groupId, String memberId, String alias, List to_lines, MessagePayload notify_message) throws Exception { String path = APIPath.Robot_Group_Set_Member_Alias; InputSetGroupMemberAlias input = new InputSetGroupMemberAlias(); input.setGroup_id(groupId); @@ -195,6 +201,6 @@ public class RobotService { input.setAlias(alias); input.setTo_lines(to_lines); input.setNotify_message(notify_message); - return RobotHttpUtils.httpJsonPost(path, input, Void.class); + return robotHttpUtils.httpJsonPost(path, input, Void.class); } } diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/utilities/RobotHttpUtils.java b/sdk/src/main/java/cn/wildfirechat/sdk/utilities/RobotHttpUtils.java index f73271b9..947def46 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/utilities/RobotHttpUtils.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/utilities/RobotHttpUtils.java @@ -23,23 +23,24 @@ import java.nio.charset.Charset; public class RobotHttpUtils { private static final Logger LOG = LoggerFactory.getLogger(RobotHttpUtils.class); - private static String url; - private static String robotId; - private static String robotSecret; + private final String url; + private final String robotId; + private final String robotSecret; - public static void init(String url, String robotId, String secret) { - RobotHttpUtils.url = url; - RobotHttpUtils.robotId = robotId; - RobotHttpUtils.robotSecret = secret; + public RobotHttpUtils(String url, String robotId, String robotSecret) { + this.url = url; + this.robotId = robotId; + this.robotSecret = robotSecret; } - public static IMResult httpJsonPost(String path, Object object, Class clazz) throws Exception{ + + public IMResult httpJsonPost(String path, Object object, Class clazz) throws Exception{ if (isNullOrEmpty(url) || isNullOrEmpty(path)) { LOG.error("Not init IM SDK correctly. Do you forget init it?"); throw new Exception("SDK url or secret lack!"); } - String url = RobotHttpUtils.url + path; + String url = this.url + path; HttpPost post = null; try { HttpClient httpClient = HttpClientBuilder.create().build();