-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
86 lines (70 loc) · 2.54 KB
/
index.js
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
const baseUrl = "https://api.ouraring.com/v2/usercollection/";
const validateQS = (qs, allowedKeys) => {
for (const key in qs) {
if (Object.prototype.hasOwnProperty.call(qs, key)) {
if (allowedKeys.includes(key) === false) {
throw `Invalid parameter '${key}'.`;
}
}
}
return true;
};
class Client {
constructor(accessToken) {
if (!accessToken) {
throw "Missing access token";
}
this.accessToken = accessToken;
}
#runRequest = async (url, qs) => {
const params = new URLSearchParams(qs);
const response = await fetch(baseUrl + encodeURI(url) + (qs ? "?" + params.toString() : ""), {
method: "GET",
headers: {
"Authorization": "Bearer " + this.accessToken
}
});
const parsed = await response.json();
if (response.status >= 200 && response.status < 300) {
return parsed;
} else {
throw `${response.status} ${response.statusText}${parsed?.detail ? " - "+JSON.stringify(parsed.detail):""} `;
}
};
async getDailyActivity(qs) {
validateQS(qs, ["start_date", "end_date", "next_token"]);
return await this.#runRequest("daily_activity", qs);
}
async getDailyReadiness(qs) {
validateQS(qs, ["start_date", "end_date", "next_token"]);
return await this.#runRequest("daily_readiness", qs);
}
async getDailySleep(qs) {
validateQS(qs, ["start_date", "end_date", "next_token"]);
return await this.#runRequest("daily_sleep", qs);
}
async GetHeartrate(qs) {
validateQS(qs, ["start_datetime", "end_datetime", "next_token"]);
return await this.#runRequest("heartrate", qs);
}
async GetPersonalInfo() {
return await this.#runRequest("personal_info");
}
async getSession(qs) {
validateQS(qs, ["start_date", "end_date", "next_token"]);
return await this.#runRequest("session", qs);
}
async getSleep(qs) {
validateQS(qs, ["start_date", "end_date", "next_token"]);
return await this.#runRequest("sleep", qs);
}
async getTag(qs) {
validateQS(qs, ["start_date", "end_date", "next_token"]);
return await this.#runRequest("tag", qs);
}
async getWorkout(qs) {
validateQS(qs, ["start_date", "end_date", "next_token"]);
return await this.#runRequest("workout", qs);
}
}
module.exports = Client;