没有求知欲的学生,就像没有翅膀的鸟。——萨迪
代码如下:
代码语言:javascript复制package com.example.agora.api.webclient;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.example.agora.api.constant.agora.RtcRequestConst;
import com.example.agora.api.pojo.dto.RtcAddKickRuleDTO;
import com.example.agora.api.pojo.dto.RtcDelKickRuleDTO;
import com.example.agora.api.pojo.dto.RtcUpdateKickRuleDTO;
import com.example.agora.api.pojo.vo.rtc.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import java.util.Base64;
@Service
public class AgoraRtcClient {
private final WebClient webClient;
@Value("${agora.rtc.app-id}")
private String appId;
@Value("${agora.rtc.api-key}")
private String apiKey;
@Value("${agora.rtc.api-secret}")
private String apiSecret;
@Autowired
public AgoraRtcClient(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("http://api.sd-rtn.com").build();
}
private String getAuthorization() {
// 拼接客户 ID 和客户密钥并使用 base64 编码
String plainCredentials = apiKey ":" apiSecret;
String base64Credentials = new String(Base64.getEncoder().encode(plainCredentials.getBytes()));
// 创建 authorization header
return "Basic " base64Credentials;
}
/**
* 封禁用户权限-创建规则
* <a href="https://docportal.shengwang.cn/cn/All/rtc_channel_management_restfulapi?platform=Android#封禁用户权限">
*/
public Mono<RtcQueryKickRuleRes> getRtcKickRule() {
return webClient.get().uri("/dev/v1/kicking-rule?appid={appId}", appId)
.header(RtcRequestConst.AUTHORIZATION_KEY, getAuthorization())
.retrieve().bodyToMono(String.class)
.map(str -> JacksonUtils.toObj(str, new TypeReference<>() {
}));
}
/**
* 封禁用户权限-获取规则列表
* <a href="https://docportal.shengwang.cn/cn/All/rtc_channel_management_restfulapi?platform=Android#获取规则列表">
*/
public Mono<RtcAddDelKickRuleRes> addRtcKickRule(RtcAddKickRuleDTO dto) {
dto.setAppId(appId);
return webClient.post().uri("/dev/v1/kicking-rule")
.header(RtcRequestConst.AUTHORIZATION_KEY, getAuthorization())
.bodyValue(dto).retrieve().bodyToMono(String.class)
.map(str -> JacksonUtils.toObj(str, new TypeReference<>() {
}));
}
/**
* 封禁用户权限-更新规则
* <a href="https://docportal.shengwang.cn/cn/All/rtc_channel_management_restfulapi?platform=Android#更新规则">
*/
public Mono<RtcAddDelKickRuleRes> delRtcKickRule(RtcDelKickRuleDTO dto) {
dto.setAppId(appId);
return webClient.method(HttpMethod.DELETE).uri("/dev/v1/kicking-rule")
.header(RtcRequestConst.AUTHORIZATION_KEY, getAuthorization())
.bodyValue(dto).retrieve().bodyToMono(String.class)
.map(str -> JacksonUtils.toObj(str, new TypeReference<>() {
}));
}
/**
* 封禁用户权限-删除规则
* <a href="https://docportal.shengwang.cn/cn/All/rtc_channel_management_restfulapi?platform=Android#删除规则">
*/
public Mono<RtcAddDelKickRuleRes> updateRtcKickRule(RtcUpdateKickRuleDTO dto) {
dto.setAppId(appId);
return webClient.put().uri("/dev/v1/kicking-rule")
.header(RtcRequestConst.AUTHORIZATION_KEY, getAuthorization())
.bodyValue(dto).retrieve().bodyToMono(String.class)
.map(str -> JacksonUtils.toObj(str, new TypeReference<>() {
}));
}
/**
* 查询在线频道信息-查询用户状态
* <a href="https://docportal.shengwang.cn/cn/All/rtc_channel_management_restfulapi?platform=Android#查询用户状态">
*/
public Mono<RtcUserInfoRes> getRtcUserInfo(Long uid, String channelName) {
return webClient.get().uri("/dev/v1/channel/user/property/{appid}/{uid}/{channelName}", appId, uid, channelName)
.header(RtcRequestConst.AUTHORIZATION_KEY, getAuthorization())
.retrieve().bodyToMono(String.class)
.map(str -> JacksonUtils.toObj(str, new TypeReference<>() {
}));
}
/**
* 查询在线频道信息-查询用户列表
* <a href="https://docportal.shengwang.cn/cn/All/rtc_channel_management_restfulapi?platform=Android#查询用户列表">
*/
public Mono<RtcUserInfosRes> getRtcUserInfos(String channelName, boolean hostsOnly) {
return webClient.get().uri("/dev/v1/channel/user/{appid}/{channelName}" (hostsOnly ? "/hosts_only" : ""),
appId, channelName)
.header(RtcRequestConst.AUTHORIZATION_KEY, getAuthorization())
.retrieve().bodyToMono(String.class)
.map(str -> JacksonUtils.toObj(str, new TypeReference<>() {
}));
}
/**
* 查询在线频道信息-查询项目的频道列表
* <a href="https://docportal.shengwang.cn/cn/All/rtc_channel_management_restfulapi?platform=Android#查询项目的频道列表">
*/
public Mono<RtcChannelInfosRes> getRtcChannelInfos() {
return webClient.get().uri("/dev/v1/channel/{appid}", appId)
.header(RtcRequestConst.AUTHORIZATION_KEY, getAuthorization())
.retrieve().bodyToMono(String.class)
.map(str -> JacksonUtils.toObj(str, new TypeReference<>() {
}));
}
}
使用的是webclient
实现反应式请求,注意响应判断请求是否成功也分为两种
package com.example.agora.api.pojo.vo.rtc;
/**
* BaseRtcRes
*
* @author VampireAchao<achao @ hutool.cn>
* @since 2023/11/14
*/
public interface BaseRtcRes {
boolean isSuccess();
}
第一种是用status
为success
判断
package com.example.agora.api.pojo.vo.rtc;
import com.example.agora.api.constant.agora.RtcResponseConst;
import lombok.Data;
/**
* BaseRtcStatusRes
*
* @author VampireAchao<achao @ hutool.cn>
* @since 2023/11/13
*/
@Data
public abstract class BaseRtcStatusRes implements BaseRtcRes {
private String status;
@Override
public boolean isSuccess() {
return RtcResponseConst.SUCCESS.equals(status);
}
}
第二种是拿success
为true
判断
package com.example.agora.api.pojo.vo.rtc;
import lombok.Data;
import org.dromara.streamquery.stream.core.variable.BoolHelper;
/**
* BaseRtcStatusRes
*
* @author VampireAchao<achao @ hutool.cn>
* @since 2023/11/13
*/
@Data
public abstract class BaseRtcSuccessRes implements BaseRtcRes {
private Boolean success;
@Override
public boolean isSuccess() {
return BoolHelper.isTruthy(success);
}
}
其他的实体类就建议自行创建吧