UNPKG

jive-sdk

Version:

Node.js SDK for Jive Software to assist with the development of add-ons

480 lines (381 loc) 12.6 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Jive SDK Source: jive-sdk-api/lib/client/jive.js</title> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/sunlight.default.css"> <link type="text/css" rel="stylesheet" href="styles/site.spacelab.css"> </head> <body> <div class="container-fluid"> <div class="navbar navbar-fixed-top navbar-inverse"> <div class="navbar-inner"> <a class="brand" href="index.html">Jive SDK</a> <ul class="nav"> <li class="dropdown"> <a href="modules.list.html" class="dropdown-toggle" data-toggle="dropdown">Modules<b class="caret"></b></a> <ul class="dropdown-menu "> <li> <a href="module-abstractDefinitions.html">abstractDefinitions</a> </li> <li> <a href="module-abstractInstances.html">abstractInstances</a> </li> <li> <a href="module-addOnRoutes.html">addOnRoutes</a> </li> <li> <a href="module-api.html">api</a> </li> <li> <a href="module-community.html">community</a> </li> <li> <a href="module-constants.html">constants</a> </li> <li> <a href="module-devRoutes.html">devRoutes</a> </li> <li> <a href="module-events.html">events</a> </li> <li> <a href="module-extensions.html">extensions</a> </li> <li> <a href="module-extstreamsInstances.html">extstreamsInstances</a> </li> <li> <a href="module-jiveutil.html">jiveutil</a> </li> <li> <a href="module-oauthRoutes.html">oauthRoutes</a> </li> <li> <a href="module-request.html">request</a> </li> <li> <a href="module-security.html">security</a> </li> <li> <a href="module-service.html">service</a> </li> <li> <a href="module-tasks.html">tasks</a> </li> <li> <a href="module-tileInstances.html">tileInstances</a> </li> <li> <a href="module-tileRoutes.html">tileRoutes</a> </li> <li> <a href="module-tilesDefinitions.html">tilesDefinitions</a> </li> <li> <a href="module-webhooks.html">webhooks</a> </li> </ul> </li> <li class="dropdown"> <a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b class="caret"></b></a> <ul class="dropdown-menu "> <li> <a href="extstreamsDefinitions.html">extstreamsDefinitions</a> </li> <li> <a href="filePersistence.html">filePersistence</a> </li> <li> <a href="memoryPersistence.html">memoryPersistence</a> </li> <li> <a href="memoryScheduler.html">memoryScheduler</a> </li> <li> <a href="oauthHandler.html">oauthHandler</a> </li> </ul> </li> </ul> </div> </div> <div class="row-fluid"> <div class="span12"> <div id="main"> <h1 class="page-title">Source: jive-sdk-api/lib/client/jive.js</h1> <section> <article> <pre class="sunlight-highlight-javascript linenums">/* * Copyright 2013 Jive Software * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * This is the jive network client. * @class jiveClient * @private */ /////////////////////////////////////////////////////////////////////////////////// // private var jive = require('../../api'); var util = require('util'); var constants = require('../util/constants'); var JIVE_OAUTH2_TOKEN_REQUEST_PATH = "/oauth2/token"; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Public /** * Utility for making generic GET request to Jive using auth header from tile instance. Attempts to resolve the promise * to the actual data in the response, not the response object. Has to strip out the security string from Jive. * * If this fails, returns the original response, so be careful to check if obj.statusCode exists in your callback. * @memberof jiveClient */ exports.getWithTileInstanceAuth = function (tileInstance, url) { return exports.tileFetch(tileInstance, url ).then(function (response) { if (!response.entity || !response.entity.body) { return response; } var body = response.entity.body; try { response.entity = JSON.parse(body); } catch (e) { //Do nothing, was not valid JSON object so response.entity.body will contain body string } return response; }); }; /** * @memberof jiveClient * @param options * @param successCallback * @param failureCallback */ exports.requestAccessToken = function (options, successCallback, failureCallback) { var accessTokenRequest = { client_id: options['client_id'], code: options['code'], grant_type: 'authorization_code' }; try { if ( !options.jiveUrl ) { throw new Error("Cannot request access token without a jiveUrl"); } else { // otherwise we deal directly with jive var tokenRequestEndPoint = options.jiveUrl + JIVE_OAUTH2_TOKEN_REQUEST_PATH; var auth = "Basic " + new Buffer(accessTokenRequest.client_id + ':' + options.client_secret).toString('base64'); var headers = { "Content-Type": "application/x-www-form-urlencoded", "Authorization": auth }; jive.util.buildRequest(tokenRequestEndPoint, "POST", accessTokenRequest, headers) .then(successCallback, failureCallback); } } catch (e) { if (failureCallback) { failureCallback(e); } else { jive.logger.error("Error requesting access token!", e); } } }; /** * @memberof jiveClient * @param options * @param successCallback * @param failureCallback */ exports.refreshAccessToken = function (options, successCallback, failureCallback) { var accessTokenRequest = { client_id: options['client_id'], refresh_token: options['refresh_token'], grant_type: 'refresh_token' }; try { if ( !options.jiveUrl ) { throw new Error("Cannot refresh token without a jiveUrl"); } else { // otherwise we deal directly with jive var tokenRequestEndPoint = options.jiveUrl + JIVE_OAUTH2_TOKEN_REQUEST_PATH; var auth = "Basic " + new Buffer(accessTokenRequest.client_id + ':' + options.client_secret).toString('base64'); var headers = { "Content-Type": "application/x-www-form-urlencoded", "Authorization": auth }; jive.util.buildRequest(tokenRequestEndPoint, "POST", accessTokenRequest, headers) .then(successCallback, failureCallback); } } catch (e) { if (failureCallback) { failureCallback(e); } else { jive.logger.error("Error requesting refresh token!", e); } } }; /** * @memberof jiveClient * @param tileInstance * @param data * @returns {*} */ exports.pushData = function (tileInstance, data) { return tilePush('PUT', tileInstance, data, tileInstance['url']); }; /** * @memberof jiveClient * @param tileInstance * @param activity * @returns {*} */ exports.pushActivity = function (tileInstance, activity) { return tilePush('POST', tileInstance, activity, tileInstance['url']); }; /** * @memberof jiveClient * @param tileInstance * @param comment * @param commentURL * @returns {*} */ exports.pushComment = function (tileInstance, comment, commentURL) { return tilePush('POST', tileInstance, comment, commentURL); }; /** * @memberof jiveClient * @param instance * @returns {*} */ exports.fetchExtendedProperties = function( instance ) { return jive.util.buildRequest( extractExternalPropsUrl( instance ), 'GET', null, makeExternalPropsHeader(instance) ); }; /** * @memberof jiveClient * @param instance * @param props * @returns {*} */ exports.pushExtendedProperties = function( instance, props ) { return jive.util.buildRequest( extractExternalPropsUrl( instance ), 'POST', props, makeExternalPropsHeader(instance) ); }; /** * @memberof jiveClient * @param instance * @returns {*} */ exports.removeExtendedProperties = function( instance ) { return jive.util.buildRequest( extractExternalPropsUrl( instance ), 'DELETE', null, makeExternalPropsHeader(instance) ); }; /** * @memberof jiveClient * @param tileInstance * @param fetchURL * @returns {*} */ exports.tileFetch = function (tileInstance, fetchURL) { var auth = 'Bearer ' + tileInstance['accessToken']; var reqHeaders = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': auth }; return jive.util.buildRequest(fetchURL, 'GET', null, reqHeaders); }; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Private var tilePush = function (method, tileInstance, data, pushURL) { var auth = 'Bearer ' + tileInstance['accessToken']; var reqHeaders = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': auth }; if ( data &amp;&amp; !data['status'] ) { // add an empty status object if it doesn't exist data['status'] = {}; } return jive.util.buildRequest(pushURL, method, data, reqHeaders); }; function endsWith(str, suffix) { return str.indexOf(suffix, str.length - suffix.length) !== -1; } var extractExternalPropsUrl = function( instance ) { var instanceURL = instance['url']; if ( endsWith(instanceURL, '/data') ) { return instanceURL.match(/(.+)\/data/)[1] + '/extprops'; } if ( endsWith(instanceURL, '/activities') ) { return instanceURL.match(/(.+)\/activities/)[1] + '/extprops'; } throw new Error( 'Could not extract external props url from instance' ); }; var makeExternalPropsHeader = function(instance ) { var auth = 'Bearer ' + instance['accessToken']; return { 'X-Client-Id': jive.context.config['clientId'], 'Authorization' : auth }; }; </pre> </article> </section> </div> <div class="clearfix"></div> <footer> <span class="copyright"> Jive Software, Inc </span> <br /> <span class="jsdoc-message"> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-dev</a> on Wed Jan 22 2014 12:29:37 GMT-0800 (PST) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>. </span> </footer> </div> <br clear="both"> </div> </div> <script src="scripts/sunlight.js"></script> <script src="scripts/sunlight.javascript.js"></script> <script src="scripts/sunlight-plugin.doclinks.js"></script> <script src="scripts/sunlight-plugin.linenumbers.js"></script> <script src="scripts/sunlight-plugin.menu.js"></script> <script src="scripts/jquery.min.js"></script> <script src="scripts/jquery.scrollTo.js"></script> <script src="scripts/jquery.localScroll.js"></script> <script src="scripts/bootstrap-dropdown.js"></script> <script src="scripts/toc.js"></script> <script> Sunlight.highlightAll({lineNumbers:true, showMenu: true, enableDoclinks :true}); </script> <script> $( function () { $( "#toc" ).toc( { selectors : "h1,h2,h3,h4", showAndHide : false, scrollTo : 60 } ); $( "#toc>ul" ).addClass( "nav nav-pills nav-stacked" ); $( "#main span[id^='toc']" ).addClass( "toc-shim" ); } ); </script> </body> </html>