Skip to content

Development #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b2c665d
HTTP api transport improvement
denisantonenko Feb 16, 2018
a5fe75d
Merge pull request #30 from devicehive/tests
Heash Feb 16, 2018
5fe8de3
Integration tests & fixes
denisantonenko Feb 21, 2018
fd57c62
Merge branch 'development' of https://github.com/devicehive/devicehiv…
denisantonenko Feb 21, 2018
af4acc3
Merge pull request #31 from devicehive/tests
itrambovetskyi Feb 22, 2018
9d81541
HTTP api transport improvement
denisantonenko Feb 16, 2018
b29fe75
Integration tests & fixes
denisantonenko Feb 21, 2018
cf2f30c
Autoupdate session
itrambovetskyi Feb 23, 2018
0870388
Autoupdate session
itrambovetskyi Feb 26, 2018
69b80da
Packages update
itrambovetskyi Feb 26, 2018
6679fac
Slight improvements
itrambovetskyi Feb 26, 2018
c7bc440
Plugin update test improvements
itrambovetskyi Feb 26, 2018
c87da78
Autoupdate session
itrambovetskyi Feb 23, 2018
14cc5f0
Autoupdate session
itrambovetskyi Feb 26, 2018
6c32fdb
Packages update
itrambovetskyi Feb 26, 2018
cd2f9d9
Slight improvements
itrambovetskyi Feb 26, 2018
c96981d
Plugin update test improvements
itrambovetskyi Feb 26, 2018
befc1a2
Merge remote-tracking branch 'origin/autoupdate-session' into autoupd…
itrambovetskyi Feb 27, 2018
4e8f0fb
Slight improvements
itrambovetskyi Feb 27, 2018
689528b
Documentation improvements
itrambovetskyi Feb 27, 2018
25f5600
Documentation improvements
itrambovetskyi Feb 27, 2018
f508893
Documentation improvements
itrambovetskyi Feb 27, 2018
ed73677
Merge pull request #32 from devicehive/autoupdate-session
itrambovetskyi Feb 27, 2018
dd8d8be
Integration tests improvements
itrambovetskyi Feb 28, 2018
15918bd
Merge pull request #34 from devicehive/autoupdate-session
itrambovetskyi Feb 28, 2018
dfb5a02
Integration tests improvements
itrambovetskyi Mar 1, 2018
0beba42
Merge pull request #35 from devicehive/autoupdate-session
itrambovetskyi Mar 1, 2018
8952f79
Unit tests improvements
itrambovetskyi Mar 1, 2018
8d3c8ae
Slight code improvements
itrambovetskyi Mar 1, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,11 @@ DeviceHive module
| [options.accessToken] | <code>string</code> | Access token |
| [options.refreshToken] | <code>string</code> | Refresh token |
| [options.login] | <code>string</code> | Login |
| [options.password] | <code>string</code> | Paaword |
| [options.password] | <code>string</code> | Password |
| options.mainServiceURL | <code>string</code> | Main Service URL |
| [options.authServiceURL] | <code>string</code> | Auth Service URL (required only for http) |
| [options.pluginServiceURL] | <code>string</code> | Alug inServi ceURL (required only for http) |
| [options.pluginServiceURL] | <code>string</code> | Plugin Service URL (required only for http) |
| [options.autoUpdateSession] | <code>boolean</code> | Flag to enable/disable autoupdating session. Default: true |

<a name="DeviceHive+connect"></a>

Expand Down Expand Up @@ -2552,7 +2553,7 @@ Creates Plugin Update Query model
| [options.returnCommands] | <code>boolean</code> | Checks if commands should be returned |
| [options.returnUpdatedCommands] | <code>boolean</code> | Checks if updated commands should be returned |
| [options.returnNotifications] | <code>boolean</code> | Checks if commands should be returned |
| [options.status] | <code>string</code> | Plugin status - active or disabled (ACTIVE | DISABLED | CREATED) |
| [options.status] | <code>string</code> | Plugin status - active or disabled (ACTIVE | INACTIVE | CREATED) |
| [options.name] | <code>string</code> | Plugin name |
| [options.description] | <code>string</code> | Plugin description |
| [options.parameters] | <code>string</code> | Plugin parameters |
Expand Down Expand Up @@ -2621,12 +2622,3 @@ Creates User List Query

### userListQuery.toObject() ⇒ <code>Object</code>
Returns instance as a plain JS object



## License

[DeviceHive] is developed by [DataArt] Apps and distributed under Open Source
[Apache 2.0 license](https://en.wikipedia.org/wiki/Apache_License).

© Copyright 2018 [DataArt] Apps © All Rights Reservedstrong text
7 changes: 6 additions & 1 deletion example/node/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const DeviceHive = require(`../../index`);


const httpDeviceHive = new DeviceHive({
login: `dhadmin`,
password: `dhadmin_#911`,
Expand All @@ -11,7 +12,8 @@ const httpDeviceHive = new DeviceHive({
const wsDeviceHive = new DeviceHive({
login: `dhadmin`,
password: `dhadmin_#911`,
mainServiceURL: 'ws://localhost:8080/dh/websocket'
mainServiceURL: 'ws://localhost:8080/dh/websocket',
autoUpdateSession: true
});

const DeviceListQuery = DeviceHive.models.query.DeviceListQuery;
Expand Down Expand Up @@ -48,7 +50,10 @@ void async function start () {
console.log(await wsDeviceHive.network.list(networkListQuery));
console.log(await wsDeviceHive.token.refresh(refreshToken));
}

} catch (error) {
console.warn(error);
}

process.exit(1);
}();
55 changes: 35 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"main": "index.js",
"scripts": {
"build": "node build.js",
"test": "node_modules/.bin/mocha test/unit/**/**.spec.js --exit"
"test": "npm run unitTest & npm run integrationTest",
"unitTest": "node_modules/.bin/mocha test/unit/**/**.spec.js --exit",
"integrationTest": "node_modules/.bin/mocha test/integration/**/**.spec.js --timeout 10000 --exit"
},
"repository": {
"type": "git",
Expand All @@ -15,21 +17,23 @@
"license": "Apache-2.0",
"dependencies": {
"isomorphic-fetch": "^2.2.1",
"jwt-decode": "^2.2.0",
"query-string": "^5.1.0",
"randomstring": "^1.1.5",
"string-template": "^1.0.0",
"universal-websocket-client": "^1.0.1"
},
"devDependencies": {
"@babel/core": "^7.0.0-beta.40",
"@babel/preset-env": "^7.0.0-beta.40",
"babel-core": "^6.26.0",
"babel-loader": "^8.0.0-beta.0",
"babel-loader": "^8.0.0-beta.2",
"babelify": "^8.0.0",
"browserify": "^15.2.0",
"chai": "^4.1.2",
"exorcist": "^1.0.1",
"mocha": "^5.0.1",
"tinyify": "^2.4.0",
"ws": "^4.0.0"
"ws": "^4.1.0"
}
}
41 changes: 37 additions & 4 deletions src/ApiStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class ApiStrategy extends EventEmitter {
static getType(url) {
let result;

if (url.startsWith('http') || url.startsWith('https')) {
if (url.startsWith(HTTP.TYPE)) {
result = HTTP;
} else if (url.startsWith('ws') || url.startsWith('wss')) {
} else if (url.startsWith(WS.TYPE)) {
result = WS;
} else {
throw new UnsupportedTransportError();
Expand All @@ -40,6 +40,8 @@ class ApiStrategy extends EventEmitter {

const me = this;

me.reconnectionHandler = null;

me.urlsMap = new Map();

me.urlsMap.set(API.MAIN_BASE, mainServiceURL);
Expand All @@ -48,7 +50,20 @@ class ApiStrategy extends EventEmitter {

me.strategy = new (ApiStrategy.getType(mainServiceURL))({ mainServiceURL, authServiceURL, pluginServiceURL });

me.strategy.on(`message`, (message) => { me.emit(`message`, message) });
me.strategy.on(`message`, (message) => {
switch (me.strategy.type) {
case HTTP.TYPE:
me.emit(`message`, message);
break;
case WS.TYPE:
if (message.subscriptionId && message.action) {
me.emit(`message`, message[message.action.split(`/`)[0]]);
} else {
me.emit(`message`, message);
}
break;
}
});
}


Expand Down Expand Up @@ -83,7 +98,25 @@ class ApiStrategy extends EventEmitter {
}

return me.strategy.send(sendData)
.then((response) => API.normalizeResponse(me.strategy.type, key, response));
.then((response) => API.normalizeResponse(me.strategy.type, key, response))
.catch(error => {
if (error === Utils.TOKEN_EXPIRED_MARK && me.reconnectionHandler) {
return me.reconnectionHandler()
.then(() => me.strategy.send(sendData))
.then((response) => API.normalizeResponse(me.strategy.type, key, response));
} else {
throw error;
}
});
}

/**
* Disconnects transport
*/
disconnect() {
const me = this;

me.strategy.disconnect();
}
}

Expand Down
58 changes: 48 additions & 10 deletions src/DeviceHive.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const Utils = require(`./utils/Utils`);
const EventEmitter = require('events');
const APIStrategy = require('./ApiStrategy');
const InfoAPI = require('./controllers/ServerInfoAPI');
Expand Down Expand Up @@ -98,7 +99,7 @@ class DeviceHive extends EventEmitter {
* @param {string} [options.authServiceURL] - Auth Service URL (required only for http)
* @param {string} [options.pluginServiceURL] - Alug inServi ceURL (required only for http)
*/
constructor({ accessToken, refreshToken, login, password, mainServiceURL, authServiceURL, pluginServiceURL }) {
constructor({ mainServiceURL, authServiceURL, pluginServiceURL, accessToken, refreshToken, login, password, autoUpdateSession = true }) {
super();

const me = this;
Expand All @@ -107,6 +108,7 @@ class DeviceHive extends EventEmitter {
me.refreshToken = refreshToken;
me.login = login;
me.password = password;
me.autoUpdateSession = autoUpdateSession;

me.strategy = new APIStrategy({ mainServiceURL, authServiceURL, pluginServiceURL });

Expand All @@ -130,29 +132,65 @@ class DeviceHive extends EventEmitter {
* Connect to the DeviceHive service
* @returns {Promise<DeviceHive>}
*/
async connect() {
async connect({ accessToken, refreshToken, login, password } = {}) {
const me = this;

if (me.accessToken || me.refreshToken || (me.login && me.password)) {
if (!accessToken && !refreshToken && !(login && password)) {
accessToken = accessToken || me.accessToken;
refreshToken = refreshToken || me.refreshToken;
login = login || me.login;
password = password || me.password;
}

if (accessToken || refreshToken || (login && password)) {
try {
if (me.accessToken) {
await me.strategy.authorize(me.accessToken);
} else if (me.refreshToken) {
const accessToken = await me.token.refresh(me.refreshToken);
if (login && password) {
const { accessToken, refreshToken } = await me.token.login(login, password);

await me.strategy.authorize(accessToken);
} else if (me.login && me.password) {
const { accessToken } = await me.token.login(me.login, me.password);

me.accessToken = accessToken;
me.refreshToken = refreshToken;
} else if (refreshToken) {
const { accessToken } = await me.token.refresh(refreshToken);

await me.strategy.authorize(accessToken);

me.accessToken = accessToken;
me.refreshToken = refreshToken;
} else if (accessToken) {
await me.strategy.authorize(accessToken);

me.accessToken = accessToken;
}

if (me.autoUpdateSession === true) {
const userTokens = await me.token.createUserToken(
Utils.createUserTokenFromJWT(me.accessToken));

me.accessToken = userTokens.accessToken;
me.refreshToken = userTokens.refreshToken;
me.strategy.reconnectionHandler = () => me.connect({ refreshToken: me.refreshToken });
}
} catch (error) {
throw new InvalidCredentialsError();
throw new InvalidCredentialsError(error);
}
} else {
throw new NoAuthCredentialsError();
}

return me;
}

/**
* Disconnects from DeviceHive server
* @returns {*|void}
*/
disconnect() {
const me = this;

return me.strategy.disconnect();
}
}


Expand Down
Loading