Skip to content

Commit

Permalink
storage: Move storage library code to this repo
Browse files Browse the repository at this point in the history
  • Loading branch information
esdrasjnr committed Jul 12, 2020
1 parent bc37bcd commit a98661f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@
},
"dependencies": {
"@cesarbr/knot-cloud-sdk-js-authenticator": "^2.1.0",
"@cesarbr/knot-cloud-sdk-js-storage": "^1.0.2",
"amqp-connection-manager": "^3.2.0",
"amqplib": "^0.5.6",
"axios": "^0.19.2",
"uniqid": "^5.2.0"
},
"husky": {
Expand Down
28 changes: 26 additions & 2 deletions src/Storage.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
import Storage from '@cesarbr/knot-cloud-sdk-js-storage';
import HTTP from './network/HTTP';

export default Storage;
export default class Client {
constructor(config = {}) {
if (!config.token) {
throw new Error('access token not provided');
}

this.http = new HTTP({
protocol: 'https',
hostname: 'storage.knot.cloud',
...config,
});
}

async listData(query) {
return this.http.get('/data', query);
}

async listDataByDevice(thingId, query) {
return this.http.get(`/data/${thingId}`, query);
}

async listDataBySensor(thingId, sensorId, query) {
return this.http.get(`/data/${thingId}/sensor/${sensorId}`, query);
}
}
48 changes: 48 additions & 0 deletions src/network/HTTP.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import axios from 'axios';

export default class HTTP {
constructor(config) {
const {
protocol,
hostname,
port = protocol === 'http' ? 80 : 443,
pathname = '',
token,
} = config;

if (protocol !== 'http' && protocol !== 'https') {
throw new Error("invalid protocol: must be either 'https' or 'http'");
}

this.baseUrl = `${protocol}://${hostname}:${port}${pathname}`;
this.header = {
auth_token: token,
};
}

async get(path, params) {
const config = {
url: `${this.baseUrl}${path}`,
method: 'GET',
headers: this.header,
params,
};

return axios(config)
.then(res => res.data || JSON.parse(res.config.data))
.catch(err => this.handleError(err));
}

async handleError(r) {
/*
* Reject with a detailed error if the response was correctly received.
* Otherwise, reject with an unexpected error.
*/
const props = r.response
? { message: r.response.data.message, code: r.response.status }
: { message: r.message, code: 500 };
const error = Error(props.message);
error.code = props.code;
throw error;
}
}
28 changes: 1 addition & 27 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -863,14 +863,6 @@
dependencies:
axios "^0.19.0"

"@cesarbr/knot-cloud-sdk-js-storage@^1.0.2":
version "1.0.2"
resolved "https://registry.yarnpkg.com/@cesarbr/knot-cloud-sdk-js-storage/-/knot-cloud-sdk-js-storage-1.0.2.tgz#94d98f42673628c1aaf95f884ff32670af5b1161"
integrity sha512-35kA7LTgSodH1VCQ7aIltmuSN5SYz0ADQEXnmXuwbmKB5rukNUejCDziGz342mdvczVlRJ8BGF8fckyiTCxWYQ==
dependencies:
axios "^0.19.0"
url "^0.11.0"

"@cnakazawa/watch@^1.0.3":
version "1.0.4"
resolved "https://registry.yarnpkg.com/@cnakazawa/watch/-/watch-1.0.4.tgz#f864ae85004d0fcab6f50be9141c4da368d1656a"
Expand Down Expand Up @@ -1478,7 +1470,7 @@ aws4@^1.8.0:
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.10.0.tgz#a17b3a8ea811060e74d47d306122400ad4497ae2"
integrity sha512-3YDiu347mtVtjpyV3u5kVqQLP242c06zwDOgpeRnybmXlYYsLbtTrUBUm8i8srONt+FWobl5aibnU1030PeeuA==

axios@^0.19.0:
axios@^0.19.0, axios@^0.19.2:
version "0.19.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.2.tgz#3ea36c5d8818d0d5f8a8a97a6d36b86cdc00cb27"
integrity sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==
Expand Down Expand Up @@ -5076,11 +5068,6 @@ pump@^3.0.0:
end-of-stream "^1.1.0"
once "^1.3.1"

punycode@1.3.2:
version "1.3.2"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.3.2.tgz#9653a036fb7c1ee42342f2325cceefea3926c48d"
integrity sha1-llOgNvt8HuQjQvIyXM7v6jkmxI0=

punycode@^2.1.0, punycode@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
Expand All @@ -5091,11 +5078,6 @@ qs@~6.5.2:
resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.2.tgz#cb3ae806e8740444584ef154ce8ee98d403f3e36"
integrity sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA==

querystring@0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.0.tgz#b209849203bb25df820da756e747005878521620"
integrity sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=

querystringify@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/querystringify/-/querystringify-2.1.1.tgz#60e5a5fd64a7f8bfa4d2ab2ed6fdf4c85bad154e"
Expand Down Expand Up @@ -6254,14 +6236,6 @@ url-parse@~1.4.3:
querystringify "^2.1.1"
requires-port "^1.0.0"

url@^0.11.0:
version "0.11.0"
resolved "https://registry.yarnpkg.com/url/-/url-0.11.0.tgz#3838e97cfc60521eb73c525a8e55bfdd9e2e28f1"
integrity sha1-ODjpfPxgUh63PFJajlW/3Z4uKPE=
dependencies:
punycode "1.3.2"
querystring "0.2.0"

use@^3.1.0:
version "3.1.1"
resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f"
Expand Down

0 comments on commit a98661f

Please sign in to comment.