-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
storage: Move storage library code to this repo
- Loading branch information
Showing
4 changed files
with
76 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters