Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h2>Social API Quick Start</h2>

<fieldset class="bcls-fieldset">
   <legend>Account Input</legend>

   <p>If you do not enter account information, a Brightcove sample account will be used.</p>

   <p>Account id: <input id="account_id_input" type="text" value="" placeholder="1752604059001" /></p>

   <p>Client id: <input id="client_id_input" type="text" size="60" value="" placeholder="c5d0a622-5479-46d8-8d8a-5f034b943fab" /></p>

   <p>Client secret: <input id="client_secret_input" type="text" size="60" value="" placeholder="w7NQYu0vUloM4GYYy2SXAxrvyFpt8fwI35qAFZcS13-VIgs0itwKNsAwHOS80sOWK" /></p>
</fieldset>

<fieldset>
  <legend>API Operations</legend>
  <p><strong>Social Sharing Status</strong></p>
  <p>
    <button class="bcls-button" id="getStatusAll">Current Status All Videos</button>
    <button class="bcls-button disabled" id="getStatusOne">Current Status One Video</button>
    <select id="videoForStatus" class="disabled">
      <option value="">Select Video for Status Request</option>
    </select>
  </p>
  <p><strong>Social Sharing Histoy</strong></p>
  <p>
    <button class="bcls-button disabled" id="getHistory">Get the Lifetime Social History for a Video</button>
    <select id="videoForHistory" class="disabled">
      <option value="">Select Video for History Request</option>
    </select>
  </p>
</fieldset>

<p><strong>API request:</strong></p>
<pre class="bcls-code"><code id="apiRequest"></code></pre>
<p><strong>API response:</strong></p>
<pre class="bcls-code"><code id="responseData"></code></pre>

              
            
!

CSS

              
                /* * The body style is just for the
 * background color of the codepen.
 * Do not include in your code.
 */
body {
  background-color: #111;
  color: #f5f5f5;
  font-family: sans-serif;
  margin: 2em;
}
/*
 * Styles essential to the sample
 * are below
 */
.bcls-code {
  color: #E4B842;
  padding: 1em;
  width: 90%;
  min-height: 2em;
  font-size: 1rem;
}
.bcls-button {
  padding: .5em;
  background-color: #333;
  border: 1px yellow solid;
  border-radius: .5em;
  color: yellow;
  cursor: pointer;
}
.disabled {
  opacity: .4;
  cursor: not-allowed;
}
fieldset {
  color: #64AAB2;
  border: 1px #64AAB2 solid;
  border-radius: .5em;
}
code {
  color: #E4B842;
}
pre {
  margin-top: 1em;
  margin-botton: 1em;
}

              
            
!

JS

              
                var BCLS = (function(window, document) {
  var baseURL = 'https://edge.social.api.brightcove.com/v1/accounts/',
    proxyURL = 'https://solutions.brightcove.com/bcls/bcls-proxy/bcls-proxy-v2.php',
    video_id,
    account_id,
    client_id,
    client_secret,
    account_id_default = '1486906377',
    account_id_input = document.getElementById('account_id_input'),
    client_id_input = document.getElementById('client_id_input'),
    client_secret_input = document.getElementById('client_secret_input'),
    getStatusAll = document.getElementById('getStatusAll'),
    getStatusOne = document.getElementById('getStatusOne'),
    getHistory = document.getElementById('getHistory'),
    videoForStatus = document.getElementById('videoForStatus'),
    videoForHistory = document.getElementById('videoForHistory'),
    apiRequest = document.getElementById('apiRequest'),
    apiMethod = document.getElementById('apiMethod'),
    responseData = document.getElementById('responseData'),
    selectorData = [],
    selectedVideo,
    callNumber = 0,
    maxCalls,
    videoData;



  /**
   * tests for all the ways a variable might be undefined or not have a value
   * @param {*} x the variable to test
   * @return {Boolean} true if variable is defined and has a value
   */
  function isDefined(x) {
    if ( x === '' || x === null || x === undefined) {
      return false;
    }
    return true;
  }

  /**
   * get selected value for single select element
   *
   @param {htmlElement} e the select element
   *
   @return {Object} object containing the `value`
   */
   function getSelectedValue(e) {
     var selected = e.options[e.selectedIndex];
      return selected.value;
   }

   /**
    * find index of an object in array of objects
    * based on some property value
    *
    * @param {array} targetArray array to search
    * @param {string} objProperty object property to search
    * @param {string} value of the property to search for
    * @return {integer} index of first instance if found, otherwise returns -1
   */
   function findObjectInArray(targetArray, objProperty, value) {
       var i, totalItems = targetArray.length, objFound = false;
       for (i = 0; i < totalItems; i++) {
           if (targetArray[i][objProperty] === value) {
               objFound = true;
               return i;
           }
       }
       if (objFound === false) {
           return -1;
       }
   }

   /**
    * disables a button so user can't submit new request until current one finishes
   * @param {htmlElement} button
   */
  function disableElement(button) {
      button.setAttribute('disabled', 'disabled');
      button.classList.add('disabled');
  }

  /**
   * re-enables a button
   */
  function enableElement(button) {
      button.removeAttribute('disabled');
      button.classList.remove('disabled');
  }

  /**
   * display the api response
   * @param  {String} response API raw response
   */
  function displayResponse(response) {
    var parsedData = JSON.parse(response);
    responseData.textContent = JSON.stringify(parsedData, null, 2);
    return;
  }

  /**
   * populate a selector element from an array of objects
   * @param {htmlElement} selector reference to the selector element
   * @param {array} dataArray an array of objects
   * @param {string} valueField name of the object field to use for option values
   * @param {string} textField name of the object field to be use for the option text (can be the same as the valueField)
   */
  function populateSelector(selector, dataArray, valueField, textField) {
    var i,
      iMax,
      option,
      item,
      frag = document.createDocumentFragment();
    iMax = dataArray.length;
    for (i = 0; i < iMax; i++) {
      item = dataArray[i];
      option = document.createElement('option');
      option.setAttribute('value', item[valueField]);
      option.textContent = item[textField];
      frag.appendChild(option);
    }
    selector.appendChild(frag);
    return;
  }

  function getInfo() {
    account_id = (isDefined(account_id_input.value)) ? account_id_input.value : account_id_default;
    client_id = (isDefined(client_id_input.value)) ? client_id_input.value : null;
    client_secret = (isDefined(client_secret_input.value)) ? client_secret_input.value : null;
    if (account_id !== account_id_default) {
      if (!isDefined(client_secret) || !isDefined(client_id)) {
        window.alert('If you are using your own account, you must provide a client id and client secret');
      }
    }
  }

  function getVideoName() {
    var endPoint,
      options = {};
    // disable buttons to prevent a new request before current one finishes
    options.proxyURL = proxyURL;
    if (isDefined(client_id)) {
      options.client_id = client_id;
      options.client_secret = client_secret;
    }
    endPoint = '/videos/' + videoData[callNumber].id;
    options.url = 'https://cms.api.brightcove.com/v1/accounts/' + account_id + endPoint;
    options.requestType = 'GET';
    makeRequest(options, function(response) {
      var obj = {};
      parsedData = JSON.parse(response);
      if (findObjectInArray(selectorData, 'id', parsedData.id) === -1) {
        obj.id = parsedData.id;
        obj.name = parsedData.name;
        selectorData.push(obj);
      }
      callNumber++;
      if (callNumber < maxCalls) {
        getVideoName();
      } else {
        populateSelector(videoForStatus, selectorData, 'id', 'name');
        populateSelector(videoForHistory, selectorData, 'id', 'name');
        enableElement(getStatusOne);
        enableElement(getHistory);
        enableElement(videoForStatus);
        enableElement(videoForHistory);
      }
    });
  }

  /**
   * sets up the data for the API request
   * @param {String} id the id of the button that was clicked
   */
  function setoptions(id) {
    var endPoint,
      options = {},
      requestBody = {},
      parsedData,
      i,
      iMax;
    // disable buttons to prevent a new request before current one finishes
    options.proxyURL = proxyURL;
    if (isDefined(client_id)) {
      options.client_id = client_id;
      options.client_secret = client_secret;
    }
    switch (id) {
      case 'getStatusAll':
        endPoint = '/videos/status';
        options.url = baseURL + account_id + endPoint;
        options.requestType = 'GET';
        apiRequest.textContent = options.url;
        makeRequest(options, function(response) {
          displayResponse(response);
          parsedData = JSON.parse(response);
          videoData = parsedData.videos;
          maxCalls = videoData.length;
          callNumber = 0;
          getVideoName();
        });
        break;
      case 'getStatusOne':
        endPoint = '/videos/' + selectedVideo + '/status';
        options.url = baseURL + account_id + endPoint;
        options.requestType = 'GET';
        apiRequest.textContent = options.url;
        makeRequest(options, function(response) {
          displayResponse(response);
        });
        break;
      case 'getHistory':
        endPoint = '/videos/' +  selectedVideo + '/history'
        options.url = baseURL + account_id + endPoint;
        options.requestType = 'GET';
        apiRequest.textContent = options.url;
        makeRequest(options, function(response) {
          displayResponse(response);
        });
        break;
      default:
        console.log('should not be here - ', id);
    }
  }

  /**
   * send API request to the proxy
   * @param  {Object} options for the request
   * @param  {String} options.url the full API request URL
   * @param  {String="GET","POST","PATCH","PUT","DELETE"} requestData [options.requestType="GET"] HTTP type for the request
   * @param  {String} options.proxyURL proxyURL to send the request to
   * @param  {String} options.client_id client id for the account (default is in the proxy)
   * @param  {String} options.client_secret client secret for the account (default is in the proxy)
   * @param  {JSON} [options.requestBody] Data to be sent in the request body in the form of a JSON string
   * @param  {Function} [callback] callback function that will process the response
   */
  function makeRequest(options, callback) {
    var httpRequest = new XMLHttpRequest(),
      response,
      proxyURL = options.proxyURL,
      // response handler
      getResponse = function() {
        try {
          if (httpRequest.readyState === 4) {
            if (httpRequest.status >= 200 && httpRequest.status < 300) {
              response = httpRequest.responseText;
              // some API requests return '{null}' for empty responses - breaks JSON.parse
              if (response === '{null}') {
                response = null;
              }
              // return the response
              callback(response);
            } else {
              alert('There was a problem with the request. Request returned ' + httpRequest.status);
            }
          }
        } catch (e) {
          alert('Caught Exception: ' + e);
        }
      };
    /**
     * set up request data
     * the proxy used here takes the following request body:
     * JSON.stringify(options)
     */
    // set response handler
    httpRequest.onreadystatechange = getResponse;
    // open the request
    httpRequest.open('POST', proxyURL);
    // set headers if there is a set header line, remove it
    // open and send request
    httpRequest.send(JSON.stringify(options));
  }

  // event listeners
  getStatusAll.addEventListener('click', function() {
    getInfo();
    setoptions('getStatusAll');
  });
  getStatusOne.addEventListener('click', function() {
    selectedVideo = getSelectedValue(videoForStatus);
    setoptions('getStatusOne');
  });
  getHistory.addEventListener('click', function() {
    selectedVideo = getSelectedValue(videoForHistory);
    setoptions('getHistory');
  });
})(window, document);

              
            
!
999px

Console