Skip to content

Commit

Permalink
Merge pull request #57 from spark/feature/client-build-targets
Browse files Browse the repository at this point in the history
Create a wrapper for `listBuildTargets`
  • Loading branch information
suda authored Feb 15, 2017
2 parents 663359c + 3fc96c9 commit 6bd9caa
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 37 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# changelog

## 6.4.3 - 15 Feb 2017

* Create a wrapper for `listBuildTargets` in `Client.js`.
* Marked `compileCode`, `signalDevice`, `listDevices` and `listBuildTargets` as deprecated. Those methods will be removed in 6.5

## 6.4.2 - 05 Jan 2017

* Create a wrapper for `listDevices` in `Client.js`.
Expand Down
24 changes: 12 additions & 12 deletions src/Agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ export default class Agent {
}

get(uri, auth, query) {
return this.request({uri, auth, method: 'get', query: query});
return this.request({ uri, auth, method: 'get', query: query });
}

head(uri, auth) {
return this.request({uri, auth, method: 'head'});
return this.request({ uri, auth, method: 'head' });
}

post(uri, data, auth) {
return this.request({uri, data, auth, method: 'post'});
return this.request({ uri, data, auth, method: 'post' });
}

put(uri, data, auth) {
return this.request({uri, data, auth, method: 'put'});
return this.request({ uri, data, auth, method: 'put' });
}

delete(uri, data, auth) {
return this.request({uri, data, auth, method: 'delete'});
return this.request({ uri, data, auth, method: 'delete' });
}


Expand All @@ -60,9 +60,9 @@ export default class Agent {
* @param {Object} files array of file names and file content
* @return {Promise} A promise. fulfilled with {body, statusCode}, rejected with { statusCode, errorDescription, error, body }
*/
request({uri, method, data = undefined, auth, query = undefined, form = undefined, files = undefined}) {
request({ uri, method, data = undefined, auth, query = undefined, form = undefined, files = undefined }) {
const requestFiles = this._sanitizeFiles(files);
return this._request({uri, method, data, auth, query, form, files: requestFiles});
return this._request({ uri, method, data, auth, query, form, files: requestFiles });
}

/**
Expand All @@ -76,8 +76,8 @@ export default class Agent {
* @param {Object} files array of file names and file content
* @return {Promise} A promise. fulfilled with {body, statusCode}, rejected with { statusCode, errorDescription, error, body }
*/
_request({uri, method, data, auth, query, form, files}) {
const req = this._buildRequest({uri, method, data, auth, query, form, files});
_request({ uri, method, data, auth, query, form, files }) {
const req = this._buildRequest({ uri, method, data, auth, query, form, files });
return this._promiseResponse(req);
}

Expand Down Expand Up @@ -112,7 +112,7 @@ export default class Agent {
shortErrorDescription = body.error_description;
}
const reason = new Error(errorDescription);
Object.assign(reason, {statusCode, errorDescription, shortErrorDescription, error, body});
Object.assign(reason, { statusCode, errorDescription, shortErrorDescription, error, body });
reject(reason);
} else {
fulfill({
Expand All @@ -123,7 +123,7 @@ export default class Agent {
});
}

_buildRequest({uri, method, data, auth, query, form, files, makerequest=request}) {
_buildRequest({ uri, method, data, auth, query, form, files, makerequest=request }) {
const req = makerequest(method, uri);
if (this.prefix) {
req.use(this.prefix);
Expand Down Expand Up @@ -165,7 +165,7 @@ export default class Agent {
if (auth.username !== undefined) {
req.auth(auth.username, auth.password);
} else {
req.set({Authorization: `Bearer ${auth}`});
req.set({ Authorization: `Bearer ${auth}` });
}
}
return req;
Expand Down
43 changes: 42 additions & 1 deletion src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default class Client {
* @return {Promise} To publish the library
*/
publishLibrary(name) {
return this.api.publishLibrary({name, auth: this.auth })
return this.api.publishLibrary({ name, auth: this.auth })
.then(payload => {
const library = payload.body.data || {};
return new Library(this, library);
Expand Down Expand Up @@ -104,15 +104,56 @@ export default class Client {
return this.api.downloadFile({ url });
}

/**
* @param {Object} files Object containing files to be compiled
* @param {Number} platformId Platform id number of the device you are compiling for
* @param {String} targetVersion System firmware version to compile against
* @return {Promise}
* @deprecated Will be removed in 6.5
*/
compileCode(files, platformId, targetVersion) {
return this.api.compileCode({ files, platformId, targetVersion, auth: this.auth });
}

/**
* @param {String} $0.deviceId Device ID or Name
* @param {Boolean} $0.signal Signal on or off
* @return {Promise}
* @deprecated Will be removed in 6.5
*/
signalDevice({ signal, deviceId }) {
return this.api.signalDevice({ signal, deviceId, auth: this.auth });
}

/**
* @return {Promise}
* @deprecated Will be removed in 6.5
*/
listDevices() {
return this.api.listDevices({ auth: this.auth });
}

/**
* @return {Promise}
* @deprecated Will be removed in 6.5
*/
listBuildTargets() {
return this.api.listBuildTargets({ onlyFeatured: true, auth: this.auth })
.then(payload => {
let targets = [];
for (let target of payload.body.targets) {
for (let platform of target.platforms) {
targets.push({
version: target.version,
platform: platform,
prerelease: target.prereleases.indexOf(platform) > -1,
firmware_vendor: target.firmware_vendor
});
}
}
return targets;
}, error => {

});
}
}
2 changes: 1 addition & 1 deletion src/Defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export default {
baseUrl: 'https://api.particle.io',
clientSecret: 'particle-api',
clientId: 'particle-api',
tokenDuration: 7776000, // 90 days
tokenDuration: 7776000 // 90 days
};
21 changes: 11 additions & 10 deletions src/EventStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,18 @@ class EventStream extends EventEmitter {
res.on('end', () => {
try {
body = JSON.parse(body);
} catch (e) {}
this.emit('response', {
statusCode,
body
});
let errorDescription = `HTTP error ${statusCode} from ${this.uri}`;
if (body && body.error_description) {
errorDescription += ' - ' + body.error_description;
} finally {
this.emit('response', {
statusCode,
body
});
let errorDescription = `HTTP error ${statusCode} from ${this.uri}`;
if (body && body.error_description) {
errorDescription += ' - ' + body.error_description;
}
reject({ statusCode, errorDescription, body });
this.req = undefined;
}
reject({ statusCode, errorDescription, body });
this.req = undefined;
});
return;
}
Expand Down
24 changes: 12 additions & 12 deletions src/Particle.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Particle {
grant_type: 'password',
client_id: this.clientId,
client_secret: this.clientSecret,
expires_in: tokenDuration,
expires_in: tokenDuration
}, method: 'post' });
}

Expand All @@ -56,7 +56,7 @@ class Particle {
return this.post('/v1/users', {
username,
password,
account_info : accountInfo,
account_info : accountInfo
});
}

Expand Down Expand Up @@ -89,7 +89,7 @@ class Particle {
*/
removeAccessToken({ username, password, token }) {
return this.delete(`/v1/access_tokens/${token}`, {
access_token: token,
access_token: token
}, { username, password });
}

Expand Down Expand Up @@ -166,14 +166,14 @@ class Particle {
return this.post('/v1/device_claims', { iccid }, auth);
}

validatePromoCode({auth, promoCode}) {
validatePromoCode({ auth, promoCode }) {
return this.get(`/v1/promo_code/${promoCode}`, auth);
}

changeProduct({ deviceId, productId, shouldUpdate, auth }) {
return this.put(`/v1/devices/${deviceId}`, {
product_id: productId,
update_after_claim: shouldUpdate || false,
update_after_claim: shouldUpdate || false
}, auth);
}

Expand All @@ -197,7 +197,7 @@ class Particle {
*/
signalDevice({ deviceId, signal, auth }) {
return this.put(`/v1/devices/${deviceId}`, {
signal: ( !!signal ? '1' : '0' ),
signal: ( signal ? '1' : '0' )
}, auth);
}

Expand Down Expand Up @@ -228,7 +228,7 @@ class Particle {
*/
flashTinker({ deviceId, auth }) {
return this.put(`/v1/devices/${deviceId}`, {
app: 'tinker',
app: 'tinker'
}, auth);
}

Expand Down Expand Up @@ -296,7 +296,7 @@ class Particle {
*/
callFunction({ deviceId, name, argument, auth }) {
return this.post(`/v1/devices/${deviceId}/${name}`, {
args: argument,
args: argument
}, auth);
}

Expand Down Expand Up @@ -433,7 +433,7 @@ class Particle {
return this.put(`/v1/sims/${iccid}`, {
country: countryCode,
promo_code: promoCode,
action: 'activate',
action: 'activate'
}, auth);
}

Expand Down Expand Up @@ -530,8 +530,8 @@ class Particle {
files, auth, method: 'post' });
}

publishLibrary({auth, name}) {
return this.request({ uri: `/v1/libraries/${name}`, auth, method: 'patch', data: {visibility:'public'}});
publishLibrary({ auth, name }) {
return this.request({ uri: `/v1/libraries/${name}`, auth, method: 'patch', data: { visibility:'public' } });
}

/**
Expand Down Expand Up @@ -588,7 +588,7 @@ class Particle {
}

client(options = {}) {
return new Client(Object.assign({ api: this}, options));
return new Client(Object.assign({ api: this }, options));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/superagent-binary-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export default function binaryParser(res, fn) {
let data = [];
res.on('data', chunk => data.push(chunk));
res.on('end', () => fn(null, Buffer.concat(data)));
};
}
48 changes: 48 additions & 0 deletions test/Client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,52 @@ describe('Client', () => {
});
});

describe('listBuildTargets', () => {
it('delegates to api', () => {
const response = {
targets: [
{
platforms: [0, 6],
prereleases: [],
version: '1.2.3',
firmware_vendor: 'Foo'
}, {
platforms: [6, 8],
prereleases: [6],
version: '4.5.6',
firmware_vendor: 'Bar'
}
]
};
const expected = [
{
version: '1.2.3',
platform: 0,
prerelease: false,
firmware_vendor: 'Foo'
}, {
version: '1.2.3',
platform: 6,
prerelease: false,

firmware_vendor: 'Foo'
}, {
version: '4.5.6',
platform: 6,
prerelease: true,
firmware_vendor: 'Bar'
}, {
version: '4.5.6',
platform: 8,
prerelease: false,
firmware_vendor: 'Bar'
},
];
api.listBuildTargets = ({auth}) => {
return Promise.resolve({body: response});
};
return expect(client.listBuildTargets()).to.eventually.eql(expected);
});
});

});

0 comments on commit 6bd9caa

Please sign in to comment.