The Video Platform provides Organization's industry standard APIs that allow for integration with third-party applications. This guide provides an example of the API Code used to manage Courses & Groups in the Video Platform.
package test;
import static org.testng.Assert.assertTrue;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import data.ApiGroupInfo;
import data.ApiUserInfo;
import data.TestConstants;
import serializer.ApiGroupInfoDeserializer;
import serializer.ApiGroupInfoSerializer;
import serializer.ApiUserInfoDeserializer;
import serializer.ApiUserInfoSerializer;
import util.ApiResponse;
import util.HttpHelper;
public class GroupsApiTest {
private static final String API_ENDPOINT = TestConstants.API_ENDPOINT;
private static final Logger logger = Logger.getLogger(GroupsApiTest.class.getName());
private static final Type GSON_USER_INFO_SERIALIZATION_TYPE = new TypeToken<List<ApiUserInfo>>() {
}.getType();
private static final Type GSON_SINGLE_USER_SERIALIZATION_TYPE = new TypeToken<ApiUserInfo>() {
}.getType();
private static final Type GSON_GROUP_INFO_SERIALIZATION_TYPE = new TypeToken<List<ApiGroupInfo>>() {
}.getType();
private static final Type GSON_SINGLE_GROUP_SERIALIZATION_TYPE = new TypeToken<ApiGroupInfo>() {
}.getType();
private Gson gson;
private Gson gsonBuild;
private Gson groupGson;
private Gson groupGsonBuilder;
private HttpHelper helper;
@BeforeClass
public void setUpBeforeClass() {
gson = new GsonBuilder().registerTypeAdapter(ApiUserInfo.class, new ApiUserInfoDeserializer()).create();
gsonBuild = new GsonBuilder().registerTypeAdapter(ApiUserInfo.class, new ApiUserInfoSerializer()).create();
groupGson = new GsonBuilder().registerTypeAdapter(ApiGroupInfo.class, new ApiGroupInfoDeserializer()).create();
groupGsonBuilder = new GsonBuilder().registerTypeAdapter(ApiGroupInfo.class, new ApiGroupInfoSerializer()).create();
helper = new HttpHelper();
}
@Test
public void CreateGroupUsingAPI() {
String curTime = Long.toString(System.currentTimeMillis());
//Create group info
ApiGroupInfo newGroupInfo = new ApiGroupInfo();
newGroupInfo.setOwnerId(TestConstants.GROUP_USER_PID);
newGroupInfo.setName("API Group: " + new Date());
newGroupInfo.setCode("API 101");
newGroupInfo.setTerm("Summer 2999");
newGroupInfo.setCustomName("z" + curTime + "_customName");
newGroupInfo.setType("ADMIN");
// create json
StringEntity customJSON = new StringEntity(groupGsonBuilder.toJson(newGroupInfo), ContentType.APPLICATION_FORM_URLENCODED);
// POST request
ApiResponse response = helper.doApiCallPost("https://" + API_ENDPOINT + "/services/groups", customJSON);
logger.info("Create group response: " + response.getResponseString());
}
@Test
public void DeleteGroupUsingAPI() {
//GET request
ApiResponse response = helper.doApiCallGet("https://" + API_ENDPOINT + "/services/groups");
assertTrue(response.isSuccess(), "Request not successful! Status: " + response.getStatusCode() + ", Message: " + response.getStatusReason());
// Parse json data if successful
List<ApiGroupInfo> groupInfo = groupGson.fromJson(response.getResponseString(), GSON_GROUP_INFO_SERIALIZATION_TYPE);
for(int i = 0; i < groupInfo.size(); i++) {
if(groupInfo.get(i).getCode().equals("API 101")) {
response = helper.doApiCallDelete("https://" + API_ENDPOINT + "/services/groups/" + groupInfo.get(i).getGroupId());
logger.info("Deleted group with code API 101: " + response.getResponseString());
}
}
}
@Test
public void AddUserToGroup() {
//GET request
ApiResponse response = helper.doApiCallGet("https://" + API_ENDPOINT + "/services/groups");
assertTrue(response.isSuccess(), "Request not successful! Status: " + response.getStatusCode() + ", Message: " + response.getStatusReason());
// Parse json data if successful
List<ApiGroupInfo> groupInfo = groupGson.fromJson(response.getResponseString(), GSON_GROUP_INFO_SERIALIZATION_TYPE);
for(int i = 0; i < groupInfo.size(); i++) {
if(groupInfo.get(i).getCode().equals("API 101")) {
logger.info("Found course with code API 101, adding test user to course");
response = helper.doApiCallPost("https://" + API_ENDPOINT + "/services/groups/" + groupInfo.get(i).getGroupId() + "/members/add/" + TestConstants.GROUP_TEST_ADD_USER_PID);
logger.info("Added test user: " + response.getResponseString());
}
}
}
@Test
public void RemoveUserFromGroup() {
//GET request
ApiResponse response = helper.doApiCallGet("https://" + API_ENDPOINT + "/services/groups");
assertTrue(response.isSuccess(), "Request not successful! Status: " + response.getStatusCode() + ", Message: " + response.getStatusReason());
// Parse json data if successful
List<ApiGroupInfo> groupInfo = groupGson.fromJson(response.getResponseString(), GSON_GROUP_INFO_SERIALIZATION_TYPE);
for(int i = 0; i < groupInfo.size(); i++) {
if(groupInfo.get(i).getCode().equals("API 101")) {
logger.info("Found course with code API 101, removing test user from course");
response = helper.doApiCallDelete("https://" + API_ENDPOINT + "/services/groups/" + groupInfo.get(i).getGroupId() + "/members/" + TestConstants.GROUP_TEST_ADD_USER_PID);
logger.info("Removed test user: " + response.getResponseString());
}
}
}
@Test
public void ListCourseUsers() {
//GET request
ApiResponse response = helper.doApiCallGet("https://" + API_ENDPOINT + "/services/groups");
assertTrue(response.isSuccess(), "Request not successful! Status: " + response.getStatusCode() + ", Message: " + response.getStatusReason());
// Parse json data if successful
List<ApiGroupInfo> groupInfo = groupGson.fromJson(response.getResponseString(), GSON_GROUP_INFO_SERIALIZATION_TYPE);
for(int i = 0; i < groupInfo.size(); i++) {
if(groupInfo.get(i).getCode().equals("API 101")) {
logger.info("Found course with code API 101, listing owners and members");
response = helper.doApiCallGet("https://" + API_ENDPOINT + "/services/groups/" + groupInfo.get(i).getGroupId() + "/content_owners");
logger.info("Owners: " + response.getResponseString());
response = helper.doApiCallGet("https://" + API_ENDPOINT + "/services/groups/" + groupInfo.get(i).getGroupId() + "/members");
logger.info("Members: " + response.getResponseString());
}
}
}
@Test
public void ChangeUserInCourseToOwner() {
//GET request
ApiResponse response = helper.doApiCallGet("https://" + API_ENDPOINT + "/services/groups");
assertTrue(response.isSuccess(), "Request not successful! Status: " + response.getStatusCode() + ", Message: " + response.getStatusReason());
// Parse json data if successful
List<ApiGroupInfo> groupInfo = groupGson.fromJson(response.getResponseString(), GSON_GROUP_INFO_SERIALIZATION_TYPE);
for(int i = 0; i < groupInfo.size(); i++) {
if(groupInfo.get(i).getCode().equals("API 101")) {
logger.info("Found course with code API 101, changing test user to owner");
response = helper.doApiCallDelete("https://" + API_ENDPOINT + "/services/groups/" + groupInfo.get(i).getGroupId() + "/members/" + TestConstants.GROUP_TEST_ADD_USER_PID);
response = helper.doApiCallPost("https://" + API_ENDPOINT + "/services/groups/" + groupInfo.get(i).getGroupId() + "/content_owners/add/" + TestConstants.GROUP_TEST_ADD_USER_PID);
logger.info("Added test user: " + response.getResponseString());
}
}
}
}