Skip to content

Commit

Permalink
Extract SDK helper fucntions from client
Browse files Browse the repository at this point in the history
  • Loading branch information
Grégoire Weber committed Apr 16, 2017
1 parent 9205059 commit e57305a
Show file tree
Hide file tree
Showing 4 changed files with 374 additions and 348 deletions.
59 changes: 9 additions & 50 deletions src/clients/BarracksClient.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* jshint maxstatements: 10 */
/* jshint maxstatements: 11 */

const HTTPClient = require('./HTTPClient');
const AccountClient = require('./AccountClient');
Expand All @@ -8,8 +8,7 @@ const PackageClient = require('./PackageClient');
const SegmentClient = require('./SegmentClient');
const TokenClient = require('./TokenClient');
const UpdateClient = require('./UpdateClient');
const BarracksSDK = require('barracks-sdk');
const logger = require('../utils/logger');
const BarracksSDKProxy = require('../utils/BarracksSDKProxy');
const config = require('../config');

function mergeAccountClient(barracksClient, options) {
Expand Down Expand Up @@ -78,6 +77,12 @@ function mergeUpdateClient(barracksClient, options) {
barracksClient.scheduleUpdate = updateClient.scheduleUpdate.bind(updateClient);
}

function mergeSDKProxy(barracksClient, options) {
const proxy = new BarracksSDKProxy(options);
barracksClient.checkUpdate = proxy.checkUpdate.bind(proxy);
barracksClient.checkUpdateAndDownload = proxy.checkUpdateAndDownload.bind(proxy);
}

class BarracksClient {

constructor(options) {
Expand All @@ -92,53 +97,7 @@ class BarracksClient {
mergeSegmentClient(this, options);
mergeTokenClient(this, options);
mergeUpdateClient(this, options);
}

checkUpdate(apiKey, device) {
return new Promise((resolve, reject) => {
logger.debug('checking update:', device);
const client = new BarracksSDK({
baseURL: this.options.baseUrl,
apiKey,
unitId: device.unitId
});

client.checkUpdate(device.versionId, device.customClientData).then(update => {
if (update) {
resolve(update);
} else {
resolve('No update available');
}
}).catch(err => {
logger.debug('check update failed:', err);
reject(err);
});
});
}

checkUpdateAndDownload(apiKey, device, path) {
return new Promise((resolve, reject) => {
logger.debug('check and download update:', device, path);
const client = new BarracksSDK({
baseURL: this.options.baseUrl,
apiKey,
unitId: device.unitId,
downloadFilePath: path
});

client.checkUpdate(device.versionId, device.customClientData).then(update => {
if (update) {
update.download().then(file => {
resolve(file);
});
} else {
resolve('No update available');
}
}).catch(err => {
logger.debug('check and download update failed:', err);
reject(err);
});
});
mergeSDKProxy(this, options);
}
}

Expand Down
58 changes: 58 additions & 0 deletions src/utils/BarracksSDKProxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const BarracksSDK = require('barracks-sdk');
const logger = require('../utils/logger');

class BarracksSDKProxy {

constructor(options) {
this.options = options;
}

checkUpdate(apiKey, device) {
return new Promise((resolve, reject) => {
logger.debug('checking update:', device);
const sdk = new BarracksSDK({
baseURL: this.options.baseUrl,
apiKey,
unitId: device.unitId
});

sdk.checkUpdate(device.versionId, device.customClientData).then(update => {
if (update) {
resolve(update);
} else {
resolve('No update available');
}
}).catch(err => {
logger.debug('check update failed:', err);
reject(err);
});
});
}

checkUpdateAndDownload(apiKey, device, path) {
return new Promise((resolve, reject) => {
logger.debug('check and download update:', device, path);
const sdk = new BarracksSDK({
baseURL: this.options.baseUrl,
apiKey,
unitId: device.unitId,
downloadFilePath: path
});

sdk.checkUpdate(device.versionId, device.customClientData).then(update => {
if (update) {
update.download().then(file => {
resolve(file);
});
} else {
resolve('No update available');
}
}).catch(err => {
logger.debug('check and download update failed:', err);
reject(err);
});
});
}
}

module.exports = BarracksSDKProxy;
Loading

0 comments on commit e57305a

Please sign in to comment.