From a98661f377d712ca39b96837e6d07b2fa0d0df34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esdras=20J=C3=BAnior?= Date: Sun, 12 Jul 2020 01:19:38 -0300 Subject: [PATCH] storage: Move storage library code to this repo --- package.json | 2 +- src/Storage.js | 28 ++++++++++++++++++++++++-- src/network/HTTP.js | 48 +++++++++++++++++++++++++++++++++++++++++++++ yarn.lock | 28 +------------------------- 4 files changed, 76 insertions(+), 30 deletions(-) create mode 100644 src/network/HTTP.js diff --git a/package.json b/package.json index f0f8afa..c313e6c 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/Storage.js b/src/Storage.js index a6a19b5..850b4f2 100644 --- a/src/Storage.js +++ b/src/Storage.js @@ -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); + } +} diff --git a/src/network/HTTP.js b/src/network/HTTP.js new file mode 100644 index 0000000..3804d2e --- /dev/null +++ b/src/network/HTTP.js @@ -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; + } +} diff --git a/yarn.lock b/yarn.lock index 24a5ecd..e8eaed4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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== @@ -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" @@ -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" @@ -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"