没有求知欲的学生,就像没有翅膀的鸟。——萨迪
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| 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() { String plainCredentials = apiKey + ":" + apiSecret; String base64Credentials = new String(Base64.getEncoder().encode(plainCredentials.getBytes())); return "Basic " + base64Credentials; }
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<>() { })); }
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<>() { })); }
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<>() { })); }
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<>() { })); }
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<>() { })); }
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<>() { })); }
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
实现反应式请求,注意响应判断请求是否成功也分为两种
1 2 3 4 5 6 7 8 9 10 11
| package com.example.agora.api.pojo.vo.rtc;
public interface BaseRtcRes { boolean isSuccess(); }
|
第一种是用status
为success
判断
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.example.agora.api.pojo.vo.rtc;
import com.example.agora.api.constant.agora.RtcResponseConst; import lombok.Data;
@Data public abstract class BaseRtcStatusRes implements BaseRtcRes { private String status;
@Override public boolean isSuccess() { return RtcResponseConst.SUCCESS.equals(status); } }
|
第二种是拿success
为true
判断
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.example.agora.api.pojo.vo.rtc;
import lombok.Data; import org.dromara.streamquery.stream.core.variable.BoolHelper;
@Data public abstract class BaseRtcSuccessRes implements BaseRtcRes { private Boolean success;
@Override public boolean isSuccess() { return BoolHelper.isTruthy(success); } }
|
其他的实体类就建议自行创建吧