UNPKG

labo-components

Version:
49 lines (45 loc) 1.41 kB
export default class PlayoutAPI { static requestAccess = async(contentServerId, contentId, desiredState, callback) => { const userInfo = await PlayoutAPI.requestUserInfo(); if(!userInfo || !userInfo.at || !userInfo.clientId) return callback(false, desiredState); const data = { contentId: contentId, clientId : userInfo.clientId, at : userInfo.at }; const url = _play + '/api/play/' + contentServerId + '/' + contentId; const xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState === XMLHttpRequest.DONE) { if(xhr.status === 200) { const resp = JSON.parse(xhr.responseText); callback(true, desiredState); } else { callback(false, desiredState); } } }; xhr.open("POST", url); xhr.withCredentials = true; xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); xhr.send(JSON.stringify(data)); }; static requestUserInfo = () => { return new Promise(resolve => { const xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState === XMLHttpRequest.DONE) { if(xhr.status === 200) { resolve(JSON.parse(xhr.responseText)); } else { resolve(null); } } }; xhr.open("POST", '/user-info'); xhr.withCredentials = true; xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); xhr.send(null); }); } }