@myamsani/brightcove-playback
Version:
npm module for fetching video data from Brightcove using playback API using the policy key and account
51 lines (42 loc) • 1.47 kB
JavaScript
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
let account_id
let policy_key
module.exports = (injected_account_id, injected_policy_key) => {
account_id = injected_account_id
policy_key = injected_policy_key
return {
getVideoData: getVideoData
}
}
function getVideoData(video_id ){
return new Promise((resolve, reject) => {
var apiBaseURL = 'https://edge.api.brightcove.com/playback/v1';
var mediaType = 'video',
requestURL = apiBaseURL + '/accounts/' + account_id + '/videos/' + video_id;
var httpRequest = new XMLHttpRequest(),
responseData;
// response handler
getResponse = function() {
try {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
responseData = httpRequest.responseText;
resolve ({data: JSON.parse(responseData)});
} else {
resolve ({error: 'There was a problem with the request. Request returned ' + httpRequest.status} );
}
}
} catch (e) {
resolve ({error: 'Caught Exception: ' + e });
}
};
// set response handler
httpRequest.onreadystatechange = getResponse;
// open the request
httpRequest.open('GET', requestURL);
// set headers
httpRequest.setRequestHeader('Accept', 'application/json;pk=' + policy_key);
// open and send request
httpRequest.send();
});
}