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

              
                <section class="bcls-section">
<h2 id="Set_default_profile_app">Add Ingest Profile to Multiple Accounts</h2>

<p>You can use the form below to select a custom ingest profile from one account and add it to one or more other accounts. There is also an option to make this profile the default for each of the other accounts.</p>

<fieldset><legend>Input</legend>

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

<p><strong style="color:#E4B842">Source</strong> Account id: <input id="account_id_input" type="text" placeholder="1752604059001" /><br>(This is the account that has to profile you want to add to other accounts)</p>

<p><strong style="color:#E4B842">Target</strong> account ids (comma-delimited list): <input id="account_ids_input" type="text" placeholder="1752604059001,921483702001,57838016001" size="60" /></p>

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

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

<p><button class="bcls-button" id="getProfiles">Get Profiles</button></p>

<p>Profile to set as default: <select name="profileSelect" id="profileSelect" style="width:200px;" class="disabled">
  <option value="null">Select the Profile to Add to Other Accounts</option>
</select></p>

<p><input type="checkbox" id="setIngestionDefault" /> <label for="setIngestionDefault">Set the Selected Profile as Default for ingestion for these Accounts</label></p>

<p><input type="checkbox" id="setClippingDefault" /> <label for="setClippingDefault">Set the Selected Profile as Default for clipping for these Accounts</label></p>

<p><button class="bcls-button disabled" id="addProfile">Add the Selected Profile to the Target Accounts</button></p>
</fieldset>

<fieldset><legend>Output</legend>

<p id="logger" style="color:rgb(237, 104, 38)">Waiting for input...</p>

<h4>Current API request</h4>

<pre>
  <code id="apiRequest">API request will appear here...</code></pre>

<h4>Request type</h4>

<pre>
  <code id="apiRequestType">API request type will appear here...</code></pre>

<h4>Response data</h4>

<pre>
  <code id="apiResponse" style="width:95%;height:30em;">Response will appear here...</code></pre>
</fieldset>
</section>

              
            
!

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: #111;
  font-family: 'Source Code Pro',monospace;
  padding: 1em;
  width: 90%;
  min-height: 5em;
  background-color: #cfcfcf;
  font-size: 1rem;
}
.bcls-button {
  padding: .5em;
  background-color: #333;
  border: 1px yellow solid;
  border-radius: .5em;
  color: yellow;
  cursor: pointer;
}
fieldset {
  color: #64AAB2;
  border: 1px #64AAB2 solid;
  border-radius: .5em;
}
code {
  color: #E4B842;
  font-family: 'Source Code Pro',monospace;
}
.disabled {
  opacity: .4;
  cursor: not-allowed;
}

              
            
!

JS

              
                var BCLS = (function(window, document) {
  var // account stuff
    account_id,
    account_ids,
    bcToken,
    client_id,
    client_secret,
    selectedProfile,
    // api stuff
    proxyURL = 'https://solutions.brightcove.com/bcls/bcls-proxy/bcls-proxy-v2.php',
    ipURL = 'https://ingestion.api.brightcove.com/v1/accounts/',
    ipAccountSuffix = '/configuration',
    ipProfileSuffix = '/profiles',
    totalCalls = 0,
    callNumber = 0,
    accountsArray = [],
    defaultAccounts = ['1485884786001', '1937897674001'],
    defaultAccount = '1752604059001',
    profilesArray = [],
    newProfileId,
    // element references
    account_id_input = document.getElementById('account_id_input'),
    account_ids_input = document.getElementById('account_ids_input'),
    client_id_input = document.getElementById('client_id_input'),
    client_secret_input = document.getElementById('client_secret_input'),
    getProfiles = document.getElementById('getProfiles'),
    profileSelect = document.getElementById('profileSelect'),
    addProfile = document.getElementById('addProfile'),
    setIngestionDefault = document.getElementById('setIngestionDefault'),
    setClippingDefault = document.getElementById('setClippingDefault'),
    logger = document.getElementById('logger'),
    apiRequest = document.getElementById('apiRequest'),
    apiResponse = document.getElementById('apiResponse'),
    apiRequestType = document.getElementById('apiRequestType'),
    // for filtering ingest profiles list
    live_profiles = ['Live - Standard', 'Live - HD', 'Live - Premium HD'];


  /**
   * remove spaces from a string
   * @param {String} str string to process
   */
  function removeSpaces(str) {
    str = str.replace(/\s/g, '');
    return str;
  }

  /**
   * determines whether an object has a certain property
   * @param {object} obj the object
   * @param {string} prop the property name
   * @returns {boolean}
   */
  function hasProperty(obj, prop) {
    if (prop in obj) {
      return true;
    }
    return false;
  }

  /**
   * determines whether specified item is in an array
   *
   * @param {array} array to check
   * @param {string} item to check for
   * @return {boolean} true if item is in the array, else false
   */
  function arrayContains(arr, item) {
    var i,
      iMax = arr.length;
    for (i = 0; i < iMax; i++) {
      if (arr[i] === item) {
        return true;
      }
    }
    return false;
  }

  /*
   * tests to see if a string is json
   * @param {String} str string to test
   * @return {Boolean}
   */
  function isJson(str) {
    try {
      JSON.parse(str);
    } catch (e) {
      return false;
    }
    return true;
  }
  /**
   * tests for all the ways a variable might be undefined or not have a value
   * @param {String|Number} 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
   */
  function getSelectedValue(e) {
    return e.options[e.selectedIndex].value;
  }

  /**
   * determines if checkbox is checked
   * @param  {htmlElement}  e the checkbox to check
   * @return {Boolean}  true if box is checked
   */
  function isChecked(e) {
    if (e.checked) {
      return true;
    }
    return false;
  }

  /**
   * get a copy of (rather than reference to) an object
   * @param  {object} obj - the object you want a copy
   * @return {object}     the copy
   */
  function copyObj(obj) {
    return JSON.parse(JSON.stringify(obj));
  }

  /**
   * 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 an element so user can't click on it
   * @param {htmlElement} el the element
   */
  function disableElement(el) {
    el.setAttribute('disabled', 'disabled');
    el.classList.add('disabled');
  }

  /**
   * re-enables an element to make it clickable
   * @param {htmlElement} el the element
   */
  function enableElement(el) {
    el.removeAttribute('disabled');
    el.classList.remove('disabled');
  }

  /**
   * 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;
  }

  /**
   * remove or add obsolete, standard and live profiles from the profiles list
   */
  function filterProfiles() {
    // below are the obsolete profiles - you just have to know their names
    var deprecated_profiles = ['balanced-nextgen-player', 'Express Standard', 'mp4-only', 'balanced-high-definition', 'low-bandwidth-devices', 'balanced-standard-definition', 'single-rendition', 'Live - Standard', 'high-bandwidth-devices', 'Live - Premium HD', 'Live - HD', 'videocloud-default-trial', 'screencast'],
      live_profiles = ['Live - Standard', 'Live - HD', 'Live - Premium HD'],
      i = profilesArray.length;
    while (i > 0) {
      i--;
      if (arrayContains(deprecated_profiles, profilesArray[i].name)) {
        profilesArray.splice(i, 1);
      } else if (profilesArray[i].brightcove_standard === true) {
        profilesArray.splice(i, 1);
      } else if (arrayContains(live_profiles, profilesArray[i].name)) {
        profilesArray.splice(i, 1);
      }
    }
    return;
  }


  /**
   * sets up the data for the API request
   * @param {String} id the id of the button that was clicked
   */
  function setOptions(id, type) {
    var i,
      iMax,
      endPoint,
      options = {},
      configurationId;
    logger.textContent = 'Getting profiles for source account'
    options.proxyURL = proxyURL;
    if (isDefined(client_id) && isDefined(client_secret)) {
      options.client_id = client_id;
      options.client_secret = client_secret;
    }
    switch (id) {
      case 'getProfiles':
        var profile;
        endPoint = account_id;
        options.url = ipURL + endPoint + ipProfileSuffix;
        options.requestType = type;
        apiRequest.textContent = options.url;
        apiRequestType.textContent = options.type;
        makeRequest(options, function(response) {
          if (isJson(response)) {
            profilesArray = JSON.parse(response);
            // filter out non-custom profiles
            filterProfiles();
            // check for display_name and if none, use name
            // amd remove fields that can't be used when adding the profile to another account
            iMax = profilesArray.length;
            for (i = 0; i < iMax; i++) {
              profile = profilesArray[i];
              if (!hasProperty(profile, 'display_name')) {
                profile.display_name = profile.name;
              }
              delete profile.date_created;
              delete profile.date_last_modified;
              delete profile.version;
            }
            // populate the profile selector
            populateSelector(profileSelect, profilesArray, 'id', 'display_name');
          }
          apiResponse.textContent = JSON.stringify(profilesArray, null, 2);
          enableElement(profileSelect);
        });
        break;
      case 'addProfile':
        var requestBody = copyObj(selectedProfile),
          responseObj;
        logger.textContent = 'Adding profile to target account ' + accountsArray[callNumber];
        delete requestBody.id;
        requestBody.account_id = accountsArray[callNumber];
        endPoint = accountsArray[callNumber];
        options.url = ipURL + endPoint + ipProfileSuffix;
        options.requestType = type;
        options.requestBody = JSON.stringify(requestBody);
        apiRequest.textContent = options.url;
        apiRequestType.textContent = options.type;
        makeRequest(options, function(response) {
          if (isJson(response)) {
            responseObj = JSON.parse(response);
            apiResponse.textContent = JSON.stringify(responseObj, null, 2);
            newProfileId = responseObj.id;
            if (isChecked(setIngestionDefault) || isChecked(setClippingDefault)) {
              if (isDefined(newProfileId)) {
                // if new profile id does not exist, there was an error, probably a conflict because a profile with that name already existed - so don't attempt to set default profile for this account
                setOptions('getDefault', 'GET');
              }
            } else {
              callNumber++;
              if (callNumber < totalCalls) {
                setOptions('addProfile', 'POST');
              } else {
                logger.textContent = 'All finished!'
              }
            }
          }
        });
        break;
      case 'getDefault':
        var parsedData;
        logger.textContent = 'Getting account configuration: ' + accountsArray[callNumber];
        endPoint = accountsArray[callNumber];
        options.url = ipURL + endPoint + ipAccountSuffix;
        options.requestType = type;
        apiRequest.textContent = options.url;
        apiRequestType.textContent = options.type;
        makeRequest(options, function(response) {
          parsedData = JSON.parse(response);
          apiResponse.textContent = JSON.stringify(parsedData, null, 2);
          configurationId = parsedData.id;
          setOptions('setDefault', 'POST')
        });
        break;
      case 'setDefault':
        var reqBody = {},
          now;
        logger.textContent = 'Processing account: ' + accountsArray[callNumber];
        endPoint = accountsArray[callNumber];
        options.url = ipURL + endPoint + ipAccountSuffix;
        options.requestType = type;
        if (isChecked(setIngestionDefault)) {
          reqBody.default_profile_id = newProfileId;
        }
        if (isChecked(setClippingDefault)) {
          reqBody.default_social_clipping_profile_id = newProfileId;
        }
        if (type === 'PUT') {
          reqBody.id = configurationId;
        }
        reqBody.account_id = accountsArray[callNumber];
        options.requestBody = JSON.stringify(reqBody);
        apiRequest.textContent = options.url;
        apiRequestType.textContent = options.type;
        makeRequest(options, function(response) {
          parsedData = JSON.parse(response);
          apiResponse.textContent = JSON.stringify(parsedData, null, 2);
          if (Array.isArray(parsedData)) {
            // we have an error, most likely a conflict because default has already been set - try update instead
            setOptions('setDefault', 'PUT');
          } else {
            callNumber++;
            if (callNumber < totalCalls) {
              setOptions('addProfile', 'POST');
            } else {
              logger.textContent = 'All finished!'
            }
          }
        });
        break;
    }
  }

  /**
   * 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));
  }

  function init() {
    // event handlers
    getProfiles.addEventListener('click', function() {
      var accountIds;
      // get the inputs
      client_id = client_id_input.value;
      client_secret = client_secret_input.value;
      newProfile = getSelectedValue(profileSelect);
      // only use entered account id if client id and secret are entered also
      if (isDefined(client_id) && isDefined(client_secret)) {
        if (isDefined(account_id_input.value) && isDefined(account_ids_input.value)) {
          account_id = account_id_input.value;
          accountIds = removeSpaces(account_ids_input.value);
          accountsArray = accountIds.split(',');
        } else {
          window.alert('To use your own account, you must specify an account id, and client id, and a client secret - since at least one of these is missing, a sample account will be used');
          account_id = defaultAccount;
          client_id = '';
          client_secret = '';
          accountsArray = defaultAccounts;
        }
      } else {
        account_id = defaultAccount;
        accountsArray = defaultAccounts;
      }
      totalCalls = accountsArray.length;
      setOptions('getProfiles', 'GET');
    });
    profileSelect.addEventListener('change', function() {
      var selected = getSelectedValue(profileSelect),
        idx = findObjectInArray(profilesArray, 'id', selected);
      selectedProfile = profilesArray[idx];
      enableElement(addProfile);
    });
    addProfile.addEventListener('click', function() {
      setOptions('addProfile', 'POST');
    })

  }
  // kick things off
  init();
})(window, document);

              
            
!
999px

Console