在 Node 中使用 PhoneGap Build API 包(二)

2014-11-24 10:36:37 · 作者: · 浏览: 1

//TODO
}

}


exports.getAllApps = function(success,fail) {
doCall("apps", function(res) {
if(res.error && res.error.length && fail) fail(res.error);
else success(res.apps);
},function(e) {
if(fail) fail(e);
});
}


exports.getApp = function(id, success, fail) {
doCall("apps/"+id, function(res) {
if(res.error && res.error.length && fail) fail(res.error);
else success(res);
},function(e) {
if(fail) fail(e);
});
}


exports.getAppIcon = function(id, success, fail) {
doCall("apps/"+id +"/icon", function(res) {
if(res.error && res.error.length && fail) fail(res.error);
else success(res.location);
},function(e) {
if(fail) fail(e);
});
}


exports.getKeys = function() {
var platform = "";
if(arguments.length == 1) {
success = arguments[0];
} else if(arguments.length === 2) {
success = arguments[0];
fail = arguments[1];
} else if(arguments.length == 3) {
platform = arguments[0];
success = arguments[1];
fail = arguments[2];
}

var path = "keys";
if(platform != "") path+="/"+platform;

doCall(path, function(res) {
if(res.error && res.error.length && fail) fail(res.error);
else success(res.keys);
},function(e) {
if(fail) fail(e);
});
}


exports.getKey = function(platform, id, success, fail) {
doCall("keys/"+platform +"/"+id, function(res) {
if(res.error && res.error.length && fail) fail(res.error);
else success(res);
},function(e) {
if(fail) fail(e);
});
}


function getConfig(path) {
return {
auth: username + ":" + password,
host:"build.phonegap.com",
port:"443",
path:"/api/v1/"+path
}
}


//I handle doing the config get, http, string contact, etc
function doCall(path, success, fail) {
var options = getConfig(path);
var req = http.get(options, function(res) {
var resultString = "";
res.on("data", function(c) {
resultString+=c;
});

res.on("end",function() {
var result = JSON.parse(resultString);
success(result);
});
}).on("error", function(e) {
if(fail) fail(e);
});
}



//CREDIT: http://onteria.wordpress.com/2011/05/30/multipartform-data-uploads-using-node-js-and-http-request/
//Note that I modified his code quite a bit


//For file uploads
function EncodeFieldPart(boundary,name,value) {
var return_part = "--" + boundary + "\r\n";
return_part += "Content-Disposition: form-data; name=\"" + name + "\"\r\n\r\n";
return_part += value + "\r\n";
return return_part;
}


function EncodeFilePart(boundary,type,name,filename) {
var return_part = "--" + boundary + "\r\n";
return_part += "Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + filename + "\"\r\n";
return_part += "Content-Type: " + type + "\r\n\r\n";
return return_part;
}


//I expect the config options, the JSON data string, and file path
function PreparePost(httpOptions,data,file,success) {
var boundary = Math.random();
var post_data = [];


post_data.push(new Buffer(EncodeFieldPart(boundary, 'data', data), 'ascii'));
post_data.push(new Buffer(EncodeFilePart(boundary, 'text/plain', 'file', path.basename(file)), 'ascii'));


var contents = fs.readFileSync(file, "asc