Created
May 4, 2025 14:16
-
-
Save TownLake/5ffeeb23dccf16fb6a866d2de02a2f6b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
export default { | |
async scheduled(event, env) { | |
const now = new Date(); | |
const since = new Date(now.getTime() - 24 * 60 * 60 * 1000); | |
const url = new URL("https://api.ouraring.com/v2/usercollection/heartrate"); | |
url.searchParams.set("start_datetime", since.toISOString()); | |
url.searchParams.set("end_datetime", now.toISOString()); | |
const resp = await fetch(url, { | |
headers: { | |
"Authorization": `Bearer ${env.OURA_TOKEN}`, | |
"Accept": "application/json" | |
} | |
}); | |
if (!resp.ok) { | |
console.error("Oura API error", resp.status); | |
return; | |
} | |
const { data } = await resp.json(); | |
if (!data?.length) { | |
console.log("⏸ no new data, keeping previous history"); | |
return; | |
} | |
await env.heart_rate.put("history", JSON.stringify(data)); | |
const latest = data[data.length - 1]; | |
await env.heart_rate.put("latest", JSON.stringify(latest)); | |
}, | |
async fetch(request, env) { | |
const url = new URL(request.url); | |
if (url.pathname === "/latest") { | |
const raw = await env.heart_rate.get("latest"); | |
return raw | |
? new Response(raw, { headers: { "Content-Type": "application/json" } }) | |
: new Response("No data yet", { status: 404 }); | |
} | |
if (url.pathname === "/history") { | |
const raw = await env.heart_rate.get("history"); | |
return raw | |
? new Response(raw, { headers: { "Content-Type": "application/json" } }) | |
: new Response("No history yet", { status: 404 }); | |
} | |
return new Response("Not found", { status: 404 }); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment