diff --git a/CHANGELOG.md b/CHANGELOG.md index a184733..5a1dd49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +# 1.0.4 (11-16-2014) + +- Added support for creating new application from tiapp.xml +- Added support for creating session from session id +- Added support for retrieving cloud environments +- Added support for both production and development logins +- User.find will now use current user if not user id passed + # 1.0.1 (09-29-2014) - Added support for creating an ACS application diff --git a/example.js b/example.js index 4e046a2..66e425a 100644 --- a/example.js +++ b/example.js @@ -8,16 +8,47 @@ if (args.length!==2) { process.exit(1); } +function Decycler() { + var cache = []; + return function(key, value) { + if (typeof value === 'object' && value !== null) { + if (cache.indexOf(value) !== -1) { + // Circular reference found, discard key + return; + } + // Store value in our collection + cache.push(value); + } + return value; + }; +} + +function die() { + console.error.apply(console.err,arguments); + process.exit(1); +} + +sdk.setDevelopment(); + sdk.Auth.login(args[0], args[1], function(err,session){ - if (err) { - console.err('Error=',err); - process.exit(1); + if (err) { die('Error=',err); } + console.log(JSON.stringify(session,Decycler(),2)); + + /* + sdk.App.create(session, '/Users/jhaynie/tmp/foo/app/tiapp.xml', function(err,result){ + console.log(arguments); + });*/ + + /*try { + sdk.Auth.createSessionFromID(session.id, function(err,session_){ + if (err) { die('Error=',err); } + sdk.App.findAll(session_, function(err, apps){ + if (err) { die('Error=',err); } + console.log(apps); + }); + }); } - sdk.Cloud.createApp(session, "foo", function(err,app){ - if (err) { - console.err('Error=',err); - process.exit(1); - } - console.log('Application created: ',app); - }); + catch (E) { + console.error(E.stack); + }*/ }); diff --git a/lib/app.js b/lib/app.js index cce22bb..a727909 100644 --- a/lib/app.js +++ b/lib/app.js @@ -3,6 +3,7 @@ */ var request = require('request'), urllib = require('url'), + fs = require('fs'), AppC = require('./index'); function App() { @@ -11,6 +12,10 @@ function App() { /** * find the apps that the logged in has access to + * + * @param {Object} session + * @param {String} org id + * @param {Function} callback */ App.findAll = function(session, org_id, callback) { if (org_id && typeof(org_id)=='function') { @@ -18,24 +23,59 @@ App.findAll = function(session, org_id, callback) { org_id = null; } var url = urllib.resolve(AppC.baseurl, '/api/v1/app' + (org_id?('?org_id'+org_id):'')); - request.get(AppC.createRequestObject(session,url),AppC.createAPIResponseHandler(callback)); + request.get(AppC.createRequestObject(session,url),AppC.createAPIResponseHandler(callback)); } /** * find a specific app by id + * + * @param {Object} session + * @param {String} app id + * @param {Function} callback */ App.find = function(session, app_id, callback) { var url = urllib.resolve(AppC.baseurl, '/api/v1/app/'+app_id); request.get(AppC.createRequestObject(session,url),AppC.createAPIResponseHandler(callback)); } +/** + * update an app + * + * @param {Object} session + * @param {Object} app object to update + * @param {Function} 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)); + request.put(obj,AppC.createAPIResponseHandler(callback)); +} + +/** + * create or update an application from tiapp.xml file + * + * @param {Object} session + * @param {String} file location (path) to tiapp.xml + * @param {Function} callback + */ +App.create = function(session, tiappxml, callback) { + if (!fs.existsSync(tiappxml)) { + return callback(new Error("tiapp.xml file doesn't not exist")); + } + var url = urllib.resolve(AppC.baseurl, '/api/v1/app/saveFromTiApp?org_id='+session.user.org_id); + console.log(url); + 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'}); + }); } module.exports = App; diff --git a/lib/auth.js b/lib/auth.js index c7b15a1..d922f9e 100644 --- a/lib/auth.js +++ b/lib/auth.js @@ -4,7 +4,9 @@ var request = require('request'), urllib = require('url'), AppC = require('./index'), - Session = require('./session'); + User = require('./user'), + Session = require('./session'), + tough = require('tough-cookie'); function Auth() { } @@ -22,6 +24,21 @@ Auth.logout = function(session, callback) { request.post(AppC.createRequestObject(session,url), next); } +function resolveUserOrg(session, next) { + // find our orgs + AppC.Org.find(session,function(err,orgs){ + if (err) return next(err); + session.orgs = {}; + // map in our orgs + orgs && orgs.forEach(function(org){ + session.orgs[org.org_id] = org; + }); + // set our org to the logged in org + session.user.org_id && (session.user.org = session.orgs[session.user.org_id]); + next(null,session); + }); +} + /** * login */ @@ -30,18 +47,7 @@ Auth.login = function(username, password, callback) { url = urllib.resolve(AppC.baseurl, '/api/v1/auth/login'), next = AppC.createAPIResponseHandler(callback, function mapper(obj, next) { session._set(obj); - // find our orgs - AppC.Org.find(session,function(err,orgs){ - if (err) return callback(err); - session.orgs = {}; - // map in our orgs - orgs && orgs.forEach(function(org){ - session.orgs[org.org_id] = org; - }); - // set our org to the logged in org - session.user.org_id && (session.user.org = session.orgs[session.user.org_id]); - next(null,session); - }); + resolveUserOrg(session,next); }), r = request.post(AppC.createRequestObject(session,url), next), form = r.form(); @@ -52,4 +58,42 @@ Auth.login = function(username, password, callback) { } +/** + * from an existing authenticated session, create a new Session object + */ +Auth.createSessionFromID = function createSessionFromID(id, callback) { + var session = new Session(), + url = require('url'), + parse = url.parse(AppC.baseurl), + host = parse.host, + path = parse.path, + cookie = new tough.Cookie(); + + cookie.key = 'connect.sid'; + cookie.value = id; + cookie.secure = true; + cookie.httpOnly = true; + cookie.path = path; + cookie.domain = host; + cookie.expires = Infinity; + cookie.hostOnly = false + cookie.creation = new Date; + cookie.lastAccessed = new Date; + + session.id = id; + session.jar = new tough.CookieJar(); + + session.jar.setCookie(cookie, AppC.baseurl, function(err, cookie){ + if (err) { return callback(err); } + // fetch the current user and set it on the session + User.find(session,function(err,user){ + if (err) { return callback(err); } + session.user = user; + resolveUserOrg(session,function(err){ + callback(err,session); + }); + }); + }); +}; + module.exports = Auth; diff --git a/lib/cloud.js b/lib/cloud.js index 6dd894f..9cd650e 100644 --- a/lib/cloud.js +++ b/lib/cloud.js @@ -2,12 +2,9 @@ var async = require('async'), request = require('request'), AppC = require('./index'); -Cloud.baseurl = 'https://api.cloud.appcelerator.com'; - function Cloud() { } - function createCloudResponseHandler(callback) { return function(err,response,body) { if (err) { return callback(err); } @@ -32,7 +29,7 @@ function createCloudResponseHandler(callback) { */ function login(session, callback) { - var url = Cloud.baseurl + "/v1/admins/login360.json?ct=enterprise&connect.sid="+session.id; + var url = Cloud.getEnvironment(session) + '/v1/admins/login360.json?ct=enterprise&connect.sid='+session.id; request(AppC.createRequestObject(session,url), createCloudResponseHandler(function(err,body){ if (!err) { @@ -55,7 +52,7 @@ Cloud.createApp = function createApp(session, name, callback) { } tasks.push(function(next){ - var url = Cloud.baseurl + '/v1/apps/create.json?_session_id='+session.acs_session+'&ct=enterprise'; + 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); } callback(null, body.response.apps[0]); @@ -70,4 +67,28 @@ Cloud.createApp = function createApp(session, name, callback) { }); }; +Cloud.NODE_ACS = 'nodeACSEndpoint'; +Cloud.ACS_BASE = 'acsBaseUrl'; +Cloud.AUTH_BASE = 'acsAuthBaseUrl'; + +/** + * return the appropriate environment url. + * + * @param {String} type is one of Cloud.NODE_ACS, Cloud.ACS_BASE or Cloud.AUTH_BASE + * @param {String} name is one of production or development or another environment name + */ +Cloud.getEnvironment = function(session, type, name) { + if (!session.user) { + throw new Error("session is not valid. missing user"); + } + if (!session.org) { + throw new Error("session is not valid. missing org"); + } + type = type || Cloud.ACS_BASE; + name = name || AppC.isProduction ? 'production' : 'development'; + return session.org.envs.filter(function(env){ + return env.name===name; + })[0][type]; +}; + module.exports = Cloud; diff --git a/lib/index.js b/lib/index.js index cb62bec..6fb31ba 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,8 +5,6 @@ function AppC() { exports = module.exports = AppC; -AppC.baseurl = 'https://dashboard.appcelerator.com'; - AppC.Auth = require('./auth'); AppC.Org = require('./org'); AppC.User = require('./user'); @@ -21,6 +19,26 @@ AppC.userAgent = pkg.name+'/'+pkg.version; AppC.createAPIResponseHandler = createAPIResponseHandler; AppC.createRequestObject = createRequestObject; +/** + * set the base url to use production + */ +AppC.setProduction = function setProduction() { + AppC.baseurl = 'https://dashboard.appcelerator.com'; + AppC.isProduction = true; +}; + +/** + * set the base url to use development + */ +AppC.setDevelopment = function setDevelopment() { + AppC.baseurl = 'https://360-preprod.appcelerator.com'; + AppC.isProduction = false; +}; + + +// set DEFAULT to production host +AppC.setProduction(); + function createRequestObject(session,url) { if (!session || !session.jar) { throw new Error("session invalid"); diff --git a/lib/user.js b/lib/user.js index 5a62ecf..490e1b4 100644 --- a/lib/user.js +++ b/lib/user.js @@ -15,7 +15,12 @@ function User() { User.find = function(session, user_id, callback) { if (typeof(user_id)==='function') { callback = user_id; - user_id = session && session.user && session.user.guid; + if (session && session.user && session.user.guid) { + user_id = session.user.guid; + } + else { + user_id = 'current'; + } } var url = urllib.resolve(AppC.baseurl, '/api/v1/user/'+user_id); request.get(AppC.createRequestObject(session,url), AppC.createAPIResponseHandler(callback)); diff --git a/package.json b/package.json index 26be1df..e34c178 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "appc-platform-sdk", - "version": "1.0.3", + "version": "1.0.4", "description": "Appcelerator Platform SDK for node.js", "main": "index.js", "scripts": {