-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
202 lines (177 loc) · 5.07 KB
/
Copy pathclient.js
File metadata and controls
202 lines (177 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const https = require('https');
const cache = require('./cache');
const ROUTES = Object.freeze({
HOSTNAME: 'client-api.8slp.net',
BASE_PATH: '/v1',
LOGIN: '/login',
USER_ME: '/users/me',
USER: '/users/{{userId}}',
TRENDS: '/users/{{userId}}/trends',
DEVICE: '/devices/{{deviceId}}'
});
/**
* POST Login
*
* @param {string} email
* @param {string} password
*/
async function login(email, password) {
if (typeof email !== 'string' || typeof password !== 'string') {
throw new Error('Eight Smart Matress email and/or password missing or invalid!');
}
return await doRequest(undefined, `${ROUTES.BASE_PATH}${ROUTES.LOGIN}`, 'POST', {
email,
password
});
}
/**
* GET User Me
*
* @param {Object} session
*/
async function getUserMe(session) {
return await doRequest(session, `${ROUTES.BASE_PATH}${ROUTES.USER_ME}`, 'GET', undefined, 5);
}
/**
* GET User By Id
*
* @param {Object} session
* @param {string} userId
*/
async function getUserById(session, userId) {
if (typeof userId !== 'string') {
throw new Error('Eight Client: Get User By Id - Missing userId');
}
return await doRequest(
session,
`${ROUTES.BASE_PATH}${ROUTES.USER.replace('{{userId}}', userId)}`,
'GET',
undefined,
5
);
}
/**
* GET Trends By User Id
*
* @param {Object} session
* @param {string} userId
*/
async function getTrendsByUserId(session, userId, timezone, from, to) {
if (typeof userId !== 'string') {
throw new Error('Eight Client: Get Trends By User Id - Missing userId');
}
return await doRequest(
session,
`${ROUTES.BASE_PATH}${ROUTES.TRENDS.replace('{{userId}}', userId)}?tz=${timezone}&from=${from}&to=${to}`,
'GET',
undefined,
5
);
}
/**
* GET Device By Id
*
* @param {Object} session
* @param {string} deviceId
* @param {Object} filter - ex: ownerId,leftUserId,rightUserId
*/
async function getDeviceById(session, deviceId, filter) {
if (typeof deviceId !== 'string') {
throw new Error('Eight Client: Get Device By Id - Missing deviceId');
}
return await doRequest(
session,
`${ROUTES.BASE_PATH}${ROUTES.DEVICE.replace('{{deviceId}}', deviceId)}${filter ? '?filter=${filter}' : ''}`,
'GET',
undefined,
5
);
}
/**
*
* @param {Object} session - session object
* @param {string} path - request path
* @param {string} method - http methods
* @param {Object} body - to be sent on the request
* @param {integer} ttl - (cache) time to live in minutes
*/
async function doRequest(session, path, method, body, ttl) {
if (method === 'GET') {
const cacheValue = cache.get(`route:${path}`);
if (cacheValue) {
return cacheValue;
}
}
const reqData = body ? JSON.stringify(body) : undefined;
if (session && !session.isValid()) {
session = await session.refreshToken();
}
return new Promise((resolve, reject) => {
const headers = reqData ? {
...getHeaders(session),
'Content-Length': reqData ? Buffer.byteLength(reqData) : undefined
} :
getHeaders(session);
const request = https.request({
method,
hostname: ROUTES.HOSTNAME,
path: encodeURI(path),
headers
},
(response) => {
let resData = '';
response.setEncoding('utf8');
response.on('data', function(chunk) {
resData += chunk;
});
response.on('end', function() {
const jsonRes = JSON.parse(resData);
if (method === 'GET' && ttl) {
cache.store(`route:${path}`, jsonRes, ttl);
}
resolve(jsonRes);
});
response.on('error', function(error) {
console.log('8slp[doRequest] - response error', error);
reject(JSON.parse(error));
});
}
);
request.on('error', (error) => {
console.log('8slp[doRequest] - request error', error);
reject(error);
});
if (reqData) {
request.write(reqData);
}
request.end();
});
}
function getHeaders(session) {
const defaultHeaders = Object.freeze({
'Content-Type': 'application/json',
Host: ROUTES.HOSTNAME,
'API-Key': 'api-key',
'Application-Id': 'morphy-app-id',
Connection: 'keep-alive',
'User-Agent': 'Eight%20AppStore/11 CFNetwork/808.2.16 Darwin/16.3.0',
'Accept-Language': 'en-gb',
'Accept-Encoding': 'gzip',
Accept: '*/*',
'app-Version': '1.10.0'
});
if (!session) {
return defaultHeaders;
}
return {
...defaultHeaders,
'Session-Token': session.token
};
}
module.exports = {
login,
getUserMe,
getUserById,
getTrendsByUserId,
getDeviceById
};