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 Registered Devices in the Video Platform.
package test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Calendar;
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.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
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.ApiDeviceInfo;
import data.CreateSessionBody;
import data.TestConstants;
import serializer.ApiDeviceInfoDeserializer;
import serializer.ApiDeviceInfoSerializer;
import util.ApiResponse;
import util.HttpHelper;
public class DeviceApiTest {
private static final String API_ENDPOINT = TestConstants.API_ENDPOINT;
private Gson gson;
private Gson gsonBuild;
private HttpHelper helper;
private final String YSS_DEVICE_ID = TestConstants.YSS_DEVICE_ID;
private final String YSS_DEVICE_PROFILE_ID = TestConstants.YSS_DEVICE_PROFILE_ID;
private final String YSS_DEVICE_PROFILE_ID_LIVE = TestConstants.YSS_DEVICE_PROFILE_ID_LIVE;
private final String YSS_USER_ID = TestConstants.YSS_USER_ID;
private final String YSS_GROUP_ID = TestConstants.YSS_GROUP_ID;
private static final Logger logger = Logger.getLogger(DeviceApiTest.class.getName());
@BeforeClass
public void setUpBeforeClass() {
gson = new GsonBuilder().registerTypeAdapter(ApiDeviceInfo.class, new ApiDeviceInfoDeserializer()).create();
gsonBuild = new GsonBuilder().registerTypeAdapter(ApiDeviceInfo.class, new ApiDeviceInfoSerializer()).create();
helper = new HttpHelper();
}
@Test
public void Schedule_New_Recording() {
CreateSessionBody createSessionBody = new CreateSessionBody();
Calendar c = Calendar.getInstance();
c.add(Calendar.HOUR, 3);
createSessionBody.start_year = Integer.toString(c.get(Calendar.YEAR));
createSessionBody.start_month = Integer.toString(c.get(Calendar.MONTH) + 1);
createSessionBody.start_day = Integer.toString(c.get(Calendar.DATE));
createSessionBody.start_hour = Integer.toString(c.get(Calendar.HOUR_OF_DAY));
createSessionBody.start_minute = Integer.toString(c.get(Calendar.MINUTE));
createSessionBody.duration_hour = "1";
createSessionBody.duration_minute = "30";
createSessionBody.login_id = YSS_USER_ID;
createSessionBody.session_title = "Test API New Scheduled: " + new Date();
createSessionBody.profile_id = YSS_DEVICE_PROFILE_ID;
createSessionBody.station_id = YSS_DEVICE_ID;
Gson g = new Gson();
StringEntity customJSON = new StringEntity(g.toJson(createSessionBody), ContentType.APPLICATION_FORM_URLENCODED);
ApiResponse response = helper.doApiCallPost("https://" + API_ENDPOINT + "/services/devices/session", customJSON);
logger.info("Results: " + response.getResponseString());
}
@Test
public void Retrieve_Device_Profiles_For_User() {
ApiResponse response = helper.doApiCallGet("https://" + API_ENDPOINT + "/services/devices/profiles/" + YSS_DEVICE_ID + "/" + YSS_USER_ID);
logger.info("Results: " + response.getResponseString());
}
@Test
public void Retrieve_Past_Scheduled_Sessions_For_Remote_Device() {
final class DeviceScheduleBody {
public String startTime;
public String endTime;
}
DeviceScheduleBody queryParameters = new DeviceScheduleBody();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Calendar c = Calendar.getInstance();
queryParameters.endTime = sdf.format(c.getTime());
c.add(Calendar.MONTH, -1);
queryParameters.startTime = sdf.format(c.getTime());
ApiResponse response = helper.doApiCallGet("https://" + API_ENDPOINT + "/services/devices/" + YSS_DEVICE_ID + "/schedule?startTime=" + queryParameters.startTime + "&endTime=" + queryParameters.endTime);
logger.info("Results: " + response.getResponseString());
}
@Test
public void Retrieve_Future_Scheduled_Sessions_For_Remote_Device() {
final class DeviceScheduleBody {
public String startTime;
public String endTime;
}
DeviceScheduleBody queryParameters = new DeviceScheduleBody();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Calendar c = Calendar.getInstance();
// c.add(Calendar.DATE, -1);
queryParameters.startTime = sdf.format(c.getTime());
c.add(Calendar.MONTH, 1);
queryParameters.endTime = sdf.format(c.getTime());
ApiResponse response = helper.doApiCallGet("https://" + API_ENDPOINT + "/services/devices/" + YSS_DEVICE_ID + "/schedule?startTime=" + queryParameters.startTime + "&endTime=" + queryParameters.endTime);
logger.info("Results: " + response.getResponseString());
}
@Test
public void Delete_the_First_Future_Scheduled_Sessions_For_Remote_Device() {
final class DeviceScheduleBody {
public String startTime;
public String endTime;
}
DeviceScheduleBody queryParameters = new DeviceScheduleBody();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Calendar c = Calendar.getInstance();
// c.add(Calendar.DATE, -2);
queryParameters.startTime = sdf.format(c.getTime());
c.add(Calendar.DATE, 2);
queryParameters.endTime = sdf.format(c.getTime());
ApiResponse response = helper.doApiCallGet("https://" + API_ENDPOINT + "/services/devices/" + YSS_DEVICE_ID + "/schedule?startTime=" + queryParameters.startTime + "&endTime=" + queryParameters.endTime);
try {
JSONArray object = (JSONArray) new JSONParser().parse(response.getResponseString());
if (object.size() == 0) {
logger.info("Cannot delete scheduled session because there are no future scheduled sessions");
} else {
long session_id = (Long)((JSONObject)object.get(0)).get("session_id");
ApiResponse deleteResponse = helper.doApiCallDelete("https://" + API_ENDPOINT + "/services/devices/" + session_id);
logger.info("Results: " + deleteResponse.getResponseString());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static class StartSessionBody {
public String is_live_broadcast;
public String group_id;
public String duration_minute;
public String station_id;
public String login_id;
public String session_title;
public String profile_id;
}
@Test
public void Start_New_Recording() {
StartSessionBody body = new StartSessionBody();
body.is_live_broadcast = "false";
body.group_id = YSS_GROUP_ID;
body.duration_minute = "120";
body.login_id = YSS_USER_ID;
body.session_title = "API Test Record: " + new Date();
body.profile_id = YSS_DEVICE_PROFILE_ID;
body.station_id = YSS_DEVICE_ID;
Gson g = new Gson();
StringEntity customJSON = new StringEntity(g.toJson(body), ContentType.APPLICATION_JSON);
ApiResponse response = helper.doApiCallPost("https://" + API_ENDPOINT + "/services/devices/startsession", customJSON);
logger.info("Results Code: " + response.getStatusCode());
logger.info("Results: " + response.getResponseString());
}
@Test
public void Start_New_Recording_Live() {
StartSessionBody body = new StartSessionBody();
body.is_live_broadcast = "true";
body.group_id = YSS_GROUP_ID;
body.duration_minute = "120";
body.login_id = YSS_USER_ID;
body.session_title = "API Test Record Live: " + new Date();
body.profile_id = YSS_DEVICE_PROFILE_ID_LIVE;
body.station_id = YSS_DEVICE_ID;
Gson g = new Gson();
StringEntity customJSON = new StringEntity(g.toJson(body), ContentType.APPLICATION_FORM_URLENCODED);
ApiResponse response = helper.doApiCallPost("https://" + API_ENDPOINT + "/services/devices/startsession", customJSON);
logger.info("Results Code: " + response.getStatusCode());
logger.info("Results: " + response.getResponseString());
}
public static class StopSessionBody {
public String station_id;
public String login_id;
}
@Test
public void Stop_Current_Recording_Session() {
StopSessionBody body = new StopSessionBody();
body.login_id = YSS_USER_ID;
body.station_id = YSS_DEVICE_ID;
Gson g = new Gson();
StringEntity customJSON = new StringEntity(g.toJson(body), ContentType.APPLICATION_FORM_URLENCODED);
ApiResponse response = helper.doApiCallPost("https://" + API_ENDPOINT + "/services/devices/stopstation", customJSON);
logger.info("Results Code: " + response.getStatusCode());
logger.info("Results: " + response.getResponseString());
}
@Test
public void Retrieve_Past_Scheduled_Sessions_Video_Link_Remote_Device() {
final class DeviceScheduleBody {
public String startTime;
public String endTime;
}
DeviceScheduleBody queryParameters = new DeviceScheduleBody();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Calendar c = Calendar.getInstance();
queryParameters.endTime = sdf.format(c.getTime());
c.add(Calendar.MONTH, -1);
queryParameters.startTime = sdf.format(c.getTime());
ApiResponse response = helper.doApiCallGet("https://" + API_ENDPOINT + "/services/devices/" + YSS_DEVICE_ID + "/schedule?startTime=" + queryParameters.startTime + "&endTime=" + queryParameters.endTime);
logger.info("Results: " + response.getResponseString());
try {
JSONArray object = (JSONArray) new JSONParser().parse(response.getResponseString());
if (object.size() == 0) {
logger.info("Cannot find any past scheduled sessions");
} else {
boolean found = false;
for (int i = 0; i < object.size(); i++) {
if (!((JSONObject)object.get(i)).get("session_name").equals(TestConstants.VIDEO_LINK_RECORDING_SESSION_NAME)) {
continue;
}
found = true;
long session_id = (Long)((JSONObject)object.get(i)).get("video_id");
if (session_id == -1) {
logger.info("The scheduled session does not have any video associated with it");
} else {
response = helper.doApiCallGet("https://" + API_ENDPOINT + "/services/media/video/directlink/" + session_id);
logger.info("Results: " + response.getResponseString());
}
}
if (!found) {
logger.info("Could not find scheduled session named: " + TestConstants.VIDEO_LINK_RECORDING_SESSION_NAME);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void Retrieve_Device_Status() {
ApiResponse response = helper.doApiCallGet("https://" + API_ENDPOINT + "/services/devices/" + YSS_DEVICE_ID + "/status");
logger.info("Results: " + response.getResponseString());
}
}