Skip to content

Commit afbf7a5

Browse files
authored
Merge pull request #109 from particle-iot/feature/product-add-devices-file
feature/product-add-devices-file
2 parents 89fc636 + 16cca54 commit afbf7a5

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

src/Particle.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,29 @@ class Particle {
305305
* Add a device to a product or move device out of quarantine.
306306
* @param {Object} options Options for this API call
307307
* @param {String} options.deviceId Device ID
308+
* @param {Object} options.file A file that contains a single-column list of device IDs, device serial numbers, device IMEIs, or devie ICCIDs.
309+
* Node: Either a path or Buffer. Browser: a File or Blob.
308310
* @param {String} options.product Add to this product ID or slug
309311
* @param {String} options.auth Access Token
310312
* @returns {Promise} A promise
311313
*/
312-
addDeviceToProduct({ deviceId, product, auth, context }){
313-
const uri = `/v1/products/${product}/devices`;
314-
return this.post(uri, {
315-
id: deviceId
316-
}, auth, context);
314+
addDeviceToProduct({ deviceId, product, file, auth, context }){
315+
let files, data;
316+
317+
if (file){
318+
files = { file };
319+
} else if (deviceId){
320+
data = { id: deviceId };
321+
}
322+
323+
return this.request({
324+
uri: `/v1/products/${product}/devices`,
325+
method: 'post',
326+
data,
327+
files,
328+
auth,
329+
context
330+
});
317331
}
318332

319333
/**

test/Particle.spec.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,10 @@ describe('ParticleAPI', () => {
350350
});
351351
});
352352
describe('.addDeviceToProduct', () => {
353-
it('sends request', () => {
354-
return api.addDeviceToProduct(propsWithProduct).then((results) => {
353+
it('sends request to add a single device by id', () => {
354+
const prodProps = Object.assign({}, propsWithProduct);
355+
delete prodProps.file;
356+
return api.addDeviceToProduct(prodProps).then((results) => {
355357
results.should.match({
356358
method: 'post',
357359
uri: `/v1/products/${product}/devices`,
@@ -362,6 +364,18 @@ describe('ParticleAPI', () => {
362364
});
363365
});
364366
});
367+
it('sends request to add multiple devices using a file', () => {
368+
return api.addDeviceToProduct(propsWithProduct).then((results) => {
369+
results.should.match({
370+
method: 'post',
371+
uri: `/v1/products/${product}/devices`,
372+
auth: props.auth,
373+
files: {
374+
file: props.file,
375+
}
376+
});
377+
});
378+
});
365379
});
366380
describe('.removeDevice', () => {
367381
describe('user scope', () => {

0 commit comments

Comments
 (0)