快速接入
Last updated
Last updated
在安装和使用保利威互动学堂Java SDK功能前,确保B端已经具备如下环境:
安装Java环境。
保利威互动学堂Java SDK支持1.8或更高版本。
将如下依赖加入到项目的POM文件中:
注意:受 Maven 中央仓库网络限制,阿里云云效 Maven 中央代理仓库可能会出现依赖查找不到的情况,建议使用中央仓库:https://repo1.maven.org/maven2/
<dependency>
<groupId>net.polyv</groupId>
<artifactId>polyv-java-vclass-sdk</artifactId>
<version>2.1.9</version>
</dependency>
注意:为了消除Java的冗长代码 ,SDK使用了jdk1.8的最新特性lombok,请在IntelliJ或者Eclipse 中配置对lombok的支持
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
在执行测试代码之前,需要B端先初始化系统配置,包括UserId \ AppId \ AppSecret , 如没有以上信息,请参考本文前提条件部分获取,初始化示例代码如下,任选一种:
/**
* 初始化配置,请配置自己的账号信息
*/
public static void initPolyvLive(){
String userId = "xxx";
String appId = "xxx";
String appSecret = "xxx";
VClassGlobalConfig.init(appId, userId, appSecret);
log.debug("--初始化完成--");
}
/**
* 初始化配置并初始化 HTTP CLIENT 连接池超时时间和最大连接数配置,请配置自己的账号信息
*/
public static void initPolyvLive(){
String userId = "xxx";
String appId = "xxx";
String appSecret = "xxx";
Integer timeOut = 20000; //HTTP CLIENT 连接池超时时间
Integer maxClientNum = 100; //HTTP CLIENT 最大连接数
VClassGlobalConfig.init( appId, userId, appSecret, timeOut , maxClientNum);
log.debug("--初始化完成--");
}
以上代码一般配置于随系统启动执行一次的全局初始化中,如用spring框架,可以参考如下:
package net.polyv.live.config;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
/**
* 默认启动配置类
* @author: thomas
**/
@Slf4j
@Component
public class StartupListener implements ApplicationContextAware {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
String userId = "xxx";
String appId = "xxx";
String appSecret = "xxx";
VClassGlobalConfig.init(appId,userId,appSecret);
log.debug("--初始化完成--");
}
}
测试创建一个课节,单元测试代码如下:
import java.text.SimpleDateFormat;
import org.junit.Assert;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import net.polyv.common.v1.exception.PloyvSdkException;
import net.polyv.vclass.v1.config.VClassGlobalConfig;
import net.polyv.vclass.v1.constant.VClassConstant;
import net.polyv.vclass.v1.entity.lesson.VClassAddLessonRequest;
import net.polyv.vclass.v1.entity.lesson.VClassAddLessonResponse;
import net.polyv.vclass.v1.service.lesson.impl.VClassLessonServiceImpl;
@Slf4j
public class VClassLessonDemo {
/**
* 调用demo,必须处理PloyvSdkException。
* <p>
* 参数合法性校验:SDK采用自定义验证框架对输入参数进行校验,如有参数不合格,将抛出PloyvSdkException异常,exception的message
* 包括具体校验不通过的字段信息,此异常是运行时异常,必须捕获处理相关业务逻辑;
* <p>
* 解析返回数据:解析返回数据,如SDK调用正常成功,将封装响应对象,正常返回,如服务器返回错误信息,SDK将将抛出PloyvSdkException异常,exception的message
* 包括具体服务器执行错误信息,此异常是运行时异常,必须捕获处理相关业务逻辑;
* @param args
*/
public static void main(String[] args) {
//全局初始化,此处应该全局执行一次
String userId = "xxx";
String appId = "xxx";
String appSecret = "xxx";
VClassGlobalConfig.init(appId, userId, appSecret);
log.debug("--初始化完成--");
try {
//封装API请求对象
VClassAddLessonRequest vClassAddLessonRequest = VClassAddLessonRequest.builder()
.name("测试创建互动学堂课节")
.startTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2022-02-28 16:00:00"))
.duration(30)
.linkNumber(5)
.watchCondition(VClassConstant.AuthType.NULL.getCode())
.build();
//调用SDK请求保利威服务器
VClassAddLessonResponse vClassAddLessonResponse = new VClassLessonServiceImpl().addLesson(
vClassAddLessonRequest);
Assert.assertNotNull(vClassAddLessonResponse);
//正常返回做B端正常的业务逻辑
if (vClassAddLessonResponse != null) {
//to do something ......
log.debug("课节创建成功 {}", JSON.toJSONString(vClassAddLessonResponse));
log.debug(
"老师上课地址:https://s.vclass.com/live-web-hi-class/teacher/login?lessonId={}, 老师登录账号:{}, 老师登录密码:{}",
vClassAddLessonResponse.getLessonId(), vClassAddLessonResponse.getTeacherMobile(),
vClassAddLessonResponse.getTeacherPasswd());
log.debug("学生上课地址:https://s.vclass.com/live-web-hi-class/student/login?lessonId={}",
vClassAddLessonResponse.getLessonId());
}
} catch (PloyvSdkException e) {
//参数校验不合格 或者 请求服务器端500错误,错误信息见PloyvSdkException.getMessage()
log.error(e.getMessage(), e);
// 异常返回做B端异常的业务逻辑,记录log 或者 上报到ETL 或者回滚事务
} catch (Exception e) {
log.error("SDK调用异常", e);
}
}
}
执行代码后,控制台应有如下关键输出,表示整合完成:
[main] DEBUG net.polyv.common.v1.base.HttpUtil - http 请求 url: https://api.polyv.net/hi-class-api/open/lesson/v1/add?requestId=61b9ac58d3114c2683fd4c5342040c43&appId=frlr1zazn3&sign=3DF2B54E330650B4A75F5E157606EC99×tamp=1644999444305 , 请求参数: {"appId":"frlr1zazn3","duration":30,"linkNumber":5,"name":"测试创建互动学堂课节","requestId":"61b9ac58d3114c2683fd4c5342040c43","sign":"3DF2B54E330650B4A75F5E157606EC99","startTime":1646035200000,"timestamp":"1644999444305","watchCondition":"NULL"}
[main] DEBUG net.polyv.common.v1.base.HttpUtil - http 请求结果: {"code":200,"status":"success","requestId":"58d56000408d4b9792ac93d1412020a1.91.16449994455159077","data":{"lessonId":15551,"lessonCode":"srs04332","name":"测试创建互动学堂课节","status":0,"startTime":1646035200000,"endTime":1646037000000,"duration":30,"watchCondition":"NULL","secretKey":"wpo5tyus97","cover":"//s1.videocc.net/default-img/small-class/default-cover.png","teacherId":"g73qb9mfz5","teacherName":"张老师","courseCount":0,"webStartUrl":"https://s.vclass.com/live-web-hi-class/teacher/login?lessonId=15551","watchStartUrl":"https://s.vclass.com/live-web-hi-class/student/login?lessonId=15551","aloneWatch":"Y","cataId":0,"teacherCode":"+86","teacherMobile":"12000002368","teacherPasswd":"pV6VW7","linkNumber":5,"autoConnectMicroEnabled":"N","autoRecordCourseEnabled":"N","autoAllVisible":"Y","resolution":0,"playResolution":360},"success":true}
[main] DEBUG cn.timelost.spring.VClassLessonDemo - 课节创建成功 {"autoConnectMicroEnabled":"N","autoRecordCourseEnabled":"N","cover":"//s1.videocc.net/default-img/small-class/default-cover.png","duration":30,"endTime":1646037000000,"lessonId":15551,"linkNumber":5,"name":"测试创建互动学堂课节","playResolution":360,"resolution":0,"secretKey":"wpo5tyus97","startTime":1646035200000,"teacherCode":"+86","teacherId":"g73qb9mfz5","teacherMobile":"12000002368","teacherName":"张老师","teacherPasswd":"pV6VW7","watchCondition":"NULL"}
还可以官网登录验证创建是否成功;
至此,B端已经完成保利威互动学堂Java SDK安装配置,可以使用互动学堂SDK进行其他功能开发和测试,如B端接入过程有任何问题,直接使用右下角在线客服找到售后技术支持提问 ,请将问题的运行环境、操作步骤、错误反馈信息、联系方式同步反馈,便于问题的快速定位和解决。