Get app updates via appstore API

Part of #293
This commit is contained in:
Girish Ramakrishnan
2017-04-13 01:23:11 -07:00
parent 894d7a6e72
commit b2d062bdf8
2 changed files with 25 additions and 16 deletions
+23
View File
@@ -6,6 +6,8 @@ exports = module.exports = {
sendAliveStatus: sendAliveStatus,
getAppUpdate: getAppUpdate,
AppstoreError: AppstoreError
};
@@ -44,6 +46,8 @@ AppstoreError.BILLING_REQUIRED = 'Billing Required';
var NOOP_CALLBACK = function (error) { if (error) debug(error); };
function getAppstoreConfig(callback) {
assert.strictEqual(typeof callback, 'function');
// Caas Cloudrons do not store appstore credentials in their local database
if (config.provider() === 'caas') {
var url = config.apiServerOrigin() + '/api/v1/exchangeBoxTokenWithUserToken';
@@ -165,3 +169,22 @@ function sendAliveStatus(data, callback) {
});
});
}
function getAppUpdate(app, callback) {
assert.strictEqual(typeof app, 'object');
assert.strictEqual(typeof callback, 'function');
getAppstoreConfig(function (error, appstoreConfig) {
if (error) return callback(error);
var url = config.apiServerOrigin() + '/api/v1/users/' + appstoreConfig.userId + '/cloudrons/' + appstoreConfig.cloudronId + '/appupdate';
superagent.get(url).query({ boxVersion: config.version(), appId: app.appStoreId, appVersion: app.manifest.version }).timeout(10 * 1000).end(function (error, result) {
if (error && !error.response) return callback(new AppstoreError(AppstoreError.EXTERNAL_ERROR, error));
if (result.statusCode === 204) return callback(null); // no update
if (result.statusCode !== 200) return callback(new AppstoreError(AppstoreError.EXTERNAL_ERROR, util.format('Bad response: %s %s', result.statusCode, result.text)));
callback(null, result.body);
});
});
}
+2 -16
View File
@@ -10,6 +10,7 @@ exports = module.exports = {
};
var apps = require('./apps.js'),
appstore = require('./appstore.js'),
async = require('async'),
config = require('./config.js'),
constants = require('./constants.js'),
@@ -57,21 +58,6 @@ function resetAppUpdateInfo(appId) {
}
}
function getAppUpdate(app, callback) {
superagent
.get(config.apiServerOrigin() + '/api/v1/apps/' + app.appStoreId + '/versions/' + app.manifest.version + '/update')
.query({ boxVersion: config.version() })
.timeout(10 * 1000)
.end(function (error, result) {
if (error && !error.response) return callback(error);
if (result.statusCode !== 200 || !('update' in result.body)) return callback(new Error(util.format('Bad response: %s %s', result.statusCode, result.text)));
callback(null, result.body.update);
});
}
function getBoxUpdates(callback) {
var currentVersion = config.version();
@@ -135,7 +121,7 @@ function checkAppUpdates(callback) {
async.eachSeries(apps, function (app, iteratorDone) {
if (app.appStoreId === '') return iteratorDone(); // appStoreId can be '' for dev apps
getAppUpdate(app, function (error, updateInfo) {
appstore.getAppUpdate(app, function (error, updateInfo) {
if (error) {
debug('Error getting app update info for %s', app.id, error);
return iteratorDone(); // continue to next