Skip to content

Commit

Permalink
Use error first callbacks and remove deprecated parts of API.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobAtticus committed May 26, 2016
1 parent e5bf7bc commit a502a7d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 98 deletions.
30 changes: 10 additions & 20 deletions src/endpoints/Devices.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,20 @@ module.exports = {
* Register the device with the iobeam.
* @param {integer} projectId - Project ID for the device
* @param {function} [callback] - Callback for the API response
* @param {string|Device} [deviceId] - Desired device ID (deprecated) or Device with desired specs.
* @param {Device} [deviceId] - Desired Device object with wanted specs.
* Otherwise randomly generated by iobeam.
* @param {string} [deviceName] - DEPRECATED: Use Device object in previous parameter. Desired device name. Otherwise set to provided
* device id, or randomly generated by iobeam.
* @param {string} [deviceType] - DEPRECATED: Use Device object. Desired device type
*/
register: function(projectId, callback, deviceId, deviceName, deviceType) {
register: function(projectId, callback, device) {
Utils.assertValidToken(_token);
const URL = _requester.getFullEndpoint("/devices");
let did = deviceId || null;
let dname = deviceName || null;
let dtype = deviceType || null;
if (deviceId instanceof Device) {
did = deviceId.getId();
dname = deviceId.getName();
dtype = deviceId.getType();
} else if (deviceId) {
let did = device || null;
let dname = null;
let dtype = null;
if (device instanceof Device) {
did = device.getId();
dname = device.getName();
dtype = device.getType();
} else if (device) {
console.warn("Please use Device instead of passing a string.");
}

Expand All @@ -97,13 +94,6 @@ module.exports = {
const bodyHandler = function(resp, body, status) {
if (status === RequestResults.SUCCESS) {
resp.device = new Device(body.device_id, body.device_name, body.device_type, body.created);
/* TODO(rrk): Deprecated, remove in v0.9.0 */
resp.device.device_id = resp.device.getId();
resp.device.device_name = resp.device.getName();
resp.device.created = resp.device.getCreated();
if (body.device_type) {
resp.device.device_type = resp.device.getType();
}
} else if (body && body.errors) {
resp.error = body.errors[0];
}
Expand Down
30 changes: 14 additions & 16 deletions src/iobeam.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,44 +176,42 @@ function _Client(projectId, projectToken, services, requester,
if (!__hasService("devices")) {
return; // TODO throw exception
} else if (_deviceId !== null && (did === _deviceId || did === null)) {
__callUserCallback(givenCb, true, dev, null);
__callUserCallback(givenCb, null, dev);
return;
}

const cb = function(deviceResp) {
let success = deviceResp.success;
const cb = function(resp) {
let success = resp.success;
let device = null;
let error = deviceResp.error;
let error = resp.error;

if (success) {
__setDeviceId(deviceResp.device.getId());
device = deviceResp.device;
__setDeviceId(resp.device.getId());
device = resp.device;
error = null;
} else if (setDupe && deviceResp.error && deviceResp.error.code === 150) {
} else if (setDupe && resp.error && resp.error.code === 150) {
__setDeviceId(did);
success = true;
error = null;
device = new Device(did);
/* TODO(rrk): Deprecated, remove in v0.9.0 */
device.device_id = device.getId();
// TODO what about device name?
}

__callUserCallback(givenCb, success, device, error);
__callUserCallback(givenCb, error, device);
__msgDone();
};

try {
__checkToken();
} catch (err) {
__callUserCallback(callback, false, null, err);
__callUserCallback(callback, err, null);
return;
}
_msgQueue.push(function() {
try {
_services.devices.register(_projectId, cb, dev);
} catch (err) {
__callUserCallback(callback, false, dev, err.message);
__callUserCallback(callback, err.message, dev);
}
});
__startMsgQueue();
Expand All @@ -233,21 +231,21 @@ function _Client(projectId, projectToken, services, requester,
}

const cb = function(resp, context) {
__callUserCallback(callback, resp.success, context.store, resp.error);
__callUserCallback(callback, resp.error, context.store);
__msgDone();
};

try {
__checkToken();
} catch (err) {
__callUserCallback(callback, false, null, err);
__callUserCallback(callback, err, null);
return;
}

for (let i = 0; i < _batches.length; i++) {
const b = _batches[i];
if (b.rows().length === 0) {
__callUserCallback(callback, true, b);
__callUserCallback(callback, null, b);
continue;
}

Expand All @@ -258,7 +256,7 @@ function _Client(projectId, projectToken, services, requester,
Utils.assertValidDeviceId(_deviceId);
_services.imports.importBatch(_projectId, _deviceId, snapshot, cb);
} catch (err) {
__callUserCallback(callback, false, snapshot, err.message);
__callUserCallback(callback, err.message, snapshot);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = {

const resp = utils.getDefaultApiResp(status, webResp);
if (!resp.timeout) {
const body = webResp.type === "application/json" ? webResp.body : webResp.text;
const body = (webResp.type === "application/json") ? webResp.body : webResp.text;
handleBody(resp, body, status);
}
callback(resp, context);
Expand Down
61 changes: 2 additions & 59 deletions tests/endpoints/test_Devices.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const DummyRequester = require("../http/DummyRequester");
const PROJECT_ID = 1;
const PROJECT_TOKEN = DummyRequester.OK_TOKEN;

describe("test sending", () => {
describe("test registering", () => {
Devices.initialize(PROJECT_TOKEN, DummyRequester);
const DEVICE_ID = "junk";
const DEVICE_NAME = "junk-name";
Expand All @@ -18,7 +18,6 @@ describe("test sending", () => {
const body = DummyRequester.getLastRequest().body;
expect(body).toBeDefined();
expect(body.project_id).toBe(PROJECT_ID);
expect(body.device_type).toBeUndefined();

expect(context).toBeDefined();
expect(context.projectId).toBe(PROJECT_ID);
Expand All @@ -36,7 +35,6 @@ describe("test sending", () => {
expect(body).toBeDefined();
expect(body.project_id).toBe(PROJECT_ID);
expect(body.device_id).toBe(DEVICE_ID);
expect(body.device_type).toBeUndefined();

expect(context).toBeDefined();
expect(context.projectId).toBe(PROJECT_ID);
Expand All @@ -45,25 +43,7 @@ describe("test sending", () => {
expect(context.deviceName).toBeNull();
expect(context.deviceType).toBeNull();
};
Devices.register(1, cb, DEVICE_ID);
});

it("tests register device name (deprecated)", () => {
const cb = (resp, context) => {
const body = DummyRequester.getLastRequest().body;
expect(body).toBeDefined();
expect(body.project_id).toBe(PROJECT_ID);
expect(body.device_name).toBe(DEVICE_NAME);
expect(body.device_type).toBeUndefined();

expect(context).toBeDefined();
expect(context.projectId).toBe(PROJECT_ID);
expect(context.device).toBeDefined();
expect(context.deviceId).toBeNull();
expect(context.deviceName).toBe(DEVICE_NAME);
expect(context.deviceType).toBeNull();
};
Devices.register(1, cb, null, DEVICE_NAME);
Devices.register(1, cb, new Device(DEVICE_ID));
});

it("tests register device name", () => {
Expand All @@ -85,25 +65,6 @@ describe("test sending", () => {
Devices.register(1, cb, dev);
});

it("tests register id & name (deprecated)", () => {
const cb = (resp, context) => {
const body = DummyRequester.getLastRequest().body;
expect(body).toBeDefined();
expect(body.project_id).toBe(PROJECT_ID);
expect(body.device_id).toBe(DEVICE_ID);
expect(body.device_name).toBe(DEVICE_NAME);
expect(body.device_type).toBeUndefined();

expect(context).toBeDefined();
expect(context.projectId).toBe(PROJECT_ID);
expect(context.device).toBeDefined();
expect(context.deviceId).toBe(DEVICE_ID);
expect(context.deviceName).toBe(DEVICE_NAME);
expect(context.deviceType).toBeNull();
};
Devices.register(1, cb, DEVICE_ID, DEVICE_NAME);
});

it("tests register id & name", () => {
const cb = (resp, context) => {
const body = DummyRequester.getLastRequest().body;
Expand All @@ -124,24 +85,6 @@ describe("test sending", () => {
Devices.register(1, cb, dev);
});

it("tests register id & name & type (deprecated)", () => {
const cb = (resp, context) => {
const body = DummyRequester.getLastRequest().body;
expect(body).toBeDefined();
expect(body.project_id).toBe(PROJECT_ID);
expect(body.device_id).toBe(DEVICE_ID);
expect(body.device_name).toBe(DEVICE_NAME);
expect(body.device_type).toBe(DEVICE_TYPE);

expect(context).toBeDefined();
expect(context.projectId).toBe(PROJECT_ID);
expect(context.deviceId).toBe(DEVICE_ID);
expect(context.deviceName).toBe(DEVICE_NAME);
expect(context.deviceType).toBe(DEVICE_TYPE);
};
Devices.register(1, cb, DEVICE_ID, DEVICE_NAME, DEVICE_TYPE);
});

it("tests register id & name & type", () => {
const cb = (resp, context) => {
const body = DummyRequester.getLastRequest().body;
Expand Down
3 changes: 1 addition & 2 deletions tests/test_iobeam.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ describe("test import error callback", () => {
const client = new iobeam.Builder(1, TOKEN).build();
const ds = client.createDataStore(["temperature"]);
ds.addNow({temperature: 72.0});
const callback = (success, context, error) => {
expect(success).toBe(false);
const callback = (error) => {
expect(error).not.toBeNull();
};
client.send(callback);
Expand Down

0 comments on commit a502a7d

Please sign in to comment.