move appstore urls into appstore.js
This commit is contained in:
@@ -3,9 +3,15 @@
|
||||
exports = module.exports = {
|
||||
getFeatures,
|
||||
|
||||
getApiServerOrigin,
|
||||
getWebServerOrigin,
|
||||
getConsoleServerOrigin,
|
||||
|
||||
downloadManifest,
|
||||
getApps,
|
||||
getApp,
|
||||
getAppVersion,
|
||||
downloadIcon,
|
||||
|
||||
registerWithLoginCredentials,
|
||||
updateCloudron,
|
||||
@@ -23,6 +29,7 @@ exports = module.exports = {
|
||||
createTicket,
|
||||
|
||||
// exported for tests
|
||||
_setApiServerOrigin: setApiServerOrigin,
|
||||
_unregister: unregister
|
||||
};
|
||||
|
||||
@@ -35,6 +42,7 @@ const apps = require('./apps.js'),
|
||||
network = require('./network.js'),
|
||||
path = require('path'),
|
||||
paths = require('./paths.js'),
|
||||
promiseRetry = require('./promise-retry.js'),
|
||||
safe = require('safetydance'),
|
||||
semver = require('semver'),
|
||||
settings = require('./settings.js'),
|
||||
@@ -65,12 +73,29 @@ function getFeatures() {
|
||||
return gFeatures;
|
||||
}
|
||||
|
||||
async function getApiServerOrigin() {
|
||||
return await settings.get(settings.API_SERVER_ORIGIN_KEY) || 'https://api.cloudron.io';
|
||||
}
|
||||
|
||||
async function setApiServerOrigin(origin) {
|
||||
assert.strictEqual(typeof origin, 'string');
|
||||
await settings.set(settings.API_SERVER_ORIGIN_KEY, origin);
|
||||
}
|
||||
|
||||
async function getWebServerOrigin() {
|
||||
return await settings.get(settings.WEB_SERVER_ORIGIN_KEY) || 'https://cloudron.io';
|
||||
}
|
||||
|
||||
async function getConsoleServerOrigin() {
|
||||
return await settings.get(settings.CONSOLE_SERVER_ORIGIN_KEY) || 'https://console.cloudron.io';
|
||||
}
|
||||
|
||||
async function login(email, password, totpToken) {
|
||||
assert.strictEqual(typeof email, 'string');
|
||||
assert.strictEqual(typeof password, 'string');
|
||||
assert.strictEqual(typeof totpToken, 'string');
|
||||
|
||||
const [error, response] = await safe(superagent.post(`${settings.apiServerOrigin()}/api/v1/login`)
|
||||
const [error, response] = await safe(superagent.post(`${await getApiServerOrigin()}/api/v1/login`)
|
||||
.send({ email, password, totpToken })
|
||||
.timeout(30 * 1000)
|
||||
.ok(() => true));
|
||||
@@ -87,7 +112,7 @@ async function registerUser(email, password) {
|
||||
assert.strictEqual(typeof email, 'string');
|
||||
assert.strictEqual(typeof password, 'string');
|
||||
|
||||
const [error, response] = await safe(superagent.post(`${settings.apiServerOrigin()}/api/v1/register_user`)
|
||||
const [error, response] = await safe(superagent.post(`${await getApiServerOrigin()}/api/v1/register_user`)
|
||||
.send({ email, password, utmSource: 'cloudron-dashboard' })
|
||||
.timeout(30 * 1000)
|
||||
.ok(() => true));
|
||||
@@ -110,7 +135,7 @@ async function getSubscription() {
|
||||
const token = await settings.get(settings.APPSTORE_API_TOKEN_KEY);
|
||||
if (!token) throw new BoxError(BoxError.LICENSE_ERROR, 'Missing token');
|
||||
|
||||
const [error, response] = await safe(superagent.get(`${settings.apiServerOrigin()}/api/v1/subscription`)
|
||||
const [error, response] = await safe(superagent.get(`${await getApiServerOrigin()}/api/v1/subscription`)
|
||||
.query({ accessToken: token })
|
||||
.timeout(30 * 1000)
|
||||
.ok(() => true));
|
||||
@@ -140,7 +165,7 @@ async function purchaseApp(data) {
|
||||
const token = await settings.get(settings.APPSTORE_API_TOKEN_KEY);
|
||||
if (!token) throw new BoxError(BoxError.LICENSE_ERROR, 'Missing token');
|
||||
|
||||
const [error, response] = await safe(superagent.post(`${settings.apiServerOrigin()}/api/v1/cloudronapps`)
|
||||
const [error, response] = await safe(superagent.post(`${await getApiServerOrigin()}/api/v1/cloudronapps`)
|
||||
.send(data)
|
||||
.query({ accessToken: token })
|
||||
.timeout(30 * 1000)
|
||||
@@ -162,7 +187,7 @@ async function unpurchaseApp(appId, data) {
|
||||
const token = await settings.get(settings.APPSTORE_API_TOKEN_KEY);
|
||||
if (!token) throw new BoxError(BoxError.LICENSE_ERROR, 'Missing token');
|
||||
|
||||
const url = `${settings.apiServerOrigin()}/api/v1/cloudronapps/${appId}`;
|
||||
const url = `${await getApiServerOrigin()}/api/v1/cloudronapps/${appId}`;
|
||||
|
||||
let [error, response] = await safe(superagent.get(url)
|
||||
.query({ accessToken: token })
|
||||
@@ -197,7 +222,7 @@ async function getBoxUpdate(options) {
|
||||
automatic: options.automatic
|
||||
};
|
||||
|
||||
const [error, response] = await safe(superagent.get(`${settings.apiServerOrigin()}/api/v1/boxupdate`)
|
||||
const [error, response] = await safe(superagent.get(`${await getApiServerOrigin()}/api/v1/boxupdate`)
|
||||
.query(query)
|
||||
.timeout(30 * 1000)
|
||||
.ok(() => true));
|
||||
@@ -239,7 +264,7 @@ async function getAppUpdate(app, options) {
|
||||
automatic: options.automatic
|
||||
};
|
||||
|
||||
const [error, response] = await safe(superagent.get(`${settings.apiServerOrigin()}/api/v1/appupdate`)
|
||||
const [error, response] = await safe(superagent.get(`${await getApiServerOrigin()}/api/v1/appupdate`)
|
||||
.query(query)
|
||||
.timeout(30 * 1000)
|
||||
.ok(() => true));
|
||||
@@ -271,7 +296,7 @@ async function registerCloudron(data) {
|
||||
|
||||
const { domain, accessToken, version, existingApps } = data;
|
||||
|
||||
const [error, response] = await safe(superagent.post(`${settings.apiServerOrigin()}/api/v1/register_cloudron`)
|
||||
const [error, response] = await safe(superagent.post(`${await getApiServerOrigin()}/api/v1/register_cloudron`)
|
||||
.send({ domain, accessToken, version, existingApps })
|
||||
.timeout(30 * 1000)
|
||||
.ok(() => true));
|
||||
@@ -302,7 +327,7 @@ async function updateCloudron(data) {
|
||||
accessToken: token
|
||||
};
|
||||
|
||||
const [error, response] = await safe(superagent.post(`${settings.apiServerOrigin()}/api/v1/update_cloudron`)
|
||||
const [error, response] = await safe(superagent.post(`${await getApiServerOrigin()}/api/v1/update_cloudron`)
|
||||
.query(query)
|
||||
.send({ domain })
|
||||
.timeout(30 * 1000)
|
||||
@@ -354,7 +379,7 @@ async function createTicket(info, auditSource) {
|
||||
info.app = info.appId ? await apps.get(info.appId) : null;
|
||||
info.supportEmail = constants.SUPPORT_EMAIL; // destination address for tickets
|
||||
|
||||
const request = superagent.post(`${settings.apiServerOrigin()}/api/v1/ticket`)
|
||||
const request = superagent.post(`${await getApiServerOrigin()}/api/v1/ticket`)
|
||||
.query({ accessToken: token })
|
||||
.timeout(30 * 1000)
|
||||
.ok(() => true);
|
||||
@@ -382,11 +407,33 @@ async function createTicket(info, auditSource) {
|
||||
return { message: `An email was sent to ${constants.SUPPORT_EMAIL}. We will get back shortly!` };
|
||||
}
|
||||
|
||||
async function downloadManifest(appStoreId, manifest) {
|
||||
if (!appStoreId && !manifest) throw new BoxError(BoxError.BAD_FIELD, 'Neither manifest nor appStoreId provided');
|
||||
|
||||
if (!appStoreId) return { appStoreId: '', manifest };
|
||||
|
||||
const parts = appStoreId.split('@');
|
||||
|
||||
const url = await getApiServerOrigin() + '/api/v1/apps/' + parts[0] + (parts[1] ? '/versions/' + parts[1] : '');
|
||||
|
||||
debug(`downloading manifest from ${url}`);
|
||||
|
||||
const [error, response] = await safe(superagent.get(url).timeout(30 * 1000).ok(() => true));
|
||||
|
||||
if (error) throw new BoxError(BoxError.EXTERNAL_ERROR, 'Network error downloading manifest:' + error.message);
|
||||
|
||||
if (response.status !== 200) throw new BoxError(BoxError.NOT_FOUND, `Failed to get app info from store. status: ${response.status} text: ${response.text}`);
|
||||
|
||||
if (!response.body.manifest || typeof response.body.manifest !== 'object') throw new BoxError(BoxError.NOT_FOUND, `Missing manifest. Failed to get app info from store. status: ${response.status} text: ${response.text}`);
|
||||
|
||||
return { appStoreId: parts[0], manifest: response.body.manifest };
|
||||
}
|
||||
|
||||
async function getApps() {
|
||||
const token = await settings.get(settings.APPSTORE_API_TOKEN_KEY);
|
||||
if (!token) throw new BoxError(BoxError.LICENSE_ERROR, 'Missing token');
|
||||
|
||||
const [error, response] = await safe(superagent.get(`${settings.apiServerOrigin()}/api/v1/apps`)
|
||||
const [error, response] = await safe(superagent.get(`${await getApiServerOrigin()}/api/v1/apps`)
|
||||
.query({ accessToken: token, boxVersion: constants.VERSION, unstable: true })
|
||||
.timeout(30 * 1000)
|
||||
.ok(() => true));
|
||||
@@ -406,7 +453,7 @@ async function getAppVersion(appId, version) {
|
||||
const token = await settings.get(settings.APPSTORE_API_TOKEN_KEY);
|
||||
if (!token) throw new BoxError(BoxError.LICENSE_ERROR, 'Missing token');
|
||||
|
||||
let url = `${settings.apiServerOrigin()}/api/v1/apps/${appId}`;
|
||||
let url = `${await getApiServerOrigin()}/api/v1/apps/${appId}`;
|
||||
if (version !== 'latest') url += `/versions/${version}`;
|
||||
|
||||
const [error, response] = await safe(superagent.get(url)
|
||||
@@ -427,3 +474,19 @@ async function getApp(appId) {
|
||||
|
||||
return await getAppVersion(appId, 'latest');
|
||||
}
|
||||
|
||||
async function downloadIcon(appStoreId, version) {
|
||||
const iconUrl = `${await getApiServerOrigin()}/api/v1/apps/${appStoreId}/versions/${version}/icon`;
|
||||
|
||||
return await promiseRetry({ times: 10, interval: 5000, debug }, async function () {
|
||||
const [networkError, response] = await safe(superagent.get(iconUrl)
|
||||
.buffer(true)
|
||||
.timeout(30 * 1000)
|
||||
.ok(() => true));
|
||||
|
||||
if (networkError) throw new BoxError(BoxError.NETWORK_ERROR, `Network error downloading icon : ${networkError.message}`);
|
||||
if (response.status !== 200) return; // ignore error. this can also happen for apps installed with cloudron-cli
|
||||
|
||||
return response.body;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user