Skip to content

Commit 6e03694

Browse files
author
German Escallon
committed
Re-organize code to make the code available through npm.
1 parent 541ba5a commit 6e03694

File tree

5 files changed

+229
-227
lines changed

5 files changed

+229
-227
lines changed

README.md

+6-8
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ The interface allows you to interact with the API, but it is mainly provided to
2222
Here's an example on how to display the user data on standard output:
2323

2424
<pre>
25-
var reader = require('./apiReader');
26-
var writer = require('./apiWriter');
25+
var api = require('phonegapbuildapi');
2726

28-
writer.createAuthToken("my@email.com:myp4ssw0rd", function(token){
29-
reader.getUserData(token, function(userData){
27+
api.createAuthToken("my@email.com:myp4ssw0rd", function(token){
28+
api.getUserData(token, function(userData){
3029
console.log(userData); //Output user data to stdout
3130
});
3231
});
@@ -37,14 +36,13 @@ Here's an example on how to display the user data on standard output:
3736
Error handling is supported through callbacks. The above example can be expanded to handle any errors found.
3837

3938
<pre>
40-
var reader = require('./apiReader');
41-
var writer = require('./apiWriter');
39+
var api = require('phonegapbuildapi');
4240

43-
writer.createAuthToken("my@email.com:myp4ssw0rd", {
41+
api.createAuthToken("my@email.com:myp4ssw0rd", {
4442

4543
success:function(token){
4644

47-
reader.getUserData(token, {
45+
api.getUserData(token, {
4846
success:function(userData){
4947
console.log(userData); //Output user data to stdout
5048
},

apiWriter.js api.js

+197-3
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,191 @@
1616
**************************************************************************/
1717

1818
var
19+
_URL = 'build.phonegap.com',
1920
_fs = require('fs'),
2021
_https = require('https'),
2122
_mime = require('mime'),
2223
_req = require('request'),
2324
_URL = 'build.phonegap.com'
2425

26+
/******************************************************************
27+
* Executes the GET Phonegap API call,
28+
* and writes the output in a JSON-formatted file
29+
******************************************************************/
30+
getApiData = function(token, apiCall, callback){
31+
var
32+
options = null;
33+
34+
options = {
35+
host: _URL,
36+
path: '/api/v1/' + apiCall + '?auth_token='+token
37+
};
38+
39+
_https.get(options, function(res){
40+
var replyData = '';
41+
res.on('data', function(data){
42+
replyData+= data;
43+
});
44+
res.on('end', function(){
45+
if(callback instanceof Function){
46+
callback(JSON.parse(replyData));
47+
}
48+
else if (callback.success instanceof Function){
49+
callback.success(JSON.parse(replyData));
50+
}
51+
});
52+
}).on('error', function(e){
53+
console.log("AJAX Err. Message: " + e.message);
54+
if(callback.error && (callback.error instanceof Function)){callback.error(e.message);}
55+
});
56+
},
57+
58+
/******************************************************************
59+
* Downloads the file at the given URL and saves it in the provided
60+
* path.
61+
******************************************************************/
62+
downloadFile = function(url, outputFilepath, callback){
63+
var successCallback = (callback instanceof Function)? callback: (callback.success instanceof Function)? callback.success: function(){};
64+
var errCallback = (callback.error instanceof Function)? callback.error: function(){};
65+
66+
_req.get(url).pipe(_fs.createWriteStream(outputFilepath))
67+
.on('error', function(e){errCallback(e.message);})
68+
.on('close', function(){successCallback(outputFilepath)});
69+
},
70+
71+
/******************************************************************
72+
* Get a JSON-encoded representation of the authenticated user,
73+
* as well as a listing of associated resources.
74+
*
75+
* This should be the starting point for applications traversing
76+
* the PhoneGap Build API. It is aliased to
77+
* https://build.phonegap.com/api/v1.
78+
*
79+
* GET https://build.phonegap.com/api/v1/me
80+
*****************************************************************/
81+
_getUserData = function(token, callback){
82+
getApiData(token, 'me', callback);
83+
},
84+
85+
/******************************************************************
86+
* Get a JSON-encoded representation of the authenticated user's
87+
* apps.
88+
*
89+
* API clients can follow the link attribute for each app to get
90+
* further details, including the associated signing keys and
91+
* collaborators.
92+
*
93+
* GET https://build.phonegap.com/api/v1/apps
94+
*****************************************************************/
95+
_getAppsData = function(token, callback){
96+
getApiData(token, 'apps', callback);
97+
},
98+
99+
100+
/******************************************************************
101+
* Get a JSON-encoded representation of a particular app, if the
102+
* authenticated user has permission to access it.
103+
*
104+
* In addition to the fields provided in the list of all apps,
105+
* this detail view includes:
106+
*
107+
* keys: all of the keys that the app is currently being built with.
108+
* This will include the owner's default key for a platform,
109+
* if selected
110+
*
111+
* collaborators: each person who has access to this app, along
112+
* with their role, if the authenticated user is the owner of
113+
* the app. Collaborators who are registered with PhoneGap Build
114+
* are listed under active; collaborators you have invited who
115+
* have not yet created an account are listed as pending.
116+
*
117+
* GET https://build.phonegap.com/api/v1/apps/:id
118+
*****************************************************************/
119+
_getAppDataById = function(token, appId, callback){
120+
getApiData(token, 'apps/' + appId, callback);
121+
},
122+
123+
/******************************************************************
124+
* Get a JSON-encoded list of all the signing keys associated with
125+
* your account.
126+
*
127+
* This returns a short listing of all the associated keys--it's
128+
* very similar to the list you'll see when requesting /api/v1/me
129+
*
130+
* GET https://build.phonegap.com/api/v1/keys
131+
*****************************************************************/
132+
_getKeysData = function(token, callback){
133+
getApiData(token, 'keys', callback);
134+
},
135+
136+
137+
/******************************************************************
138+
* Get a JSON-encoded list of all the signing keys associated with
139+
* your account, for a specific platform. That platform can be one
140+
* of ios, android, or blackberry.
141+
*
142+
* GET https://build.phonegap.com/api/v1/keys/:platform
143+
*****************************************************************/
144+
_getPlatformKeys = function(platform){
145+
getApiData('keys/' + platform, METADATA_DIR + '/keys_' + platform + '.json');
146+
},
147+
148+
/******************************************************************
149+
* Get a JSON-encoded representation of a single signing key.
150+
*
151+
* GET https://build.phonegap.com/api/v1/keys/:platform/:appId
152+
*****************************************************************/
153+
_getPlatformKeyById = function(platform, appId){
154+
getApiData('keys/' + platform + '/' + appId, METADATA_DIR + '/keys_' + platform + '_' + appId + '.json');
155+
},
156+
157+
/******************************************************************
158+
* Download the app package for the given platform; available
159+
* platforms are android, blackberry, ios, symbian, webos and
160+
* winphone.
161+
*
162+
* In the successful case, this API method will return a 302
163+
* redirect to the application binary - the actual body of the
164+
* response will point to the resource's correct location:
165+
*
166+
* If using the optional argument for the download location,
167+
* please ensure that you are using the right extension for
168+
* the platform you are downloading.
169+
*
170+
* apk for Android
171+
* ipa for iOS
172+
* ipk for webOS
173+
* jad for unsigned BlackBerry builds; zip if you've uploaded your BlackBerry signing keys
174+
* wgz for Symbian
175+
* xap for Windows Phone
176+
*
177+
* GET https://build.phonegap.com/api/v1/apps/:id/:platform
178+
*****************************************************************/
179+
_downloadApp = function(token, appId, platform, outputFilepath, callback){
180+
var url= 'https://' + _URL + '/api/v1/apps/' + appId + '/' + platform + '?auth_token='+token;
181+
console.info("\n\nStarting Download...");
182+
downloadFile(url, outputFilepath, callback);
183+
},
184+
185+
/*****************************************************************
186+
* Get the main icon associated with an app - this is either the
187+
* biggest icon specified in your config.xml file, or an icon you
188+
* have uploaded through the API or the PhoneGap Build web interface.
189+
*
190+
* GET https://build.phonegap.com/api/v1/apps/:id/:icon
191+
*****************************************************************/
192+
_downloadIcon = function(token, appId, outputFilepath, callback){
193+
var url= 'https://' + _URL + '/api/v1/apps/' + appId + '/icon';
194+
downloadFile(url, outputFilepath, callback);
195+
},
196+
197+
/************************************
198+
* Init Write API
199+
*/
200+
201+
202+
203+
25204
encodeFieldHeader = function(boundary, name, value){
26205
var result = [
27206
"--" + boundary + "\r\n",
@@ -237,12 +416,27 @@ _createAuthToken = function(rawCredentials, callback){
237416
}
238417
});
239418
}
240-
;
241-
419+
420+
242421

422+
423+
/******************************************************************
424+
* Module Public Members
425+
*****************************************************************/
243426
module.exports = {
427+
//Read API
428+
getUserData: _getUserData,
429+
getAppsData: _getAppsData,
430+
getAppDataById: _getAppDataById,
431+
getKeysData: _getKeysData,
432+
getPlatformKeys:_getPlatformKeys,
433+
getPlatformKeyById: _getPlatformKeyById,
434+
downloadApp: _downloadApp,
435+
downloadIcon:_downloadIcon,
436+
437+
//Write API
244438
createFileBasedApp:_createFileBasedApp,
245439
updateFileBasedApp:_updateFileBasedApp,
246440
uploadAppIcon: _uploadAppIcon,
247-
createAuthToken: _createAuthToken,
441+
createAuthToken: _createAuthToken
248442
};

0 commit comments

Comments
 (0)