Permalink
Browse files

Re-organize code to make the code available through npm.

  • Loading branch information...
1 parent 541ba5a commit 6e03694d77ec67bf9a29a07ea3b306f3aab85138 @germallon committed Jun 27, 2012
Showing with 229 additions and 227 deletions.
  1. +6 −8 README.md
  2. +197 −3 apiWriter.js → api.js
  3. +0 −208 apiReader.js
  4. +7 −8 interface.js
  5. +19 −0 package.json
View
@@ -22,11 +22,10 @@ The interface allows you to interact with the API, but it is mainly provided to
Here's an example on how to display the user data on standard output:
<pre>
- var reader = require('./apiReader');
- var writer = require('./apiWriter');
+ var api = require('phonegapbuildapi');
- writer.createAuthToken("my@email.com:myp4ssw0rd", function(token){
- reader.getUserData(token, function(userData){
+ api.createAuthToken("my@email.com:myp4ssw0rd", function(token){
+ api.getUserData(token, function(userData){
console.log(userData); //Output user data to stdout
});
});
@@ -37,14 +36,13 @@ Here's an example on how to display the user data on standard output:
Error handling is supported through callbacks. The above example can be expanded to handle any errors found.
<pre>
- var reader = require('./apiReader');
- var writer = require('./apiWriter');
+ var api = require('phonegapbuildapi');
- writer.createAuthToken("my@email.com:myp4ssw0rd", {
+ api.createAuthToken("my@email.com:myp4ssw0rd", {
success:function(token){
- reader.getUserData(token, {
+ api.getUserData(token, {
success:function(userData){
console.log(userData); //Output user data to stdout
},
View
@@ -16,12 +16,191 @@
**************************************************************************/
var
+_URL = 'build.phonegap.com',
_fs = require('fs'),
_https = require('https'),
_mime = require('mime'),
_req = require('request'),
_URL = 'build.phonegap.com'
+/******************************************************************
+ * Executes the GET Phonegap API call,
+ * and writes the output in a JSON-formatted file
+ ******************************************************************/
+getApiData = function(token, apiCall, callback){
+ var
+ options = null;
+
+ options = {
+ host: _URL,
+ path: '/api/v1/' + apiCall + '?auth_token='+token
+ };
+
+ _https.get(options, function(res){
+ var replyData = '';
+ res.on('data', function(data){
+ replyData+= data;
+ });
+ res.on('end', function(){
+ if(callback instanceof Function){
+ callback(JSON.parse(replyData));
+ }
+ else if (callback.success instanceof Function){
+ callback.success(JSON.parse(replyData));
+ }
+ });
+ }).on('error', function(e){
+ console.log("AJAX Err. Message: " + e.message);
+ if(callback.error && (callback.error instanceof Function)){callback.error(e.message);}
+ });
+ },
+
+/******************************************************************
+ * Downloads the file at the given URL and saves it in the provided
+ * path.
+ ******************************************************************/
+downloadFile = function(url, outputFilepath, callback){
+ var successCallback = (callback instanceof Function)? callback: (callback.success instanceof Function)? callback.success: function(){};
+ var errCallback = (callback.error instanceof Function)? callback.error: function(){};
+
+ _req.get(url).pipe(_fs.createWriteStream(outputFilepath))
+ .on('error', function(e){errCallback(e.message);})
+ .on('close', function(){successCallback(outputFilepath)});
+},
+
+/******************************************************************
+ * Get a JSON-encoded representation of the authenticated user,
+ * as well as a listing of associated resources.
+ *
+ * This should be the starting point for applications traversing
+ * the PhoneGap Build API. It is aliased to
+ * https://build.phonegap.com/api/v1.
+ *
+ * GET https://build.phonegap.com/api/v1/me
+ *****************************************************************/
+_getUserData = function(token, callback){
+ getApiData(token, 'me', callback);
+},
+
+/******************************************************************
+ * Get a JSON-encoded representation of the authenticated user's
+ * apps.
+ *
+ * API clients can follow the link attribute for each app to get
+ * further details, including the associated signing keys and
+ * collaborators.
+ *
+ * GET https://build.phonegap.com/api/v1/apps
+ *****************************************************************/
+_getAppsData = function(token, callback){
+ getApiData(token, 'apps', callback);
+ },
+
+
+/******************************************************************
+ * Get a JSON-encoded representation of a particular app, if the
+ * authenticated user has permission to access it.
+ *
+ * In addition to the fields provided in the list of all apps,
+ * this detail view includes:
+ *
+ * keys: all of the keys that the app is currently being built with.
+ * This will include the owner's default key for a platform,
+ * if selected
+ *
+ * collaborators: each person who has access to this app, along
+ * with their role, if the authenticated user is the owner of
+ * the app. Collaborators who are registered with PhoneGap Build
+ * are listed under active; collaborators you have invited who
+ * have not yet created an account are listed as pending.
+ *
+ * GET https://build.phonegap.com/api/v1/apps/:id
+ *****************************************************************/
+_getAppDataById = function(token, appId, callback){
+ getApiData(token, 'apps/' + appId, callback);
+ },
+
+/******************************************************************
+ * Get a JSON-encoded list of all the signing keys associated with
+ * your account.
+ *
+ * This returns a short listing of all the associated keys--it's
+ * very similar to the list you'll see when requesting /api/v1/me
+ *
+ * GET https://build.phonegap.com/api/v1/keys
+ *****************************************************************/
+_getKeysData = function(token, callback){
+ getApiData(token, 'keys', callback);
+ },
+
+
+/******************************************************************
+ * Get a JSON-encoded list of all the signing keys associated with
+ * your account, for a specific platform. That platform can be one
+ * of ios, android, or blackberry.
+ *
+ * GET https://build.phonegap.com/api/v1/keys/:platform
+ *****************************************************************/
+_getPlatformKeys = function(platform){
+ getApiData('keys/' + platform, METADATA_DIR + '/keys_' + platform + '.json');
+ },
+
+/******************************************************************
+ * Get a JSON-encoded representation of a single signing key.
+ *
+ * GET https://build.phonegap.com/api/v1/keys/:platform/:appId
+ *****************************************************************/
+_getPlatformKeyById = function(platform, appId){
+ getApiData('keys/' + platform + '/' + appId, METADATA_DIR + '/keys_' + platform + '_' + appId + '.json');
+ },
+
+/******************************************************************
+ * Download the app package for the given platform; available
+ * platforms are android, blackberry, ios, symbian, webos and
+ * winphone.
+ *
+ * In the successful case, this API method will return a 302
+ * redirect to the application binary - the actual body of the
+ * response will point to the resource's correct location:
+ *
+ * If using the optional argument for the download location,
+ * please ensure that you are using the right extension for
+ * the platform you are downloading.
+ *
+ * apk for Android
+ * ipa for iOS
+ * ipk for webOS
+ * jad for unsigned BlackBerry builds; zip if you've uploaded your BlackBerry signing keys
+ * wgz for Symbian
+ * xap for Windows Phone
+ *
+ * GET https://build.phonegap.com/api/v1/apps/:id/:platform
+ *****************************************************************/
+_downloadApp = function(token, appId, platform, outputFilepath, callback){
+ var url= 'https://' + _URL + '/api/v1/apps/' + appId + '/' + platform + '?auth_token='+token;
+ console.info("\n\nStarting Download...");
+ downloadFile(url, outputFilepath, callback);
+ },
+
+/*****************************************************************
+ * Get the main icon associated with an app - this is either the
+ * biggest icon specified in your config.xml file, or an icon you
+ * have uploaded through the API or the PhoneGap Build web interface.
+ *
+ * GET https://build.phonegap.com/api/v1/apps/:id/:icon
+*****************************************************************/
+_downloadIcon = function(token, appId, outputFilepath, callback){
+ var url= 'https://' + _URL + '/api/v1/apps/' + appId + '/icon';
+ downloadFile(url, outputFilepath, callback);
+ },
+
+/************************************
+ * Init Write API
+ */
+
+
+
+
encodeFieldHeader = function(boundary, name, value){
var result = [
"--" + boundary + "\r\n",
@@ -237,12 +416,27 @@ _createAuthToken = function(rawCredentials, callback){
}
});
}
-;
-
+
+
+
+/******************************************************************
+ * Module Public Members
+ *****************************************************************/
module.exports = {
+ //Read API
+ getUserData: _getUserData,
+ getAppsData: _getAppsData,
+ getAppDataById: _getAppDataById,
+ getKeysData: _getKeysData,
+ getPlatformKeys:_getPlatformKeys,
+ getPlatformKeyById: _getPlatformKeyById,
+ downloadApp: _downloadApp,
+ downloadIcon:_downloadIcon,
+
+ //Write API
createFileBasedApp:_createFileBasedApp,
updateFileBasedApp:_updateFileBasedApp,
uploadAppIcon: _uploadAppIcon,
- createAuthToken: _createAuthToken,
+ createAuthToken: _createAuthToken
};
Oops, something went wrong.

0 comments on commit 6e03694

Please sign in to comment.