Skip to content

Commit

Permalink
fix api-cache to consider the api-js implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomontero committed Oct 11, 2023
1 parent 7f90a3e commit 9690d3a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/cmd/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ module.exports = class ParticleApi {
);
}

getDevice({ deviceId: id }) {
return this.api.getDevice({ deviceId: id, auth: this.accessToken });
}

_wrap(promise){
return Promise.resolve(promise)
.then(result => result.body || result)
Expand Down
14 changes: 7 additions & 7 deletions src/lib/api-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ const ParticleCache = require('./particle-cache');

class ApiCache {
constructor(api) {
this.particleApi = api;
this.api = api;
this.cache = new ParticleCache();
}

async getDeviceOsVersions(platformId, version) {
const key = this.cache._generateKey('device_os_version', { platformId, version });
try {
const deviceOsVersion = await this.particleApi.getDeviceOsVersions(platformId, version);
const deviceOsVersion = await this.api.getDeviceOsVersions(platformId, version);
this.cache.set(key, deviceOsVersion);
return deviceOsVersion;
} catch (error) {
Expand All @@ -28,7 +28,7 @@ class ApiCache {
async getDevice({ deviceId: id, auth }) {
const key = this.cache._generateKey('device', { deviceIdOrName: id });
try {
const device = await this.particleApi.api.getDevice({ deviceId: id, auth });
const device = await this.api.getDevice({ deviceId: id, auth });
this.cache.set(key, device);
return device;
} catch (error) {
Expand All @@ -52,10 +52,10 @@ const proxyHandler = {
get: (target, prop) => {
if (typeof target[prop] === 'function') {
return target[prop].bind(target);
} else if (typeof target.particleApi[prop] === 'function') {
return target.particleApi[prop].bind(target.particleApi);
} else if (target.particleApi.api && typeof target.particleApi.api[prop] === 'function') {
return target.particleApi.api[prop].bind(target.particleApi.api);
} else if (prop === 'api') {
return createApiCache(target.api);
} else if (typeof target.api[prop] === 'function') {
return target.api[prop].bind(target.api);
} else {
return target[prop];
}
Expand Down
12 changes: 4 additions & 8 deletions src/lib/api-cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,20 @@ describe('api-cache', () => {
describe('getDevice', () => {
it('create cache file for device os version if there is no error on getting data', async () => {
const api = {
api: {
getDevice: sinon.stub().resolves({})
}
getDevice: sinon.stub().resolves({})
};

const apiCache = createApiCache(api);
const expectedKey = apiCache.cache._generateKey('device', { deviceIdOrName: 'abc123' });
await apiCache.getDevice({ deviceId: 'abc123', auth: 'abc' });
expect(api.api.getDevice).to.have.been.calledWith({ deviceId: 'abc123', auth: 'abc' });
expect(api.getDevice).to.have.been.calledWith({ deviceId: 'abc123', auth: 'abc' });
expect(ParticleCache.prototype.set).to.have.been.calledWith(expectedKey, {});
});

it('calls cache get if there is an error on getting data', async () => {
let error;
const api = {
api: {
getDevice: sinon.stub().rejects(new Error('ECONNREFUSED'))
}
getDevice: sinon.stub().rejects(new Error('ECONNREFUSED'))
};
const apiCache = createApiCache(api);
const expectedKey = apiCache.cache._generateKey('device', { deviceIdOrName: 'abc123' });
Expand All @@ -72,7 +68,7 @@ describe('api-cache', () => {
} catch (_error) {
error = _error;
}
expect(api.api.getDevice).to.have.been.calledWith({ deviceId: 'abc123', auth: 'abc' });
expect(api.getDevice).to.have.been.calledWith({ deviceId: 'abc123', auth: 'abc' });
expect(ParticleCache.prototype.get).to.have.been.calledWith(expectedKey);
expect(error.message).to.equal('Device abc123 not found in cache and there was an internet connection error');
});
Expand Down

0 comments on commit 9690d3a

Please sign in to comment.