appc-platform-sdk
Advanced tools
Comparing version 1.0.45 to 1.0.46
@@ -11,12 +11,11 @@ /** | ||
*/ | ||
var request = require('appc-request-ssl'), | ||
urllib = require('url'), | ||
fs = require('fs'), | ||
var fs = require('fs'), | ||
debug = require('debug')('appc:sdk'), | ||
AppC = require('./index'); | ||
module.exports = App; | ||
function App() { | ||
} | ||
/** | ||
@@ -29,3 +28,3 @@ * find the apps that the logged in has access to | ||
*/ | ||
App.findAll = function findAll(session, org_id, callback) { | ||
App.findAll = function(session, org_id, callback) { | ||
if (org_id && typeof(org_id)==='function') { | ||
@@ -35,4 +34,3 @@ callback = org_id; | ||
} | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/app' + (org_id?('?org_id'+org_id):'')); | ||
request.get(AppC.createRequestObject(session,url),AppC.createAPIResponseHandler(callback)); | ||
AppC.createRequest(session, '/api/v1/app' + (org_id?('?org_id='+org_id):''), callback); | ||
}; | ||
@@ -47,5 +45,4 @@ | ||
*/ | ||
App.find = function find(session, app_id, callback) { | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/app/'+app_id); | ||
request.get(AppC.createRequestObject(session,url),AppC.createAPIResponseHandler(callback)); | ||
App.find = function(session, app_id, callback) { | ||
AppC.createRequest(session, '/api/v1/app/'+app_id, callback); | ||
}; | ||
@@ -60,9 +57,8 @@ | ||
*/ | ||
App.update = function update(session, app, callback) { | ||
App.update = function(session, app, callback) { | ||
var guid = app.app_guid; | ||
if (!guid) { throw new Error("no app_guid property found"); } | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/app/'+guid), | ||
obj = AppC.createRequestObject(session,url); | ||
obj.json = app; | ||
request.put(obj,AppC.createAPIResponseHandler(callback)); | ||
if (!guid) { | ||
return callback(new Error("no app_guid property found")); | ||
} | ||
AppC.createRequest(session, '/api/v1/app/'+guid, 'put', callback, app); | ||
}; | ||
@@ -78,7 +74,13 @@ | ||
*/ | ||
App.create = function create(session, tiappxml, org_id, callback) { | ||
if (typeof(org_id)==='object' || org_id===null) { | ||
callback = org_id; | ||
App.create = function(session, tiappxml, org_id, callback) { | ||
if (typeof(org_id)==='function' || org_id===null) { | ||
if(!!org_id) { | ||
callback = org_id; | ||
} | ||
// use the current session | ||
org_id = session.user.org_id; | ||
if(session && session.user && session.user.org_id) { | ||
org_id = session.user.org_id; | ||
} else { | ||
return callback(new Error("session is not valid")); | ||
} | ||
} | ||
@@ -88,12 +90,10 @@ if (!fs.existsSync(tiappxml)) { | ||
} | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/app/saveFromTiApp?org_id='+org_id); | ||
fs.readFile(tiappxml, function(err, data) { | ||
if (err) { return callback(err); } | ||
var obj = AppC.createRequestObject(session,url), | ||
req = request.post(obj, AppC.createAPIResponseHandler(callback)), | ||
form = req.form(); | ||
form.append('tiapp',data, {filename:'tiapp.xml'}); | ||
debug('form parameters for %s, %o',url,form); | ||
var req = AppC.createRequest(session, '/api/v1/app/saveFromTiApp?org_id='+org_id, 'post', callback); | ||
if(req) { | ||
var form = req.form(); | ||
form.append('tiapp',data, {filename:'tiapp.xml'}); | ||
debug('form parameters for %s, %o',req.url,form); | ||
} | ||
}); | ||
@@ -103,5 +103,3 @@ }; | ||
App.crittercismID = function crittercismID(session, guid, callback) { | ||
var url = urllib.resolve(AppC.baseurl, 'api/v1/app/'+guid+'/crittercism_id'), | ||
obj = AppC.createRequestObject(session, url); | ||
request.get(obj, AppC.createAPIResponseHandler(callback)); | ||
AppC.createRequest(session, 'api/v1/app/'+guid+'/crittercism_id', callback); | ||
}; | ||
@@ -111,10 +109,15 @@ | ||
* find an application package by application guid | ||
* | ||
* can be called with token or session | ||
* INTERNAL ONLY | ||
*/ | ||
App.findPackage = function findPackage(session, guid, token, callback) { | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/app/'+guid+'/package'), | ||
obj = AppC.createRequestObject(session,url); | ||
obj.headers = { 'x-auth-token': token }; | ||
request.get(obj,AppC.createAPIResponseHandler(callback)); | ||
App.findPackage = function(session, guid, token, callback) { | ||
if(typeof(session) === 'string'){ | ||
callback = token; | ||
token = guid; | ||
guid = session; | ||
session = null; | ||
} else if(typeof(token) === 'function') { | ||
callback = token; | ||
} | ||
AppC.createRequest(session || token, '/api/v1/app/'+guid+'/package', callback); | ||
}; | ||
@@ -125,8 +128,14 @@ | ||
*/ | ||
App.findTeamMembers = function findTeamMembers(session, guid, callback) { | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/app/'+guid+'/team'), | ||
obj = AppC.createRequestObject(session,url); | ||
request.get(obj,AppC.createAPIResponseHandler(callback)); | ||
App.findTeamMembers = function(session, guid, callback) { | ||
AppC.createRequest(session, '/api/v1/app/'+guid+'/team', callback); | ||
}; | ||
module.exports = App; | ||
/** | ||
* delete a specific app by id | ||
* @param {Object} session | ||
* @param {String} app id | ||
* @param {Function} callback | ||
*/ | ||
App.delete = function(session, app_id, callback) { | ||
AppC.createRequest(session, '/api/v1/app/'+app_id, 'del', callback); | ||
}; |
101
lib/auth.js
@@ -11,6 +11,3 @@ /** | ||
*/ | ||
var request = require('appc-request-ssl'), | ||
urllib = require('url'), | ||
AppC = require('./index'), | ||
User = require('./user'), | ||
var AppC = require('./index'), | ||
Session = require('./session'), | ||
@@ -23,2 +20,5 @@ tough = require('tough-cookie'), | ||
module.exports = Auth; | ||
function Auth() { | ||
@@ -41,8 +41,6 @@ } | ||
Auth.logout = function logout(session, callback) { | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/auth/logout'), | ||
next = function() { | ||
session._invalidate(); | ||
callback && callback(); | ||
}; | ||
request.post(AppC.createRequestObject(session,url), next); | ||
AppC.createRequest(session,'/api/v1/auth/logout', 'post', function(e) { | ||
session._invalidate(); | ||
callback && callback(e); | ||
}); | ||
}; | ||
@@ -98,3 +96,2 @@ | ||
var session = new Session(), | ||
url = urllib.resolve(AppC.baseurl, '/api/v1/auth/login'), | ||
checkError = function(err, result) { | ||
@@ -122,10 +119,8 @@ if (err) { | ||
callback(null,result); | ||
}, | ||
next = AppC.createAPIResponseHandler(checkError, function mapper(obj, next) { | ||
}; | ||
var r = AppC.createRequest(session, '/api/v1/auth/login', 'post', checkError, function mapper(obj, next) { | ||
session._set(obj); | ||
resolveUserOrg(session,next); | ||
}), | ||
r = request.post(AppC.createRequestObject(session,url), next), | ||
form = r.form(); | ||
form.append('username',username); | ||
@@ -137,3 +132,3 @@ form.append('password',password); | ||
debug('device id is %s',macAddress); | ||
debug('form parameters for %s, %o',url,form); | ||
debug('form parameters for %s, %o', r.url,form); | ||
}); | ||
@@ -194,3 +189,2 @@ }; | ||
host = parse.host, | ||
path = parse.path, | ||
tok = host.split('.'), | ||
@@ -200,2 +194,4 @@ subdomain = tok.splice(tok.length-2,2).join('.'), | ||
session.id = id; | ||
// for now, since we are transitioning cookies both from FQDN to base domain | ||
@@ -220,3 +216,6 @@ // AND we are renaming the cookie, we need to go ahead and set for all cases | ||
// fetch the current user and set it on the session | ||
User.find(session,function(err,user){ | ||
AppC.User.find(session,function(err,user){ | ||
if (err && err.code === 403) { | ||
return callback(makeError('invalid session',Auth.ERROR_NOT_AUTHORIZED)); | ||
} | ||
if (err) { return callback(err); } | ||
@@ -241,5 +240,4 @@ session.user = user; | ||
Auth.requestLoginCode = function requestLoginCode(session, sms, callback) { | ||
User.find(session, function(err,user){ | ||
AppC.User.find(session, function(err,user){ | ||
if (err) { return callback(err); } | ||
var error; | ||
if (user.disable_2fa) { | ||
@@ -251,12 +249,27 @@ return callback(makeError('Two-factor authentication is disabled',Auth.ERROR_TWOFACTOR_DISABLED)); | ||
} | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/auth/deviceauth/resend'), | ||
next = function(err,body) { | ||
if (err) { return callback(err); } | ||
callback(null,body); | ||
}, | ||
r = request.post(AppC.createRequestObject(session,url), AppC.createAPIResponseHandler(next)), | ||
form = r.form(); | ||
var r = AppC.createRequest(session, '/api/v1/auth/deviceauth/resend', 'post', function(err,body) { | ||
if (err) { return callback(err); } | ||
callback(null,body); | ||
}); | ||
if (r) { | ||
var form = r.form(); | ||
form.append('sendby',sms ? 'sms' : 'email'); | ||
form.append('sendto',sms ? user.phone : user.email); | ||
} | ||
}); | ||
}; | ||
form.append('sendby',sms ? 'sms' : 'email'); | ||
form.append('sendto',sms ? user.phone : user.email); | ||
/** | ||
* validate a session with platform, returns basic user identity if success or | ||
* error if invalid session | ||
* | ||
* @param {Object} session object | ||
* @param {Function} callback returns session details (as 2nd parameter) if valid | ||
*/ | ||
Auth.validateSession = function validateSession(session, callback) { | ||
AppC.createRequest(session, '/api/v1/auth/checkSession','get', function(err){ | ||
if (err) { | ||
return callback(makeError('invalid session',Auth.ERROR_NOT_AUTHORIZED)); | ||
} | ||
callback.apply(null,arguments); | ||
}); | ||
@@ -273,19 +286,17 @@ }; | ||
Auth.verifyLoginCode = function verifyLoginCode(session, code, callback) { | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/auth/deviceauth'), | ||
next = function(err,result) { | ||
if (err) { return callback(err); } | ||
var error; | ||
if (result) { | ||
if (result.expired) { | ||
return callback(makeError('Your authorization code has expired.',Auth.ERROR_AUTH_CODE_EXPIRED)); | ||
} | ||
return callback(null, result.valid); | ||
var r = AppC.createRequest(session,'/api/v1/auth/deviceauth', 'post', function(err,result) { | ||
if (err) { return callback(err); } | ||
if (result) { | ||
if (result.expired) { | ||
return callback(makeError('Your authorization code has expired.',Auth.ERROR_AUTH_CODE_EXPIRED)); | ||
} | ||
return callback(makeError('Your authorization code was invalid.',Auth.ERROR_AUTH_CODE_INVALID)); | ||
}, | ||
r = request.post(AppC.createRequestObject(session,url), AppC.createAPIResponseHandler(next)), | ||
form = r.form(); | ||
form.append('code', code); | ||
return callback(null, result.valid); | ||
} | ||
return callback(makeError('Your authorization code was invalid.',Auth.ERROR_AUTH_CODE_INVALID)); | ||
}); | ||
if (r) { | ||
var form = r.form(); | ||
form.append('code', code); | ||
} | ||
}; | ||
module.exports = Auth; |
171
lib/cloud.js
@@ -8,4 +8,2 @@ /** | ||
var async = require('async'), | ||
request = require('appc-request-ssl'), | ||
urllib = require('url'), | ||
AppC = require('./index'), | ||
@@ -48,10 +46,14 @@ debug = require('debug')('appc:sdk'); | ||
function login(session, callback) { | ||
var url = Cloud.getEnvironment(session) + '/v1/admins/login360.json?ct=enterprise&connect.sid='+session.id; | ||
debug('acs login %s',url); | ||
request(AppC.createRequestObject(session,url), createCloudResponseHandler(function(err,body){ | ||
if (!err) { | ||
session.acs_session = body.meta.session_id; | ||
} | ||
try{ | ||
var url = Cloud.getEnvironment(session) + '/v1/admins/login360.json?ct=enterprise&connect.sid='+session.id; | ||
debug('acs login %s',url); | ||
AppC.createRequestCustomResponseHandler(session, url, createCloudResponseHandler(function(err,body){ | ||
if (!err && body) { | ||
session.acs_session = body.meta.session_id; | ||
} | ||
return callback(err, body); | ||
})); | ||
} catch (err) { | ||
return callback(err); | ||
})); | ||
} | ||
} | ||
@@ -67,59 +69,63 @@ | ||
Cloud.createNamedApp = function createNamedApp(session, name, callback) { | ||
var tasks = [], | ||
apps; | ||
try{ | ||
var tasks = [], | ||
apps; | ||
if (!session.acs_session) { | ||
if (!session.acs_session) { | ||
tasks.push(function(next){ | ||
login(session, next); | ||
}); | ||
} | ||
tasks.push(function(next){ | ||
login(session, next); | ||
var url = Cloud.getEnvironment(session) + '/v1/apps/create.json?_session_id='+session.acs_session+'&ct=enterprise'; | ||
var r = AppC.createRequestCustomResponseHandler(session, url, 'post', createCloudResponseHandler(function(err,body){ | ||
if (err) { return next(err); } | ||
apps = body.response.apps; | ||
if (!apps || apps.length===0) { | ||
return next(new Error("couldn't create cloud application. please try again.")); | ||
} | ||
next(); | ||
})); | ||
if (r) { | ||
var form = r.form(); | ||
form.append('name',name); | ||
debug('form parameters for %s, %o', r.url,form); | ||
} | ||
}); | ||
} | ||
tasks.push(function(next){ | ||
var url = Cloud.getEnvironment(session) + '/v1/apps/create.json?_session_id='+session.acs_session+'&ct=enterprise'; | ||
var r = request.post(AppC.createRequestObject(session,url), createCloudResponseHandler(function(err,body){ | ||
if (err) { return next(err); } | ||
apps = body.response.apps; | ||
if (!apps || apps.length===0) { | ||
return next(new Error("couldn't create cloud application. please retry again.")); | ||
} | ||
next(); | ||
})); | ||
var form = r.form(); | ||
form.append('name',name); | ||
debug('form parameters for %s, %o',url,form); | ||
}); | ||
tasks.push(function(next){ | ||
async.each(apps, function(app,cb){ | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/api'), | ||
obj = AppC.createRequestObject(session, url), | ||
req = request.post(obj, createACSResponseHandler('apps',function(err,result){ | ||
tasks.push(function(next){ | ||
async.each(apps, function(app,cb){ | ||
var req = AppC.createRequestCustomResponseHandler(session, '/api/v1/api', 'post', createACSResponseHandler('apps',function(err,result){ | ||
if (err) { return next(err); } | ||
if (result.length===0) { | ||
return next(new Error("no applications created. please re-try your request again.")); | ||
return next(new Error("no applications created. please try your request again.")); | ||
} | ||
app.dashboard = result; | ||
console.log('set',app); | ||
cb(); | ||
})), | ||
form = req.form(); | ||
})); | ||
if(req) { | ||
var form = req.form(); | ||
form.append('name', name); | ||
form.append('id', app.id); | ||
form.append('type', 'data'); | ||
form.append('org_id', session.user.org_id); | ||
debug('form parameters for %s, %o',req.url,form); | ||
} | ||
},function(err){ | ||
if (err) { return next(err); } | ||
callback(null, apps); | ||
}); | ||
}); | ||
form.append('name', name); | ||
form.append('id', app.id); | ||
form.append('type', 'data'); | ||
form.append('org_id', session.user.org_id); | ||
debug('form parameters for %s, %o',url,form); | ||
},function(err){ | ||
if (err) { return next(err); } | ||
callback(null, apps); | ||
async.series(tasks, function(err){ | ||
if (err) { return callback(err); } | ||
}); | ||
}); | ||
async.series(tasks, function(err){ | ||
if (err) { return callback(err); } | ||
}); | ||
} catch (err) { | ||
return callback(err); | ||
} | ||
}; | ||
function createACSResponseHandler(key, callback) { | ||
return AppC.createAPIResponseHandler(function(err,body) { | ||
return function(err,body) { | ||
if (err) { return callback(err); } | ||
@@ -143,3 +149,3 @@ if (body) { | ||
return callback(); | ||
}); | ||
}; | ||
} | ||
@@ -149,5 +155,7 @@ | ||
* create a new cloud app for a given application (returns an array of apps, one for each environment) | ||
* app_name, org_id or app_guid are optional (use null rather than omitting the value) | ||
* | ||
* @param {Object} session | ||
* @param {String} name of the app | ||
* @param {String} name of the app Appclerator by default | ||
* @param {String} org_id id of the org to create in | ||
* @param {String} app_guid application guid | ||
@@ -157,24 +165,16 @@ * @param {Function} callback | ||
Cloud.createApp = function createApp (session, app_name, org_id, app_guid, callback) { | ||
if (typeof(org_id) === 'function') { | ||
callback = org_id; | ||
app_guid = null; | ||
org_id = session.user.org_id; | ||
var req = AppC.createRequest(session, '/api/v1/acs', 'post', createACSResponseHandler('apps',callback)); | ||
if(req) { | ||
var form = req.form(); | ||
if (app_guid) { | ||
form.append('app_guid', app_guid); | ||
} | ||
if (app_name) { | ||
form.append('app_name',app_name); | ||
} | ||
if(org_id) { | ||
form.append('org_id',''+org_id); | ||
} | ||
debug('form parameters for %s, %o',req.url,form); | ||
} | ||
if (typeof(app_guid) === 'function') { | ||
callback = app_guid; | ||
app_guid = null; | ||
} | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/acs'), | ||
obj = AppC.createRequestObject(session, url), | ||
req = request.post(obj, createACSResponseHandler('apps',callback)), | ||
form = req.form(); | ||
if (app_guid) { | ||
form.append('app_guid', app_guid); | ||
} | ||
if (app_name) { | ||
form.append('app_name',app_name); | ||
} | ||
form.append('org_id',''+org_id); | ||
debug('form parameters for %s, %o',url,form); | ||
}; | ||
@@ -186,16 +186,15 @@ | ||
* @param {Object} session | ||
* @param {String} app_guid application guid | ||
* @param {String} guid api guid | ||
* @param {Object} keyvalues object of the properties to pass to create | ||
* @param {Function} callback | ||
*/ | ||
Cloud.createUser = function createUser (session, app_guid, keyvalues, callback) { | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/acs/'+app_guid+'/data.next/user'), | ||
obj = AppC.createRequestObject(session, url), | ||
req = request.post(obj, createACSResponseHandler('users',callback)), | ||
form = req.form(); | ||
Object.keys(keyvalues).forEach(function(key){ | ||
form.append(key, String(keyvalues[key])); | ||
}); | ||
debug('form parameters for %s, %o',url,form); | ||
Cloud.createUser = function createUser (session, guid, keyvalues, callback) { | ||
var req = AppC.createRequest(session, '/api/v1/acs/'+guid+'/data.next/user', 'post', createACSResponseHandler('users',callback)); | ||
if (req) { | ||
var form = req.form(); | ||
Object.keys(keyvalues).forEach(function(key){ | ||
form.append(key, String(keyvalues[key])); | ||
}); | ||
debug('form parameters for %s, %o',req.url,form); | ||
} | ||
}; | ||
@@ -202,0 +201,0 @@ |
@@ -11,5 +11,3 @@ /** | ||
*/ | ||
var request = require('appc-request-ssl'), | ||
urllib = require('url'), | ||
querystring = require('querystring'), | ||
var querystring = require('querystring'), | ||
AppC = require('./index'); | ||
@@ -37,6 +35,5 @@ | ||
} | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/feed?'+querystring.stringify(opts)); | ||
request.get(AppC.createRequestObject(session,url), AppC.createAPIResponseHandler(callback)); | ||
AppC.createRequest(session, '/api/v1/feed?'+querystring.stringify(opts), callback); | ||
}; | ||
module.exports = Feed; |
102
lib/index.js
@@ -25,3 +25,3 @@ /** | ||
// lazy load modules | ||
// lazy load modules | ||
props.forEach(function(tuple) { | ||
@@ -46,6 +46,2 @@ Object.defineProperty(AppC, tuple[0], { | ||
AppC.createAPIResponseHandler = createAPIResponseHandler; | ||
AppC.createRequestObject = createRequestObject; | ||
AppC.createRequest = createRequest; | ||
/** | ||
@@ -56,2 +52,4 @@ * set the base url to use production | ||
AppC.baseurl = 'https://dashboard.appcelerator.com'; | ||
AppC.registryurl = 'https://4db53bb79d364bf3ea0595f95cb2bd1d55f355cb.cloudapp-enterprise.appcelerator.com'; | ||
AppC.securityurl = 'https://4503ef0cc4daae71d3bb898f66c72b886c9f6d61.cloudapp-enterprise.appcelerator.com'; | ||
AppC.isProduction = true; | ||
@@ -68,2 +66,4 @@ AppC.supportUntrusted = false; | ||
AppC.baseurl = 'https://360-preprod.cloud.appctest.com'; | ||
AppC.registryurl = 'https://8d2938f67044d8367d468453b5a6c2536185bcea.cloudapp-enterprise-preprod.appctest.com'; | ||
AppC.securityurl = 'https://de7a3ab4b12bf1d3d4b7fde7f306c11bc2b98f67.cloudapp-enterprise-preprod.appctest.com'; | ||
AppC.isProduction = false; | ||
@@ -78,5 +78,5 @@ AppC.supportUntrusted = true; | ||
AppC.setLocal = function setLocal() { | ||
AppC.setDevelopment(); | ||
AppC.baseurl = 'https://360-local.appcelerator.com:8443'; | ||
AppC.isProduction = false; | ||
AppC.supportUntrusted = true; | ||
debug('set local to '+AppC.baseurl); | ||
@@ -91,2 +91,4 @@ }; | ||
AppC.baseurl = opts.baseurl || 'https://360-local.appcelerator.com:8443'; | ||
AppC.registryurl = opts.registry || AppC.registryurl; | ||
AppC.securityurl = opts.security || AppC.securityurl; | ||
AppC.isProduction = typeof opts.isProduction !== 'undefined' ? opts.isProduction : false; | ||
@@ -99,3 +101,3 @@ AppC.supportUntrusted = typeof opts.supportUntrusted !== 'undefined' ? opts.supportUntrusted : true; | ||
return process.env.NODE_ACS_URL && | ||
process.env.NODE_ACS_URL.indexOf('.appctest.com') > 0 || | ||
process.env.NODE_ACS_URL.indexOf('.appctest.com') > 0 || | ||
process.env.NODE_ENV==='preproduction' || | ||
@@ -109,3 +111,3 @@ process.env.APPC_ENV==='preproduction' || | ||
if (isRunningInPreproduction()) { | ||
AppC.setDevelopment(); | ||
AppC.setPreproduction(); | ||
} | ||
@@ -116,9 +118,35 @@ else { | ||
function createRequestObject(session,url) { | ||
if (!session || !session.jar) { | ||
throw new Error("session invalid"); | ||
/* | ||
* wrapper for existing functions which use createRequestObject. | ||
* This allows xauth token or session to be specified when creating an object | ||
*/ | ||
function createRequestObject(auth, url) { | ||
if(typeof(auth) === 'object') { | ||
if(!auth.jar) { | ||
} | ||
// auth is a session | ||
return _createRequestObject(auth, url); | ||
} | ||
if(typeof(auth) === 'string') { | ||
//auth is a token | ||
return _createRequestObject(url, auth); | ||
} | ||
} | ||
function _createRequestObject(session, url, authToken) { | ||
if(typeof(session) === 'object') { | ||
if (!session || !session.jar) { | ||
throw new Error("session is not valid"); | ||
} | ||
if(!url) { | ||
url = session; | ||
} | ||
} | ||
if(typeof(session) === 'string') { | ||
authToken = url; | ||
url = session; | ||
session = null; | ||
} | ||
var opts = { | ||
url: url || session, | ||
jar: session.jar, | ||
url: url, | ||
headers: { | ||
@@ -133,4 +161,11 @@ 'User-Agent': AppC.userAgent | ||
} | ||
if(authToken) { | ||
opts.headers['x-auth-token'] = authToken; | ||
} | ||
if(session) { | ||
opts.jar = session.jar; | ||
} | ||
debug('fetching',url); | ||
return opts; | ||
} | ||
@@ -169,9 +204,44 @@ | ||
*/ | ||
function createRequest(session, path, method, callback) { | ||
AppC.createRequest = function(session, path, method, callback, mapper, json) { | ||
if (typeof(method)==='function') { | ||
json = mapper; | ||
mapper = callback; | ||
callback = method; | ||
method = 'get'; | ||
} | ||
var url = require('url').resolve(AppC.baseurl, path); | ||
return require('appc-request-ssl')[method.toLowerCase()](AppC.createRequestObject(session,url),AppC.createAPIResponseHandler(callback||function(){})); | ||
if (typeof(mapper) === 'object') { | ||
json = mapper; | ||
mapper = null; | ||
} | ||
var responseHandler = createAPIResponseHandler(callback||function(){}, mapper||null); | ||
return _createRequest(session, path, method, responseHandler, json); | ||
}; | ||
/** | ||
* create a request to the platform and return the request object. this time with a custom handler | ||
*/ | ||
AppC.createRequestCustomResponseHandler = function(session, path, method, responseHandler, json) { | ||
if (typeof(method)==='function') { | ||
json = responseHandler; | ||
responseHandler = method; | ||
method = 'get'; | ||
} | ||
return _createRequest(session, path, method, responseHandler, json); | ||
}; | ||
function _createRequest(session, path, method, responseHandler, json) { | ||
var request = require('appc-request-ssl'); | ||
try { | ||
if(path[0] === '/') { | ||
path = require('url').resolve(AppC.baseurl, path); | ||
} | ||
var obj = createRequestObject(session, path); | ||
if(json) { | ||
obj.json = json; | ||
} | ||
return request[method.toLowerCase()](obj, responseHandler); | ||
} catch (e) { | ||
responseHandler(e); | ||
// don't return the callback since it expects a request | ||
} | ||
} |
@@ -7,21 +7,16 @@ /** | ||
*/ | ||
/** | ||
* make a request to AppC platform for fetching notifications | ||
*/ | ||
var request = require('appc-request-ssl'), | ||
urllib = require('url'), | ||
AppC = require('./index'); | ||
var AppC = require('./index'); | ||
function Notification() { | ||
} | ||
module.exports = Notification; | ||
function Notification() {} | ||
/** | ||
* find all the notifications for the logged in user | ||
*/ | ||
Notification.findAll = function findAll(session, callback) { | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/notification'); | ||
request.get(AppC.createRequestObject(session,url), AppC.createAPIResponseHandler(callback)); | ||
}; | ||
module.exports = Notification; | ||
Notification.findAll = function(session, callback) { | ||
AppC.createRequest(session, '/api/v1/notification', callback); | ||
}; |
@@ -11,5 +11,3 @@ /** | ||
*/ | ||
var request = require('appc-request-ssl'), | ||
urllib = require('url'), | ||
AppC = require('./index'), | ||
var AppC = require('./index'), | ||
cachedOrgKey, | ||
@@ -26,2 +24,7 @@ cachedOrg; | ||
Org.find = function find(session, callback) { | ||
if(!session || !session.id || !session.user) { | ||
return callback(new Error('session is not valid')); | ||
} | ||
if (cachedOrg && cachedOrgKey===session.id) { | ||
@@ -32,17 +35,21 @@ return callback(null, cachedOrg); | ||
((session && session.user && session.user.is_staff) ? '&include_all=true' : ''); | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/user/organizations' + params), | ||
next = function(err,org) { | ||
if (err) { return callback(err); } | ||
cachedOrg = org; | ||
cachedOrgKey = session.id; | ||
callback(null,org); | ||
}; | ||
request.get(AppC.createRequestObject(session,url),AppC.createAPIResponseHandler(next)); | ||
AppC.createRequest(session, '/api/v1/user/organizations' + params, 'get', function(err,org) { | ||
if (err) { return callback(err); } | ||
cachedOrg = org; | ||
cachedOrgKey = session.id; | ||
return callback(null,org); | ||
}); | ||
}; | ||
/** | ||
* return an organization by | ||
* return an organization by checking the session | ||
*/ | ||
Org.getById = function getById(session, id) { | ||
return session && session.orgs && session.orgs[id]; | ||
Org.getById = function getById(session, id, callback) { | ||
try { | ||
var org_id = session.orgs[id]; | ||
if(!org_id) { return callback(new Error('id is not valid')); } | ||
return callback(null, org_id); | ||
} catch (e) { | ||
return callback(new Error('session is not valid')); | ||
} | ||
}; | ||
@@ -54,4 +61,4 @@ | ||
Org.findById = function findById(session, id, callback) { | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/org/'+id); | ||
request.get(AppC.createRequestObject(session,url),AppC.createAPIResponseHandler(callback)); | ||
if(!id) { return callback(new Error('id is not valid')); } | ||
AppC.createRequest(session,'/api/v1/org/'+id, callback); | ||
}; | ||
@@ -62,11 +69,16 @@ | ||
*/ | ||
Org.getByName = function getByName(session, name) { | ||
var keys = Object.keys(session.orgs || {}), | ||
length = keys.length; | ||
for (var c=0;c<length;c++) { | ||
var org_id = keys[c], | ||
org = session.orgs[org_id]; | ||
if (org.name===name) { | ||
return org; | ||
Org.getByName = function getByName(session, name, callback) { | ||
try { | ||
var keys = Object.keys(session.orgs || {}), | ||
length = keys.length; | ||
for (var c=0;c<length;c++) { | ||
var org_id = keys[c], | ||
org = session.orgs[org_id]; | ||
if (org.name===name) { | ||
return callback(null, org); | ||
} | ||
} | ||
return callback(new Error('Org not found')); | ||
} catch (e) { | ||
return callback(new Error('session is not valid')); | ||
} | ||
@@ -78,4 +90,8 @@ }; | ||
*/ | ||
Org.getCurrent = function getCurrent(session) { | ||
return session.user.org; | ||
Org.getCurrent = function getCurrent(session, callback) { | ||
try { | ||
return callback(null, session.user.org); | ||
} catch (e) { | ||
return callback(new Error('session is not valid')); | ||
} | ||
}; | ||
@@ -82,0 +98,0 @@ |
@@ -9,6 +9,4 @@ /** | ||
var Auth = require('./auth'), | ||
request = require('appc-request-ssl'), | ||
urllib = require('url'), | ||
AppC = require('./index'); | ||
var AppC = require('./'), | ||
request = require('appc-request-ssl'); | ||
@@ -25,3 +23,3 @@ function Session(host, subdomain) { | ||
Session.prototype.isValid = function isValid() { | ||
return this.jar && this.user && this.id; | ||
return !!(this.jar && this.user && this.id); | ||
}; | ||
@@ -34,7 +32,6 @@ | ||
if (this.isValid()) { | ||
Auth.logout(this); | ||
AppC.Auth.logout(this); | ||
} | ||
}; | ||
//---------------------- private methods --------------------------- | ||
@@ -41,0 +38,0 @@ |
@@ -11,5 +11,3 @@ /** | ||
*/ | ||
var request = require('appc-request-ssl'), | ||
urllib = require('url'), | ||
AppC = require('./index'), | ||
var AppC = require('./index'), | ||
debug = require('debug')('appc:sdk'), | ||
@@ -45,12 +43,10 @@ cachedUser, | ||
} | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/user/'+user_id), | ||
next = function(err,user) { | ||
if (err) { return callback(err); } | ||
if (key) { | ||
cachedUserKey = key; | ||
cachedUser = user; | ||
} | ||
return callback(null, user); | ||
}; | ||
request.get(AppC.createRequestObject(session,url), AppC.createAPIResponseHandler(next)); | ||
AppC.createRequest(session, '/api/v1/user/'+user_id, function(err,user) { | ||
if (err) { return callback(err); } | ||
if (key) { | ||
cachedUserKey = key; | ||
cachedUser = user; | ||
} | ||
return callback(null, user); | ||
}); | ||
}; | ||
@@ -62,17 +58,18 @@ | ||
User.switchLoggedInOrg = function switchLoggedInOrg(session, org_id, callback) { | ||
var org = AppC.Org.getById(session, org_id); | ||
if (!org) { return callback("Cannot find org_id: "+org_id); } | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/auth/switchLoggedInOrg'), | ||
next = AppC.createAPIResponseHandler(callback, function mapper(obj, next) { | ||
AppC.Org.getById(session, org_id, function(err, org){ | ||
if(err) { return callback(err); } | ||
var req = AppC.createRequest(session,'/api/v1/auth/switchLoggedInOrg', 'post', callback, function mapper(obj, next) { | ||
session.user.org = org; | ||
session.user.org_id = org_id; | ||
next(null,obj); | ||
}), | ||
r = request.post(AppC.createRequestObject(session,url), next), | ||
form = r.form(); | ||
}); | ||
if (req) { | ||
var form = req.form(); | ||
form.append('org_id',org_id); | ||
debug('form parameters for %s, %o',req.url,form); | ||
} | ||
}); | ||
form.append('org_id',org_id); | ||
debug('form parameters for %s, %o',url,form); | ||
}; | ||
module.exports = User; |
{ | ||
"name": "appc-platform-sdk", | ||
"version": "1.0.45", | ||
"version": "1.0.46", | ||
"description": "Appcelerator Platform SDK for node.js", | ||
@@ -32,4 +32,11 @@ "main": "index.js", | ||
"ensure-latest": "^1.0.2", | ||
"appc-registry-sdk": "*", | ||
"grunt": "^0.4.5", | ||
"grunt-mocha-test": "^0.11.0", | ||
"grunt-contrib-jshint": "^0.10.0", | ||
"prompt": "^0.2.14", | ||
"should": "^4.0.4", | ||
"mocha": "2.1.0", | ||
"underscore": "1.7.0", | ||
"webdriverio": "2.2.2", | ||
"prompt": "^0.2.14" | ||
@@ -36,0 +43,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
# Appcelerator Platform SDK | ||
# Appcelerator Platform SDK [![Build Status](https://travis-ci.org/appcelerator/appc-platform-sdk.svg)](https://travis-ci.org/appcelerator/appc-platform-sdk) [![npm version](https://badge.fury.io/js/appc-platform-sdk.svg)](http://badge.fury.io/js/appc-platform-sdk) | ||
@@ -3,0 +3,0 @@ This is a Node module that will allow you to make API requests to the Appcelerator Platform. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43452
1073
10
1