Skip to content

Commit 711b907

Browse files
authored
feat: stream data from request
* Fix: add super agent stream * Fix: add units api * Fix: move stream to options * Fix: move stream to options * Fix: change response and deadline
1 parent 0752a55 commit 711b907

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

lib/Auth/index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ class Auth {
168168
cache: false,
169169
response: 60000,
170170
deadline: 90000,
171-
error: {}
171+
error: {},
172+
stream: false
172173
},
173174
opts
174175
);
@@ -204,15 +205,20 @@ class Auth {
204205
opts.headers.authorization = `Bearer ${clientToken}`;
205206
}
206207

207-
begin = new Date().getTime();
208-
({ status, body } = await superagent(method, url)
208+
const req = superagent(method, url)
209209
.set(opts.headers)
210210
.timeout({
211211
response: opts.response,
212212
deadline: opts.deadline
213213
})
214-
.query(opts.query)
215-
.send(opts.payload));
214+
.query(opts.query);
215+
216+
if (opts.stream) {
217+
return req.send(opts.payload).pipe(opts.stream);
218+
}
219+
220+
begin = new Date().getTime();
221+
({ status, body } = await req.send(opts.payload));
216222

217223
if (
218224
this.isCacheEnabled &&

lib/Services/delorean-es.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,35 @@ class DeloreanES {
106106
return this.auth.send(this.service, 'GET', url, { query });
107107
}
108108

109+
/**
110+
* @param {String} userId
111+
* @param {String} deviceId
112+
* @param {Object} query
113+
*/
114+
getUnits(userId, deviceId, query) {
115+
const url = `${this.url}/devices/${deviceId}/readings/units`;
116+
query = { ...query, user_id: userId };
117+
return this.auth.send(this.service, 'GET', url, { query });
118+
}
119+
120+
/**
121+
* @param {String} userId
122+
* @param {String} deviceId
123+
* @param {Object} query
124+
* @param {Object} stream
125+
*/
126+
getDeviceReportStream(userId, deviceId, query, stream) {
127+
const url = `${this.url}/devices/${deviceId}/stream/report`;
128+
query = { ...query, user_id: userId };
129+
130+
return this.auth.send(this.service, 'GET', url, {
131+
query,
132+
stream,
133+
response: 90000 * 3,
134+
timeout: 90000 * 3
135+
});
136+
}
137+
109138
/**
110139
* @param {String} userId
111140
* @param {String} deviceId

0 commit comments

Comments
 (0)