添加频道sdk

This commit is contained in:
heavyrain.lee
2020-01-04 17:04:43 +08:00
parent 998f01a584
commit e4da3780f5
11 changed files with 327 additions and 15 deletions
@@ -0,0 +1,65 @@
package cn.wildfirechat.sdk;
import cn.wildfirechat.common.APIPath;
import cn.wildfirechat.pojos.*;
import cn.wildfirechat.proto.ProtoConstants;
import cn.wildfirechat.sdk.model.IMResult;
import cn.wildfirechat.sdk.utilities.AdminHttpUtils;
import cn.wildfirechat.sdk.utilities.ChannelHttpUtils;
import cn.wildfirechat.sdk.utilities.RobotHttpUtils;
import java.util.List;
//仅专业版支持,社区版不支持
public class ChannelServiceApi {
private ChannelHttpUtils httpUtils;
public ChannelServiceApi(String imurl, String channelId, String secret) {
httpUtils = new ChannelHttpUtils(imurl, channelId, secret);
}
public IMResult<InputOutputUserInfo> getUserInfo(String userId) throws Exception {
String path = APIPath.Channel_User_Info;
InputGetUserInfo getUserInfo = new InputGetUserInfo(userId, null, null);
return httpUtils.httpJsonPost(path, getUserInfo, InputOutputUserInfo.class);
}
public IMResult<Void> modifyChannelInfo(/*ProtoConstants.ModifyChannelInfoType*/int type, String value) throws Exception {
String path = APIPath.Channel_Update_Profile;
InputModifyChannelInfo modifyChannelInfo = new InputModifyChannelInfo();
modifyChannelInfo.setType(type);
modifyChannelInfo.setValue(value);
return httpUtils.httpJsonPost(path, modifyChannelInfo, Void.class);
}
public IMResult<SendMessageResult> sendMessage(int line, List<String> targets, MessagePayload payload) throws Exception {
String path = APIPath.Channel_Message_Send;
SendChannelMessageData messageData = new SendChannelMessageData();
messageData.setLine(0);
messageData.setTargets(targets);
messageData.setPayload(payload);
return httpUtils.httpJsonPost(path, messageData, SendMessageResult.class);
}
public IMResult<Void> subscribe(List<String> targets) throws Exception {
String path = APIPath.Channel_Subscribe;
InputChannelSubscribe input = new InputChannelSubscribe();
input.setTargets(targets);
input.setSubscribe(1);
return httpUtils.httpJsonPost(path, input, Void.class);
}
public IMResult<Void> unsubscribe(List<String> targets) throws Exception {
String path = APIPath.Channel_Unsubscribe;
InputChannelSubscribe input = new InputChannelSubscribe();
input.setTargets(targets);
input.setSubscribe(0);
return httpUtils.httpJsonPost(path, input, Void.class);
}
public IMResult<OutputStringList> getSubscriberList() throws Exception {
String path = APIPath.Channel_Subscriber_List;
return httpUtils.httpJsonPost(path, null, OutputStringList.class);
}
}
@@ -10,6 +10,7 @@ import cn.wildfirechat.sdk.utilities.RobotHttpUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import static cn.wildfirechat.proto.ProtoConstants.SystemSettingType.Group_Max_Member_Count;
@@ -455,6 +456,15 @@ public class Main {
}
voidIMResult = UserAdmin.destroyUser("user11");
if (voidIMResult != null && voidIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("destroy user success");
} else {
System.out.println("destroy user failure");
System.exit(-1);
}
//初始化机器人API
RobotHttpUtils.init("http://localhost", "robot1", "123456");
//***********************************************
@@ -483,8 +493,87 @@ public class Main {
System.exit(-1);
}
//测试频道API功能,仅专业版支持
//1. 先创建频道
inputCreateChannel = new InputCreateChannel();
inputCreateChannel.setName("testChannel");
inputCreateChannel.setOwner("user1");
resultCreateChannel = GeneralAdmin.createChannel(inputCreateChannel);
if (resultCreateChannel != null && resultCreateChannel.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("success");
} else {
System.out.println("create channel failure");
System.exit(-1);
}
voidIMResult = UserAdmin.destroyUser("user1");
//2. 初始化api
ChannelServiceApi channelServiceApi = new ChannelServiceApi("http://localhost", inputCreateChannel.getTargetId(), inputCreateChannel.getSecret());
//3. 测试channel api功能
resultVoid = channelServiceApi.subscribe(Arrays.asList("user2","user3"));
if (resultVoid != null && resultVoid.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("block user done");
} else {
System.out.println("block user failure");
System.exit(-1);
}
IMResult<OutputStringList> resultStringList = channelServiceApi.getSubscriberList();
if (resultStringList != null && resultStringList.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS && resultStringList.getResult().getList().contains("user2") && resultStringList.getResult().getList().contains("user3")) {
System.out.println("block user done");
} else {
System.out.println("block user failure");
System.exit(-1);
}
resultVoid = channelServiceApi.unsubscribe(Arrays.asList("user2"));
if (resultVoid != null && resultVoid.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("block user done");
} else {
System.out.println("block user failure");
System.exit(-1);
}
resultStringList = channelServiceApi.getSubscriberList();
if (resultStringList != null && resultStringList.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS && resultStringList.getResult().getList().contains("user2") && !resultStringList.getResult().getList().contains("user3")) {
System.out.println("block user done");
} else {
System.out.println("block user failure");
System.exit(-1);
}
resultGetUserInfo1 = channelServiceApi.getUserInfo("user2");
if (resultGetUserInfo1 != null && resultGetUserInfo1.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("get user info success");
} else {
System.out.println("get user info by name failure");
System.exit(-1);
}
payload = new MessagePayload();
payload.setType(1);
payload.setSearchableContent("hello world");
resultSendMessage = channelServiceApi.sendMessage(0, null,payload);
if (resultSendMessage != null && resultSendMessage.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("send message success");
} else {
System.out.println("send message failure");
System.exit(-1);
}
payload.setSearchableContent("hello to user2");
resultSendMessage = channelServiceApi.sendMessage(0, Arrays.asList("user2"),payload);
if (resultSendMessage != null && resultSendMessage.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("send message success");
} else {
System.out.println("send message failure");
System.exit(-1);
}
voidIMResult = channelServiceApi.modifyChannelInfo(ProtoConstants.ModifyChannelInfoType.Modify_Channel_Desc, "this is a test channel, update at:" + new Date().toString());
if (voidIMResult != null && voidIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("destroy user success");
} else {
@@ -22,23 +22,23 @@ import java.nio.charset.Charset;
public class ChannelHttpUtils {
private static final Logger LOG = LoggerFactory.getLogger(ChannelHttpUtils.class);
private static String url;
private static String channelId;
private static String channelSecret;
private String imurl;
private String channelId;
private String channelSecret;
public static void init(String url, String channelId, String secret) {
ChannelHttpUtils.url = url;
ChannelHttpUtils.channelId = channelId;
ChannelHttpUtils.channelSecret = secret;
public ChannelHttpUtils(String imurl, String channelId, String secret) {
this.imurl = imurl;
this.channelId = channelId;
this.channelSecret = secret;
}
public static <T> IMResult<T> httpJsonPost(String path, Object object, Class<T> clazz) throws Exception{
if (isNullOrEmpty(url) || isNullOrEmpty(path)) {
public <T> IMResult<T> httpJsonPost(String path, Object object, Class<T> clazz) throws Exception{
if (isNullOrEmpty(imurl) || isNullOrEmpty(path)) {
LOG.error("Not init IM SDK correctly. Do you forget init it?");
throw new Exception("SDK url or secret lack!");
}
String url = ChannelHttpUtils.url + path;
String url = imurl + path;
HttpPost post = null;
try {
HttpClient httpClient = HttpClientBuilder.create().build();
@@ -54,8 +54,10 @@ public class ChannelHttpUtils {
post.setHeader("Connection", "Keep-Alive");
post.setHeader("nonce", nonce + "");
post.setHeader("timestamp", "" + timestamp);
post.setHeader("cid", channelId);
post.setHeader("sign", sign);
String jsonStr = null;
if (object != null) {
jsonStr = new Gson().toJson(object);