前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >对接声网rtc-restful-api

对接声网rtc-restful-api

作者头像
阿超
发布2023-11-18 16:06:00
1420
发布2023-11-18 16:06:00
举报
文章被收录于专栏:快乐阿超快乐阿超

没有求知欲的学生,就像没有翅膀的鸟。——萨迪

代码如下:

代码语言: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#%E5%B0%81%E7%A6%81%E7%94%A8%E6%88%B7%E6%9D%83%E9%99%90">
     */
    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#%E8%8E%B7%E5%8F%96%E8%A7%84%E5%88%99%E5%88%97%E8%A1%A8">
     */
    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#%E6%9B%B4%E6%96%B0%E8%A7%84%E5%88%99">
     */
    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#%E5%88%A0%E9%99%A4%E8%A7%84%E5%88%99">
     */
    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#%E6%9F%A5%E8%AF%A2%E7%94%A8%E6%88%B7%E7%8A%B6%E6%80%81">
     */
    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#%E6%9F%A5%E8%AF%A2%E7%94%A8%E6%88%B7%E5%88%97%E8%A1%A8">
     */
    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#%E6%9F%A5%E8%AF%A2%E9%A1%B9%E7%9B%AE%E7%9A%84%E9%A2%91%E9%81%93%E5%88%97%E8%A1%A8">
     */
    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实现反应式请求,注意响应判断请求是否成功也分为两种

代码语言:javascript
复制
package com.example.agora.api.pojo.vo.rtc;

/**
 * BaseRtcRes
 *
 * @author VampireAchao<achao @ hutool.cn>
 * @since 2023/11/14
 */
public interface BaseRtcRes {
    boolean isSuccess();
}

第一种是用statussuccess判断

代码语言:javascript
复制
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);
    }
}

第二种是拿successtrue判断

代码语言:javascript
复制
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);
    }
}

其他的实体类就建议自行创建吧

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-11-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档